/*
 Written by HAYZAM SHERIF ! USE IT HOWEVER YOU WANT :D
*/


#include <iostream>
#include <string>

using namespace std;

char name[100];
wchar_t words[100];
int choice;
int main()


{ //main
    string userstring;

    string mystring;
    mystring = "THIS GAME IS COOL";

cout<<"Hello Welcome to the Code Game!"<<endl;    
cout<<"--------------x----------------"<<endl;
cout<<endl;
cout<<"Please enter yourname : ";
cin>>name;
cout<<endl;
cout<<"Welcome to the code game, ";
cout<<name<<endl;

cout<<"1. New game"<<endl;
cout<<"2. Level Select"<<endl;
cout<<"3. About"<<endl;
cout<<"--------------x----------------"<<endl;
cout<<endl;
cout<<"Please enter choice : "<<endl;
cin>>choice;

if (choice == 1)
{
           cout<<"New game huh? Cool ! Let's start with some small codes!"<<endl;
           cout<<endl;
           cout<<endl;

           cout<<"1    2    3"<<endl;
           cout<<"ABC  DEF  GHI"<<endl;
           cout<<"4    5    6"<<endl;
           cout<<"JKL  MNO  PQR"<<endl;
           cout<<"7    8    9"<<endl;
           cout<<"STU  VXY  Z "<<endl;

           cout<<endl;
           cout<<endl;

           cout<<"I know there is no 'w' it is supposed to be like that."<<endl;
           cout<<endl;
           cout<<"While playing this game please turn on your caps lock."<<endl;
           cout<<endl;
           cout<<endl;
           cout<<"Try not to use Paper/Pen."<<endl;
           cout<<endl;
           cout<<"Here is your first code : [7337] [3152] [37] [1554] "<<endl;
           cout<<endl;
           cout<<endl;
           cout<<"The sentence is : ";
           cin>>userstring;
           cout<<endl;

           if (userstring.compare("THIS GAME IS COOL"))
           {
               cout<<"You're a right! Congratulations!"<<endl;
               //if for within if answer
                        }

                        else 
                        {
                             cout<<"This is WRONG! No bharat ratna for you!"<<endl;  
                                               } //else for within if answer 
                                               } // main if  

else if (choice == 2)
{//else if for real choice (2)
     cout<<"Please select Level"<<endl;
     } //else if for real choice (2)
else if(choice == 3)
{ //else if for real choice (3)
     cout <<"This game was made by HAYZA<! "<<endl;
     }     //else if for real choice (3)
else 
{ //else for no real choice 
     cout << "Sorry this is not a real choice."<<endl;
     }    //else for no real choice 



system("PAUSE"); 
    return 0;
}   //main

The error that I get is that, the program show's me that my sentence is correct even when it's wrong! What will I do ? Any help is really appretiated, thanks!

Recommended Answers

All 13 Replies

I'll answer your question with another question: what does string.compare return and what does that value represent?

    string userstring; //string that user inputs

    string mystring;
    mystring = "THIS GAME IS COOL";



if (mystring.compare(userstring))
           {
               cout<<"You're a right! Congratulations!"<<endl;
               //if for within if answer
                        }

                        else 
                        {
                             cout<<"This is WRONG!"<<endl;  
                                               } //else for within if answer 
                                               } // main if 

Correct?

Could you please fix the code for me? Please?

I'll repeat the question of decepticon: "What does string compare return?"
Look it up in whatever you use for reference manual.

l repeat the question of decepticon: "What does string compare return?"
Look it up in whatever you use for reference manual.

Okay so it should return 0 :P

I am now trying this using char, but still I get the same issue. Here's the code :

char ans[50]; //declaring the variable

if(ans == "THIS GAME IS COOL" || "this game is cool" || "This game is cool")
           {
                  cout<<"Correct!"<<endl;
                  }

                  else
                  {
                      cout<<"WRONG!"<<endl;
                      }

Now what is wrong with the code? If I compile and try to use the program, I always get it saying the sentence is correct! How can I fix this?

I think I found the issue but cannot solve it. Here is the new code and the problem I have

    string used("THIS GAME IS COOL");
    string in; 

           cin>>in;
           getline(cin,in);
           cout<<in;

           if (in == used)
           {


               cout<<"You're right! Congratulations!"<<endl;
               //if for within if answer
                        }

                        else 
                        {
                             cout<<"This is WRONG!"<<endl;  
                                               } //else for within if answer 
                                               } // main if  

In the output I get this issue, If the answer typed by the user is 'THIS GAME IS COOL' The computer doesn't read the whole line it justs reads 'GAME IS COOL' Why is that? (I found this out by 'coutting' the 'in' string.)

cin>>in;

This reads the first word in the stream.

getline(cin,in);

This reads the rest of the line in the stream. Remove the cin>>in; line and getline should include all of the user's input rather than a part of it.

cin>>in;

get's the input from the user, if I remove that how will I get the input from the user?

getline also gets input from the user...and from the same place (cin), which is the root of your problem.

But I cannot input any data if I remove the, cin>>in; line..

    string used("THIS GAME IS COOL");
    string in;

    cout<<"The sentence is : ";
           getline(cin, in);
           if (in == used)
           {
               cout<<"You're right! Congratulations!"<<endl;
               //if for within if answer
                        }

                        else 
                        {
                             cout<<"This is WRONG!"<<endl;  
                                               } //else for within if answer 
                                               } // main if  

This is what the code looks like now..If I run this I just end up getting "This is wrong" without even entering the data i.e (string in)

EDIT : It's fixed now, but I have to add the same code twice.. I don't know why. Here check this out :

       getline(cin, in);
       getline(cin, in);
       cout<<in;
       if (in == used)
       {
           cout<<"You're right! Congratulations!"<<endl;
           //if for within if answer
                    }

                    else 
                    {
                         cout<<"This is WRONG!"<<endl;  
                                           } //else for within if answer 
                                           } // main if 

This works perfectly!

Your problem is known and common. Take a look at this thread for details and resolutions.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.