943,957 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 1498
  • C++ RSS
Sep 30th, 2006
1

Help with Program I wrote (C++)

Expand Post »
C++ Syntax (Toggle Plain Text)
  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
Similar Threads
Reputation Points: 17
Solved Threads: 0
Newbie Poster
TravisC555 is offline Offline
3 posts
since Sep 2006
Sep 30th, 2006
1

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

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.
Reputation Points: 14
Solved Threads: 0
Junior Poster
Niklas is offline Offline
104 posts
since Sep 2005
Sep 30th, 2006
1

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

> 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.
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005
Sep 30th, 2006
0

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

Click to Expand / Collapse  Quote originally posted by Salem ...
> 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
Reputation Points: 17
Solved Threads: 0
Newbie Poster
TravisC555 is offline Offline
3 posts
since Sep 2006
Sep 30th, 2006
0

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

Try something like:

C++ Syntax (Toggle Plain Text)
  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.
Super Moderator
Featured Poster
Reputation Points: 3233
Solved Threads: 720
Failure as a human
~s.o.s~ is offline Offline
8,872 posts
since Jun 2006
Sep 30th, 2006
1

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

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
Reputation Points: 17
Solved Threads: 0
Newbie Poster
TravisC555 is offline Offline
3 posts
since Sep 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: The round-robin scheduler
Next Thread in C++ Forum Timeline: Problems with my c++ program





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC