Thread: How would you go back to a section of code?

  1. #1
    Registered User
    Join Date
    Jul 2004
    Posts
    12

    How would you go back to a section of code?

    I'm just a newb so this is very hard for me.
    I'm trying to make a guess what number this is game and I want to go back to a section of code like I would in Qbasic. The problem is that C++ doesn't use labels like 10, 20, 30 ,40, etc..
    Any help would be appreciated.

    The code so far is:
    Code:
    #include <iostream>
    int x=0
    int main()
    {
    cout<< "You get three trys.\n"
    cin>>x
    x=x+1}
    if (x=1)
    {
    cout<< "First try"
    }
    if (x=2)
    {
    cout<< "Second try"
    }
    if (x=3)
    {
    cout<< "Last try"
    }

  2. #2
    Registered User
    Join Date
    Oct 2004
    Posts
    12
    do it through while() { loops

    and if you want the while loop to stop, just use the function break;

    Also, you're forgetting all the semi-colons at the end of certain functions/declarations.
    Last edited by s3abass; 10-10-2004 at 06:03 PM.

  3. #3
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,401
    Code:
    if (x=1)
    You probably mean if (x==1).
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  4. #4
    Registered User
    Join Date
    Jul 2004
    Posts
    12
    Quote Originally Posted by s3abass
    do it through while() { loops

    and if you want the while loop to stop, just use the function break;
    Could you give me an example?
    I'm just a newb

  5. #5
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    Code:
    while ( condition )
    {
        ...
    }
    As long as condition evaluates to being true, the code will repeat. Sometimes you need to increment values to cont through an array for example, but in that case you would probably use a for loop. There are also do while loops that are very similar.

  6. #6
    hacker in training AdamLAN's Avatar
    Join Date
    Sep 2004
    Posts
    56
    At one point you say:
    x=x+1}

    I'm not certain what the "}" is for in this statement, but you may want to replace it with a semicolon. I think your compiler thinks that that may refer to the end of int main().

  7. #7
    Registered User
    Join Date
    Jul 2004
    Posts
    12
    Quote Originally Posted by sean_mackrory
    Code:
    while ( condition )
    {
        ...
    }
    As long as condition evaluates to being true, the code will repeat. Sometimes you need to increment values to cont through an array for example, but in that case you would probably use a for loop. There are also do while loops that are very similar.
    Then I have to make 3 while commands?

  8. #8
    i dont know Vicious's Avatar
    Join Date
    May 2002
    Posts
    1,200
    Code:
    #include <iostream>
    using namespace std;
    
    // Function that will be called every loop
    void Function ()
    {
    	
    	cout << "Some program stuff";
    	cout << endl;
    	
    }
    
    int main ()
    {
    	
    	// Variable for our choice
    	char choice;
    	
    	// Infinite loop. This will not end until we break out of it.
    	// Because 1 is always 1.. it will never end
    	while ( 1 ) {
    		
    		// Call the function
    		Function ();
    		
    		cout << "Type Q to exit";
    		cin  >> choice;
    		
    		// If user typed 'q' then break;
    		if ( choice == 'q' ) break;
    		
    	}
    	
    	return 0;
    	
    }
    What is C++?

  9. #9
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    Well I've been looking at your code and it actually does nothing close to what you want it to do. You can do labels in C++, and sometimes it is necessary, but it is generally a bad idea so I won't even show you how to do it until you learn the alternatives.

    like I would in Qbasic
    I warned someone about this the other day - FORGET QBASIC. Not that you've done anything wrong, but get out of that mindset right now. It's going to lead to really sloppy programming style. QBasic is good for teaching the basic concepts, but when you learn a new language, you've got to keep your mind really open.

    I'll tell you what I'll do - write out a QBasic version of what you want done, test that it works the way you want it, email it to me, and I'll send you the C++ version, with an explanation of the concepts. I don't do this for everyone, but I'm in a good mood, and you don't have the attitude problems that a lot of newbies browse this board with.

  10. #10
    Registered User
    Join Date
    Jul 2004
    Posts
    12
    My Qbasic skills are horrible too so I can't send anything. The description would be a program that would give you three chances to guess a certain number, and after three guess's it would say "too bad" and end.

  11. #11
    Registered User
    Join Date
    Oct 2004
    Posts
    12
    Yes, i us like 100's of while(1) loops in my "games"

  12. #12
    Registered User
    Join Date
    Jun 2003
    Posts
    361
    This is by no means working code...but I added the main parts, and left the rest to you. Af far as I can tell, this should accomplish what you're looking for (with minimal runtime errors)
    Code:
    int main()
    {
    	bool Win = false; //Keeps track of win
    
    	for(int x = 0; x < MaxNumberOfGuesses; x++;)
    	{
    		//Print the instructions here and get the user to input a guess (Note: x+1 is the guess# they're on)
    		//If the Guess == Correct Answer, set Win = true and break out of the loop
    			//Otherwise, let the for loop do it's job and increment x by one.
    	}
    	//If you get out here and Win = true, you know the user must have guessed right
    		//Display a message accordingly
    	//If Win = false, then they did not guess it in time, and let them know they lost...huge
    
    	//Throw in a pause function (fflush()/getch() or getchar() are recommended to any system() calls)
    		//So that it doesn't quit before they know their result
    
    	//Exit
    	return 0;
    }
    Last edited by Epo; 10-10-2004 at 10:09 PM.

  13. #13
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398

    Lightbulb BASIC vs C++

    I learned BASIC first too...

    Like Sean said, BASIC is great for learning the concepts that apply to all programming languages... things like:

    1 - A program is a sequnce of instructions.
    2 - Different types of variables (integers, floating, strings).
    3 - An assignment statement is NOT an equation (x = x+1).
    4 - A program can have loops.
    5 - A program can have conditional branching (if-statements and switch-case statements ).


    goto / labels are rarely used in C++. These are generally considered bad practice in structured programming and object oriented programming.

    Functions:
    Everything in C++ is done with functions. You may have used functions in BASIC. But if not, functions are alot like subroutines. The program-flow goes-off and executes the function, and then returns to where it came from, optionally bringing-back a return value with it.

    Now, there are a couple of issues that make C++ functions more difficult than BASIC subroutines: A function can only return one value, and global variables are also considered bad practice. This means that you have to use some "tricks", like pointers and references.

    Finally, C++ is a much more complex language than BASIC. "Hello World" is ONE LINE in BASIC I have a BASIC book with the entire language reference in the back half of the book. I have not found a book with ALL of the C/C++ library functions! And of course, C++ adds Object Oriented Programming.

  14. #14
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    "Hello World" is ONE LINE in BASIC
    What've you been smoking? If I remember correctly...

    Code:
    REM Hello World.bas
    REM Hello World
    REM
    REM Prints "Hello World" to the screen 
    REM (C) 2004 Sean Mackrory
    
    CLS
    LOCATE 1,1:
    COLOR 7,0
    STRING$ = "HELLO WORLD"
    PRINT STRING$
    END
    I was going to draw Hello World with the geometry functions, but that would just be overkill, now wouldn't it?

    Or alternately...

    Code:
    PRINT "HELLO WORLD"

  15. #15
    Registered User
    Join Date
    Aug 2004
    Location
    San Diego, CA
    Posts
    313
    Sean? Does the word "overkill" come to mind?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with section of code.
    By unejam2005 in forum C++ Programming
    Replies: 3
    Last Post: 12-11-2005, 06:38 PM
  2. Writing Code
    By ILoveVectors in forum C++ Programming
    Replies: 4
    Last Post: 06-13-2005, 12:27 AM
  3. how to loop back for this code ?
    By Jasonymk in forum Windows Programming
    Replies: 3
    Last Post: 02-28-2003, 12:40 PM
  4. Seems like correct code, but results are not right...
    By OmniMirror in forum C Programming
    Replies: 4
    Last Post: 02-13-2003, 01:33 PM
  5. Replies: 4
    Last Post: 01-16-2002, 12:04 AM