CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Nov 2004
    Location
    Texas, earth, Mecca, soul
    Posts
    58

    Post "Would you like to play a game?"

    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?)

    Code:
    
    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
    "Nay, you love this present life!"

    Save your keystrokes!

  2. #2
    Join Date
    Apr 2004
    Location
    Canada
    Posts
    1,342

    Re: "Would you like to play a game?"

    I'm afraid you don't really understand your code...

    First of all

    Code:
    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

    Code:
    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured