Problems showing entered word in cout.

Ok In this code It wont go to the the first IF when i type Jorge. It always goes to the incorrect text and it wont show the word typed here " cout<<" You entered the word " << word1 << "!\n"; ". If anyone could help it would be appreciated and if someone culd tell me how to make the code return to the beginning after the 2nd IF.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
 case 1:
      char word1[50];

      cout<<" Good choice! \n";
      cout<<" Ok Lets begin. Please type the word Jorge: ";
      cin.getline ( word1, 50 );
      cin.ignore();
      if ( strcmp ( word1, "Jorge" ) == 0 ) {
        cout<<" Correct! Lets move on. \n";
        return 0;
      }
      if ( strcmp ( word1, "Jorge" ) != 0 ) {
        cout<<" You entered the word " << word1 << "!\n";
        cout<<" You are incorrect! Try again. ";
        return 0;


      break; 

Using a Boolean called "cont" & a do-while loop I think this provides a fairly simple solution to your problem:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
char word1[50];
bool cont=false;
      cout<<" Good choice! \n Ok let's begin.\n";
	  do
	  {
	  cout<<" Please type the word Jorge ";
	  cin.getline ( word1, 50 );
	  if(strcmp(word1,"Jorge")==0)
	  {
	  cout<<" Correct! Let's move on.\n";
	  cont=true;
	  }
	  else
	  {
	  cout<<" You entered the word "<<word1<<"!\nYou are incorrect! Try again.\n";
	  }
	  }
	  while(!cont);
Hey thankyou ill start practicing on this code and figure out how u got that. But would i have to add another bool to keep on repeating when the words are typed incorrect? If i when i want it to go to the next one and not exit after it was typed wronge
Last edited on
I'm try to use with the orther way to turn it;if it wrong please exlain it to me:))
char word1[50]
cout<<" Good choice! \n Ok let's begin.\n";
while(1) //turn and do not stop
{
cout<<" Please type the word Jorge ";
cin.getline ( word1, 50 );
if(strcmp(word1,"Jorge")==0)
{
cout<<" Correct! Let's move on.\n";
cont=true;
break; //that to exit the program or use"exit(1);"to return the true answer
}
else
{
cout<<" You entered the word "<<word1<<"!\nYou are incorrect! Try again.\n";
}
}
Topic archived. No new replies allowed.