Thread: C++ If statment question

  1. #1
    Registered User
    Join Date
    May 2008
    Posts
    18

    C++ If statment question

    hi guys, i am currently learning c++ and am curious. Are you able to use an if statement with in an if statement as long as you have { ....} separating it from int main??? Sorry if that is not written correctly, im still new. Thanks

    Code:
    if ( guess < 74 ) { 
                    cout << "Sorry, you guessed wrong!\n";
    		cout << "Would you like a hint?\n" << "\n" << "\n" << "<y/n>";
    		cin >> clue;
    			if ( clue == y )
    
    
            }
    Code is incomplete, but it should give u an idea.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Roughly, yes, though you can do:
    Code:
    if (x)
        if (y)
            if (z)
                foo();
    but that can be prone to incorrect maintenance when someone inserts a statement and forgets to add the braces.
    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

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Hehe, although in laser's example, you could just do:
    Code:
    if (x && y && z)
        foo();
    It's less error prone.
    But you can have as many (?) nested if statements as you wish. Everything at the first line
    following the if (see example above) or within the braces (
    Code:
    { and }
    ) are considered part of the if statement.
    You can, of course, also have several if statements after each other in a function.
    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.

  4. #4
    Registered User
    Join Date
    May 2008
    Posts
    18
    im sorry to bother, but could you further clarify what you mean? For instance, what is foo();

    Code:
    #include <iostream>	
    
    using namespace std;
    		
    int main() {
     
    	
    	int guess;                     
            int clue;
    	
    	
    	 cout << "Hello Internet\n";
    	 cout << "Today, we are going to play the guessing game!\n";
    	 cout << "\n";
    	 cout << "You are going to pick a number from 1-100 and if you need help\n" << "Id be happy to give you clues!\n" << "\n";
    	 
    	 cout << "Pick a number: "; 
    	 cin >> guess;
    	 cout << "\n";
    	 cin.ignore();                       
      
    		if ( guess < 74 ) { 
                    cout << "Sorry, you guessed wrong!\n";
    		cout << "Would you like a hint?\n" << "\n" << "\n" << "<y/n>";
    		cin >> clue;
    			if ( clue == y )
                             
                      // Add in couts etc...
    
            }
    
            else if ( guess > 74 ) {            
            cout << "high\n";
    	    }
    
            else {
            cout << "You won the game!\n";    
            }
    
      cin.get();
     
      return 0;
    }
    here is my full code that i have right now to better clarify. im new to c++ and all coding, as in i started doing research yesterday and am loving it. I cant really explain the feeling when writing something and having it work lol. thanks
    Last edited by TehClutchKiller; 05-28-2008 at 01:53 AM.

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    This is valid code, but I think you had better take a better look at indenting:
    http://cpwiki.sourceforge.net/Indentation
    http://cpwiki.sourceforge.net/User:Elysia/Indentation
    And also, characters must be surrounded with '' (ie, 'y').
    If you simply type y, the compiler will think it's a variable, and there's no such thing, is there?
    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.

  6. #6
    Registered User
    Join Date
    May 2008
    Posts
    18
    lol, when i copy pasted, it realigns things lol. sorry

  7. #7
    Registered User
    Join Date
    May 2008
    Posts
    18
    Code:
     if ( clue == 'y' )
    do u mean like that? I apologize again
    Last edited by TehClutchKiller; 05-28-2008 at 01:58 AM.

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by TehClutchKiller View Post
    Code:
     if ( clue == 'y' )
    do u mean like that? I apologize again
    Yes, that's right.
    But I did notice you were mixing spaces and tabs. This usually creates messed up indentation when copying and pasting.
    I do suggest you avoid it. Pick one.
    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.

  9. #9
    Registered User
    Join Date
    May 2008
    Posts
    18
    Is this code incorrect? When i run it, if i choose y, it just says press enter to continue with out displaying the cout. Whats wrong?

    Code:
             if ( guess < 74 ) { 
             cout << "Sorry, you guessed wrong!\n";
             cout << "Would you like a hint?\n" << "\n" << "\n" << "<y/n>";
    	 cin >> clue;
    		if ( clue =='y' ) {
    		cout << "You guessed low, sorry! Try again!\n";
    		}
    
             }

  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Code:
            if ( guess < 74 ) { 
    	        cout << "Sorry, you guessed wrong!\n";
            	cout << "Would you like a hint?\n" << "\n" << "\n" << "<y/n>";
    		char clue;
    		cin >> clue;
    		if ( clue =='y' ) {
    			cout << "You guessed low, sorry! Try again!\n";
    		}
             }
    Try adding something such as that.
    It doesn't like to read a char into an int, apparently.
    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.

  11. #11
    Registered User
    Join Date
    May 2008
    Posts
    18
    That worked, thank you very much. So i have to ask what exactly it did if you don't mind.

  12. #12
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    It read the data into the local variable clue (which is char, not int).
    The char clue variable hides the int clue variable in the outer scope.
    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.

  13. #13
    Registered User
    Join Date
    May 2008
    Posts
    18
    ok, i get it. thanks

  14. #14
    Registered User
    Join Date
    May 2008
    Location
    Paris
    Posts
    248
    you had better take a better look at indenting:
    Use emacs. Pressing 'Tab' indents your code (if not already indented automatically), if there is a syntax error you'll see it automatically in the indentation.
    There is a command 'Untabify' to convert tabs in spaces, there is a lot of flexibility in indentation etc etc etc.
    Much, so much better than the Visual Studio editor or Notepad++ ....

    EDIT: a link : http://www.gnu.org/software/emacs/

    You can customize emacs entirely, the standard installation already supports many languages (more than visual studio's editor): html, xml, C/C++, fortran, python, java, LaTeX, bibtex
    Last edited by MarkZWEERS; 05-28-2008 at 07:11 AM.

  15. #15
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by MarkZWEERS View Post
    Much, so much better than the Visual Studio editor...
    I doubt it. That's your individual opinion.
    VS allows for auto indentation, can indent code you copy into it, as well.
    No need for indentation "error" notification, if you just let it indent by itself.

    Again, this is individual preference.
    Last edited by Elysia; 05-28-2008 at 07:17 AM.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Debugging question
    By o_0 in forum C Programming
    Replies: 9
    Last Post: 10-10-2004, 05:51 PM
  2. Question about pointers #2
    By maxhavoc in forum C++ Programming
    Replies: 28
    Last Post: 06-21-2004, 12:52 PM
  3. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  4. Question about linked lists.
    By cheeisme123 in forum C++ Programming
    Replies: 6
    Last Post: 02-25-2003, 01:36 PM
  5. Question, question!
    By oskilian in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 12-24-2001, 01:47 AM