# Problems with an If loop..

Show 80 post(s) from this thread on one page
Page 1 of 4 1234 Last
• 01-27-2008
Sedvan
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; } } } } }```
• 01-27-2008
Elysia
Indentation is horrible. I'm afraid you're going to have to fix that first:
http://cpwiki.sf.net/User:Elyisa/Indentation
• 01-27-2008
Salem
> for(int index=0; index<1; index++)
index isn't going to remain <1 for very long.
• 01-28-2008
pravi
Hi,

Where have you assigned the value to "realanswer"?

Here is a tweaked code of the same....!
You can use the "while" loop to make it much better in case of "for".

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;                //This was missing in your program         }         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;                         }                 }         } }```
• 01-28-2008
Sedvan
Thanks
Wow...that clears things up a bit...It seems like I was putting to many if and else statements in there..it works now :P
• 01-28-2008
Sedvan
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;                         }                 }         } }```
• 01-28-2008
Sedvan
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?
• 01-28-2008
Sedvan
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;                         } } } } }```
• 01-29-2008
Elysia
Learn to indent and use int main, not void main. It's undefined.
THEN work on your problem. You are starting to have serious indentation problems, making the code difficult to read. And don't mix tabs and spaces. Stick to one.
• 01-29-2008
Sedvan
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;                         }                 }         } } }```
• 01-29-2008
Elysia
Code doesn't make any sense.
So you want it to generate two random numbers, ask what the sum of those becomes, and give the user two chances to answer right, and loop the whole thing 10 times, right?

You need to understand how your logic in the program is bad.
And you're still mixing tabs and spaces. Try removing one and it will do wonders to the code.
Btw, you also seem to be confused with the first for loop. Everything inside the { and } belongs to the loop, but since you placed it AFTER the first {, it means only the first line after the loop belongs to the loop.
• 01-29-2008
hk_mp5kpdw
Quote:

it works fine until i try to make it go 10 times
Make what go 10 times? Do you want a series of 10 random addition problems with 2 tries at the answer for each?

Code:

```{     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;         }```
That first brace '{' is in the wrong place... currently the only thing being done 10 times in that bit of code is that you only calculate random_integer1 over and over throwing away intermediate values.
• 01-29-2008
Sedvan
Is this better?
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;                         }                 }         } }```
• 01-29-2008
Elysia
No!