Question:
C++ Math practice program?
anonymous
2010-12-03 20:57:48 UTC
So I am trying to make a program that asks, for a users name, then grade (k through 6), then based on whether or not they are k-4 or 5&6. Give option for addition, subtraction, division, or multiplication. If k - 4. just addition or subtraction, and 5&6 have option of all 4. So far I have code for user name input and part of the way for input of grade. However I feel there has to be an easier way than declaring a string for each individual grade and doing if else statements for all. I want to use the rand () function which I know how to set up. Maybe use an array. I want to have the user to be able to answer as many questions as they like, and then when they are done. print out the questions they did and show the ones they answered right, then the correct answer to what they did wrong. And give a percentage. its alot i know. But any help along the way would be awesome Ill also paste my code I have so far.


#include
#include
#include
// more libraries
using namespace std;
//global variables
// and function declarations (prototypes) - reminder - end with semi colon

int main ()
{
string Name;
string Grade;
string P1; //Problems for k - 4
string P2; //Problems for 5 & 6

cout << "Hello, and welcome to Math 101..." << endl;
cout << "Please Enter You Name...";
cin >> Name;
cout << "Hello " + Name + ", Please enter you Grade...";
cout << "1. Kindergarden";
cout << "2. First Grade";
cout << "3. Second Grade";
cout << "4. Third Grade";
cout << "5. Fourth Grade";
cout << "6. Fifth Grade";
cout << "7. Sixth Grade";

cin >> Grade;
if (Grade = k,K,1,2,3,4);
cout << "Okay, what would you like to work on? " << endl;
cout << "For Addition. Type A " << endl;
cout << "For Subtraction, Type S" << endl;
cin >> P1;
if (P1 = A);

if (P1 = S);

else cout << "What was that?..." << endl;
return 0;
system ("pause");
}
Three answers:
Robc
2010-12-03 23:51:06 UTC
Change:

string Grade;

to

int Grade;



Since P1 will be a single letter, recommend making it

char P1 = ' ';



Note: By convention variable names will begin with a lowercase letter. This isn't a syntax rule, just a convention.



cout << "Hello " + Name + ", Please enter you Grade...";

should be

cout << "Hello " + Name + ", Please enter the number corresponding to your Grade...";

// corrected grammar (changed you to your)

Instead of doing:

cout << "1. Kindergarden";

cout << "2. First Grade";

cout << "3. Second Grade";

cout << "4. Third Grade";

cout << "5. Fourth Grade";

cout << "6. Fifth Grade";

cout << "7. Sixth Grade";

Which will display as:

1. Kindergarden2. First Grade3. Second Grade4. Third Grade5. Fourth Grade6. Fifth Grade7. Sixth Grade



You should do:

cout << "1. Kindergarten\n" // corrected spelling

<< "2. First Grade\n"

<< "3. Second Grade\n"

<< "4. Third Grade\n"

<< "5. Fourth Grade\n"

<< "6. Fifth Grade\n"

<< "7. Sixth Grade" << endl;



Then:

if (Grade = k,K,1,2,3,4);

cout << "Okay, what would you like to work on? " << endl;

...



should be:

if (Grade >=1 || Grade <= 5)

{

do

{

out << "Okay, what would you like to work on? " << endl;

...

cour << "To quit, type Q" << endl;

switch P1

{

case 'A': // single quotes around the A

// put action for A here

break;

case 'S':

// put action for A here

break;

case 'Q':

// nothing goes here

break;

default:

cout << "I didn't understand your answer" << endl;

}while(P1 != 'Q');
Ratchetr
2010-12-03 21:14:35 UTC
Your goals are much loftier than your grasp of C++.



Before you try to achieve those goals, you really need to spend some time on the basics.



What on earth is this:

if (Grade = k,K,1,2,3,4);



At best, if k and K were defined as variables, this would at least compile. And it would assign the value 4 to grade. And that's about it.



Learn the basics first:

You don't put a ; at the end of an if statement.

== is how you test for equality in C++, not =



So stop writing:

if(P1 = A);



You want:

if(P1 == A)

// code for if true here.
anonymous
2016-12-12 13:20:36 UTC
relies upon on the application. First timer courses many times require the abilities of plugging in numbers for variables and checking if the numbers are interior of logical bounds (e.g., you are able to't purchase decrease than 0 eggs). Logical skills are more suitable major in programming; as long as you recognize straight forward math operations you are able to start programming straight forward purposes.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...