i made a program that acts like a calculator. the does its function but it needs to keep asking the user for new numbers and an operator until the user enters 'N' or 'n'. however it skips the process completely I tried moving the while statement around but I only got an infinite loop when i did so.
#include <iostream>
using namespace std;
int main(){
char operation;
int number1, number2;
cout << "Enter N to Quit";
cout << endl;
cout << "Enter a number ==> ";
cin >> number1;
cout << endl;
cout << "Enter second number ==> ";
cin >> number2;
cout << endl;
cout << "Choose on of the following; *, /, %, +, -";
cin >> operation;
while(number1 != 'n' || number1 != 'N')
cout << endl;
cout << "Enter second number ==> ";
cin >> number2;
cout << endl;
cout << "Choose on of the following; *, /, %, +, -";
int main()
{
char loopControl = 'y';
while(loopControl == 'y')
{
cout << "Would you like to make another calculation?";
cin << loopControl;
cout << endl;
if (loopControl == 'y')
{
//calculation code inside this if statement
}
} //While bracket
//You don't need an "if else", or an "else" statement if loopControl is
//something other than 'y' it won't do the "if" statement and continue with
//the rest of the program.
//Such as.
cin.ignore(2); //Or system("pause");
return 0;
}
//And the program will close.
I hope this makes sense and that it helps.
If it doesn't make sense I will write it up in C++ and post it again with only the code no comments.
You don't need the (cin>>operation) in the while statement. As well you cannot put a character into an "int" variable. When you use a "char" variable you don't need to use the cin.get(); function unless your performing a specific operation with it. You can just use cin >> charVariable;. If you are being instructed not to use a intvariable then you will need to find a way around it. Like setting the character 'n', and 'N' equal to the ANSII value and then using the value in the "int" variable.
#include <iostream>
usingnamespace std;
int main()
{
char ctrl = 'y';
int num;
while(ctrl == 'y')
{
cout << "Would you like to make a selection : ";
cin >> ctrl;
cout << endl;
}
system("pause");
return 0;
}
Create a new program and copy and paste this exact code. I'm using MS VC++, I do not know the syntax to any other compiler. But if you put "num" in for "ctrl" you will get an error, and an infinite loop.
Also for inputing code like vasilenko93 said type [ code ] paste your code here [ /code ],(with no spaces, don't know how you outed that vasilenko93) or click the button that looks like this <>, to your right, under "Format:".