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

Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Sep 2009
Posts: 40
Reputation: Towely is an unknown quantity at this point 
Solved Threads: 0
Towely Towely is offline Offline
Light Poster

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

 
0
  #1
Oct 7th, 2009
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?

  1. #include <iostream>
  2. using namespace std;
  3. int main ()
  4.  
  5. {
  6.  
  7. int n1,n2
  8. float n3
  9.  
  10. cout << "Enter the numerator: "
  11. cin >> n1
  12. cout << "Enter the denominator: "
  13. cin >> n2
  14.  
  15. if ((n1 <10) && (n2 <10))
  16. cout << "You want to see:" << endl;
  17. cout << "" << endl;
  18. cout << " " << n1 << endl;
  19. cout << " -" << endl;
  20. cout << " " << n2 << endl;
  21. cout << " " << endl;
  22. n3 = (n1 / n2) * 1.0000
  23. if (n3 < 10)
  24. cout << " " << n3;
  25. else if ((n3 >= 10) && (n3 < 100))
  26. cout << " " << n3;
  27. else if ((n3 >= 100) && (n3 <1000))
  28. cout << " " << n3
  29.  
  30. else if ((n1 >= 10) && (n1 <100) && (n2 <100))
  31. cout << "You want to see:" << endl;
  32. cout << "" << endl;
  33. cout << " " << n1 << endl;
  34. cout << " --" << endl;
  35. cout << " " << n2 << endl;
  36. cout << " " << endl;
  37. n3 = (n1 / n2) * 1.0000
  38. if (n3 < 10)
  39. cout << " " << n3;
  40. else if ((n3 >= 10) && (n3 < 100))
  41. cout << " " << n3;
  42. else if ((n3 >= 100) && (n3 <1000))
  43. cout << " " << n3
  44.  
  45. else if ((n2 >= 10) && (n2 <100) && (n1 <100))
  46. cout << "You want to see:" << endl;
  47. cout << "" << endl;
  48. cout << " " << n1 << endl;
  49. cout << " --" << endl;
  50. cout << " " << n2 << endl;
  51. cout << " " << endl;
  52. n3 = (n1 / n2) * 1.0000
  53. if (n3 < 10)
  54. cout << " " << n3;
  55. else if ((n3 >= 10) && (n3 < 100))
  56. cout << " " << n3;
  57. else if ((n3 >= 100) && (n3 <1000))
  58. cout << " " << n3
  59.  
  60. else if (n1 >= 100)
  61. cout << "You want to see:" << endl;
  62. cout << "" << endl;
  63. cout << " " << n1 << endl;
  64. cout << " ---" << endl;
  65. cout << " " << n2 << endl;
  66. cout << " " << endl;
  67. n3 = (n1 / n2) * 1.0000
  68. if (n3 < 10)
  69. cout << " " << n3;
  70. else if ((n3 >= 10) && (n3 < 100))
  71. cout << " " << n3;
  72. else if ((n3 >= 100) && (n3 <1000))
  73. cout << " " << n3
  74.  
  75. else if (n2 >= 100)
  76. cout << "You want to see:" << endl;
  77. cout << "" << endl;
  78. cout << " " << n1 << endl;
  79. cout << " ---" << endl;
  80. cout << " " << n2 << endl;
  81. cout << " " << endl;
  82. n3 = (n1 / n2) * 1.0000;
  83. if (n3 < 10)
  84. cout << " " << n3;
  85. else if ((n3 >= 10) && (n3 < 100))
  86. cout << " " << n3;
  87. else if ((n3 >= 100) && (n3 <1000))
  88. cout << " " << n3
  89.  
  90. }
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 358
Reputation: dkalita will become famous soon enough dkalita will become famous soon enough 
Solved Threads: 56
dkalita's Avatar
dkalita dkalita is offline Offline
Posting Whiz
 
0
  #2
Oct 7th, 2009
see the syntax of "if" statement in C.

u r doing

  1. if(condition)
  2. statement;
  3. statement;
which is actually
  1. if(condition)
  2. statement;/*will execute depending on the condition*/
  3. statement;/*will always execute*/

do it as
  1. if(condition)
  2. {
  3. statement;
  4. statement;
  5. }/*use curly brace*/
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 343
Reputation: JasonHippy is just really nice JasonHippy is just really nice JasonHippy is just really nice JasonHippy is just really nice 
Solved Threads: 63
JasonHippy's Avatar
JasonHippy JasonHippy is offline Offline
Posting Whiz
 
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.
  1. if(condition)
  2. {
  3. doSomething();
  4. something = something_else;
  5. }
  6. else
  7. {
  8. foo=bar;
  9. bar=another_thing;
  10. }

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.
  1. if (condition)
  2. doSomething();
  3. else
  4. something = something_else;

or perhaps
  1. if(condition)
  2. doSomething();
  3.  
  4. foo = bar;
  5. bar = something;
  6.  
  7. if(foo==bar)
  8. 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....
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 40
Reputation: Towely is an unknown quantity at this point 
Solved Threads: 0
Towely Towely is offline Offline
Light Poster
 
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?

  1. int main ()
  2.  
  3. {
  4.  
  5. int n1,n2
  6. float n3
  7.  
  8. cout << "Enter the numerator: "
  9. cin >> n1
  10. cout << "Enter the denominator: "
  11. cin >> n2
  12.  
  13. if ((n1 <10) && (n2 <10))
  14. {
  15. cout << "You want to see:" << endl;
  16. cout << "" << endl;
  17. cout << " " << n1 << endl;
  18. cout << " -" << endl;
  19. cout << " " << n2 << endl;
  20. cout << " " << endl;
  21.  
  22. n3 = (n1 / n2) * 1.0000;
  23. if (n3 < 10)
  24. {
  25. cout << " " << n3;
  26. }
  27. else
  28. {
  29. if
  30. ((n3 >= 10) && (n3 < 100))
  31. {
  32. cout << " " << n3;
  33. }
  34. }
  35. else
  36. {
  37. if ((n3 >= 100) && (n3 <1000))
  38. {
  39. cout << " " << n3;
  40. }
  41. }
  42. }
  43.  
  44. // ------------------------------------------------
  45.  
  46.  
  47. else
  48. {
  49. if ((n1 >= 10) && (n1 <100) && (n2 <100))
  50. {
  51. cout << "You want to see:" << endl;
  52. cout << "" << endl;
  53. cout << " " << n1 << endl;
  54. cout << " --" << endl;
  55. cout << " " << n2 << endl;
  56. cout << " " << endl;
  57.  
  58. n3 = (n1 / n2) * 1.0000;
  59. if (n3 < 10)
  60. {
  61. cout << " " << n3;
  62. }
  63. else
  64. {
  65. if
  66. ((n3 >= 10) && (n3 < 100))
  67. {
  68. cout << " " << n3;
  69. }
  70. }
  71. else
  72. {
  73. if ((n3 >= 100) && (n3 <1000))
  74. {
  75. cout << " " << n3;
  76. }
  77. }
  78. }
  79. }
  80.  
  81. // -------------------------------------------
  82.  
  83.  
  84. else
  85. {
  86. if ((n2 >= 10) && (n2 <100) && (n1 <100))
  87. {
  88. cout << "You want to see:" << endl;
  89. cout << "" << endl;
  90. cout << " " << n1 << endl;
  91. cout << " --" << endl;
  92. cout << " " << n2 << endl;
  93. cout << " " << endl;
  94.  
  95. n3 = (n1 / n2) * 1.0000;
  96. if (n3 < 10)
  97. {
  98. cout << " " << n3;
  99. }
  100. else
  101. {
  102. if
  103. ((n3 >= 10) && (n3 < 100))
  104. {
  105. cout << " " << n3;
  106. }
  107. }
  108. else
  109. {
  110. if ((n3 >= 100) && (n3 <1000))
  111. {
  112. cout << " " << n3;
  113. }
  114. }
  115. }
  116. }
  117.  
  118. // -----------------------------
  119.  
  120. else
  121. {
  122. if (n1 >= 100)
  123. {
  124. cout << "You want to see:" << endl;
  125. cout << "" << endl;
  126. cout << " " << n1 << endl;
  127. cout << " ---" << endl;
  128. cout << " " << n2 << endl;
  129. cout << " " << endl;
  130.  
  131. n3 = (n1 / n2) * 1.0000;
  132. if (n3 < 10)
  133. {
  134. cout << " " << n3;
  135. }
  136. else
  137. {
  138. if
  139. ((n3 >= 10) && (n3 < 100))
  140. {
  141. cout << " " << n3;
  142. }
  143. }
  144. else
  145. {
  146. if ((n3 >= 100) && (n3 <1000))
  147. {
  148. cout << " " << n3;
  149. }
  150. }
  151. }
  152. }
  153.  
  154. // ---------------------------------------
  155.  
  156. else
  157. {
  158. if (n2 >= 100)
  159. {
  160. cout << "You want to see:" << endl;
  161. cout << "" << endl;
  162. cout << " " << n1 << endl;
  163. cout << " ---" << endl;
  164. cout << " " << n2 << endl;
  165. cout << " " << endl;
  166.  
  167. n3 = (n1 / n2) * 1.0000;
  168. if (n3 < 10)
  169. {
  170. cout << " " << n3;
  171. }
  172. else
  173. {
  174. if
  175. ((n3 >= 10) && (n3 < 100))
  176. {
  177. cout << " " << n3;
  178. }
  179. }
  180. else
  181. {
  182. if ((n3 >= 100) && (n3 <1000))
  183. {
  184. cout << " " << n3;
  185. }
  186. }
  187. }
  188. }
  189.  
  190.  
  191. }
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,461
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 254
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c
 
0
  #5
Oct 7th, 2009
Originally Posted by Towely View Post
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.
"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
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 40
Reputation: Towely is an unknown quantity at this point 
Solved Threads: 0
Towely Towely is offline Offline
Light Poster
 
0
  #6
Oct 7th, 2009
Originally Posted by Dave Sinkula View Post
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.
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 19
Reputation: wyett is an unknown quantity at this point 
Solved Threads: 0
wyett wyett is offline Offline
Newbie Poster
 
0
  #7
Oct 7th, 2009
Look at your posts above with code. Lines 5, 6, 8, 9, 10, 11. All missing semi colons.
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 40
Reputation: Towely is an unknown quantity at this point 
Solved Threads: 0
Towely Towely is offline Offline
Light Poster
 
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:

  1. #include <iostream>
  2. #include <iomanip>
  3. using namespace std;
  4. int main ()
  5. {
  6. double n1,n2,n3;
  7.  
  8. cout << "Enter the numerator: ";
  9. cin >> n1;
  10. cout << "Enter the denominator: ";
  11. cin >> n2;
  12. if ((n1 <10) && (n2 <10))
  13. {
  14. cout << "You want to see:" << endl;
  15. cout << "" << endl;
  16. cout << " " << n1 << endl;
  17. cout << " -" << endl;
  18. cout << " " << n2 << endl;
  19. cout << " " << endl;
  20.  
  21. n3 = (n1 / n2) * 1.0000;
  22. if (n3 < 10)
  23. {
  24. cout << " " << n3;
  25. }
  26. else
  27. {
  28. if
  29. ((n3 >= 10) && (n3 < 100))
  30. {
  31. cout << " " << n3;
  32. }
  33. }
  34. else
  35. {
  36. if ((n3 >= 100) && (n3 <1000))
  37. {
  38. cout << " " << n3;
  39. }
  40. }
  41. }
  42.  
  43. // ------------------------------------------------
  44.  
  45.  
  46. else
  47. {
  48. if ((n1 >= 10) && (n1 <100) && (n2 <100))
  49. {
  50. cout << "You want to see:" << endl;
  51. cout << "" << endl;
  52. cout << " " << n1 << endl;
  53. cout << " --" << endl;
  54. cout << " " << n2 << endl;
  55. cout << " " << endl;
  56.  
  57. n3 = (n1 / n2) * 1.0000;
  58. if (n3 < 10)
  59. {
  60. cout << " " << n3;
  61. }
  62. else
  63. {
  64. if
  65. ((n3 >= 10) && (n3 < 100))
  66. {
  67. cout << " " << n3;
  68. }
  69. }
  70. else
  71. {
  72. if ((n3 >= 100) && (n3 <1000))
  73. {
  74. cout << " " << n3;
  75. }
  76. }
  77. }
  78. }
  79.  
  80. // -------------------------------------------
  81.  
  82.  
  83. else
  84. {
  85. if ((n2 >= 10) && (n2 <100) && (n1 <100))
  86. {
  87. cout << "You want to see:" << endl;
  88. cout << "" << endl;
  89. cout << " " << n1 << endl;
  90. cout << " --" << endl;
  91. cout << " " << n2 << endl;
  92. cout << " " << endl;
  93.  
  94. n3 = (n1 / n2) * 1.0000;
  95. if (n3 < 10)
  96. {
  97. cout << " " << n3;
  98. }
  99. else
  100. {
  101. if
  102. ((n3 >= 10) && (n3 < 100))
  103. {
  104. cout << " " << n3;
  105. }
  106. }
  107. else
  108. {
  109. if ((n3 >= 100) && (n3 <1000))
  110. {
  111. cout << " " << n3;
  112. }
  113. }
  114. }
  115. }
  116.  
  117. // -----------------------------
  118.  
  119. else
  120. {
  121. if (n1 >= 100)
  122. {
  123. cout << "You want to see:" << endl;
  124. cout << "" << endl;
  125. cout << " " << n1 << endl;
  126. cout << " ---" << endl;
  127. cout << " " << n2 << endl;
  128. cout << " " << endl;
  129.  
  130. n3 = (n1 / n2) * 1.0000;
  131. if (n3 < 10)
  132. {
  133. cout << " " << n3;
  134. }
  135. else
  136. {
  137. if
  138. ((n3 >= 10) && (n3 < 100))
  139. {
  140. cout << " " << n3;
  141. }
  142. }
  143. else
  144. {
  145. if ((n3 >= 100) && (n3 <1000))
  146. {
  147. cout << " " << n3;
  148. }
  149. }
  150. }
  151. }
  152.  
  153. // ---------------------------------------
  154.  
  155. else
  156. {
  157. if (n2 >= 100)
  158. {
  159. cout << "You want to see:" << endl;
  160. cout << "" << endl;
  161. cout << " " << n1 << endl;
  162. cout << " ---" << endl;
  163. cout << " " << n2 << endl;
  164. cout << " " << endl;
  165.  
  166. n3 = (n1 / n2) * 1.0000;
  167. if (n3 < 10)
  168. {
  169. cout << " " << n3;
  170. }
  171. else
  172. {
  173. if
  174. ((n3 >= 10) && (n3 < 100))
  175. {
  176. cout << " " << n3;
  177. }
  178. }
  179. else
  180. {
  181. if ((n3 >= 100) && (n3 <1000))
  182. {
  183. cout << " " << n3;
  184. }
  185. }
  186. }
  187. }
  188.  
  189.  
  190. }
Last edited by Towely; Oct 7th, 2009 at 10:54 pm.
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,461
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 254
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c
 
0
  #9
Oct 7th, 2009
Originally Posted by Towely View Post
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:
  1. #include <iostream>
  2. #include <iomanip>
  3. using namespace std;
  4. int main ()
  5. {
  6. double n1,n2,n3;
  7.  
  8. cout << "Enter the numerator: ";
  9. cin >> n1;
  10. cout << "Enter the denominator: ";
  11. cin >> n2;
  12. if ( (n1 <10) && (n2 <10) )
  13. {
  14. cout << "You want to see:" << endl;
  15. cout << "" << endl;
  16. cout << " " << n1 << endl;
  17. cout << " -" << endl;
  18. cout << " " << n2 << endl;
  19. cout << " " << endl;
  20.  
  21. n3 = (n1 / n2) * 1.0000;
  22. if ( n3 < 10 )
  23. {
  24. cout << " " << n3;
  25. }
  26. else
  27. {
  28. if
  29. ( (n3 >= 10) && (n3 < 100) )
  30. {
  31. cout << " " << n3;
  32. }
  33. }
  34. else
  35. {
  36. if ((n3 >= 100) && (n3 <1000))
  37. {
  38. cout << " " << n3;
  39. }
  40. }
  41. }
  42.  
  43. // ------------------------------------------------
  44.  
  45.  
  46. else
  47. {
  48. if ((n1 >= 10) && (n1 <100) && (n2 <100))
  49. {
  50. cout << "You want to see:" << endl;
  51. cout << "" << endl;
  52. cout << " " << n1 << endl;
  53. cout << " --" << endl;
  54. cout << " " << n2 << endl;
  55. cout << " " << endl;
  56.  
  57. n3 = (n1 / n2) * 1.0000;
  58. if (n3 < 10)
  59. {
  60. cout << " " << n3;
  61. }
  62. else
  63. {
  64. if
  65. ((n3 >= 10) && (n3 < 100))
  66. {
  67. cout << " " << n3;
  68. }
  69. }
  70. else
  71. {
  72. if ((n3 >= 100) && (n3 <1000))
  73. {
  74. cout << " " << n3;
  75. }
  76. }
  77. }
  78. }
  79.  
  80. // -------------------------------------------
  81.  
  82.  
  83. else
  84. {
  85. if ((n2 >= 10) && (n2 <100) && (n1 <100))
  86. {
  87. cout << "You want to see:" << endl;
  88. cout << "" << endl;
  89. cout << " " << n1 << endl;
  90. cout << " --" << endl;
  91. cout << " " << n2 << endl;
  92. cout << " " << endl;
  93.  
  94. n3 = (n1 / n2) * 1.0000;
  95. if (n3 < 10)
  96. {
  97. cout << " " << n3;
  98. }
  99. else
  100. {
  101. if
  102. ((n3 >= 10) && (n3 < 100))
  103. {
  104. cout << " " << n3;
  105. }
  106. }
  107. else
  108. {
  109. if ((n3 >= 100) && (n3 <1000))
  110. {
  111. cout << " " << n3;
  112. }
  113. }
  114. }
  115. }
  116.  
  117. // -----------------------------
  118.  
  119. else
  120. {
  121. if (n1 >= 100)
  122. {
  123. cout << "You want to see:" << endl;
  124. cout << "" << endl;
  125. cout << " " << n1 << endl;
  126. cout << " ---" << endl;
  127. cout << " " << n2 << endl;
  128. cout << " " << endl;
  129.  
  130. n3 = (n1 / n2) * 1.0000;
  131. if (n3 < 10)
  132. {
  133. cout << " " << n3;
  134. }
  135. else
  136. {
  137. if
  138. ((n3 >= 10) && (n3 < 100))
  139. {
  140. cout << " " << n3;
  141. }
  142. }
  143. else
  144. {
  145. if ((n3 >= 100) && (n3 <1000))
  146. {
  147. cout << " " << n3;
  148. }
  149. }
  150. }
  151. }
  152.  
  153. // ---------------------------------------
  154.  
  155. else
  156. {
  157. if (n2 >= 100)
  158. {
  159. cout << "You want to see:" << endl;
  160. cout << "" << endl;
  161. cout << " " << n1 << endl;
  162. cout << " ---" << endl;
  163. cout << " " << n2 << endl;
  164. cout << " " << endl;
  165.  
  166. n3 = (n1 / n2) * 1.0000;
  167. if (n3 < 10)
  168. {
  169. cout << " " << n3;
  170. }
  171. else
  172. {
  173. if
  174. ((n3 >= 10) && (n3 < 100))
  175. {
  176. cout << " " << n3;
  177. }
  178. }
  179. else
  180. {
  181. if ((n3 >= 100) && (n3 <1000))
  182. {
  183. cout << " " << n3;
  184. }
  185. }
  186. }
  187. }
  188.  
  189.  
  190. }
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
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 40
Reputation: Towely is an unknown quantity at this point 
Solved Threads: 0
Towely Towely is offline Offline
Light Poster
 
0
  #10
Oct 7th, 2009
Originally Posted by Dave Sinkula View Post
My beautifier gave up where the indentation stops:
Forgive my ignorance, does that mean that is where the error is?
Reply With Quote Quick reply to this message  
Reply

Message:




Views: 467 | Replies: 20
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC