Thread: Newbie problem in restarting program

  1. #1
    Registered User
    Join Date
    Feb 2005
    Posts
    2

    Newbie problem in restarting program

    I'm trying to get a program to restart when a variable is proven true like the following code.

    Code:
    #include <stdio.h>
    #include <math.h>
    
    int main()
    {
        double x;
    
        printf("Enter a number lower than 10");
        scanf("%lf",&x);
    
        if (x > 10)
        {    
              printf("Your number was greater than 10, try again.\n");
              return(0);
        }
        printf("Your number, %lf, was lower than 10",x);
        return(0);
    }
    When the number is found to be greater than 10, I want it to go back to step one to the printf("Enter a number lower than 10"). How do I do this?

    Thanks.

  2. #2
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    You need a loop of some kind. Take a look at the following code:
    Code:
    #include <stdio.h>
    
    int main()
    {
        int i;
    
    	while(1)
    	{
    		printf("Enter in a number (0 to quit)\n");
    		scanf("%d",&i);
    		if(i == 0) break;
    		printf("You entered %d\n",i);
    	}
    	return 0;
    }
    This will loop until the variable i is 0, and the break line is reached.

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    184
    do you mean this

    Code:
    #include<stdio.h>
    
    int main()
    {
            double x;
    
            while(1)
            {
                   printf("Enter a number with in 10\n?");
                   scanf("%lf",&x);
            
                   if(x>10) continue;
                  else
                 {
                            printf("Your number, %lf, was lower than 10",x);
                             break;
                 }
          }
    return 0;
    }
    regards
    ssharish

  4. #4
    Registered User Scribbler's Avatar
    Join Date
    Sep 2004
    Location
    Aurora CO
    Posts
    266
    I think a do / while loop would be a bit better for this example. But I guess that's probably a matter of opinion.

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    184
    [tag]
    originally posted by scribbler
    I think a do / while loop would be a bit better for this example. But I guess that's probably a matter of opinion
    [/tag]

    yes it could work fine for this case compared to while

  6. #6
    Registered User
    Join Date
    Feb 2005
    Posts
    2
    Thanks everyone, my problem has bee solved with a simple continue command.

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    No, your problem was solved with a loop. You weren't using any. continue isn't needed at all, and isn't the best way to do this. The better solution was given in the first reply. Most cases where you use continue there's a better way to do it without using it.

    Quzah.
    Hope is the first step on the road to disappointment.

  8. #8
    ---
    Join Date
    May 2004
    Posts
    1,379
    As quzah said.
    There is a better way in doing it. Go back to your design stage and start again. Programming is all about logical thinking.

  9. #9
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >No, your problem was solved with a loop. You weren't using any.
    Right, continues are pretty useless without loops.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 05-25-2008, 12:31 AM
  2. Inheritance and Dynamic Memory Program Problem
    By goron350 in forum C++ Programming
    Replies: 1
    Last Post: 07-02-2005, 02:38 PM
  3. Replies: 20
    Last Post: 06-12-2005, 11:53 PM
  4. Replies: 2
    Last Post: 04-25-2005, 11:59 AM
  5. Replies: 3
    Last Post: 03-04-2005, 02:46 PM