![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Hobbyist Programmer
Join Date: Sep 2004
Location: Cyprus
Posts: 147
Rep Power: 5
![]() |
Write a program to test the user on a multiplication table, asking them to enter the multiplication table he/she wants and then asking them to enter the answer to each of the 12 multiplications and printing the string “Correct” or “Incorrect” as appropriate.
I was able to solve the proram, but I want to add one more thing, I need to have the option for the user to exit any time he/she wants. I tried adding the else if (number == 100 ) return (0); But it seems to ignore it. Can anyone help? #include <iostream>
#include <stdlib.h>
using namespace std;
int main()
{
*
*
*
*int number, result;
*int i;
*
*cout << "Please Enter a number:" << endl;
*cin >> number;
*
*for ( i=1;i<=12;i++)
{
* *
* *cout << number << " * " << i << " = ";
* *cin >> result;
* *
* *if ( result == i * number)
* *cout << "Correct Answer " <<endl;
* *
* *else
* *cout << "Incorrect Answer, Please Try Again" << endl;
* *
} * *
*
*
*system("PAUSE");
*return 0;
}
__________________
Personal Portfolio TecBrain Support Forum Linux VS Windows ... Dont Even Think of it .. Distribution: Slackware if (OS==Linux) return success There are 10 kinds of people, those who can read binary numbers and those who can't. |
|
|
|
|
|
#2 |
|
Expert Programmer
Join Date: Sep 2004
Location: Ontario, Canada
Posts: 579
Rep Power: 5
![]() |
You post a lot of homework assignments <_< I'm not sure about C++, but in C you use exit(0);
__________________
Johnny was a chemist's son but Johnny is no more, for what Johnny thought was H2O was H2SO4 |
|
|
|
|
|
#3 | |
|
Hobbyist Programmer
Join Date: Sep 2004
Location: Cyprus
Posts: 147
Rep Power: 5
![]() |
Quote:
Do You mind?? <_< The assignment is solved, I want to add this is an extra work.
__________________
Personal Portfolio TecBrain Support Forum Linux VS Windows ... Dont Even Think of it .. Distribution: Slackware if (OS==Linux) return success There are 10 kinds of people, those who can read binary numbers and those who can't. |
|
|
|
|
|
|
#4 |
|
Hobbyist Programmer
Join Date: Sep 2004
Location: Cyprus
Posts: 147
Rep Power: 5
![]() |
Ok guys I managed to do something, maybe not professional but it does what I want for now.
Thanks. #include <iostream>
#include <stdlib.h>
using namespace std;
int main()
{
int number, result;
int i;
cout << "Please Enter a number:" << endl;
cin >> number;
for ( i=1;i<=12;i++)
{
if (number == -1 ) return(0);
cout << number << " * " << i << " = ";
cin >> result;
if (result == -1 ) return(0);
if ( result == i * number)
cout << "Correct Answer " <<endl;
else
cout << "Incorrect Answer, Please Try Again" << endl;
}
system("PAUSE");
return 0;
}
__________________
Personal Portfolio TecBrain Support Forum Linux VS Windows ... Dont Even Think of it .. Distribution: Slackware if (OS==Linux) return success There are 10 kinds of people, those who can read binary numbers and those who can't. |
|
|
|
|
|
#5 |
|
Programming Guru
![]() ![]() |
Why not use a while loop. Have a menu that gets looped unless the number that the user enters is like zero or somthing like that. That why your program has room for other stuff later, with little work. Then inside the while loop have a switch. The user input gets fed into the switch and they get to go the different parts of the program. So somthing like this:
#include <iostream.h>
#include <conio.h>
int main()
{
int input;
While (x<>4)
{
cout<<"1 Math Test";
cout<<"2 Play a Game";
cout<<"3 Other Stuff";
cout<<"4 Exit";
cout<<"What do you want to do?";
cin>>x;
switch (x)
{
case 1: math_test();
//Insert your code for math test here.
break;
case 2: play_game();
//Insert game code here.
break;
case 3: other_stuff();
//Yeah i think you get the idea.
break;
case 4:
return 0;
break;
default:
cout<<"Bad Input, Please Try Again....";
}
}
return 0;
}Anyways, i havn't tested that code, it was more of a thing to give you an idea. Cheers.
__________________
Profanity is the one language that all programmers understand. Check out my Blog <---updated Nov 30 2007! |
|
|
|
|
|
#6 |
|
Hobbyist Programmer
Join Date: Sep 2004
Location: Cyprus
Posts: 147
Rep Power: 5
![]() |
Thanks Pizentios for sharing, I'll make sure that I will test it.
Cheers.
__________________
Personal Portfolio TecBrain Support Forum Linux VS Windows ... Dont Even Think of it .. Distribution: Slackware if (OS==Linux) return success There are 10 kinds of people, those who can read binary numbers and those who can't. |
|
|
|
|
|
#7 |
|
Programming Guru
![]() ![]() |
No Prob. With your current code, you really don't need it since your program does only one thing. I guess you could add some other math testing funtions. like adding and such.
Also i can see somthing that could be changed in my code. i edited the post and changed it, just amkes it a little cleaner.
__________________
Profanity is the one language that all programmers understand. Check out my Blog <---updated Nov 30 2007! |
|
|
|
|
|
#8 |
|
I eat cake for breakfast.
![]() ![]() ![]() ![]() Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9
![]() |
Also, with Pizentios's code, you might wanna change (x<>4) to (x!=4) - this is C++ here.
|
|
|
|
|
|
#9 |
|
Programming Guru
![]() ![]() |
Sorry, i am used to vb and PHP. Heh, i don't get to use my ~outdated~ knowledge of c++ very offten. It's sad really. Kinda like a Dog that a kid forgets about because they got a xbox for christmas.
__________________
Profanity is the one language that all programmers understand. Check out my Blog <---updated Nov 30 2007! |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|