#include <cmath>
#include <ctime>
#include<iostream>
using namespace std;
int main()

{

srand (time(0));

int c1, c2, c3, c4, guess, guess2;



c1 = 1 + rand() % ( 9 - 1 + 1);
c2 = 1 + rand() % ( 9 - 1 + 1);
c3 = 1 + rand() % ( 9 - 1 + 1);
c4 = 1 + rand() % ( 9 - 1 + 1);




cout << c1 << " " << "X" << " " << c2 << " " << "=" << " ";
cin >> guess;


 if (guess == true)
        cout <<"Correct" << endl;
 else
        cout <<"\nWrong" << endl;



char play;

cout <<"\nWould you like to play again?(Y/N)";
cin >> play;

if (play == 'Y' || play == 'y')

cout << c3<< " " << "X" << " " << c4<< " " << "=" << " ";
cin >> guess2;


if (guess2 == true)
        cout <<"Correct" << endl;
else
        cout <<"\nWrong" << endl;



system("pause");
return 0;
}

so my task is to create a program that generates two random numbers 1-9 and asks the product.

so where im stuck at, lets say it generates 1 X 5 = so you would put 5 obviously. now how do i get it to say correct. since u put the right asnwer and it says wrong. i know if i were to put the 2 numbers myself instead of random generating 1 X 5 = i couldt esily just put in my if statement , if (guess == 5) cout << "correct" but since im random generating it how would i do it ?

also the program ask the user type y/n if you want to play again. if 'y' cout another problem but lets say he type 'n' for no i dont want to play again, what do i need to put so it can just close the program "press any key to continue"

hope what i said made sense.

help if you can, thanks.

Recommended Answers

All 8 Replies

for ur first question, u could put if( guess = = (c1 *c2)) { cout<< correct;}

for ur second question ,
if(play == 'N') { exit(0);}

this will exit the porogram

thanks!

but im still having trouble with the (play == 'N" || play == 'n')
exit(0);

i type N then it goes to the next line and need to hit another key so it can exit.

hey this might help

#include <cmath>
#include <ctime>
#include<iostream>
using namespace std;
int main()

{

srand (time(0));

int c1, c2, c3, c4, guess, guess2;



c1 = 1 + rand() % ( 9 - 1 + 1);
c2 = 1 + rand() % ( 9 - 1 + 1);
c3 = 1 + rand() % ( 9 - 1 + 1);
c4 = 1 + rand() % ( 9 - 1 + 1);


int a =c1*c2;

cout << c1 << " " << "X" << " " << c2 << " " << "=" << " ";
cin >> guess;


 if (guess == a)
        cout <<"Correct" << endl;
 else
        cout <<"\nWrong" << endl;



char play;

cout <<"\nWould you like to play again?(Y/N)";
cin >> play;

if (play == 'Y' || play == 'y'){

cout << c3<< " " << "X" << " " << c4<< " " << "=" << " ";
cin >> guess2;

int b=c3*c4;

if (guess2 == b){
        cout <<"Correct" << endl;
}
else{
        cout <<"\nWrong" << endl;
}
}
else {
    return 1;
}

system("pause");
return 0;
}

ha! that works thanks a lot. i probably will be back asking for more help since im a noob X] but thank you all for the help.

hmmm for arpy giri, lets say i want to continue playing so i add another problem(c5 and c6) and after i ask the user if he want to keep playing well i basically copied the part "else { return 1;}" and i type N and it just gives you another problem instead of exiting.

in that case ,i think u ought to declare a function like this

#include <cmath>
#include <ctime>
#include<iostream>
using namespace std;
int choose();

int main()

{
choose();
return 0;
}
int choose(){
srand (time(0));

int c1, c2,guess;



c1 = 1 + rand() % ( 9 - 1 + 1);
c2 = 1 + rand() % ( 9 - 1 + 1);



int a =c1*c2;

cout << c1 << " " << "X" << " " << c2 << " " << "=" << " ";
cin >> guess;


 if (guess == a)
        cout <<"Correct" << endl;
 else
        cout <<"\nWrong" << endl;



char play;

cout <<"\nWould you like to play again?(Y/N)";
cin >> play;

if (play == 'Y' || play == 'y'){

choose();
}

else {
    return 1;
}

}

did this help?

if not try exit then
but return 1 works well in my GNU compilier with linux as my OS

why don't you use the label command if the user wants to play again and again.

#include <cmath>
#include <ctime>
#include<iostream>
using namespace std;

int main()

{

srand (time(0));
int c1, c2, guess;
char play;
p:

for(int y=0;y<2;y++)
{
    c1 = 1 + rand() % ( 9 - 1 + 1);
    c2 = 1 + rand() % ( 9 - 1 + 1);
    cout << c1 << " " << "X" << " " << c2 << " " << "=" << " ";
    cin >> guess;
    int p=c1*c2; // the correct calculation

    if (guess == p) // if your guess = the correct calculation
    {
        cout <<"Correct" << endl; // then it would display correct
    }
    else  // else it would display incorrect and would give the correct answer as well
    {
        cout <<"\nWrong ! ,the correct answer is " <<p<< endl;
    }
}

    cout <<"\nWould you like to play again?(Y/N)"; // ask if the user wants to play the game again
    cin >> play;

    if (play == 'Y' || play == 'y')

        goto p; // this will send it back to the point where it says " p: " on the top .. but this is not used in java or languages that came after c++ as programmers found it difficult to work with larger programs.

    else
            cout <<"Thank You For Participating !" << endl; // saying bye

system("pause"); 
return 0;
}

// good luck !
// local p ...
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.