make it keep executing until user enters correct answer

// *******add do or while loop in it , because u dont want the user to excute again to make the user guess correctly.


#include <iostream>

#include <cstring>
using namespace std;
int main ()
{
char szKey[] = "orange";
char szInput[80];


cout <<" Guess What is my favourite fruit? ";
cin >> szInput ;

if (strcmp (szKey,szInput) == 0)
cout << "Correct answer!\n";
else
cout << "oooooppppppss !!!wrong answer\n";
return 0;
}
Last edited on
thanks but i still can,t do it , i tried that but i think in a wrong way ,


// *******add do or do while loop in it , because u dont want the user to excute again to make the user guess correctly.


#include <iostream>

#include <cstring>
using namespace std;
int main ()
{
char szKey[] = "orange";
char szInput[80];
do {
cout <<" Guess What is my favourite fruit? ";
cin >> szInput ;

if (strcmp (szKey,szInput) == 0)
cout << "Correct answer!\n";
else
cout << "oooooppppppss !!!wrong answer\n";
}
while ( szInput == szKey);
return 0;
}
Does it have to be a character array?

I'd be inclined to use
#include<string>
replace char with string

// *******add do or do while loop in it , because u dont want the user to excute again to make the user guess correctly.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include <string>
#include <cstring>
using namespace std;
int main ()
{
string szKey = "orange";
string szInput;
do { 
cout <<" Guess What is my favourite fruit? ";
getline(szInput, cin); // Allows for multiple word answers e.g. Passion fruit

if (szInput == szKey){              // If Key and Input equal
cout << "Correct answer!\n";}  // Print correct answer
else {
cout << "oooooppppppss !!!wrong answer\n";
};
while ( szInput != szKey);                 // Do UNTIL right answer is got i.e. repeat while Input is not equal to szKey
return 0;
}


This isn't tested, but should work.
Last edited on
Thanks , but i copied yours but i has little 4 errors , so i am unable to fix them ...can u please compilt and see yourself ??? Thanks
closed account (D80DSL3A)
This is (lightly) tested and appears to work:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
using namespace std;
int main()
{
	char szKey[] = "orange";
	char szInput[80];
	bool correct = false;

	do
	{
		cout <<" Guess What is my favourite fruit? ";
		cin >> szInput ;

		if (strcmp (szKey,szInput) == 0)
		{
			cout << "Correct answer!\n";
			correct = true;
		}
		else
			cout << "oooooppppppss !!!wrong answer\n";
	}
	while( !correct );
	return 0;
}
you should #include <string> and use std::strings instead of char arrays, the only things that will need to change are that youll need to use getline() rather than cin to take input, and you dont need to use strcmp any more you can compare two strings with the == operator.
Ah; rookie error on my part - when I used getline I had the arguments the wrong way round.

Error #1: Getline takes the input stream (cin) as the first argument and variable as the second. So if you want the input to be taken as a string input change the getline statement:
getline(cin, szInput); // Allows for multiple word answers e.g. Passion fruit

Error #2: Program ends before closing brace - add another } where required

After these two are fixed it runs fine for me.
Topic archived. No new replies allowed.