C and C++

Moderators: None (Apply to moderate this forum)
Number of threads: 28629
Number of posts: 94611

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
Not the results I am looking for ** HELP ** Posted by dlmagers on 29 Feb 2012 at 12:08 PM
Ok, I have got my program working but it is displaying to many results. It is displaying the answer behind of the word Sum
. If I input 'M' then the answer is still displaying behind the word Sum
. Same thing for 'S' (for subtract) and 'D' (divide). Here is my code:


//This program will add, subtract, multiply or divide depending on what the user inputs

#include <iostream>

using namespace std;

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')

{
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;;

}

else if (operation == 'S' || 's')
{
if (num1 > num2)
{
answer = num1 - num2;
}
else
answer = num2 - num1;

cout <<"Difference: " << answer << endl;
}


else if (operation == 'M' || 'm')
{
answer = num1 * num2;
cout <<"Product: " << answer << endl;
}

else if (operation == 'D' || 'd')
{
if (num1 > num2)
{
answer = num1 / num2;
}
else
answer = num2 / num1;

cout <<"Quotient: " << answer << endl;
}

system("pause");
return 0;
}//end of main function

would someone please help me debug this program to find the problem? Thank you, Diana
Report
Re: Not the results I am looking for ** HELP ** Posted by sarabellum on 1 Mar 2012 at 8:18 AM
I am facing some problems with this code..Any suggestion..

Thanks in advance.

[url=http://homeremedieslog.com/baby-constipation]Baby Constipation[/url]
Report
Re: Not the results I am looking for ** HELP ** Posted by iCinCode on 2 Mar 2012 at 3:35 PM
If you want there to just be the answer displayed enter:

cout <<"" << answer << endl;

Your code contained "Sum: ", this is why it was giving you thas output.
Report
Re: Not the results I am looking for ** HELP ** Posted by iCinCode on 2 Mar 2012 at 3:46 PM
Also, there is still a problem with your code, it always adds, it never subtracts, multiplies or divides.

Hint: look at your logical operators.



 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.