Thread: C++ If Statements Help

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    5

    C++ If Statements Help

    Hello,

    My name is MP and I am new to C++. I am trying to create a project and am having no luck.

    I am trying to finish a assignment, but I am baffled. The assignment is to Write a program that reads in three integers and determines and prints the middle number numerically in the group. The values are NOT necessarily entered in numeric order. Also compound if statements and nested if statements are not needed or allowed).

    Here is the code I wrote, but keep getting the 3 error C2059: syntax error : 'if'.

    Code:
     
    // Three integers printing middle interger numberically: 1/14/2008, 2:05PM
    
    //
    
    #include <iostream> // allows program to output data to the screen
    
    #include "Rhod_2a.h"
    
    using std::cout; // program uses cout
    
    using std::cin; // program uses cin
    
    using std::endl; // program uses endl
    
    // function main begins program execution
    
    int main()
    
    {
    
    int number1; // first integer to compare
    
    int number2; // second integer to compare
    
    int number3; // third integer to compare
    
    cout << "Enter three integers: "; //prompt user for data
    
    cin >> number1 >> number2 >> number3; // read three integers from user
    
    
    if ( number1 <= number2 )
    
    cout << number1 <<
    
    if (number2 < number1 )
    
    cout << number2 <<
    
    if ( number3 < number1 )
    
    cout << number3 << endl;
    
    if ( number1 > number2 )
    
    cout << number1 <<
    
    if ( number2 > number1 )
    
    cout << number 2 <<
    
    if ( number2 < number3 )
    
    cout << number2 <<
    
    if ( number3 <= number2 )
    
    cout << number3 << endl;
    
    if ( number1 > number2 )
    
    cout << number1 <<
    
    if ( number1 > number3 )
    
    cout << number1 <<
    
    if ( number2 > number1 )
    
    cout << number2 <<
    
    if ( number2 > number3 )
    
    cout << number2 <<
    
    if ( number3 > number1 )
    
    cout << number3 << 
    
    if ( number3 > number2 )// number3 is greater than number2
    
    cout << number3 << endl;
    
    return 0; // indicate that program ended successfully
    
    } //end function main
    
     
    
    Errors:
    
    ------ Build started: Project: Rhod_2a, Configuration: Debug Win32 ------
    
    Compiling...
    
    Rhod_2a.cpp
    
    c:\documents and settings\.your-jz14f3vb7a\my documents\visual studio 2005\projects\rhod_2a\rhod_2a\rhod_2a.h(33) : error C2059: syntax error : 'if'
    
    c:\documents and settings\.your-jz14f3vb7a\my documents\visual studio 2005\projects\rhod_2a\rhod_2a\rhod_2a.h(42) : error C2059: syntax error : 'if'
    
    c:\documents and settings\.your-jz14f3vb7a\my documents\visual studio 2005\projects\rhod_2a\rhod_2a\rhod_2a.h(54) : error C2059: syntax error : 'if'
    
    Build log was saved at "file://c:\Documents and Settings\.YOUR-JZ14F3VB7A\My Documents\Visual Studio 2005\Projects\Rhod_2a\Rhod_2a\Debug\BuildLog.htm"
    
    Rhod_2a - 3 error(s), 0 warning(s)
    
    ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Yes, the compiler doesn't see the end of your first if-statement before it finds the next if-statement. Each if-statement should end with a semicolon.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Jan 2008
    Posts
    5
    PS Also am I correct in this coding? I would really like direction?

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Good to see that you posted your code in code tags, but kindly indent your code properly.

    The problem is just that you have repeated the line:
    Code:
    cout << number1 <<
    The above is clearly a syntax error, and the compiler interprets it as:
    Code:
    cout << number1 << if
    and thus informs you that the if keyword is out of place here.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Your code also doesn't quite do what you are asked to do - first of all, you have far to many "cout << ..." statements.

    You need to use some sort of if-statement that splits the high and low parts away from the "middle". I would probably write it using a variable called "middle". I'm not going to show you how to do that just yet, because I think you need to learn from experience, not from reading what others have done.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Code:
     
    // Three integers printing middle interger numberically: 1/14/2008, 2:05PM
    
    //
    
    #include <iostream> // allows program to output data to the screen
    #include "Rhod_2a.h"
    using std::cout; // program uses cout
    using std::cin; // program uses cin
    using std::endl; // program uses endl
    
    // function main begins program execution
    int main()
    {
    	int number1; // first integer to compare
    	int number2; // second integer to compare
    	int number3; // third integer to compare
    
    	cout << "Enter three integers: "; //prompt user for data
    	cin >> number1 >> number2 >> number3; // read three integers from user
    
    	if ( number1 <= number2 )
    	cout << number1 << if (number2 < number1 )
    		cout << number2 << if ( number3 < number1 )
    			cout << number3 << endl;
    	if ( number1 > number2 )
    		cout << number1 << if ( number2 > number1 )
    			cout << number 2 << if ( number2 < number3 )
    				cout << number2 << if ( number3 <= number2 )
    					cout << number3 << endl;
    	if ( number1 > number2 )
    		cout << number1 << if ( number1 > number3 )
    			cout << number1 << if ( number2 > number1 )
    				cout << number2 << if ( number2 > number3 )
    					cout << number2 << if ( number3 > number1 )
    						cout << number3 <<  if ( number3 > number2 )
    							cout << number3 << endl;
    	return 0; // indicate that program ended successfully
    } //end function main
    This is what it would look like if you indented properly.
    And Visual Studio indents for you, so why not just accept the default indentation it uses instead of making a mess of everything?
    As I can see, there's plenty of errors. It's illegal to output an if! Not to mention the program doesn't make sense.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    Approach it like this.

    First, name your variables something like:

    int low, middle, high ;

    Then, have a temporary int that lets you swap values:

    int temp ;

    Then, get your numbers like you are. That's good.

    Then, do the compares. For instance, if low is higher than middle, swap the two. Then, if low is higher than high, swap them as well. Now you are done with low, move on to middle. When you are done with middle, variable high has already taken care of itself.

    Todd

  8. #8
    Registered User
    Join Date
    Jan 2008
    Posts
    5

    Thanks Todd & Matsp

    Thanks for your kindness and patience with this novice! I retried the coding, still with the incorrect results. I get 3 warnings and the middle value does not present itself. Could you please review and steer me in the direction I need to be?

    Code:
    #include <iostream> // allows
    
    using std::cout;
    using std::cin;
    using std::endl;
    
    int main ()
    {
    	int number1, number2, number3, middle;
    	cout << "Enter three numbers.\n";
    	cout << "First: ";
    	cin >> number1;
    	cout << "\nSecond: ";
    	cin >> number2;
    	cout << "\nThird: ";
    	cin >> number3;
    	cout << "\n";
    
    	if (number2 < number1 < number3)
    		middle = number1; if (number1 < number2 < number3) 
    			middle = number2; if (number1 < number3 < number2) 
    				middle = number3;
    
    	cout << "Middle: " << middle;
    	cout << "\n";
    
    		return 0;
    }
    
    ----- Build started: Project: Rhod_2, Configuration: Debug Win32 ------
    Compiling...
    Rhod_2.cpp
    c:\documents and settings\mary pat rhodes.your-jz14f3vb7a\my documents\visual studio 2008\projects\rhod_2\rhod_2\rhod_2.h(29) : warning C4804: '<' : unsafe use of type 'bool' in operation
    c:\documents and settings\mary pat rhodes.your-jz14f3vb7a\my documents\visual studio 2008\projects\rhod_2\rhod_2\rhod_2.h(30) : warning C4804: '<' : unsafe use of type 'bool' in operation
    c:\documents and settings\mary pat rhodes.your-jz14f3vb7a\my documents\visual studio 2008\projects\rhod_2\rhod_2\rhod_2.h(31) : warning C4804: '<' : unsafe use of type 'bool' in operation
    Linking...
    Embedding manifest...
    Build log was saved at "file://c:\Documents and Settings\Mary Pat Rhodes.YOUR-JZ14F3VB7A\My Documents\Visual Studio 2008\Projects\Rhod_2\Rhod_2\Debug\BuildLog.htm"
    Rhod_2 - 0 error(s), 3 warning(s)
    ========== Build: 1 succeeded, 0 failed, 0 up-to-date, 0 skipped ==========
    Cheers,
    Mary Pat

  9. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Code:
    #include <iostream> // allows
    
    using std::cout;
    using std::cin;
    using std::endl;
    
    int main ()
    {
    	int number1, number2, number3, middle;
    	cout << "Enter three numbers.\n";
    	cout << "First: ";
    	cin >> number1;
    	cout << "\nSecond: ";
    	cin >> number2;
    	cout << "\nThird: ";
    	cin >> number3;
    	cout << "\n";
    
    	if (number2 < number1 < number3)
    		middle = number1;
    	if (number1 < number2 < number3) 
    		middle = number2;
    	if (number1 < number3 < number2) 
    		middle = number3;
    
    	cout << "Middle: " << middle;
    	cout << "\n";
    
    	return 0;
    }
    Come on. Visual Studio indents for you. Let it indent for you and don't place more than one statement on the same line.
    This is not a coding problem; it's a logic problem. Think! How would you do this in real life, given 3 numbers and print them in order from smallest to biggest?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  10. #10
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    Hey Mary Pat.
    Code:
    	if (number2 < number1 < number3)
    		middle = number1; if (number1 < number2 < number3) 
    			middle = number2; if (number1 < number3 < number2) 
    				middle = number3;
    You can't code like this - it's not valid syntax. How that reads is:

    if number2 is less than number1 is less than number 3 ...

    Stick with one condition inside the parens for now until you gain more experience.

    Try something like this:
    Code:
    int temp ; 
    if (low > middle) { 
      temp = low ;
      low = middle ; 
      middle = temp
    }
    And then do the same thing with low and high (or number"x" and number"y"). Then, do middle and high.

    Todd
    Last edited by Dino; 01-17-2008 at 03:07 PM. Reason: typo

  11. #11
    Registered User
    Join Date
    Jan 2008
    Posts
    5

    Making Progress...maybe

    Here is the code so far

    Code:
     // List Middle Value Integer Out of Three ; Mary Pat Rhodes; Jan. 17, 2008; 1:54 PM
    //
    #include <iostream> // allows
    
    using std::cout;
    using std::cin;
    using std::endl;
    
    int main ()
    {
    	int number1, number2, number3, middle;
    	cout << "Enter three numbers.\n";
    	cout << "First: ";
    	cin >> number1;
    	cout << "\nSecond: ";
    	cin >> number2;
    	cout << "\nThird: ";
    	cin >> number3;
    	cout << "\n";
    
    	if (number1 > number2) 
    		if (number1 < number3)
    		middle = number1;
    
    	if (number1 < number2)
    		if(number2 < number3) 
    		middle = number2; 
    
    	if (number1 < number3)
    		if(number3 < number2) 
    		middle = number3;
    
    	cout << "Middle: " << middle;
    	cout << "\n";
    
    		return 0;
    }
    But i getting a debugging error that has me really baffled!

    Code:
    'Rhod_2a.exe': Loaded 'C:\Documents and Settings\Mary Pat Rhodes.YOUR-JZ14F3VB7A\My Documents\Visual Studio 2005\Projects\Rhod_2a\debug\Rhod_2a.exe', Symbols loaded.
    'Rhod_2a.exe': Loaded 'C:\WINDOWS\system32\ntdll.dll', No symbols loaded.
    'Rhod_2a.exe': Loaded 'C:\WINDOWS\system32\kernel32.dll', No symbols loaded.
    'Rhod_2a.exe': Loaded 'C:\WINDOWS\WinSxS\x86_Microsoft.VC80.DebugCRT_1fc8b3b9a1e18e3b_8.0.50727.42_x-ww_f75eb16c\msvcp80d.dll', No symbols loaded.
    'Rhod_2a.exe': Loaded 'C:\WINDOWS\WinSxS\x86_Microsoft.VC80.DebugCRT_1fc8b3b9a1e18e3b_8.0.50727.42_x-ww_f75eb16c\msvcr80d.dll', No symbols loaded.
    'Rhod_2a.exe': Loaded 'C:\WINDOWS\system32\msvcrt.dll', No symbols loaded.
    Run-Time Check Failure #3 - The variable 'middle' is being used without being defined.
    The program '[3584] Rhod_2a.exe: Native' has exited with code 0 (0x0).
    Where, what, how do I clear this error??

    Thanks again for all your patience and kindness.

    Cheers,
    MP

  12. #12
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Code:
    if (number1 > number2) 
    	if (number1 < number3)
    		middle = number1;
    Look out. I detect something that is called a nested if in your code and your requirements say that they are not allowed.

    I think Todd has given you some important hints.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  13. #13
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    I think your runtime check has detected that the variable middle was never set, or could not ever get set, and thusly, you'll be using an uninitialized value in the COUT.

    Consider the case when the user enters 3, 3 and 3 - none of your IF conditions will be met.

    Todd

  14. #14
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Are you listening?
    Quote Originally Posted by Elysia View Post
    Come on. Visual Studio indents for you. Let it indent for you and don't place more than one statement on the same line.
    Is there a reason you're changing the default indentation Visual Studio uses for you?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  15. #15
    Registered User
    Join Date
    Jan 2008
    Posts
    5
    Todd,

    I am sorry to have bothered you. I am just not getting what you are saying. Should I be putting "=" signs in too? Maybe "<="?

    I am trying to get the info you are providing, but I am letting the rude comments of Elysia interfer.

    Thanks again,
    Mary Pat

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Unknown memory leak with linked lists...
    By RaDeuX in forum C Programming
    Replies: 6
    Last Post: 12-07-2008, 04:09 AM
  2. newbie question - if statements without conditions
    By c_h in forum C++ Programming
    Replies: 2
    Last Post: 07-18-2008, 10:42 AM
  3. Efficiency of case statements
    By Yasir_Malik in forum C Programming
    Replies: 26
    Last Post: 05-23-2006, 11:36 AM
  4. Statements and Functions
    By GeekFreak in forum C++ Programming
    Replies: 5
    Last Post: 08-15-2002, 12:34 PM
  5. Switch statements for strings
    By cxs00u in forum C++ Programming
    Replies: 5
    Last Post: 04-17-2002, 03:38 PM