Thread: Using Loops

  1. #1
    Registered User
    Join Date
    Feb 2013
    Posts
    7

    Using Loops

    I'm in an intro computer science class so I'm very new to programming. Below is the assignment. I did get my program to compile, but it doesn't work properly when you first enter a number between 1 and 10. It only works when you enter it incorrectly first. What did I do wrong? Should I be using a different loop? Any insight would be greatly appreciated!


    Goal: Write a program that asks the user for a number to count to (in the range of 1 to 10), once the user has entered in an appropriate number, count to that number

    Ask the user to enter a number between 1 and 10.
    If the user did so correctly, count from 0 to the number entered by the user.
    If the user did not enter the number correctly, tell the user “I’m sorry, that is incorrect.” and then prompt the user to enter the number again. Repeat as many times as necessary.
    You must use two different loop types.

    Code:
    #include <stdio.h>
    
    
    int main(void)
    {
            //DECLARE VARIABLES
            int i, stored_number;
    
    
            //PURPOSE OF PROGRAM
            printf ("\nThis program will count from 0 to a number you pick.\n");
    
    
            //PICK AND STORE NUMBER
            printf ("\nPlease enter a number (between 1 and 10):");
            scanf ("%d", &stored_number);
    
    
            //DO LOOP
            i=1;
            do
            {
                    printf ("\nI'm sorry, that is incorrect.\n");
                    printf ("Please enter a number (between 1 and 10):");
                    scanf ("%d", &stored_number);
            }       while (stored_number>10);
            printf ("\n");
    
            //WHILE LOOP
            printf ("\n");
            i=0;
            while (i<=stored_number)
            {
                    printf ("%d...", i);
                    i++;
            }
            printf ("done!");
    
    
            //END PROGRAM
            printf ("\n\n");
    
    
            return 0;
    }


  2. #2
    Registered User
    Join Date
    May 2012
    Posts
    505
    A do ... while loop is always exectued at least once, so the error message is always output. You need a while ... loop.

    You'll find that do ... while loops are quite rare in real programs. Almost always you have a null case which means that you don't want to run the loop at all.
    I'm the author of MiniBasic: How to write a script interpreter and Basic Algorithms
    Visit my website for lots of associated C programming resources.
    https://github.com/MalcolmMcLean


  3. #3
    Registered User
    Join Date
    Feb 2013
    Posts
    7
    Thank you for your feedback. I'm sorry, I'm still so new to this so I don't fully understand. I can get rid of the do .... while loop, but then I'm going to need another one since the assignment asks for two different loops. Should I add a for ... loop?

  4. #4
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Your second ("while()") loop would be better suited as a "for()" loop, allowing you to follow the advice of Malcolm McLean.

  5. #5
    Registered User
    Join Date
    Feb 2013
    Posts
    7
    Okay, I rewrote it to start with a for... loop statement. What would my other loop look like if a user entered a number over 10?

    Thanks for your help!

    Code:
    #include <stdio.h>
    
    int main(void)
    {
            //DECLARE VARIABLES
            int i, stored_number;
    
    
            //PURPOSE OF PROGRAM
            printf ("\nThis program will count from 0 to a number you pick.\n");
    
    
            //PICK AND STORE NUMBER
            printf ("\nPlease enter a number (between 1 and 10):");
            scanf ("%d", &stored_number);
    
    
            //FOR LOOP
            printf ("\n");
            for (i=0; i<=stored_number; i++)
            {
                    printf ("%d...", i);
            }
            printf (" done!");
    
    
            //END PROGRAM
            printf ("\n\n");

  6. #6
    Registered User
    Join Date
    Feb 2013
    Posts
    22
    You could try using a do while loop to get the number from the user and to display the error message if the number is incorrect
    Then use a for loop to print the number.

    Code:
    do
    {
        printf("Enter a number from one to 10: ");
        scanf("%d",&stored_number);
        if(stored_number > 10)
        {
               printf("I'm sorry, that is incorrect");
               continue;
        }
        else
        break;
    }while(1==1);
    }
    The 1==1 is to make sure the loop keeps running until stored_number is less than 10.
    Last edited by Dakshin; 02-24-2013 at 12:11 AM.

  7. #7
    Registered User
    Join Date
    Mar 2012
    Posts
    33
    Hmm, the problem with that ^^^ is that it excepts 0 as a valid answer. I would do:

    Code:
        while(number < 1 || number > 10){      
          puts("Incorrect Input");
          scanf("%d", &number); 
       }
    Last edited by atac; 02-24-2013 at 02:21 AM.

  8. #8
    Registered User
    Join Date
    Feb 2013
    Posts
    7
    Thank you atac and Dakshin for your help. I just have one problem; the assignment says I'm not allowed to use a break. If I used a while ... loop for a number greater than 10, it would just go on forever, correct? How else could I create a 2nd loop for an incorrect response to the initial prompt?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. converting for loops to while loops
    By nabhatt in forum C Programming
    Replies: 3
    Last Post: 02-16-2012, 09:45 PM
  2. Replies: 3
    Last Post: 06-01-2011, 04:19 PM
  3. loops, menu loops
    By gloworm in forum C Programming
    Replies: 17
    Last Post: 04-12-2010, 07:59 PM
  4. Loops
    By goosematt in forum C Programming
    Replies: 1
    Last Post: 10-20-2003, 11:59 AM
  5. help with loops
    By ica0330 in forum C++ Programming
    Replies: 6
    Last Post: 01-14-2003, 09:15 PM

Tags for this Thread