Sunday, July 12, 2009

I need help with my C++ program?

Write a program that can be used as a math tutor for a young student. The program should display two random numbers that are to be added. The program should wait for the student to enter the answer. If the answer is correct, a message of congratulations should be printed. If the answer is incorrect,a message should be printed showing the correct answer.

I need help with my C++ program?
#include %26lt;iostream%26gt;


#include %26lt;ctime%26gt;





using namespace std;





int DoMenu();


void addition(int random1, int random2);


void subtraction(int random1, int random2);


void multiplication(int random1, int random2);


void division(int random1, int random2);





int main()


{


int random1, random2;


srand(time(0));





int choice;





do


{


random1 = 1+(rand()%20); // from 1-20


random2 = 1+(rand()%20); // from 1-20





choice = DoMenu();





switch (choice)


{


case 0: cout %26lt;%26lt; endl; break;


case 1: addition(random1,random2); break;


case 2: subtraction(random1,random2); break;


case 3: multiplication(random1,random2); break;


case 4:


{


if (random2 == 0)


cout %26lt;%26lt; "Error: Division by zero, cannot proceed\n";


else


division(random1,random2);


} break;


default : cout %26lt;%26lt; "Error in input\n\n"; break;


}





} while (choice != 0);





return 0;


}





int DoMenu()


{


int MenuChoice;


cout %26lt;%26lt; "(1)Addition\n";


cout %26lt;%26lt; "(2)Subtraction\n";


cout %26lt;%26lt; "(3)Multiplication\n";


cout %26lt;%26lt; "(4)Division\n";


cout %26lt;%26lt; "(0)Quit\n";


cout %26lt;%26lt; "-----------------\n";


cout %26lt;%26lt; "Choice: ";


cin %26gt;%26gt; MenuChoice;


return MenuChoice;


}





void addition(int random1, int random2)


{


int answer;





cout %26lt;%26lt; "\n" %26lt;%26lt; random1 %26lt;%26lt; " + " %26lt;%26lt; random2 %26lt;%26lt; " = ";


cin %26gt;%26gt; answer;





if (answer == (random1+random2))


cout %26lt;%26lt; "Congratulations\n\n";


else


cout %26lt;%26lt; "Incorrect\n\n";


}





void subtraction(int random1, int random2)


{


int answer;





cout %26lt;%26lt; "\n" %26lt;%26lt; random1 %26lt;%26lt; " - " %26lt;%26lt; random2 %26lt;%26lt; " = ";


cin %26gt;%26gt; answer;





if (answer == (random1-random2))


cout %26lt;%26lt; "Congratulations\n\n";


else


cout %26lt;%26lt; "Incorrect\n\n";


}





void multiplication(int random1, int random2)


{


int answer;





cout %26lt;%26lt; "\n" %26lt;%26lt; random1 %26lt;%26lt; " * " %26lt;%26lt; random2 %26lt;%26lt; " = ";


cin %26gt;%26gt; answer;





if (answer == (random1*random2))


cout %26lt;%26lt; "Congratulations\n\n";


else


cout %26lt;%26lt; "Incorrect\n\n";


}





void division(int random1, int random2)


{


int answer;





cout %26lt;%26lt; "\n" %26lt;%26lt; random1 %26lt;%26lt; " / " %26lt;%26lt; random2 %26lt;%26lt; " = ";


cin %26gt;%26gt; answer;





if (answer == (random1/random2))


cout %26lt;%26lt; "Congratulations\n\n";


else


cout %26lt;%26lt; "Incorrect\n\n";


}





Hey Samantha, use this as a guide to help you. If all you need it to do is generate addition problems, you can take the rest of the processes out(subtraction, multiplication, and division). Make sure you don't forget your comments! Also, you might want to remove the "W" off of your Y!/A user name because your teacher could possibly be on here too. It's highly unlikely but it's possible. Take care.
Reply:To be honest, if you are struggling with this assignment, then you might want to look at another career!





However, take a look at the "rand()" function and also the "+" operator.





You could also use Yahoo! Search
Reply:First make an effort then we will help you :-)
Reply:have you even tried to do it?


start by writing your ipo worksheet %26amp; then the pseudocode.


omg, yes you have to think


try reading the book
Reply:If you try writing the code and get stuck, post your code here and we'll help you fix it. When you do that, let us know whether you're getting compiler errors, or whether it's just not behaving the way you expected.


No comments:

Post a Comment