# Problems with an If loop..

This is a discussion on Problems with an If loop.. within the C++ Programming forums, part of the General Programming Boards category; Alright, Now the purpose of the program is to generate two random numbers both from the pool of 1-50 and ...

1. ## 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 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;
}

{

system("PAUSE");
}

{
{

cout << "You are Right! " << endl;
}
else
{

{
cout << "Whoops Try again! :" << endl;
}
else
{
{
cout << "You are Right :" << endl;
}
else
{
}
system("PAUSE");

return 0;
}
}
}
}
}```

2. Indentation is horrible. I'm afraid you're going to have to fix that first:
http://cpwiki.sf.net/User:Elyisa/Indentation

3. > for(int index=0; index<1; index++)
index isn't going to remain <1 for very long.

4. 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 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;

}

for(i=0;i<2;i++)
{
{
cout<<"You are Right"<<"\n";
break;
}
else
{
if(i==1)
{
}
else
{
cout<<"Oops! You are wrong. Try again...\n";
}
}
}
}```

5. ## 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

6. ## 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 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;

}

for(i=0;i<2;i++)
{
{
cout<<"You are Right"<<"\n";
break;
}
else
{
if(i==1)
{
}
else
{
cout<<"Oops! You are wrong. Try again...\n";
}

}
}
}```

7. ## 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?

8. ## 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 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;

}

//Determining factor of correctness of question.
for(i=0;i<2;i++)
{
{
cout<<"You are Right"<<"\n";
break;
}
else
{
if(i==1)
{
}
else
{
cout<<"Oops! You are wrong. Try again...\n";
}

}
}
}
}```

9. 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.

10. ## 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 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;

}

{
for(i=0;i<2;i++)
{
{
cout<<"You are Right"<<"\n";
break;
}
else
{
if(i==1)
{
}
else
{
cout<<"Oops! You are wrong. Try again...\n";
}
}
}
}
}```

11. 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.

12. 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;

}```
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.

13. ## Is this better?

Code:
```#include <iostream>
#include <ctime>
#include <cstdlib>

using namespace std;

int main()
{
srand((unsigned)time(0));
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;

}

for(i=0;i<2;i++)
{
{
cout<<"You are Right"<<"\n";
break;
}
else
{
if(i==1)
{
}
else
{
cout<<"Oops! You are wrong. Try again...\n";
}
}
}
}```

14. No!