Hi, first off you should really be using code tags to make your code much easier to read. Put these either side of your code: [code][/code]
Also what exactly are you trying to do? Make the program continuously repeat until the user decides to close?
cout << "Press 1 to Add , 2 to subtract , 3 to multiply , 4 to divide" << endl;
I this line tell the user to press whatever key to exit. Then have an if statement checking if the key was pressed and if it was, change a bool variable from false to true. Have a do while statement around most of the code, the condition of which is while variable == false.
Ah sorry I should have explained better. Put these around your code when you are posting to the forums. It lets the website know that it should be code, and it displays it in a way that is much easier to read.
It is probably a 'do while' loop that you want instead of a while loop, the difference between the two, is that a do while will always run at least once. It can still be very easily do with a while loop though. Have a look at this though, it explains a lot of the different control structures and things, and shows you the correct layout of them.
Oh i have to use a while loop cuz it is part of the assignment that i was given . Umm if i were to make the program say "Press 1 to continue , 2 to stop ".
how do i make 1 to repeat and 2 to stop ?
1 2 3 4 5 6 7 8 9 10 11 12 13
cout << "Would you like to do another task ? (Type 1 for yes , 2 for no)" << endl;
cin >> Decision;
if (Decision == Decision_1)
if (Decision == Decision_2)
{
cout << "Thank you ." << endl;
system ("PAUSE");
}
what should i write onif (Decision == Decision_1) ?
you could have declared a bool called some thing along the lines of 'exitProgram' set it as false while you declare it and have then within your if statement, exitProgram = true. Then in your while loop have the condition exitProgram == false. Then when exitProgram is set to true, your program will be able to get past the end of the while loop.
int main()
{
bool exitLoop = false;
while(exitLoop == false){
int decision;
::std::cout << "do you want to exit the loop? press 1 to exit, 2 to loop" << ::std::endl;
::std::cin >> decision;
if(decision == true){
exitLoop = true;
}else{
//can put stuff here, but for this example I don't need to
}
}
return 0;
}
I haven't compiled it so I might have missed a detail here or there, but that should give you an idea.
// Calculator, by Your Name
// Include the iostream library
#include <iostream>
//Use the standard namespace
usingnamespace std;
int main ( )
{
// Declare the variables
float Number_1;
float Number_2;
float Result;
int Decision;
int Decision_1 = 1;
int Decision_2 = 2;
int Which_Calculation = 1;
// Give Instructions
bool exitProgram = false;
while (exitProgram == false)
{
cout << "Press 1 to Add , 2 to subtract , 3 to multiply , 4 to divide" << endl;
cin >> Which_Calculation;
// Get numbers
cout << "Please enter the first number." << endl;
cin >> Number_1;
cout << "Please enter the second number." << endl;
cin >> Number_2;
if (Which_Calculation == 1)
{
// Calculate the result
Result = Number_1 + Number_2;
}
if (Which_Calculation == 2)
{
// Calculate the result
Result = Number_1 - Number_2;
}
if (Which_Calculation == 3)
{
// Calculate the result
Result = Number_1 * Number_2;
}
if (Which_Calculation == 4)
{
// Calculate the result
Result = Number_1 / Number_2;
}
// Print the answer is...
cout << "The answer is..." << endl;
//Print the result
cout << Result << endl;
cout << "Would you like to do another task ? (Type 1 for yes , 2 for no)" << endl;
cin >> Decision;
if (Decision == false)
{
exitProgram = false;
}
else
{
exitProgram = true;
cout << "Thank you." << endl;
system ("PAUSE");
}
}
}
umm this is what i did so far , can you take a look at it and guide me through ?
Most of that seems to be right. Probably the main thing you've done wrong is your if and else statements at the end.
You have if (Decision == false) however you had told the user to input 1 or 2. so if should be:
if (Decision == 2)
Although you don't actually need that bit there at all. i would just do:
1 2 3 4 5 6 7
cout << "Would you like to do another task ? (Type 1 for yes , 2 for no)" << endl;
cin >> Decision;
if (Decision == 1)
{
exitProgram = true;
}
and put the thank you part at the very end of your main function.
You really should try and improve your use of white space as well.
Read this wikipedia article. And as a beginner you probably think it's just me being awkward and that it doesn't really matter. But as you start to write more complicated code, it's the difference between the code being easy to understand, and even you yourself not having a clue what is going on.
EDIT: also there are several different styles in that article, it pretty much doesn't matter which you choose, just pick one that looks good to you and be consistent through out each program.
I have never used system("CLOSE)" before and can't say I know anything about it. Generally any of the system functions are considered bad practice so it would maybe be best to not use it. Once the program gets to the end of your main function it should just finish. If it doesn't it will be something strange with your compiler or settings, not actually c++. I'd probably not be able to help, but if your still not sure, explain the problem as best you can and hopefully someone will help out.
Here is a little cleaner way to write it. You should really work on practicing error correcting too because it helps go a long way when you write real programs and better grades in class. That is why I used the while statements in some of your choices. It isn't perfected, I leave that for you to work on but it is a great start. This is also an important feature of a while statement you can never overlook. Here, this while statement naturally would go on forever but if you use break, you can exit anytime. This is great for larger programs where you have numerous reasons to break a while statement and one variable is too much of a hassle to use. Also the best reason, it is one variable less to have in your program which means a smaller program and also less chances of mistakes later. Here is how it would look.
#include <iostream>
usingnamespace std;
int main()
{
while(true)
{
float Number_1, Number_2, Result;
int Decision = 0, Decision_2 = 0, Decision_2 = 0, Which_Calculation = 0;
while(Which_Calculation < 1 || Which_Calculation > 4)
{
cout << "Press 1 to Add , 2 to subtract , 3 to multiply , 4 to divide" << endl;
cin >> Which_Calculation;
}
cout << "Please enter the first number." << endl;
cin >> Number_1;
cout << "Please enter the second number." << endl;
cin >> Number_2;
if (Which_Calculation == 1)
{
// Calculate the result
Result = Number_1 + Number_2;
}
if (Which_Calculation == 2)
{
// Calculate the result
Result = Number_1 - Number_2;
}
if (Which_Calculation == 3)
{
// Calculate the result
Result = Number_1 * Number_2;
}
if (Which_Calculation == 4)
{
// Calculate the result
Result = Number_1 / Number_2;
}
// Print the answer is...
cout << "The answer is..." << endl << Result << endl;
cout << "Would you like to do another task ? (Type 1 for yes , 2 for no)" << endl;
cin >> Decision;
if (Decision == 2)
{
cout << "Thank you." << endl;
system ("PAUSE");
break;
}
}
}
I also agree not to use the system command when possible. Try either getch() or getchar(). Either one will work and will work on non windows operating systems.