Probably a basic question, but...

This is a discussion on Probably a basic question, but... within the C Programming forums, part of the General Programming Boards category; I don't understand how I could make a program repeat itself. The entire program, or most of for at least. ...

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    14

    Probably a basic question, but...

    I don't understand how I could make a program repeat itself.

    The entire program, or most of for at least.

    This is the program:

    Code:
     #include <stdio.h>
    
    int main()
    {
        int x;
        
        printf("Please guess the Number.\n The number is under 120.\n ");
        scanf( "%d", &x );
        getchar(); 
              
        if ( "%d", x != 90 ) {
             printf("You entered %d . This number is wrong. Press enter to continue.", x );
        getchar(); }
        
        else {
             printf("You found out the number! Press enter to continue. ", x );
             getchar(); 
             
             }
        return 0;
    }
    I just need a way to make the program ask you to guess the number until you eventually guess it.

    BTW this is just a random proof project, something to prove to myself I know what I'm doing, which apparently I'm not.

    I understand I might be able to implement this with while, for, or do..while, but I don't understand who I could implement that.

    Any help/solutions would be appreciated!

  2. #2
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    A while loop would be an option:
    Code:
    while(x != 90) {
       /* Do something */
    }

  3. #3
    Registered User
    Join Date
    Sep 2005
    Posts
    7
    you can use...............
    Code:
    while(1){
    //do something
    if(some condition) break;
    //do something
    }

  4. #4
    Registered User
    Join Date
    Sep 2005
    Posts
    2
    take a flag. initialise it to zero. if the user guesses the ans flag=1.
    use do while and continue till flag =1. could use goto instead of flag but thts bad prograamin pract rite?

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    It's much better to use a while loop than goto[s].

    I understand I might be able to implement this with while, for, or do..while, but I don't understand [how] I could implement that.
    Try a program like this:
    Code:
    #include <stdio.h>
    
    int main(void) {
        int input;
    
        do {
            printf("\nEnter a number to square (0 to quit): ");
            scanf("&i", &input);
            printf("%i squared is %i\n", input, input * input);
        } while(input != 0);
    
        return 0;
    }
    You might also want to look into the break statement.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  6. #6
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    Try a program like this:
    That program will just throw the user in an endless loop. There's an error in the scanf() statement.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A basic math programming question
    By hebali in forum C Programming
    Replies: 38
    Last Post: 02-25-2008, 03:18 PM
  2. Basic question about GSL ODE func RK4
    By cosmich in forum Game Programming
    Replies: 1
    Last Post: 05-07-2007, 02:27 AM
  3. Basic question about RK4
    By cosmich in forum C++ Programming
    Replies: 0
    Last Post: 05-07-2007, 02:24 AM
  4. A very basic question
    By AshFooYoung in forum C Programming
    Replies: 8
    Last Post: 10-07-2001, 03:37 PM

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