Multiple cout statements within an If statement

seems to take me out of the If statement...

Page 1 of 1

6 Replies - 1387 Views - Last Post: 08 October 2009 - 12:44 AM Rate Topic: -----

#1 Towely  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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  Icon User is offline

  • Admiral Fancy Pants
  • member icon

Reputation: 5363
  • View blog
  • Posts: 27,325
  • 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.
Was This Post Helpful? 0
  • +
  • -

#3 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1086
  • View blog
  • Posts: 4,556
  • 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;
}


Was This Post Helpful? 0
  • +
  • -

#4 Towely  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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.
Was This Post Helpful? 0
  • +
  • -

#5 Towely  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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

Was This Post Helpful? 0
  • +
  • -

#6 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1086
  • View blog
  • Posts: 4,556
  • 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
Was This Post Helpful? 0
  • +
  • -

#7 Towely  Icon User is offline

  • New D.I.C Head

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

Re: Multiple cout statements within an If statement

Posted 08 October 2009 - 12:44 AM

Thanks for the reply..
I couldn't figure out where the problem was, so I started again from scratch and got it to work out perfectly.
I still don't know where I was going wrong, but it's all good now.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1