Problems with an If loop..
Alright, Now the purpose of the program is to generate two random numbers both from the pool of 1-50 and have them come in the form of the question asking the user to input the sum. They get two tries and after that it says your wrong and prompts you to press return and the program ends. So far it seemed fine til i got the if loop. Something isn't right.
Could someone tell me what I'm doing wrong? Thanks!
Code:
#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;
int main()
{
srand((unsigned)time(0));
int answer;
int realanswer;
int random_integer1;
int random_integer2;
int lowest=0, highest=49;
int range=(highest-lowest)+1;
for(int index=0; index<1; index++){
random_integer1 = lowest+int(range*rand()/(RAND_MAX + 1.0));
endl;
}
{
for(int index=0; index<1; index++){
random_integer2 = lowest+int(range*rand()/(RAND_MAX + 1.0));
endl;
cout << "If I add " << random_integer1 << " and " << random_integer2 <<
" then what do I have? " << endl;
cin >> answer;
}
{
system("PAUSE");
}
{
if (answer == realanswer)
{
cout << "You are Right! " << endl;
}
else
{
if (answer != realanswer)
{
cout << "Whoops Try again! :" << endl;
}
else
{
if (answer == realanswer)
{
cout << "You are Right :" << endl;
}
else
{
if (answer != realanswer)
cout << "The answer was :" << realanswer << endl;
}
system("PAUSE");
return 0;
}
}
}
}
}
How could I repeat this loop 10 times?
if i have this, could i put the for loop inside another loop and so that the student can take a little pop quiz and see the percent wrong?
Code:
#include <iostream>
#include <ctime>
#include <conio.h>
#include <cstdlib>
using namespace std;
void main()
{
srand((unsigned)time(0));
int answer,i;
int realanswer;
int random_integer1;
int random_integer2;
int lowest=0, highest=49;
int range=(highest-lowest)+1;
char ch;
{
random_integer1 = lowest+int(range*rand()/(RAND_MAX + 1.0));
random_integer2 = lowest+int(range*rand()/(RAND_MAX + 1.0));
cout << "If I add " << random_integer1 << " and " << random_integer2 <<
" then what do I have? " << endl;
cin >> answer;
realanswer=random_integer1+random_integer2;
}
for(i=0;i<2;i++)
{
if (answer == realanswer)
{
cout<<"You are Right"<<"\n";
break;
}
else
{
if(i==1)
{
cout<<"You are wrong. The right answer is "<<realanswer<<"\n";
}
else
{
cout<<"Oops! You are wrong. Try again...\n";
cin>>answer;
}
}
}
}
Could I use a while loop?
If the while loop repeats code until a condition is met then You can cause that loop that causes the answer to be correct or incorrect to repeat itself?
Okay So I've been tweaking it and
It atleast runs the questions 10 times. But it doesn't grade them after each time. hrm..any ideas?
Code:
#include <iostream>
#include <ctime>
#include <conio.h>
#include <cstdlib>
#include <string>
using namespace std;
void main()
{
srand((unsigned)time(0));
float itiner[9];
string num[10];
int answer,i;
int realanswer;
int random_integer1;
int random_integer2;
int lowest=0, highest=49;
int range=(highest-lowest)+1;
char ch;
//Repeating The question
{ for(int i = 0;i <= 9;i++)
{
itiner[i] = i + 1;
}
for(int i = 0;i <= 9;i++)
{
random_integer1 = lowest+int(range*rand()/(RAND_MAX + 1.0));
random_integer2 = lowest+int(range*rand()/(RAND_MAX + 1.0));
cout << "If I add " << random_integer1 << " and " << random_integer2 <<
" then what do I have? " << endl;
cin >> answer;
realanswer=random_integer1+random_integer2;
}
//Determining factor of correctness of question.
for(i=0;i<2;i++)
{
if (answer == realanswer)
{
cout<<"You are Right"<<"\n";
break;
}
else
{
if(i==1)
{
cout<<"You are wrong. The right answer is "<<realanswer<<"\n";
}
else
{
cout<<"Oops! You are wrong. Try again...\n";
cin>>answer;
}
}
}
}
}
Progress but it still stinks.
Okay so I've been working on it, and I removed void like you wanted me to..hasn't really changed the program much..I really need a pointer on how to fix my loop..it works fine until i try to make it go 10 times with the " for (int j = 0; j < 10; ++j) " line.
Code:
#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;
int main()
{
srand((unsigned)time(0));
int answer,i;
int realanswer;
int random_integer1;
int random_integer2;
int lowest=0, highest=49;
int range=(highest-lowest)+1;
char ch;
{
for (int j = 0; j < 10; ++j)
random_integer1 = lowest+int(range*rand()/(RAND_MAX + 1.0));
random_integer2 = lowest+int(range*rand()/(RAND_MAX + 1.0));
cout << "If I add " << random_integer1 << " and " << random_integer2 <<
" then what do I have? " << endl;
cin >> answer;
realanswer=random_integer1+random_integer2;
}
{
for(i=0;i<2;i++)
{
if (answer == realanswer)
{
cout<<"You are Right"<<"\n";
break;
}
else
{
if(i==1)
{
cout<<"You are wrong. The right answer is "<<realanswer<<"\n";
}
else
{
cout<<"Oops! You are wrong. Try again...\n";
cin>>answer;
}
}
}
}
}