I'm a noob c++ student and also new here, so do let me know if I'm doing anything wrong!
Anyway, I really need help on a problem that has been plaguing me for awhile.
I've created a program for an assignment that randomly generates 10 arithmetic questions to be answered, and it all worked out fine except for the division part. I can't seem to get the correct answer all the time!
We are supposed to also incorporate something new into the program that our lecturer hasn't taught us, so it really is headache-inducing for me!
My lecturer told us to use the fabs function, but after much searching I could not understand how to incorporate it into my program!
Help, please?
Here's my program (sorry if it is messy; it took me more than 24 hours just to generate this! I don't know how you guys do it!):
#include <iostream.h>
#include <conio.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void main() {
int a, b, answer, sum, count;
char next = 'y';
srand((unsigned)time(NULL));
cout << "\n Welcome to ~ Arithmetic Game ~!!";
cout << "\nThere will be 10 questiond per session.";
cout << "\nEach question carries 10 marks.";
cout << "\nLet's see how good you are at maths.";
cout << "\nLet the game begin!\n\n";
while (next == 'Y' || next == 'y') {
sum = 0;
count = 1;
while (count <= 10) {
do {
int temp = rand() % 4 + 1;
a = rand() % 100 + 1;
b = rand() % 100 + 1;
if (count <= 10)
cout << "\nQuestion " << count << "\n";
switch( temp ) {
case 1:
cout << a << "+" << b << "= ";
cin >> answer;
if(answer == ( a + b )) {
cout << "Correct! +10 marks!\n\n";
sum = sum + 10;
} else {
cout << "Wrong! -5 marks!\n\n";
sum = sum - 5;
}
break;
case 2:
cout << a << "-" << b << "= ";
cin >> answer;
if (answer == (a - B)/>) {
cout << "Correct! +10 marks!\n\n";
sum = sum + 10;
} else {
cout << "Wrong! -5 marks!\n\n";
sum = sum - 5;
}
break;
case 3:
cout << a << "*" << b << "= ";
cin >> answer;
if (answer == (a * B)/>) {
cout << "Correct! +10 marks!\n\n";
sum = sum + 10;
} else {
cout << "Wrong! -5 marks!\n\n";
sum = sum - 5;
}
break;
case 4:
cout << a << "/" << b << "= ";
cin >> answer;
if (answer == (a / B)/>) {
cout << "Correct! +10 marks!\n\n";
sum = sum + 10;
} else {
cout << "Wrong! -5 marks!\n\n";
sum = sum - 5;
}
break;
}
count = count + 1;
}
while (count <= 10);
cout << "\nYou have scored " << sum << "marks.";
}
cout << "\n\nDo you want to keep playing?\n";
cout << "Type 'y' for yes and 'n' for no:";
cin >> next;
}
if (( next != 'Y' ) || ( next != 'y' ) )
cout << "\nGame Over!\n";
cout << "Please press any key to exit...";
getch();
}
Thank you very much for your time; hope to learn something new today!
This post has been edited by NickDMax: 05 March 2010 - 08:44 PM
Reason for edit:: Added Code tags: [code]paste code here[/code]

New Topic/Question
Reply
MultiQuote








|