Hi everyone. As my nick indicates, newbie here. Please help if you can. Tks.
This is what I'm trying to do:
Write a program which accepts two integers from the user. Then it asks the user what he wants to do with those two numbers. If the user choice is
‘A’ – Add the two numbers
‘B’ – Subtract the second number from the first number
‘C’ – Multiply the first number by the second number
‘D’ – Divide the first number by the second number
‘Q’ – End the operation
Your program should continue to accept the two numbers and the user choice unless the user enters ‘Q’ as choice.
My code (below)doesn't seem to like the way I declared the variable that accepts the user input as a character. I think the IF statement fails because of that. Also, I had to comment out the do..while loop because it won't even build with that in there.
Any help is GREATLY appreciated.
// Lab3.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
usingnamespace std;
int main()
{
int X, Y, Z;
char Action;
//do {
cout<<"Please enter a number: " << endl;
cin>>X;
cout<<"Please enter a second number: " << endl;
cin>>Y;
cout<<"Using the table below, please enter a code for the action you would like performed on your numbers"<< endl<< "A – Add the two numbers"<< endl<< "B – Subtract the second number from the first number"<< endl<< "C – Multiply the first number by the second number"<< endl<< "D – Divide the first number by the second number"<< endl<< "Q – End the operation"<< endl;
cin>>Action;
if ( Action='A' ) { // If the user chose option A
Z = X + Y; //not sure if we need a C++ statement
cout<<"Their sum is: "<< Z<<endl;
}
elseif (Action='B') { // else if the user chose option B
Z = X - Y; //subtract the second number from the first
cout<<"Their difference is: "<< Z<<endl;
}
elseif (Action='C') { // else if the user chose option C
Z = X * Y; // multiply the first number by the second number
cout<<"Their product is: "<< Z<<endl;
}
elseif (Action='D') { // else if the user chose option D
Z = X / Y; // Divide the first number by the second number
cout<<"Their quotient is: "<< Z<<endl;
}
else {
cout<<"Thank you for playing"<<endl; // Executed if no other statement is true
}
return 0;
}
//while ( Action !='Q' ); //close the while loop when the user chooses Action Q}
//}
//return 0
//}
Oh, wow! I tried that first but was getting errors. I think I had double quotes around the letters instead of single quotes. Now I tried == again, leaving the single qutoes, and it seems to work. Thank you!!
Any chance you can help me with the do..while loop?
Or maybe help me figure out the console window automatically closes quickly after displaying one result? It's supposed to stay open and keep looping.
// Lab3.cpp : Defines the entry point for the console application.
//
//
#include "stdafx.h"
#include <iostream>
usingnamespace std;
int main()
{
int X, Y, Z;
char Action;
//do {
cout<<"Please enter a number: " << endl;
cin>>X;
cout<<"Please enter a second number: " << endl;
cin>>Y;
cout<<"Using the table below, please enter a code for the action you would like performed on your numbers"<< endl<< "A – Add the two numbers"<< endl<< "B – Subtract the second number from the first number"<< endl<< "C – Multiply the first number by the second number"<< endl<< "D – Divide the first number by the second number"<< endl<< "Q – End the operation"<< endl;
cin>> Action;
if ( Action=='A' ) { // If the user chose option A
Z = X + Y; //not sure if we need a C++ statement
cout<<"Their sum is: "<< Z<<endl;
}
elseif (Action=='B') { // else if the user chose option B
Z = X - Y; //subtract the second number from the first
cout<<"Their difference is: "<< Z<<endl;
}
elseif (Action=='C') { // else if the user chose option C
Z = X * Y; // multiply the first number by the second number
cout<<"Their product is: "<< Z<<endl;
}
elseif (Action=='D') { // else if the user chose option D
Z = X / Y; // Divide the first number by the second number
cout<<"Their quotient is: "<< Z<<endl;
}
else {
cout<<"Thank you for playing"<<endl; // Executed if no other statement is true
}
}
//return 0;
// }
//while ( Action !='Q' ); //close the while loop when the user chooses Action Q}
//}
//return 0
//}
@noo1
Or maybe help me figure out the console window automatically closes quickly after displaying one result? It's supposed to stay open and keep looping.
Read here
http://www.cplusplus.com/forum/beginner/1988/
I added it. Thanks! No errors, but window still closes. :-(
I'm listing some msgs I receive. Any idea on them or on the do...while loop?
Symbols loaded.
Cannot find or open the PDB file
Cannot find or open the PDB file
Cannot find or open the PDB file
Symbols loaded.
Symbols loaded.
The program '[10648] Native' has exited with code 0 (0x0).
// Lab3.cpp : Defines the entry point for the console application.
//
//
#include "stdafx.h"
#include <limits>
#include <iostream>
usingnamespace std;
int main()
{
int X, Y, Z;
char Action;
//do {
cout<<"Please enter a number: " << endl;
cin>>X;
cout<<"Please enter a second number: " << endl;
cin>>Y;
cout<<"Using the table below, please enter a code for the action you would like performed on your numbers"<< endl<< "A – Add the two numbers"<< endl<< "B – Subtract the second number from the first number"<< endl<< "C – Multiply the first number by the second number"<< endl<< "D – Divide the first number by the second number"<< endl<< "Q – End the operation"<< endl;
cin>> Action;
if ( Action=='A' ) { // If the user chose option A
Z = X + Y; //not sure if we need a C++ statement
cout<<"Their sum is: "<< Z<<endl;
}
elseif (Action=='B') { // else if the user chose option B
Z = X - Y; //subtract the second number from the first
cout<<"Their difference is: "<< Z<<endl;
}
elseif (Action=='C') { // else if the user chose option C
Z = X * Y; // multiply the first number by the second number
cout<<"Their product is: "<< Z<<endl;
}
elseif (Action=='D') { // else if the user chose option D
Z = X / Y; // Divide the first number by the second number
cout<<"Their quotient is: "<< Z<<endl;
}
else {
cout<<"Thank you for playing"<<endl; // Executed if no other statement is true
}
cin.ignore();
}
//return 0;
// }
//while ( Action !='Q' ); //close the while loop when the user chooses Action Q}
//}
//return 0
//}
// Lab3.cpp : Defines the entry point for the console application.
//
//
#include "stdafx.h"
#include <iostream>
usingnamespace std;
int main()
{
int X, Y, Z;
char Action;
do {
cout<<"Please enter a number: " << endl;
cin>>X;
cout<<"Please enter a second number: " << endl;
cin>>Y;
cout<<"Using the table below, please enter a code for the action you would like performed on your numbers"<< endl<< "A - Add the two numbers"<< endl<< "B - Subtract the second number from the first number"<< endl<< "C - Multiply the first number by the second number"<< endl<< "D - Divide the first number by the second number"<< endl<< "Q - End the operation"<< endl;
cin>> Action;
if ( Action=='A' ) { // If the user chose option A
Z = X + Y; //not sure if we need a C++ statement
cout<<"Their sum is: "<< Z<<endl;
}
elseif (Action=='B') { // else if the user chose option B
Z = X - Y; //subtract the second number from the first
cout<<"Their difference is: "<< Z<<endl;
}
elseif (Action=='C') { // else if the user chose option C
Z = X * Y; // multiply the first number by the second number
cout<<"Their product is: "<< Z<<endl;
}
elseif (Action=='D') { // else if the user chose option D
Z = X / Y; // Divide the first number by the second number
cout<<"Their quotient is: "<< Z<<endl;
}
else {
cout<<"Thank you for playing"<<endl; // Executed if no other statement is true
}
}
while ( Action !='Q' ); //close the while loop when the user chooses Action Q
return 0;
}
Awesome...this is what I came up with. (but sure wish someone would have mentioned it to me before...) Anyways, very glad another posted a question about Switch Case because I'd never even heard of it before reading the post.
//Lab3C.cpp
#include <iostream>
usingnamespace std;
int main()
{
intlonglong X, Y;
char Action;
cout<<"Please enter a whole number: ";
cin>>X;
cout<<"Please enter a second whole number: ";
cin>>Y;
cout<<"Using the table below, please enter a code for the action"<<endl<<"you would like performed on your numbers"<< endl<< "A - Add the two numbers"<< endl<< "B - Subtract the second number from the first number"<< endl<< "C - Multiply the first number by the second number"<< endl<< "D - Divide the first number by the second number"<< endl<< "Q - End the operation"<< endl;
cin>>Action;
while (Action != 'Q' && Action != 'q')
{
switch(Action)
{
case'a':
case'A': cout<<"Here is your sum: "<<X<<" + "<<Y<<"= "<<X+Y<<endl;
break;
case'b':
case'B': cout<<"Here is your difference: "<<X<<" - "<<Y<<"= "<<X-Y<<endl;
break;
case'c':
case'C': cout<<"Here is your product: "<<X<<" * "<<Y<<" = "<<X*Y<<endl;
break;
case'd':
case'D': cout<<"Here is your quotient: "<<X<<" / "<<Y<<" = "<<X/Y<<endl;
break;
default : cout<<"That is not a choice."<<endl;
}
cout<<"Select a new action (A-D,Q): "<<endl;
cin >> Action;
}
cout<<"Thank you for playing."<<endl;
return 0;
}