# Multiple cout statements within an If statement

Page 1 of 1

## 6 Replies - 1827 Views - Last Post: 08 October 2009 - 12:44 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=130380&amp;s=cd4878e00b32762efe764ae4810841c1&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 Towely

Reputation: 0
• Posts: 29
• Joined: 29-September 09

# Multiple cout statements within an If statement

Posted 06 October 2009 - 11:02 PM

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?

Oh, and just to clarify, all of the spaces within the quotes need to be there for formatting purposes.

```#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

}
```

This post has been edited by Towely: 06 October 2009 - 11:03 PM

Is This A Good Question/Topic? 0

## Replies To: Multiple cout statements within an If statement

### #2 no2pencil

• Professor Snuggly Pants

Reputation: 5959
• Posts: 29,128
• Joined: 10-May 07

## Re: Multiple cout statements within an If statement

Posted 06 October 2009 - 11:10 PM

Just randomly grabbing one of your code blocks :

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

```

Here is how that conditional if statement will be handled.

start
if n2 is greater than or equal to one hundred, output "You want to see:" << endl;
exit the if statement
continue
output everything
set n3 equal to ...

In short : You must include brackets on your if statements.

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

```

Not only will it function correctly, it's much easier to read.

### #3 jjl

• Engineer

Reputation: 1130
• Posts: 4,686
• Joined: 09-June 09

## Re: Multiple cout statements within an If statement

Posted 06 October 2009 - 11:37 PM

Well there are about 100 syntax errors in your code, ever heard of a semi colon? you need brackets.
Your logic makes no sence, you do the same thing in every condition, so whats the point of the if statments.

Heres code that makes sense according to your description
```#include <iostream>

using namespace std;

int main()
{
double quotient;
int numerator, denominator;
cout<<"Enter Numerator :";
cin>>numerator;
cout<<"Enter Denominator :";
cin>>denominator;

quotient = (double)numerator/(double)denominator;

cout<<numerator<<endl
<<"----"<<" = "<<quotient<<endl
<<denominator<<endl;

cin.ignore();
cin.get();

return 0;
}

```

### #4 Towely

Reputation: 0
• Posts: 29
• Joined: 29-September 09

## Re: Multiple cout statements within an If statement

Posted 07 October 2009 - 03:25 AM

Thanks, no2pencil! I totally forgot about using brackets. That was exactly what I needed. Very helpful post!

ImaSexy, There's a different amount of spaces in each of the things that look the same to you. It's a formatting thing... but I assure you that the same thing isn't happening in all the if statements.

### #5 Towely

Reputation: 0
• Posts: 29
• Joined: 29-September 09

## Re: Multiple cout statements within an If statement

Posted 07 October 2009 - 05:17 PM

Okay, so - 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;
}
}
}
}

}
```

This post has been edited by Towely: 07 October 2009 - 06:52 PM

### #6 jjl

• Engineer

Reputation: 1130
• Posts: 4,686
• Joined: 09-June 09

## Re: Multiple cout statements within an If statement

Posted 07 October 2009 - 08:36 PM

you can have control structures with consecutive else's, you need to use else if throughout your program

### #7 Towely

Reputation: 0
• Posts: 29
• Joined: 29-September 09

## Re: Multiple cout statements within an If statement

Posted 08 October 2009 - 12:44 AM