Almost got it to work issues with complier

I had rcast answer most of the issues with the code and a few other questions. Now if i could please get some assistance with my complier issue. He has fixed the problems that I had with the code as shown in the post and runs fine with VC however if i run with DEV it will not run correctly. Any suggestions
Last edited on
1.) Your code places the randomization of the math problem outside of the loop, which doesn't allow new questions to be generated.


2.) You must include the opening and closet brackets, otherwise while only affects the line directly below the declaration of the loop

 
while(response == "y" || response == "y")


should be

1
2
3
4
while(response == "y" || response == "y")
{
...
}


Also, you've added code for a second question, when the first question segment was sufficent. Here is a fully functioning code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include <iostream>
#include <string>
#include <cmath>
#include <iomanip>
#include <ctime>
#include <cstdlib>
using namespace std;

string name;
int randNum1;
int randNum2;
int signRoll;
int answer;
int userAnswer;
string addition = "+";
string subtract = "-";
string sign;
string divider = "--------";
string answerQuestion= "What is your answer?";
string good =" Nice Job";
string bad = "I'm sorry, that is not correct";
string response = "y";
int main()

{
cout <<"Welcome, Please enter your Name:";
cin>> name;
cout <<"Thanks!,"<< name;

while(response == "y" || response == "y") {

srand((unsigned)time(0));
randNum1 = 1 + rand() % 20;
randNum2 = 1 + rand() % 20;
signRoll= 1 + rand() % 1;

if(signRoll == 1)
{sign = addition;
cout<< fixed << showpoint<< setprecision(1);
cout<<left<<setw(4)<<right<<setw(5)<< randNum1 <<endl;
cout<<left<< setw(2)<<sign <<right<<setw(3)<< randNum2 <<endl;
cout << divider << endl;
answer = randNum1 + randNum2 ;
cout <<""<< endl;
cout<< answerQuestion << endl;
cin >> userAnswer;
if(answer == userAnswer)
{cout <<good; }
else{cout << bad << endl;
cout <<"The Correct answer is: "<< answer << endl;
cout << endl;}
cout << " Do you want another problem? (Y/N)";
cin >> response;}

}
return 0;
}
Last edited on
So I just want to make sure I understand you correctly if this is going to effect the entire program to include problem one and problem 2 so on and so forth then write the above and have the brackets at start of problem 1 and at the very last possible problem. ??
the first problem is repeating itself instead of randomly generatoring #;s for the next problem and it also isnt working for subtraction.
See my answer above _ it's edited with all the correct info.

The while loop {.. } should house everything the program will do, that ever needs to be repeated while the condition of the while loop (response == y) is true.
Last edited on
okay so that is the part I was misunderstanding in the tutorial. The while for the response is actually the loop to continue to either answer more problems or stop. I think i got it. So will it change from subtraction to addition and vic versa on its own?? Thanks for the assistance also
see my above post rcast even with the code that you have put above I've tested it and its still outputting the same problem every time. trying to find out why if it has a generator srand(unsigned)(time)(0) is this not correct??
No - there were a few mistakes but you were close

Here is the code that will work fully, but I hope your son learned about negative numbers because the way you have it setup the random number to be subtracted could be lesser than the number to subtract

Basically, i redid the if statement that defines signRoll and changed the signRoll to equal 1 + rand() % 2 instead.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include "stdafx.h"
#include <iostream>
#include <string>
#include <cmath>
#include <iomanip>
#include <ctime>
#include <cstdlib>
using namespace std;

string name;
int randNum1;
int randNum2;
int signRoll;
int answer;
int userAnswer;
string addition = "+";
string subtract = "-";
string sign;
string divider = "--------";
string answerQuestion= "What is your answer?";
string good =" Nice Job";
string bad = "I'm sorry, that is not correct";
string response = "y";
int main()

{
cout <<"Welcome, Please enter your Name:";
cin>> name;
cout <<"Thanks!,"<< name;

while(response == "y" || response == "y") {

	srand((unsigned)time(0));
	randNum1 = 1 + rand() % 20;
	randNum2 = 1 + rand() % 20;
	signRoll= 1 + rand() % 2;

	// Decide to use addition or subtraction
	if(signRoll == 1){
		sign = addition;
		answer = randNum1 + randNum2 ;
	}else{
		sign = subtract;
		answer = randNum1 - randNum2 ;
	}

	cout<< fixed << showpoint<< setprecision(1);
	cout<<left<<setw(4)<<right<<setw(5)<< randNum1 <<endl;
	cout<<left<< setw(2)<<sign <<right<<setw(3)<< randNum2 <<endl;
	cout << divider << endl;
	cout <<""<< endl;
	cout<< answerQuestion << endl;
	cin >> userAnswer;

	if(answer == userAnswer)
	{
		cout <<good;
	}else{
		cout << bad << endl;
		cout <<"The Correct answer is: "<< answer << endl;
		cout << endl;
	}
	cout << " Do you want another problem? (Y/N)";
	cin >> response;
}
return 0;
}
Last edited on
do you have a suggestion as how to fix this so it would not allow that to happen
Here you go, code to fix the negative number issue: Just added variable tempNum and a small if condition inside the else of signRoll if condition, that tests if randNum1 is < randNum2 and if it is, have the two variables trade values.

Now all you have to do is fix the formatting and spacing issues with the whitespaces.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include <iostream>
#include <string>
#include <cmath>
#include <iomanip>
#include <ctime>
#include <cstdlib>
using namespace std;

string name;
int randNum1;
int randNum2;
int tempNum;
int signRoll;
int answer;
int userAnswer;
string addition = "+";
string subtract = "-";
string sign;
string divider = "--------";
string answerQuestion= "What is your answer?";
string good =" Nice Job";
string bad = "I'm sorry, that is not correct";
string response = "y";
int main()

{
cout <<"Welcome, Please enter your Name:";
cin>> name;
cout <<"Thanks!,"<< name;

while(response == "y" || response == "y") {

	srand((unsigned)time(0));
	randNum1 = 1 + rand() % 20;
	randNum2 = 1 + rand() % 20;
	signRoll= 1 + rand() % 2;

	// Decide to use addition or subtraction
	if(signRoll == 1){
		sign = addition;
		answer = randNum1 + randNum2 ;
	}else{
                sign = subtract;
		if (randNum1 < randNum2){
			tempNum=randNum1;
			randNum1=randNum2;
			randNum2=tempNum;
		}
	        answer = randNum1 - randNum2 ;
	}

	cout<< fixed << showpoint<< setprecision(1);
	cout<<left<<setw(4)<<right<<setw(5)<< randNum1 <<endl;
	cout<<left<< setw(2)<<sign <<right<<setw(3)<< randNum2 <<endl;
	cout << divider << endl;
	cout <<""<< endl;
	cout<< answerQuestion << endl;
	cin >> userAnswer;

	if(answer == userAnswer)
	{
		cout <<good;
	}else{
		cout << bad << endl;
		cout <<"The Correct answer is: "<< answer << endl;
		cout << endl;
	}
	cout << " Do you want another problem? (Y/N)";
	cin >> response;
}
return 0;
}


Now math is a positive thing for your child! Get it? ha..ha..
Last edited on
Thanks so much for your help. I think I'm still missing something here and since I'm just a beginner at this I can't exactly explain what it is thats wrong or what I'm looking for.

The code when ran is displaying i.e 10+14 = What is the answer ___ then you type your answer 24 so it tells you your correct. This is all working great.

However the best way i can explain it is that the loop and rand num generator arent talking because its looping but not regeneratoring a new problem so after you respond Y for another problem

it simply goes back to 10 + 14 again ...

Also if you hit n for no it still asks you what is the answer
Hmm this is odd, because when I run the program I get a random stream of addition and subtraction problems, and when I type n, it closes. It runs perfect for me?? Maybe it's your compiler (i've seen a lot of people on this forum say that if two people are getting different results - not really sure about what it means though since I just use VC++ 2008)

Try erasing ALL of your code from the screen - and copy + pasting the last version of my code above into your program and compile and run
Last edited on
Okay, thats why i wanted to ask I'm using netbeans and Dev C ++. Do you know of anything that I would need to change. Maybe there is something that I'm missing for my complier or program. I'm in a windows 7 environment and the complier shows no errors it opens CMD line perfectly and displays the first problem with no issues.

I noticed that i copy your code and put it into my program and I had to delete the first line of text because its from VC and its giving me an error also

I have the mingw as the complier with dev c currently running.

I have deleted everything and recopied it however maybe I'm creating the wrong type of file or something would that have anything to do with it.

I'm creating a console application.
Last edited on
Try erasing ALL of your code from the screen - and copy + pasting the last version of my code above into your program and compile and run.

If this doesn't work, break emergency glass and extract hammer.

There are smart people in this forum and somebody will help you identify why I can run the program with successful results and you cannot. Just check back to this thread later.

In the mean time I can recommend getting Visual C++ Express (free at microsoft) or code::blocks instead. Dev C++ I've read doesn't have as many features.
Last edited on
Thanks for all your help. I hope to learn more. I have actually ran the code you posted in a different complier successfully. I just have to figure out why its not allowing it to run that way in the console cmd line.
44
45
46
47
48
		if (randNum1 < randNum2){
			randNum1=tempNum;
			randNum1=randNum2;
			randNum2=tempNum;
		}

Above code shows the right intention to swap the values, but the variables are the wrong way round.

It should be more like this:
44
45
46
47
48
            if (randNum1 < randNum2) {
                int tempNum = randNum1;
                randNum1=randNum2;
                randNum2=tempNum;
            }

Notice also, there is no need for tempNum to be declared as a global variable. In general the scope of a variable should be limited to the part of the code where it will be used, in order to avoid accidental side-effects.
Topic archived. No new replies allowed.