Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Managed C++/CLI
  4. Why Error???

Why Error???

Scheduled Pinned Locked Moved Managed C++/CLI
helpquestion
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    ashok123
    wrote on last edited by
    #1

    I am not sure why I am getting error in displaying the output. I want output such that when I enter a string aaaabaaba... for "aaa" it shud give output 0 and "aba" it should give output 1. I am not sure where the error is??? Please help thanks is advance. #include #include #include #include char* in; void main() { cout << "Enter your string here \n"; cin>>in; while(*in != ' ') //it crashes here { if(strncmp(in, "aaa", 3)==0) cout << "0"; if(strncmp(in, "aba", 3)==0) cout << "1"; in+=3; } } A

    C 1 Reply Last reply
    0
    • A ashok123

      I am not sure why I am getting error in displaying the output. I want output such that when I enter a string aaaabaaba... for "aaa" it shud give output 0 and "aba" it should give output 1. I am not sure where the error is??? Please help thanks is advance. #include #include #include #include char* in; void main() { cout << "Enter your string here \n"; cin>>in; while(*in != ' ') //it crashes here { if(strncmp(in, "aaa", 3)==0) cout << "0"; if(strncmp(in, "aba", 3)==0) cout << "1"; in+=3; } } A

      C Offline
      C Offline
      Christian Graus
      wrote on last edited by
      #2

      ashok123 wrote:

      char* in;

      This sets aside the memory to store an address, but it does not actually create a block of memory to store your string to. You have a pointer, but it's not pointing to anything.

      ashok123 wrote:

      while(*in != ' ') //it crashes here

      This code is wrong on a number of levels. First of all, it requires that the string have a space in it. Secondly, because you step by three further down, it assumes that the string will be either a space, or characters in a multiple of three, and then a space. The easiest way to solve all of this is to use std::string instead of a char *, and then use std::string's functions to check the contents of the string. Christian Graus - Microsoft MVP - C++

      A 1 Reply Last reply
      0
      • C Christian Graus

        ashok123 wrote:

        char* in;

        This sets aside the memory to store an address, but it does not actually create a block of memory to store your string to. You have a pointer, but it's not pointing to anything.

        ashok123 wrote:

        while(*in != ' ') //it crashes here

        This code is wrong on a number of levels. First of all, it requires that the string have a space in it. Secondly, because you step by three further down, it assumes that the string will be either a space, or characters in a multiple of three, and then a space. The easiest way to solve all of this is to use std::string instead of a char *, and then use std::string's functions to check the contents of the string. Christian Graus - Microsoft MVP - C++

        A Offline
        A Offline
        ashok123
        wrote on last edited by
        #3

        Thanks Chris...:-D

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups