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

}

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*/

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.

commented: that was a detailed answer.... :) +1

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


}

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.

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.

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

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


}

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


}

My beautifier gave up where the indentation stops:

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

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

}

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!

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.

Can you reccomend any good ones?

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

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?

Or dev cpp from bloodshed, it's free xD

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

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

Thanks for the suggestions. I'll be trying Visual C++ Express.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.