here is my code
Assignment 2.cpp
here is my code
Assignment 2.cpp
Your code is short enough to post in [code][/code] bbcode tags. One problem I see is that you need to be careful about the difference between = and ==
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
i was able to fix it so that it runs now (redid the entire code, removing the loop), but now the program is not randomly assigning the operand(sign/sgn/+-) for the operations, but it generates a random number for the number1 and number2 every startup.
heres the code
#include <iostream> //brings in librarys for certain functions
#include <iomanip>
#include <string>
#include <stdlib.h>
using namespace std;
int main()
{
int number1;
int number2;
string sign;
bool sgn;
int answer;
int correct;
srand(time(NULL));
number1 = rand() % 1000 + 1;
number2 = rand() % 1000 + 1;
sgn = rand() % 2;
if (sgn = 0)
sign = "+";
else if (sgn = 1)
sign = "-";
else
cout << "Error" << endl;
std::cout.width(5); cout << std::right << number1 << endl;
cout << sign << std::left << " " << number2 << endl;
std::cout.width(5); cout << "_____" << endl;
cout << "Please Type your Answer: " << endl;
cin >> answer;
if (sgn = 1)
correct = number1 + number2;
else
correct = number1 - number2;
if (answer == correct)
cout << "You are Correct" << endl;
else
cout << "You are Incorrect" << endl;
cout << "Have a Nice Day" << endl;
std::cout << "Press ENTER to continue... " << flush; //this pauses the prompt window
cin.ignore(cin.rdbuf()->in_avail() + 1);
return 0;
}
Last edited by Chuck Charles; 02-06-2015 at 12:58 PM.
Put code in code tags.
Code:#include <iostream> //brings in librarys for certain functions #include <iomanip> #include <string> #include <stdlib.h> using namespace std; int main() { int number1; int number2; string sign; bool sgn; int answer; int correct; srand(time(NULL)); number1 = rand() % 1000 + 1; number2 = rand() % 1000 + 1; sgn = rand() % 2; if (sgn = 0) sign = "+"; else if (sgn = 1) sign = "-"; else cout << "Error" << endl; std::cout.width(5); cout << std::right << number1 << endl; cout << sign << std::left << " " << number2 << endl; std::cout.width(5); cout << "_____" << endl; cout << "Please Type your Answer: " << endl; cin >> answer; if (sgn = 1) correct = number1 + number2; else correct = number1 - number2; if (answer == correct) cout << "You are Correct" << endl; else cout << "You are Incorrect" << endl; cout << "Have a Nice Day" << endl; std::cout << "Press ENTER to continue... " << flush; //this pauses the prompt window cin.ignore(cin.rdbuf()->in_avail() + 1); return 0; }
Found out why it never switched between + and -, i had it as a bool, and when you do the rand() % 2; it pulls a random integer not a T or F('1' or '0') and when it runs it gets stuck at 1.
No, that is not the problem. Conversion from int to bool is well defined: zero is converted to false; non-zero is converted to true. The problem is what I noted in post #2: you need to be careful about the difference between = and ==. Look at:Originally Posted by Chuck Charles
Code:sgn = rand() % 2; if (sgn = 0) sign = "+"; else if (sgn = 1) sign = "-"; else cout << "Error" << endl;
- Suppose that rand() returns an even number. sgn would then be equal to 0. In the if statement, you then assign 0 to sgn. Since 0 evaluates to false, control proceeds to the else. If the if statement, you then assign 1 to sgn. Since 1 evaluates to true, you then assign "-" to sign.
- Suppose that rand() returns an odd number. sgn would then be equal to 1. In the if statement, you then assign 0 to sgn. Since 0 evaluates to false, control proceeds to the else. If the if statement, you then assign 1 to sgn. Since 1 evaluates to true, you then assign "-" to sign.
In other words, no matter what rand() returns, you always end up with "-" assigned to sign. Rather, you should use == to compare for equality, e.g.,
[code]sgn = rand() % 2;
But this is a little dumb: sgn is a bool, so why compare it with 0? In fact, you do not need sgn at all:Code:if (sgn == 0) { sign = "+"; } else { sign = "-"; }
I continue to use the braces here even though the block only contains one statement because I find that this makes a mistake when adding statements to a block less likely.Code:if (rand() % 2 == 0) { sign = "+"; } else { sign = "-"; }
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)