| | |
Help with Program I wrote (C++)
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Sep 2006
Posts: 3
Reputation:
Solved Threads: 0
C++ Syntax (Toggle Plain Text)
//Program adds, subtracts, multiplies, or divides any two numbers that the user enters. #include <iostream> using std::cout; using std::cin; using std::endl; int main() { char operation = ' '; int num1 = 0; int num2 = 0; int answer = 0; cout <<"Enter A (add) or S (subtract) or M (multiply) or D (divide): "; cin >> operation; if (operation != 'A' || 'a' || 'S' || 's' || 'M' || 'm' || 'D' || 'd') { cout <<"Error: Wrong letter entered" << endl; return 0; } cout <<"Enter first number: "; cin >> num1; cout <<"Enter second number: "; cin >> num2; if (operation == 'A' || 'a') { answer = num1 + num2; cout <<"Sum: " << answer << endl;; }//end if if (operation == 'S' || 's') { if (num1 > num2) { answer = num1 - num2; }//end if else answer = num2 - num1; cout <<"Difference: " << answer << endl; }//end if if (operation == 'M' || 'm') { answer = num1 * num2; cout <<"Product: " << answer << endl; }//end if if (operation == 'D' || 'd') { if (num1 > num2) { answer = num1 / num2; }//end if else answer = num2 / num1; cout <<"Quotient: " << answer << endl; }//end if return 0; }//end of main function
It is suppose to display the error message if the user enters an invalid letter. Otherwise it should continue one and ask the user for the 2 numbers. What it does is it always dispalys the error message in cout and statements that as the user to enter the number never appear.
Any help appreciated!
Thanks.
Last edited by TravisC555; Sep 30th, 2006 at 1:41 am. Reason: Made post clearer
This is an older version of a calculator that I made when I was just srated. Look around and find some snippets you might like to see how they work or add to yours. For example use of the if statement for displaying an error message, and having a repeating loop asking if a person wants to use the calculator again.
[PHP]
#include <iostream>
using namespace std;
int main()
{
float num1;
float num2;
char op;
float result;
char again = 'y';
while (again == 'y')
{
cin >> num1;
cout <<"\n num 1 = " << num1 << endl;
cin >> num2;
cout << "\n num2 = " << num2 << endl;
cout <<"\n Now enter an operating symbol (+ - / *)"<<endl;
cout <<"\n You can also enter 2 to sqaure a # and 3 to cube a #" <<endl;
cin >> op;
if (op == '+')
cout << "\n num1 + num2 = " << num1 + num2 << endl;
if (op == '-')
cout <<"\n num1 - num2 = " << num1 - num2 << endl;
if (op == '*')
cout <<"\n num1 * num2 = " << num1 * num2 << endl;
if (op == '/')
cout << " \n num1 / num2 = " << num1 / num2 << endl;
if (op == '2')
cout <<" \n num1 to the second power = " << num1 * num1 << endl;
if (op == '3')
cout <<" \n num1 to the third power = " << num1 * num1 * num1 << endl;
else
cout << "\n Invalid Operation";
cout << "\n Do you want to try again? (y/n): ";
cin >> again;
}
return 0;
}[/PHP]
[PHP]
#include <iostream>
using namespace std;
int main()
{
float num1;
float num2;
char op;
float result;
char again = 'y';
while (again == 'y')
{
cin >> num1;
cout <<"\n num 1 = " << num1 << endl;
cin >> num2;
cout << "\n num2 = " << num2 << endl;
cout <<"\n Now enter an operating symbol (+ - / *)"<<endl;
cout <<"\n You can also enter 2 to sqaure a # and 3 to cube a #" <<endl;
cin >> op;
if (op == '+')
cout << "\n num1 + num2 = " << num1 + num2 << endl;
if (op == '-')
cout <<"\n num1 - num2 = " << num1 - num2 << endl;
if (op == '*')
cout <<"\n num1 * num2 = " << num1 * num2 << endl;
if (op == '/')
cout << " \n num1 / num2 = " << num1 / num2 << endl;
if (op == '2')
cout <<" \n num1 to the second power = " << num1 * num1 << endl;
if (op == '3')
cout <<" \n num1 to the third power = " << num1 * num1 * num1 << endl;
else
cout << "\n Invalid Operation";
cout << "\n Do you want to try again? (y/n): ";
cin >> again;
}
return 0;
}[/PHP]
Last edited by Niklas; Sep 30th, 2006 at 3:11 am.
>
There is no compare with a list type function, you need to compare with each one in turn
Look up the toupper() function
then you would only need half as many comparisons.
if (operation != 'A' || 'a' || 'S' ||There is no compare with a list type function, you need to compare with each one in turn
if (operation != 'A' || operation != 'a' || operation != 'S' ||Look up the toupper() function
operation = toupper(operation);then you would only need half as many comparisons.
•
•
Join Date: Sep 2006
Posts: 3
Reputation:
Solved Threads: 0
•
•
•
•
>if (operation != 'A' || 'a' || 'S' ||
There is no compare with a list type function, you need to compare with each one in turn
if (operation != 'A' || operation != 'a' || operation != 'S' ||
Look up the toupper() function
operation = toupper(operation);
then you would only need half as many comparisons.
I have done the if (operation != 'A' || operation != 'a') and so on for the rest of the letters.
But when I run it and enter either A,a,S,s,M,m,D,d it still displays the error message and the program doesn't ask the user for the numbers.
Last edited by TravisC555; Sep 30th, 2006 at 1:59 pm. Reason: Changed some things
Try something like:
Just make the changes as mentionned by Mr. Salem and try the snippet i have placed in front of you and your program will definately work.
C++ Syntax (Toggle Plain Text)
int main() { int continue_session = 1 ; do { // your entire code goes here printf ("do you want to continue (0 if not) ? ") ; scanf ("%d", &continue_session) ; } while ( continue_session != 0 ) ; return 0; }
Just make the changes as mentionned by Mr. Salem and try the snippet i have placed in front of you and your program will definately work.
I don't accept change; I don't deserve to live.
•
•
Join Date: Sep 2006
Posts: 3
Reputation:
Solved Threads: 0
Well I got it to work. 
I just changed the || to && in the if statement that checks what the user entered.

I just changed the || to && in the if statement that checks what the user entered.
//Program adds, subtracts, multiplies, or divides any two numbers that the user enters.
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
int main()
{
char operation = ' ';
int num1 = 0;
int num2 = 0;
int answer = 0;
cout <<"Enter A (add) or S (subtract) or M (multiply) or D (divide): ";
cin >> operation;
operation = toupper(operation);
if (operation != 'A' && operation != 'S' && operation != 'S' && operation != 'M' && operation != 'D')
//Changed this to && instead of ||
{
cout <<"Error: Wrong letter entered" << endl;
return 0;
}
cout <<"Enter first number: ";
cin >> num1;
cout <<"Enter second number: ";
cin >> num2;
if (operation == 'A' || 'a')
{
answer = num1 + num2;
cout <<"Sum: " << answer << endl;;
}//end if
if (operation == 'S' || 's')
{
if (num1 > num2)
{
answer = num1 - num2;
}//end if
else
answer = num2 - num1;
cout <<"Difference: " << answer << endl;
}//end if
if (operation == 'M' || 'm')
{
answer = num1 * num2;
cout <<"Product: " << answer << endl;
}//end if
if (operation == 'D' || 'd')
{
if (num1 > num2)
{
answer = num1 / num2;
}//end if
else
answer = num2 / num1;
cout <<"Quotient: " << answer << endl;
}//end if
return 0;
}//end of main function Last edited by TravisC555; Sep 30th, 2006 at 2:34 pm. Reason: Changed some stuff
![]() |
Similar Threads
- Can anyone figure out what is wrong in my program? (C++)
- What's the HARDEST program you've written? (Computer Science)
- Need help writing a program (C++)
- Can any1 tell me whatz wrong in this program...this does not show the output!!! (C++)
- Factorial program (Java)
- *Pointer program problem. (C++)
Other Threads in the C++ Forum
- Previous Thread: The round-robin scheduler
- Next Thread: Problems with my c++ program
| Thread Tools | Search this Thread |
api array arrays based beginner binary bitmap c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete deploy developer dll download dynamiccharacterarray email encryption error file forms fstream function functions game generator getline givemetehcodez graph homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference rpg sorting string strings struct temperature template text tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






