I am inputting in the console but as I type the number I can only see the first character. If I typed 345 on the 3 would show in the console. The calculations are being performed by the code and outputted correctly.
How do I change settings for the console?
all of my variables are double. Should I make some thing a float, int, or something else. Eventually, I have to calculate percentage of correct answers so I need something with a precision of at least 2 places after the decimal.
The assignment is to build a program that will help elementary student practice multiplication. The randomly generated numbers to multiply sometimes produce a product with two digits. When I enter the two digits in my console only the first one is visible. The calculations are still performed by the program though.
#include <iostream>
#include <cstdlib>
#include <iomanip>
#include <string>
#include <ctime>
#include <cmath>
#include <fstream>
usingnamespace std;
//#include "GradeBook.h"
//int argc, const char * argv[]
int main(int argc, constchar * argv[])
{
double correctAnswer = 0;
double incorrectAnswer = 0;
double percentCorrect;
for(int counter = 1; counter<=5; counter++)
{
srand( (unsigned)time(0) );//randomize the randomizer with time as the seed
double x = rand() % 9 +1;//randomly generated number between 1 and 10
double y = rand() % 9 +1;//randomly generated number between 1 and 10
double product = x * y;//the correct answer
double answer;//student's answer input
cout << "What is " << x << " times " << y << "?" << endl;//prompt user for mulitplication answer to randomly generated problem
cin >> answer;//users answer to be checked and compared with computer's product
if (answer == product)//checking to see correctness of user's answer
{
++correctAnswer;
int goodStatement = rand() % 3 +1;
switch (goodStatement)
{
case 1:
puts("Very good!");
break;
case 2:
puts("Excellent");
break;
case 3:
puts("Nice work!");
break;
case 4:
puts("Keep up the good work");
break;
default:
puts("Default good");
break;
}
}
elsewhile (answer!= product)
{//while loop shall run with the same x and y values being presented. The program will not move forward until a correct answer is entered by user.
++incorrectAnswer;
int badStatement = rand() % 3 +1;
switch (badStatement)
{
case 1:
puts("No. Please try again");
break;
case 2:
puts("Wrong. Try once more");
break;
case 3:
puts("Don't give up");
break;
case 4:
puts("No. Keep trying");
break;
default:
puts("This is the default no");
break;
}
cout << "What is " << x << " times " << y << "?" << endl;
cin >> answer;
if (answer == product)//checking to see correctness of user's answer
{
++correctAnswer;
int goodStatement = rand() % 3 +1;
switch (goodStatement)
{
case 1:
puts("Very good!");
break;
case 2:
puts("Excellent");
break;
case 3:
puts("Nice work!");
break;
case 4:
puts("Keep up the good work");
break;
default:
puts("Default good");
break;
}
}
}
}
cout << "You got " << correctAnswer <<" of them correct. Great work!" << endl;
cout << "You missed " << incorrectAnswer << "." << endl;
percentCorrect = (correctAnswer / correctAnswer + incorrectAnswer)*100;
cout << percentCorrect << (setprecision(2)) << "%" << endl;
if (percentCorrect < .75) {
cout << "Please ask you teacher for extra help." << endl;
} else {
cout << "Congratulations, you are ready to go t the next level!" << endl;
}
}
This may not be related to your problem, but there are problems comparing doubles like you have. Floating point is stored as a binary fraction - not all numbers can be represented exactly. This always fails:
1 2
float a = 0.1; //a == 0.09999997
if (10 * a == 1.0) //fail == 0.99999997
changing the type to double doesn't help, still the same type of problem.
You can use numeric_limits<double>epsilon. Google to find out how to use it. Also look at the reference page at the top left of this page.
I tried what you posted and no it did not work. I can still only see one input character. The out put is fine. it printed 7 or 8 characters or a number in scientific notation.
I am starting to think that it is not my code but a setting or preference in Xcode.
The same thing happens for me, however I have noticed that if you switch the output drop down to "Target" or "All" after you compile the program, the proper numbers will show up.
That makes me feel a little better that the problem is not with my machine. I have bookmarked the forum post on Apple's communities that you sent us. I will be eagerly waiting for that update.