Click to See Complete Forum and Search --> : "Would you like to play a game?"


Mansoor8
December 3rd, 2004, 02:35 PM
Good Day:
I am working on a coding project and have come to an area where the logic does respond correctly; this is normal, of course, but even when I manipulate and alter this snippet in every way I know how, I only get a different error. I appreciate if some one could take the time to view and analyze this bit and offer their opinion. If there is anything you might need to know concerning the whole, overall code to this project please ask me here.

(*also, I am easily confused by curly-brace use for functions, and now I have read while researching the topic that curly-brackets are optional in many cases: how can this be and what is the truth for their implementation in C++ code work?)



cout<< "Would you like to play a game?\n"<< endl;
cin.getline(string, 256, '\n');
std::string::size_type idx3;//--VAR
idx3 = name.find("sure");
idx3 = name.find("no");

std::string::size_type idx4;//--VAR???????
idx3 = name.find("abcdeghijklmqrtuvwxyz");//---??????????
cout<< "\n"<< endl; //-- Spacer


if(idx3 !=string::npos ) //--works
cout<< "Oh boy, what fun!\n"<< endl;


else if(idx3 !=string::npos ) //--works
cout<< "Why not?\n"<< endl;

else if (idx4 !=string::npos ) //--works
cout<< "Fine.\n"<< endl;






Thank-you so much...
Mansoor

HighCommander4
December 3rd, 2004, 07:29 PM
I'm afraid you don't really understand your code...

First of all

cin.getline(string, 256, '\n');

"string" is the name of a type, certainly not the name of a valid variable. This line could well be producing errors. You need to name your variable soem other name, since string is already an identifier.

Second, you assign the return values of two different calls to std::string::find() to "idx3" and do nothing with them.

You declare a variable "idx4", do not initalize it in any way, then use it in the if statement

else if (idx4 !=string::npos )

as if it would be initialized.

Also, it would be helpful the post the segment of your code where you declare the variable "name". In fcat, it would be helpful if you posted your complete code in the first place.