C++ If statment question

This is a discussion on C++ If statment question within the C++ Programming forums, part of the General Programming Boards category; hi guys, i am currently learning c++ and am curious. Are you able to use an if statement with in ...

  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
    20,968
    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.
    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

  3. #3
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,170
    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.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  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をこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,170
    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.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  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をこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,170
    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.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  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をこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,170
    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.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  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をこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,170
    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.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  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をこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,170
    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.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

Page 1 of 2 12 LastLast
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, 12:36 PM
  5. Question, question!
    By oskilian in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 12-24-2001, 12:47 AM

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