Break in If statement

This is a discussion on Break in If statement within the C++ Programming forums, part of the General Programming Boards category; Good Morning: I`ve read my books and several sites about "if" statements but I can`t get a feel for why ...

  1. #1
    Registered User
    Join Date
    Aug 2006
    Posts
    23

    Break in If statement

    Good Morning:
    I`ve read my books and several sites about "if" statements but I can`t get a feel for why "break" can`t be placed where I have placed it. DevC++ tells me it was expecting a primary expression before "break" but I`ve been trying different ways to give it what it wants and it stills pukes on me. Can you guys explain to me what I`m not doing correctly by looking at my example?

    Code:
    #include <iostream>
    using namespace std;
    
    int main() {
    double c;
    double f;
    double t;
    
    cout << "In order to convert celsius to farenheit \n";
    cout << "or farenheit to celsius, enter either -c- or -f-";
     cin >> t;
    
     if (t != c || f)
     cout << "I`m outta here!";
     (break;)
    
     else if (t == f)
     {cout << "Enter celsius temperature to convert to farenheit: ";
     cin >> c;
     cout << "The temperature in farenheit is: " << (t * (1.8)) + 32;}
    
     else (t == c)
     {cout << "Enter farenheit temperature to convert to celsius: ";
     cin >> f;
     cout << "The temperature in celsius is: " << (t - 32) * .555;) }
    
    system("PAUSE");
    return 0;
    }
    ??????????????????
    Any advice is well appreciated...............

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,159
    The break statement is for loops. You're also missing braces. Without braces only the statement following the if or else is considered part of the condition.

    Also, what is if (t != c || f) checking for?
    Last edited by itsme86; 09-07-2006 at 10:10 AM.
    If you understand what you're doing, you're not learning anything.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,968
    You need to indent your code properly to understand it.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    Aug 2006
    Posts
    23
    I wanted (t != f || c) to make sure the user would have to start over or get an error message if he did not enter "f" or "c". And thanks for the comment on indenting. I`m still fuzzy on this but will start doing it where I think it needs to be done.

    Now....since break only works in loops can I put a return command at the if statement or demand that the user enter the proper letter?...........
    Thanks to all for your replies.

  5. #5
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,262
    Change (t != f || c) to ( (t!=f) || (t!=c) ). You may not necessarily need so many brackets. || and && etc operators don't work like you think they do.

    Why do you want/need a break or a return?

  6. #6
    Registered User
    Join Date
    Aug 2006
    Posts
    23
    Actually, now that you mention it, I should ask the user to correctly enter his request right? And not get out of the program. Thank You! And yes, I didn`t realize the switches had to be in their own parentheses. Thanks all!

  7. #7
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,262
    >> ( t * 1.8 ) + 32
    Don't you mean -> ( c * 1.8 ) + 32

    >> (t - 32) * .555
    and -> (f - 32) * .555


    This would have been shown a lot sooner if you had formatted your code!! And for the above, they don't need ()'s, I think it's easier to see that way, but they need their own == or != -> ( t!=f || t!=c )

  8. #8
    Ethernal Noob
    Join Date
    Nov 2001
    Posts
    1,901
    You need to put your if statement code in blocks, because after loops and condition statements, it will only execute at most one statement without braces, otherwise put your code in a block.

    and do what twomers said.

  9. #9
    Registered Luser risby's Avatar
    Join Date
    Jun 2006
    Posts
    72
    Quote Originally Posted by plain
    Code:
    double c;
    double f;
    double t;
    
    cout << "In order to convert celsius to farenheit \n";
    cout << "or farenheit to celsius, enter either -c- or -f-";
     cin >> t;
    
     if (t != c || f)
    ??????????????????
    Any advice is well appreciated...............
    You have asked the user to input the character 'c' or the character 'f'.

    You are then storing their character in a double precision floating point number variable.

    This is wrong.

    You need a character variable in which to store their choice of fahrenheit or celsius. You must then compare this character variable first against one character constant and then against the other character constant.

    Code:
    double c;
    double f;
    char t;
    
    cout << "In order to convert celsius to farenheit \n";
    cout << "or farenheit to celsius, enter either -c- or -f-";
     cin >> t;
    
    if (t != 'c' && t != 'f') {
      /* code for bad choice */
    }
    Next, you ask for an actual temperature and store it in the variable c
    Code:
    {cout << "Enter celsius temperature to convert to farenheit: ";
     cin >> c;
     cout << "The temperature in farenheit is: " << (t * (1.8)) + 32;}
    but you use the variable t in your calculation where t is the character they input to choose either fahrenheit or celsius.

    Hope this helps.

  10. #10
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    >> Now....since break only works in loops can I put a return command at the if statement or demand that the user enter the proper letter?
    You should use a loop (e.g. while) which will loop until the user enters a valid character.

    >> Change (t != f || c) to ( (t!=f) || (t!=c) ).
    Already fixed in risby's code, but this is wrong. ( (t!=f) || (t!=c) ) will always be true, it should be &&.

  11. #11
    Registered User
    Join Date
    Aug 2006
    Posts
    23
    Thanks everyone! LOL I can see I have more studying to do. You`ve all been great!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Number to Word (Billions)
    By myphilosofi in forum C Programming
    Replies: 34
    Last Post: 02-04-2009, 01:09 AM
  2. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  3. Break statement issues, how to return to start of loop?
    By brdiger31 in forum C++ Programming
    Replies: 3
    Last Post: 06-01-2007, 03:29 PM
  4. ascii rpg help
    By aaron11193 in forum C Programming
    Replies: 18
    Last Post: 10-29-2006, 01:45 AM
  5. syntax question
    By cyph1e in forum C Programming
    Replies: 19
    Last Post: 03-30-2006, 11:59 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21