Click to See Complete Forum and Search --> : If problems in C++
Tech^salvager
09-08-2004, 04:25 PM
My program compiles fine, it is that It will still put all the awnsers down though the if statments seem to work. Does anybody here know what I'm doing wrong?
#include <iostream>
//Version 0.0.2 of caluclator program
//Made by Justin R. Barrera
using namespace std;
int main()
{
int d=1;
while (d != 0) //Loop
{
int c=0;
int b=0;
int m=0;
cout << "Please input a number ";
cin >> c;
cout << "Please input a number ";
cin >> b;
{
cin >> m;
if (m == 1)
cout << "The product is ";
cout << c * b << endl;
if (m == 2)
cout << "The quotient is ";
cout << c / b << endl;
if (m == 3)
cout << "The Difference is ";
cout << c - b << endl;
if (m == 4)
cout << "The sum is ";
cout << c + b << endl << endl;
}
cout << "To exit enter 0" << endl;
cin >> d;
}
return 0;
}
Thanks Tech^:t
Tech^salvager
09-08-2004, 07:01 PM
Well I messed around more now it works like I want it to, but I still would like to know why it did what it did before. Heres the new code
#include <iostream>
//Version 0.0.3 of caluclator program
//Made by Justin R. Barrera
using namespace std;
int main()
{
int d=1;
while (d != 0) //Loop
{
int c=0;
int b=0;
int m=0;
cout << "Please input a number ";
cin >> c;
cout << "Please input a number ";
cin >> b;
{
cin >> m;
if (m == 1)
cout << "The product is " << c * b << endl;
if (m == 2)
cout << "The quotient is " << c / b << endl;
if (m == 3)
cout << "The Difference is " << c - b << endl;
if (m == 4)
cout << "The sum is " << c + b << endl;
}
cout << "To exit enter 0" << endl;
cin >> d;
}
return 0;
}
ScaryBinary
09-08-2004, 08:51 PM
You need to put curly braces around all the statements you want to execute in your "if" block, otherwise only the first statement after the "if" gets run.
In your original code, you broke your output into two statements. If you wanted both statements to be run when your "if" condition is met, you'd have to code it like:
if (m == 1) {
cout << "The product is ";
cout << c * b << endl;
}
Since you didn't have the curly braces, only the first "cout" was run if m==1, but the second "cout" was always run. This is a C thing.
In your second attempt, you put the output all in the same statement, so it worked.
I always put curly braces around my if blocks, even if they're only one line. It tends to avoid future confusion! Hence I would code your second attempt like:
if (m == 1) {
cout << "The product is " << c * b << endl;
}
even though what you have there will work fine. In the future, if you do have to add another statement inside your if block, you won't forget to add the curly braces if they're already there!
Tech^salvager
09-08-2004, 09:27 PM
Scarybinary how would I use
if
else if
else if
else
?
I tried that first but it didn't work so i did all ifs
ScaryBinary
09-08-2004, 10:25 PM
You'd do it like:
cin >> m;
if (m == 1)
{
/* Compute and display product. */
cout << "The product is " << c * b << endl;
}
else if (m == 2)
{
/* Compute and display quotient. */
cout << "The quotient is " << c / b << endl;
}
else if (m == 3)
{
/* Compute and display difference. */
cout << "The Difference is " << c - b << endl;
}
else if (m == 4)
{
/* Compute and display sum. */
cout << "The sum is " << c + b << endl;
}
else
{
/* User entered invalid input. Print error. */
cout << "Invalid input. Try again.";
}
why not use switch/case?
Nicer syntax
switch(m){
case 1:
cout<<"You entered 1"<<endl;
break;
case 2:
cout<<"You entered 2"<<endl;
break;
default:
cout<<"You didnt enter 1 or 2"<<endl;
}
klatty
09-29-2004, 11:41 AM
I agree, switch statements for a program like this is definitely the more elegant way to go.
Tech^salvager
09-29-2004, 01:02 PM
Originally posted by klatty
I agree, switch statements for a program like this is definitely the more elegant way to go.
yep
#include <iostream>
//Version 0.0.32 of caluclator program
//Made by Justin R. Barrera
using namespace std;
int main()
{
int restart=1;
while (restart != 0) //Loop
{
float c=0, b=0;
int m=0;
cout << "Please type in a number then press enter " << endl;
cin >> c;
cout << "Please type in a number then press enter " << endl;
cin >> b;
cout << "Choose a mathmatical operation" << endl;
cout << "1=Multiplication" << endl;
cout << "2=Division" << endl;
cout << "3=Subtraction" << endl;
cout << "4=Addition" << endl;
cin >> m;
switch(m)
{
case 1:
cout << "The product is " << c * b << endl;
break;
case 2:
cout << "The quotient is " << c / b << endl;
break;
case 3:
cout << "The Difference is " << c - b << endl;
break;
case 4:
cout << "The sum is " << c + b << endl;
break;
default:
cout << "HAHA don't input that" << endl;
break;
}
cout << "To exit enter 0" << endl;
cin >> restart;
}
return 0;
}
SysOpt.com
Copyright Internet.com Inc. All Rights Reserved.