| | |
Multiple "cout"s and "if"s within an If statement
Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Sep 2009
Posts: 40
Reputation:
Solved Threads: 0
I'm trying to create a program that takes 2 numbers from a user (a numerator and a denominator), displays one ontop of the other separated by dashes, and then shows the answer of one number divided by the other.
Inside an If statement, I want to output several lines using multiple cout statements, along with using an embedded if statement, but after the first cout statement, it seems to take me out of the If. What am I doing wrong?
Inside an If statement, I want to output several lines using multiple cout statements, along with using an embedded if statement, but after the first cout statement, it seems to take me out of the If. What am I doing wrong?
C++ Syntax (Toggle Plain Text)
#include <iostream> using namespace std; int main () { int n1,n2 float n3 cout << "Enter the numerator: " cin >> n1 cout << "Enter the denominator: " cin >> n2 if ((n1 <10) && (n2 <10)) cout << "You want to see:" << endl; cout << "" << endl; cout << " " << n1 << endl; cout << " -" << endl; cout << " " << n2 << endl; cout << " " << endl; n3 = (n1 / n2) * 1.0000 if (n3 < 10) cout << " " << n3; else if ((n3 >= 10) && (n3 < 100)) cout << " " << n3; else if ((n3 >= 100) && (n3 <1000)) cout << " " << n3 else if ((n1 >= 10) && (n1 <100) && (n2 <100)) cout << "You want to see:" << endl; cout << "" << endl; cout << " " << n1 << endl; cout << " --" << endl; cout << " " << n2 << endl; cout << " " << endl; n3 = (n1 / n2) * 1.0000 if (n3 < 10) cout << " " << n3; else if ((n3 >= 10) && (n3 < 100)) cout << " " << n3; else if ((n3 >= 100) && (n3 <1000)) cout << " " << n3 else if ((n2 >= 10) && (n2 <100) && (n1 <100)) cout << "You want to see:" << endl; cout << "" << endl; cout << " " << n1 << endl; cout << " --" << endl; cout << " " << n2 << endl; cout << " " << endl; n3 = (n1 / n2) * 1.0000 if (n3 < 10) cout << " " << n3; else if ((n3 >= 10) && (n3 < 100)) cout << " " << n3; else if ((n3 >= 100) && (n3 <1000)) cout << " " << n3 else if (n1 >= 100) cout << "You want to see:" << endl; cout << "" << endl; cout << " " << n1 << endl; cout << " ---" << endl; cout << " " << n2 << endl; cout << " " << endl; n3 = (n1 / n2) * 1.0000 if (n3 < 10) cout << " " << n3; else if ((n3 >= 10) && (n3 < 100)) cout << " " << n3; else if ((n3 >= 100) && (n3 <1000)) cout << " " << n3 else if (n2 >= 100) cout << "You want to see:" << endl; cout << "" << endl; cout << " " << n1 << endl; cout << " ---" << endl; cout << " " << n2 << endl; cout << " " << endl; n3 = (n1 / n2) * 1.0000; if (n3 < 10) cout << " " << n3; else if ((n3 >= 10) && (n3 < 100)) cout << " " << n3; else if ((n3 >= 100) && (n3 <1000)) cout << " " << n3 }
0
#2 Oct 7th, 2009
see the syntax of "if" statement in C.
u r doing
which is actually
do it as
u r doing
C++ Syntax (Toggle Plain Text)
if(condition) statement; statement;
C++ Syntax (Toggle Plain Text)
if(condition) statement;/*will execute depending on the condition*/ statement;/*will always execute*/
do it as
C++ Syntax (Toggle Plain Text)
if(condition) { statement; statement; }/*use curly brace*/
1
#3 Oct 7th, 2009
To put dkalitas point a little more succinctly:
If you want to execute several statements under an if() statement, then you should enclose them with curly braces '{}'.
e.g.
If you only want to use one statement under an if, then you don't need to use the braces...Braces are optional in that case!
e.g.
or perhaps
So in your code, because you haven't used any braces under any of your if() statements; The first statement under the if() will only get executed if the if statements condition is met...All subsequent statements will be executed regardless of the result of the if statement. (see dkalita's 2nd snippet. That's how the compiler will interpret your code without the braces!)
Cheers for now,
Jas.
If you want to execute several statements under an if() statement, then you should enclose them with curly braces '{}'.
e.g.
C++ Syntax (Toggle Plain Text)
if(condition) { doSomething(); something = something_else; } else { foo=bar; bar=another_thing; }
If you only want to use one statement under an if, then you don't need to use the braces...Braces are optional in that case!
e.g.
C++ Syntax (Toggle Plain Text)
if (condition) doSomething(); else something = something_else;
or perhaps
C++ Syntax (Toggle Plain Text)
if(condition) doSomething(); foo = bar; bar = something; if(foo==bar) doSomethingElse();
So in your code, because you haven't used any braces under any of your if() statements; The first statement under the if() will only get executed if the if statements condition is met...All subsequent statements will be executed regardless of the result of the if statement. (see dkalita's 2nd snippet. That's how the compiler will interpret your code without the braces!)
Cheers for now,
Jas.
If you're into metal, check out my new band at:
http://www.myspace.com/kinasis
Now booking gigs for 2010....
http://www.myspace.com/kinasis
Now booking gigs for 2010....
•
•
Join Date: Sep 2009
Posts: 40
Reputation:
Solved Threads: 0
0
#4 Oct 7th, 2009
Thanks for the replies!
I've added in the brackets; I did it in Programmer's Notepad so I made sure I did all of them absolutely right.
But, when I tried to compile the program with g++, (In Unix) I got this error:
"error: expected unqualified-id before else"
I went through the code several times over, and can't quite figure out. Where is the error coming from?
I've added in the brackets; I did it in Programmer's Notepad so I made sure I did all of them absolutely right.
But, when I tried to compile the program with g++, (In Unix) I got this error:
"error: expected unqualified-id before else"
I went through the code several times over, and can't quite figure out. Where is the error coming from?
C++ Syntax (Toggle Plain Text)
int main () { int n1,n2 float n3 cout << "Enter the numerator: " cin >> n1 cout << "Enter the denominator: " cin >> n2 if ((n1 <10) && (n2 <10)) { cout << "You want to see:" << endl; cout << "" << endl; cout << " " << n1 << endl; cout << " -" << endl; cout << " " << n2 << endl; cout << " " << endl; n3 = (n1 / n2) * 1.0000; if (n3 < 10) { cout << " " << n3; } else { if ((n3 >= 10) && (n3 < 100)) { cout << " " << n3; } } else { if ((n3 >= 100) && (n3 <1000)) { cout << " " << n3; } } } // ------------------------------------------------ else { if ((n1 >= 10) && (n1 <100) && (n2 <100)) { cout << "You want to see:" << endl; cout << "" << endl; cout << " " << n1 << endl; cout << " --" << endl; cout << " " << n2 << endl; cout << " " << endl; n3 = (n1 / n2) * 1.0000; if (n3 < 10) { cout << " " << n3; } else { if ((n3 >= 10) && (n3 < 100)) { cout << " " << n3; } } else { if ((n3 >= 100) && (n3 <1000)) { cout << " " << n3; } } } } // ------------------------------------------- else { if ((n2 >= 10) && (n2 <100) && (n1 <100)) { cout << "You want to see:" << endl; cout << "" << endl; cout << " " << n1 << endl; cout << " --" << endl; cout << " " << n2 << endl; cout << " " << endl; n3 = (n1 / n2) * 1.0000; if (n3 < 10) { cout << " " << n3; } else { if ((n3 >= 10) && (n3 < 100)) { cout << " " << n3; } } else { if ((n3 >= 100) && (n3 <1000)) { cout << " " << n3; } } } } // ----------------------------- else { if (n1 >= 100) { cout << "You want to see:" << endl; cout << "" << endl; cout << " " << n1 << endl; cout << " ---" << endl; cout << " " << n2 << endl; cout << " " << endl; n3 = (n1 / n2) * 1.0000; if (n3 < 10) { cout << " " << n3; } else { if ((n3 >= 10) && (n3 < 100)) { cout << " " << n3; } } else { if ((n3 >= 100) && (n3 <1000)) { cout << " " << n3; } } } } // --------------------------------------- else { if (n2 >= 100) { cout << "You want to see:" << endl; cout << "" << endl; cout << " " << n1 << endl; cout << " ---" << endl; cout << " " << n2 << endl; cout << " " << endl; n3 = (n1 / n2) * 1.0000; if (n3 < 10) { cout << " " << n3; } else { if ((n3 >= 10) && (n3 < 100)) { cout << " " << n3; } } else { if ((n3 >= 100) && (n3 <1000)) { cout << " " << n3; } } } } }
0
#5 Oct 7th, 2009
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
•
•
Join Date: Sep 2009
Posts: 40
Reputation:
Solved Threads: 0
0
#6 Oct 7th, 2009
•
•
•
•
Nope. You didn't get all of them. And you're missing semicolons on a number of statements as well.
Where am I missing semicolons? Could you just point out one place so I know what I'm doing wrong?
This assignment is due in 4 hours, so any quick replies would be appreciated.
•
•
Join Date: Sep 2009
Posts: 40
Reputation:
Solved Threads: 0
0
#8 Oct 7th, 2009
Oh, Thank you for pointing that out, Wyett! That was my old code; I copied and pasted the wrong thing.
Here's the actual code that is giving me the error message:
Here's the actual code that is giving me the error message:
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <iomanip> using namespace std; int main () { double n1,n2,n3; cout << "Enter the numerator: "; cin >> n1; cout << "Enter the denominator: "; cin >> n2; if ((n1 <10) && (n2 <10)) { cout << "You want to see:" << endl; cout << "" << endl; cout << " " << n1 << endl; cout << " -" << endl; cout << " " << n2 << endl; cout << " " << endl; n3 = (n1 / n2) * 1.0000; if (n3 < 10) { cout << " " << n3; } else { if ((n3 >= 10) && (n3 < 100)) { cout << " " << n3; } } else { if ((n3 >= 100) && (n3 <1000)) { cout << " " << n3; } } } // ------------------------------------------------ else { if ((n1 >= 10) && (n1 <100) && (n2 <100)) { cout << "You want to see:" << endl; cout << "" << endl; cout << " " << n1 << endl; cout << " --" << endl; cout << " " << n2 << endl; cout << " " << endl; n3 = (n1 / n2) * 1.0000; if (n3 < 10) { cout << " " << n3; } else { if ((n3 >= 10) && (n3 < 100)) { cout << " " << n3; } } else { if ((n3 >= 100) && (n3 <1000)) { cout << " " << n3; } } } } // ------------------------------------------- else { if ((n2 >= 10) && (n2 <100) && (n1 <100)) { cout << "You want to see:" << endl; cout << "" << endl; cout << " " << n1 << endl; cout << " --" << endl; cout << " " << n2 << endl; cout << " " << endl; n3 = (n1 / n2) * 1.0000; if (n3 < 10) { cout << " " << n3; } else { if ((n3 >= 10) && (n3 < 100)) { cout << " " << n3; } } else { if ((n3 >= 100) && (n3 <1000)) { cout << " " << n3; } } } } // ----------------------------- else { if (n1 >= 100) { cout << "You want to see:" << endl; cout << "" << endl; cout << " " << n1 << endl; cout << " ---" << endl; cout << " " << n2 << endl; cout << " " << endl; n3 = (n1 / n2) * 1.0000; if (n3 < 10) { cout << " " << n3; } else { if ((n3 >= 10) && (n3 < 100)) { cout << " " << n3; } } else { if ((n3 >= 100) && (n3 <1000)) { cout << " " << n3; } } } } // --------------------------------------- else { if (n2 >= 100) { cout << "You want to see:" << endl; cout << "" << endl; cout << " " << n1 << endl; cout << " ---" << endl; cout << " " << n2 << endl; cout << " " << endl; n3 = (n1 / n2) * 1.0000; if (n3 < 10) { cout << " " << n3; } else { if ((n3 >= 10) && (n3 < 100)) { cout << " " << n3; } } else { if ((n3 >= 100) && (n3 <1000)) { cout << " " << n3; } } } } }
Last edited by Towely; Oct 7th, 2009 at 10:54 pm.
0
#9 Oct 7th, 2009
•
•
•
•
I'm 100% positive I got all of the brackets... I quadruple checked. If I am missing one, could you point out where?
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <iomanip> using namespace std; int main () { double n1,n2,n3; cout << "Enter the numerator: "; cin >> n1; cout << "Enter the denominator: "; cin >> n2; if ( (n1 <10) && (n2 <10) ) { cout << "You want to see:" << endl; cout << "" << endl; cout << " " << n1 << endl; cout << " -" << endl; cout << " " << n2 << endl; cout << " " << endl; n3 = (n1 / n2) * 1.0000; if ( n3 < 10 ) { cout << " " << n3; } else { if ( (n3 >= 10) && (n3 < 100) ) { cout << " " << n3; } } else { if ((n3 >= 100) && (n3 <1000)) { cout << " " << n3; } } } // ------------------------------------------------ else { if ((n1 >= 10) && (n1 <100) && (n2 <100)) { cout << "You want to see:" << endl; cout << "" << endl; cout << " " << n1 << endl; cout << " --" << endl; cout << " " << n2 << endl; cout << " " << endl; n3 = (n1 / n2) * 1.0000; if (n3 < 10) { cout << " " << n3; } else { if ((n3 >= 10) && (n3 < 100)) { cout << " " << n3; } } else { if ((n3 >= 100) && (n3 <1000)) { cout << " " << n3; } } } } // ------------------------------------------- else { if ((n2 >= 10) && (n2 <100) && (n1 <100)) { cout << "You want to see:" << endl; cout << "" << endl; cout << " " << n1 << endl; cout << " --" << endl; cout << " " << n2 << endl; cout << " " << endl; n3 = (n1 / n2) * 1.0000; if (n3 < 10) { cout << " " << n3; } else { if ((n3 >= 10) && (n3 < 100)) { cout << " " << n3; } } else { if ((n3 >= 100) && (n3 <1000)) { cout << " " << n3; } } } } // ----------------------------- else { if (n1 >= 100) { cout << "You want to see:" << endl; cout << "" << endl; cout << " " << n1 << endl; cout << " ---" << endl; cout << " " << n2 << endl; cout << " " << endl; n3 = (n1 / n2) * 1.0000; if (n3 < 10) { cout << " " << n3; } else { if ((n3 >= 10) && (n3 < 100)) { cout << " " << n3; } } else { if ((n3 >= 100) && (n3 <1000)) { cout << " " << n3; } } } } // --------------------------------------- else { if (n2 >= 100) { cout << "You want to see:" << endl; cout << "" << endl; cout << " " << n1 << endl; cout << " ---" << endl; cout << " " << n2 << endl; cout << " " << endl; n3 = (n1 / n2) * 1.0000; if (n3 < 10) { cout << " " << n3; } else { if ((n3 >= 10) && (n3 < 100)) { cout << " " << n3; } } else { if ((n3 >= 100) && (n3 <1000)) { cout << " " << n3; } } } } }
Last edited by Dave Sinkula; Oct 7th, 2009 at 11:15 pm.
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
![]() |
Similar Threads
- "Craps", Game Help (C++)
- Function being "skipped" in switch statement (C++)
- After 2nd element read... cout is "scattered" (C++)
- google "keyword" question (Search Engine Optimization)
- Deitel's "C++ How To Program" exercise 2.18 (C++)
- cout << "Please Help" << endl; (C++)
- system("PAUSE") (C++)
- I need special stuff in my project like system("cls") (C++)
Other Threads in the C++ Forum
- Previous Thread: question about string element
- Next Thread: problem with inheritance
Views: 467 | Replies: 20
| Thread Tools | Search this Thread |
Tag cloud for C++
6 add api array arrays beginner binary bitmap c++ c/c++ calculator char class classes code compile compiler console conversion convert count data delete desktop directshow dll encryption error file forms fstream function functions game getline givemetehcodez google graph homeworkhelper iamthwee ifstream input int integer java lazy lib linkedlist linux loop looping loops map math matrix memory microsoft newbie news node number output parameter pointer problem program programming project proxy python random read recursion recursive reference return sort string strings struct studio system template templates test text tree unix url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






