How to repeat a line

This is a discussion on How to repeat a line within the Game Programming forums, part of the General Programming Boards category; Now I know that there is a tutorial section about this, but I don't quite get it. So what I ...

  1. #1
    Registered User
    Join Date
    Jan 2009
    Posts
    10

    Question How to repeat a line

    Now I know that there is a tutorial section about this, but I don't quite get it. So what I want to do is this:

    Code:
    {
    int failorwin;
    
    cout<<"Press 1 to fail, press 2 to win.\n";
    cin>> failorwin;
    cin.ignore;
    
    if ( failorwin == 1 //here I want it to display a gameover message and return to the first cout
    )
    if (failorwin == 2 ) {
    cout<<"you won!\n";
    system("pause");
    return(0);
    }

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,755
    Use a while() loop.

    Or a do loop.

    Eg
    do while ( failorwin == 1 )
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Jan 2008
    Posts
    70
    Code:
    {
    	int failorwin;
    	do{
    		cout<<"Press 1 to fail, press 2 to win.\n";
    		cin>> failorwin;
    		cin.ignore;
    		if( failorwin == 1 ){
    			cout<<"You fail.";
    		}
    	}while( failorwin !=2 )
    
    	cout<<"you won!\n";
    	system("pause");
    	return(0);
    }
    edit: This would also be a nice time to use an enumerator.

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,904
    Quote Originally Posted by Drac View Post
    Code:
    {
    	int failorwin;
    	do{
    		cout<<"Press 1 to fail, press 2 to win.\n";
    		cin>> failorwin;
    		cin.ignore();
    		if( failorwin == 1 ){
    			cout<<"You fail.";
    		}
    	}while( failorwin !=2 )
    
    	cout<<"you won!\n";
    	system("pause");
    	return(0);
    }
    edit: This would also be a nice time to use an enumerator.
    Missing () in red.
    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.

  5. #5
    Registered User
    Join Date
    Jan 2009
    Posts
    10
    When I copy the code it doesn't work?

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,904
    Define doesn't work. We're not mind readers.
    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
    Registered User
    Join Date
    Jan 2008
    Posts
    70
    Well if you just use my lines they won't work because there is no main function declaration.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to do encryption in C
    By sankarv in forum C Programming
    Replies: 33
    Last Post: 12-28-2010, 11:01 AM
  2. Pointer and Polymorphism help.
    By Skyy in forum C++ Programming
    Replies: 29
    Last Post: 12-18-2008, 09:17 PM
  3. Printing Length of Input and the Limited Input
    By dnguyen1022 in forum C Programming
    Replies: 33
    Last Post: 11-29-2008, 04:13 PM
  4. Finding carriage returns (\c) in a line
    By JizJizJiz in forum C++ Programming
    Replies: 37
    Last Post: 07-19-2006, 06:44 PM

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