954,498 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Multiple "cout"s and "if"s within an If statement

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?

#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

}
Towely
Light Poster
40 posts since Sep 2009
Reputation Points: 10
Solved Threads: 0
 

see the syntax of "if" statement in C.

u r doing

if(condition)
   statement;
   statement;

which is actually

if(condition)
   statement;/*will execute depending on the condition*/
statement;/*will always execute*/


do it as

if(condition)
{
   statement;
   statement;
}/*use curly brace*/
dkalita
Posting Pro in Training
402 posts since Sep 2009
Reputation Points: 121
Solved Threads: 61
 

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(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.

if (condition)
     doSomething();
else
    something = something_else;


or perhaps

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.

JasonHippy
Master Poster
772 posts since Jan 2009
Reputation Points: 590
Solved Threads: 125
 

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?

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


}
Towely
Light Poster
40 posts since Sep 2009
Reputation Points: 10
Solved Threads: 0
 
I've added in the brackets; I did it in Programmer's Notepad so I made sure I did all of them absolutely right.

Nope. You didn't get all of them. And you're missing semicolons on a number of statements as well.

Dave Sinkula
long time no c
Team Colleague
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
 
Nope. You didn't get all of them. And you're missing semicolons on a number of statements as well.

I'm 100% positive I got all of the brackets... I quadruple checked. If I am missing one, could you point out where?

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.

Towely
Light Poster
40 posts since Sep 2009
Reputation Points: 10
Solved Threads: 0
 

Look at your posts above with code. Lines 5, 6, 8, 9, 10, 11. All missing semi colons.

wyett
Newbie Poster
22 posts since Sep 2009
Reputation Points: 10
Solved Threads: 0
 

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:

#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;
}
}
}
}


}
Towely
Light Poster
40 posts since Sep 2009
Reputation Points: 10
Solved Threads: 0
 
I'm 100% positive I got all of the brackets... I quadruple checked. If I am missing one, could you point out where?

My beautifier gave up where the indentation stops:

#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;
}
}
}
}


}
Dave Sinkula
long time no c
Team Colleague
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
 
My beautifier gave up where the indentation stops:

Forgive my ignorance, does that mean that is where the error is?

Towely
Light Poster
40 posts since Sep 2009
Reputation Points: 10
Solved Threads: 0
 

Okay,
So I took the time and "beautified" the code myself, to make it easier to spot the errors. Here is the code... This stuff is due in 3 hours, so anyone that could help me spot the error, I'd be really grateful!

Here are the error codes I'm getting now:

error: expected primary-expression before "else"
error: expected ; before "else"
error: expected } at end of input

So there's slightly more errors now than there was before.. I'm going to keep digging through the code, but any help would be great.

#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 << setprecision(5);
      cout << "       " << n3;
   }
   }
 else
      {
         if ((n3 >= 10) && (n3 <100))
         {
      cout << setprecision (6);
      cout << "      " << n3;
         }
      }
else
{
         if ((n3 >= 100) && (n3 < 1000))
         {
            cout << setprecision (7);
            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 << setprecision(5);
      cout << "       " << n3;
   }

      else
      {
         if ((n3 >= 10) && (n3 <100))
         {
      cout << setprecision (6);
      cout << "      " << n3;
         }
      }
   }
}
else
{
         if ((n3 >= 100) && (n3 < 1000))
         {
            cout << setprecision (7);
            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 << setprecision(5);
      cout << "       " << n3;
   }
   }
      else
      {
         if ((n3 >= 10) && (n3 <100))
         {
      cout << setprecision (6);
      cout << "      " << n3;
         }
      }
}
else
{
         if ((n3 >= 100) && (n3 < 1000))
         {
            cout << setprecision (7);
            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 << setprecision(5);
      cout << "       " << n3;
   }
   }
      else
      {
         if ((n3 >= 10) && (n3 <100))
         {
      cout << setprecision (6);
      cout << "      " << n3;
         }
      }
}
else
{
         if ((n3 >= 100) && (n3 < 1000))
         {
            cout << setprecision (7);
            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 << setprecision(5);
      cout << "       " << n3;
   }
   }
      else
      {
         if ((n3 >= 10) && (n3 <100))
         {
      cout << setprecision (6);
      cout << "      " << n3;
         }
      }
}
else
{
         if ((n3 >= 100) && (n3 < 1000))
         {
            cout << setprecision (7);
            cout << "     " << n3;
         }
}

}
Towely
Light Poster
40 posts since Sep 2009
Reputation Points: 10
Solved Threads: 0
 

Well, I couldn't find where I was going wrong, so I just scrapped the whole thing and started from scratch.
It's working perfectly now. I don't know what I did differently. But thanks to everyone that helped!

Towely
Light Poster
40 posts since Sep 2009
Reputation Points: 10
Solved Threads: 0
 

Why don't you use a compiler that actually tells you were your have made an error ? Looking through 190 rows of code is abit harsh.

K0ns3rv
Junior Poster in Training
92 posts since Oct 2009
Reputation Points: 11
Solved Threads: 8
 

Can you reccomend any good ones?

Towely
Light Poster
40 posts since Sep 2009
Reputation Points: 10
Solved Threads: 0
 

Well my experience is only working on windows machines so naturally i don't know any good linux editors, but a quick google might yield some result.

EDIT: Found this one http://sourceforge.net/projects/dev-cpp/files/

//K0ns3rv

K0ns3rv
Junior Poster in Training
92 posts since Oct 2009
Reputation Points: 11
Solved Threads: 8
 

I'm actually working on a Windows machine too. My school uses Unix, but I just access their unix machines with a Virtual Network program that I run on my Win XP laptop. All I do with Unix is hand the projects in.

So, what is a good Windows-based C++ compiler?

Towely
Light Poster
40 posts since Sep 2009
Reputation Points: 10
Solved Threads: 0
 

- Visual Studio C++ Express
- Code::blocks

Both free for download

Nick Evan
Not a Llama
Moderator
10,112 posts since Oct 2006
Reputation Points: 4,142
Solved Threads: 403
 

Or dev cpp from bloodshed, it's free xD

K0ns3rv
Junior Poster in Training
92 posts since Oct 2009
Reputation Points: 11
Solved Threads: 8
 
Or dev cpp from bloodshed, it's free xD

So are my suggestions. The problem with Dev-CPP is that it is no longer being updated, so it will become outdated very soon.
Better to choose an IDE which is still alive and kickin' ;)

Nick Evan
Not a Llama
Moderator
10,112 posts since Oct 2006
Reputation Points: 4,142
Solved Threads: 403
 

Wasn't aware of that lol. Kind of sad.

K0ns3rv
Junior Poster in Training
92 posts since Oct 2009
Reputation Points: 11
Solved Threads: 8
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You