I have put together Guess the result Calculator where user gets prompted to enter their guess to randomly generated ( multiplication , addition or subtraction ) operation . everything works until i put while loop on in case if user doesn't enter correct guess or guesses result right but wants to try again. without while loop everything works up until they select Y for yes' to continue or 'no ' to quit after which programme obviously gets terminated no matter they select 'Y' on 'N'.with while loop on Xcode reports "build succeeded" and that the end of it no output at all . can somebody tell me where is the mistake or is it Xcode playing tricks on me ? Thx GC
#include <iostream>
#include <ctime>
#include <string>
using namespace std;int main (){
srand((int) time(0));
bool end{ false };
while (!end){}{
int RanNumber1 = rand() 0;
int RanNumber2 = rand() 0;
int mult = RanNumber1 * RanNumber2;
int add = RanNumber1 RanNumber2;
int sub = RanNumber1 - RanNumber2 ;
cout << " What's the result of " << endl;
//
int rand_num = int((std::rand() % 3));
switch (rand_num) {
case 0:
std::cout << RanNumber1 << " * " << RanNumber2 << " = " << std::endl;
break;
case 1:
std::cout << RanNumber1 << " " << RanNumber2 << " = " << std::endl;
break;
case 2:
std::cout <<RanNumber1 << " / " << RanNumber2 << " = " <<std::endl;
break;
}
int guess;
cin >> guess ;
if ( guess == mult || guess == add || guess == sub )
{
cout << " correct " << endl;
}
else if ( guess != mult || guess != add || guess != sub )
{
cout << " not right ! " << endl;
}
cout << "Do you want me to try again ? (Y | N) : "<< endl;
char go_on;
cin >> go_on;
end = ((go_on == 'Y') || (go_on == 'y')) ? false : true;
}
return 0;
}
====================================================================================
Here is now working one with While Loop on
====================================================================================
#include <iostream>
#include <ctime>
#include <string>
using namespace std;
int main (){
srand((int) time(0));
bool end{ false };
while (!end){}{
int RanNumber1 = rand() 0;
int RanNumber2 = rand() 0;
int mult = RanNumber1 * RanNumber2;
int add = RanNumber1 RanNumber2;
int sub = RanNumber1 - RanNumber2 ;
cout << " What's the result of " << endl;
//
int rand_num = int((std::rand() % 3));
switch (rand_num) {
case 0:
std::cout << RanNumber1 << " * " << RanNumber2 << " = " << std::endl;
break;
case 1:
std::cout << RanNumber1 << " " << RanNumber2 << " = " << std::endl;
break;
case 2:
std::cout <<RanNumber1 << " / " << RanNumber2 << " = " <<std::endl;
break;
}
int guess;
cin >> guess ;
if ( guess == mult || guess == add || guess == sub )
{
cout << " correct " << endl;
// cout << "Do you want me to try again ? (Y | N) : "<<endl;
}
else if ( guess != mult || guess != add || guess != sub )
{
cout << " not right ! " << endl;
// cout << "Do you want me to try again ? (Y | N) : "<<endl;
}
cout << "Do you want me to try again ? (Y | N) : "<< endl;
char go_on;
cin >> go_on;
end = ((go_on == 'Y') || (go_on == 'y')) ? false : true;
}
return 0;
}
CodePudding user response:
Your putting the While statement wrong, you should wrap the code in a while loop.
#include <iostream>
#include <ctime>
#include <string>
int main () {
srand((int) time(0));
bool end = true;
int RanNumber1 = std::rand() 0;
int RanNumber2 = std::rand() 0;
int mult = RanNumber1 * RanNumber2;
int add = RanNumber1 RanNumber2;
int sub = RanNumber1 - RanNumber2 ;
while (end) {
std::cout << " What's the result of " << std::endl;
int rand_num = int((std::rand() % 3));
switch (rand_num) {
case 0:
std::cout << RanNumber1 << " * " << RanNumber2 << " = " << std::endl;
break;
case 1:
std::cout << RanNumber1 << " " << RanNumber2 << " = " << std::endl;
break;
case 2:
std::cout <<RanNumber1 << " / " << RanNumber2 << " = " <<std::endl;
break;
}
int guess;
std::string message;
std::cin >> guess ;
message = ( guess == mult || guess == add || guess == sub ) ? " correct " : " not right ! ";
std::cout << message << std::endl;
std::cout << "Do you want me to try again ? (Y | N) : "<< std::endl;
char go_on;
std::cin >> go_on;
end = ((go_on == 'Y') || (go_on == 'y')) ? true : false;
}
return 0;
}
CodePudding user response:
Actually it works the way it is . tried again and it works . Xcode was tempered with !!!