473,385 Members | 1,766 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Nested if statement syntax issue

I am having trouble with the nested if statement.
can anyone tell me where i went wrong with this code?
the syntax error starts by the else statement within the target of the
first if statement.

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. using namespace std;
  3. int main()
  4. {
  5.     int num, feet;
  6.     double zar, usd, c, temp, pound, kg, meter, length, width, area;
  7.  
  8.     cout << "Welcome to converter 1.2\n" << "\n"; 
  9.     cout << "Choose between the following:\n"; 
  10.     cout << "\n" << "1.Convert feet to meters.\n";
  11.     cout << "2.Calculate area of triangle.\n" << "3.Convert USD to ZAR.\n"; 
  12.     cout << "4.Convert degrees c to degrees f.\n"; 
  13.     cout << "5.Convert pounds to kilograms.\n";
  14.     cin >> num;
  15.  
  16.     if(num > 1)  {
  17.            if(num == 2)  cout << "2.Calculate area of rectangle.\n"; 
  18.            cout << "Enter length:\n";
  19.            cin >> length;
  20.            cout << "Enter width:\n";
  21.            cin >> width;
  22.            area = length * width;
  23.            cout << "The area is " << area;
  24.  
  25.            if(num == 3) cout << "3.Convert USD to ZAR.\n"; 
  26.            cout << "Enter value in USD:\n";
  27.            cin >> usd;
  28.            zar = usd * 7.85;
  29.            cout << usd << " USD is " << zar << " ZAR.";
  30.  
  31.            if(num == 4) cout << "4.Convert degrees c to degrees f.\n"; 
  32.            cout << "Enter value in degrees c:\n";
  33.            cin >> c;
  34.            temp = c * 33.8;
  35.            cout << c << " degrees c is " << temp << " degrees f.";
  36.  
  37.            else cout << "5.Convert pounds to kilograms.\n";/* the suntax error 
  38.            comes from this statement*/ 
  39.            cout << "Enter number in pounds:\n";
  40.            cin >> pound;
  41.            kg = pound * 0.45;
  42.            cout << pound << " pound/s is " << kg << " kilogram/s.";
  43.     }
  44.     else(num < 2) cout << "1.Convert feet to meters.\n"; 
  45.     cout << "Enter number in feet:\n";
  46.     cin >> feet;
  47.     meter = feet / 3.28;
  48.     cout << feet << " feet is " << meter << " meters.";
  49.  
  50.     cin.get();
  51.     cin.get();
  52.     return 0;
  53. }
Apr 15 '10 #1
4 3542
code green
1,726 Expert 1GB
Looks like a missing closing brace before the else.
Not sure where you want it though, because no CODE tags and no indents
Apr 15 '10 #2
it is always a good practice to cover all if/else statements with { } to make the block clear.. Unless you make it, the compiler cannot decide for which if statement the else statement is an alternative..

Here is what the erroneous part of your code means by the moment:

Expand|Select|Wrap|Line Numbers
  1. if(num == 4) 
  2. {
  3. cout << "4.Convert degrees c to degrees f.\n";
  4.  
  5. /* the below statements are not treated in if block that violates the rule that each else statement must occur just after a if statement..
  6. cout << "Enter value in degrees c:\n";
  7. cin >> c;
  8. temp = c * 33.8;
  9. cout << c << " degrees c is " << temp << " degrees f.";
  10.  
  11. else 
  12. {
  13. cout << "5.Convert pounds to kilograms.\n";
  14. }
Apr 15 '10 #3
donbock
2,426 Expert 2GB
How is this different than your help needed in C++ with the if-else-if ladder post?
Apr 16 '10 #4
code green
1,726 Expert 1GB
As a newbie you should be solving these trivial syntax errors independently.
This is how you gain experience and a feel for the flow of code.
Apr 16 '10 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

15
by: Xah Lee | last post by:
Here's the belated Java solution. import java.util.List; import java.util.ArrayList; import java.lang.Math; class math { public static List range(double n) { return range(1,n,1); }
2
by: d2r2 | last post by:
Hi, I'm trying to run a nested (UNION) query against a MSAccessXP database (version 2002; build 10.6501.6714; SP3) In Access the SQL-statement executes just fine. When I run it in a asp-page I...
3
by: Tcs | last post by:
My backend is DB2 on our AS/400. While I do HAVE DB2 PE for my PC, I haven't loaded it yet. I'm still using MS Access. And no, I don't believe this is an Access question. (But who knows? I...
18
by: Rhino | last post by:
I am having a problem with a nested SQL Procedure on DB2 V8.2.1 on Windows. This simple-looking proc is giving me this error message when I try to build it in the Development Center: ...
13
by: eman1000 | last post by:
I was recently looking at the prototype library (http://prototype.conio.net/) and I noticed the author used the following syntax: Object.extend(MyObj.prototype, { my_meth1: function(){},...
7
by: Steven Bethard | last post by:
I've updated PEP 359 with a bunch of the recent suggestions. The patch is available at: http://bugs.python.org/1472459 and I've pasted the full text below. I've tried to be more explicit about...
37
by: Tim N. van der Leeuw | last post by:
Hi, The following might be documented somewhere, but it hit me unexpectedly and I couldn't exactly find this in the manual either. Problem is, that I cannot use augmented assignment operators...
14
by: Jan Schmidt | last post by:
Hi, in a nested do-while-loop structure I would like to "continue" the outer loop. With goto this should be no problem in while-loops. However, for do-while I cannot get it to work (without a...
3
by: Dieter Maurer | last post by:
I met the following surprising behaviour .... for i in range(3): .... def gen1(): .... yield i .... yield i, gen1() .... .... 0 0 1 1
3
by: Cousson, Benoit | last post by:
I don't think so; my original email was mainly a question. I do agree that they are other ways to do what I'm trying to achieve; there are always several ways to solve an issue. Few days ago, I...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.