Help with Program I wrote (C++)

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Sep 2006
Posts: 3
Reputation: TravisC555 is an unknown quantity at this point 
Solved Threads: 0
TravisC555 TravisC555 is offline Offline
Newbie Poster

Help with Program I wrote (C++)

 
1
  #1
Sep 30th, 2006
  1. //Program adds, subtracts, multiplies, or divides any two numbers that the user enters.
  2.  
  3. #include <iostream>
  4.  
  5. using std::cout;
  6. using std::cin;
  7. using std::endl;
  8.  
  9. int main()
  10. {
  11. char operation = ' ';
  12. int num1 = 0;
  13. int num2 = 0;
  14. int answer = 0;
  15.  
  16. cout <<"Enter A (add) or S (subtract) or M (multiply) or D (divide): ";
  17. cin >> operation;
  18.  
  19. if (operation != 'A' || 'a' || 'S' || 's' || 'M' || 'm' || 'D' || 'd')
  20. {
  21. cout <<"Error: Wrong letter entered" << endl;
  22. return 0;
  23. }
  24.  
  25. cout <<"Enter first number: ";
  26. cin >> num1;
  27. cout <<"Enter second number: ";
  28. cin >> num2;
  29.  
  30. if (operation == 'A' || 'a')
  31. {
  32. answer = num1 + num2;
  33. cout <<"Sum: " << answer << endl;;
  34.  
  35. }//end if
  36.  
  37. if (operation == 'S' || 's')
  38. {
  39. if (num1 > num2)
  40. {
  41. answer = num1 - num2;
  42. }//end if
  43. else
  44. answer = num2 - num1;
  45.  
  46. cout <<"Difference: " << answer << endl;
  47. }//end if
  48.  
  49.  
  50. if (operation == 'M' || 'm')
  51. {
  52. answer = num1 * num2;
  53. cout <<"Product: " << answer << endl;
  54. }//end if
  55.  
  56. if (operation == 'D' || 'd')
  57. {
  58. if (num1 > num2)
  59. {
  60. answer = num1 / num2;
  61. }//end if
  62. else
  63. answer = num2 / num1;
  64.  
  65. cout <<"Quotient: " << answer << endl;
  66. }//end if
  67.  
  68. return 0;
  69. }//end of main function
I'm having trouble with the error message that I need to display at the beginning if the user enters an invalid letter. It's the first if statement.

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
Reply With Quote Quick reply to this message  
Join Date: Sep 2005
Posts: 102
Reputation: Niklas is an unknown quantity at this point 
Solved Threads: 0
Niklas's Avatar
Niklas Niklas is offline Offline
Junior Poster

Re: Help with Program I wrote (C++)

 
1
  #2
Sep 30th, 2006
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]
Last edited by Niklas; Sep 30th, 2006 at 3:11 am.
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: Help with Program I wrote (C++)

 
1
  #3
Sep 30th, 2006
> 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.
Reply With Quote Quick reply to this message  
Join Date: Sep 2006
Posts: 3
Reputation: TravisC555 is an unknown quantity at this point 
Solved Threads: 0
TravisC555 TravisC555 is offline Offline
Newbie Poster

Re: Help with Program I wrote (C++)

 
0
  #4
Sep 30th, 2006
Originally Posted by Salem View Post
> 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.
Thanks for all the replies and code.

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
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,619
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 468
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

Re: Help with Program I wrote (C++)

 
0
  #5
Sep 30th, 2006
Try something like:

  1. int main()
  2. {
  3. int continue_session = 1 ;
  4. do
  5. {
  6. // your entire code goes here
  7. printf ("do you want to continue (0 if not) ? ") ;
  8. scanf ("%d", &continue_session) ;
  9. }
  10. while ( continue_session != 0 ) ;
  11. return 0;
  12. }

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.
Reply With Quote Quick reply to this message  
Join Date: Sep 2006
Posts: 3
Reputation: TravisC555 is an unknown quantity at this point 
Solved Threads: 0
TravisC555 TravisC555 is offline Offline
Newbie Poster

Re: Help with Program I wrote (C++)

 
1
  #6
Sep 30th, 2006
Well I got it to work.

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
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC