Why Error???
-
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
-
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
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++
-
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++