Thread: help trying to do while loops from user input

  1. #1
    Registered User
    Join Date
    Oct 2017
    Posts
    10

    help trying to do while loops from user input

    Code:
    #define_CRT_SECURE_NO_WARNINGS
    #include< stdio.h >
    
    
    
    
    int main() {
    /
    	int Input;
    	printf("Type in a number from 1-20\n");
    	scanf(" %d", &Input);
    
    
    	while (NumInp != 0) {
    //While Loop to show print
    		printf("\nGo Spartan\n");
    		Input--;
    	}
    
    
    //Do while with number input by user
    	do {
    		printf("Programming is cool\n");
    		Input++;
    	} while (Input <= 20);
    
    
    // a for loop just like the ones before
    	for (Input = 1; Input <= 20; Input++) {
    		printf("We have nice winters\n");
    
    
    	}
    
    
    return 0;
    }
    So for the first one I managed to print the message the number of times the user input but the second 2 are printed 20 times each. How do I write a condition for the do while loop and the for loop to match the number of times the user input is?

  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Are you posting your actual code? Because you used the wrong variable name in the first while loop, so this wouldn't even compile.

    Your biggest mistake is modifying the variable that holds the user input. This value should not be changed after reading it from the user, since it is used several times throughout the program. (Hint: Use another variable.)

    The value of "Input" should be checked in the conditional statement for each loop.

  3. #3
    Banned
    Join Date
    Aug 2017
    Posts
    861
    I've seen this code before, class project?

    user puts in an amount of times, how do you match that same amount using the same data? if I got five and you got zero, you can never match my five. no you cannot have my five, I am keeping it. you will have to use something else to match what I have.

    tried compiling it. you're almost there
    Code:
    $ gcc gospartins.c
    gospartins.c: In function ‘main’:
    gospartins.c:14:9: error: ‘NumInp’ undeclared (first use in this function)
      while (NumInp != 0) {
             ^~~~~~
    gospartins.c:14:9: note: each undeclared identifier is reported only once for each function it appears in
    userx@slackwhere:~/bin
    $ 
    userx@slackwher
    learn to use errors to figure out the why
    Last edited by userxbw; 10-18-2017 at 05:33 PM.

  4. #4
    Registered User
    Join Date
    Oct 2017
    Posts
    10
    Quote Originally Posted by Matticus View Post
    Are you posting your actual code? Because you used the wrong variable name in the first while loop, so this wouldn't even compile.

    Your biggest mistake is modifying the variable that holds the user input. This value should not be changed after reading it from the user, since it is used several times throughout the program. (Hint: Use another variable.)

    The value of "Input" should be checked in the conditional statement for each loop.

    So the error in the variable was from copying it numerous times and i forgot to fix it but i went ahead and fixed it. The first statement comes out fine but the last 2 statements are repeated 20 times each. How do I modify the conditions so that the statements repeat the number of times input from the user?

  5. #5
    Banned
    Join Date
    Aug 2017
    Posts
    861
    Quote Originally Posted by carancho View Post
    So the error in the variable was from copying it numerous times and i forgot to fix it but i went ahead and fixed it. The first statement comes out fine but the last 2 statements are repeated 20 times each. How do I modify the conditions so that the statements repeat the number of times input from the user?
    if that were true then that would not be happening therefore something is wrong, no one can see what you are looking at. requires posting your updated code.

    modifying code requires changing it. that requires typing in something else other then what is already there to replace what is already there. do not be afraid to change it then re compile it then run it again. using comment it out then writing under it your modification then saving it recompiling it then running it too is not a bad idea.
    Code:
    int g;
    g = 1+1;
    //looking for a result of 5
    //oops didn't work;
    
    //g = 1+1;
    g = 1 + 2;
    // try that, if not work repeat process.
    Last edited by userxbw; 10-18-2017 at 05:55 PM.

  6. #6
    Registered User
    Join Date
    Oct 2017
    Posts
    10
    Code:
    
    #define _CRT_SECURE_NO_WARNINGS
    #include < stdio.h > 
    
    
      int main() {
        
        int Input;
        printf("Type in a number from 1-20\n");
        scanf(" %d", &Input);
    
    
        while (Input != 0) {
          
          printf("\nGo Spartan\n");
          Input--;
        }
    
    
        
        do {
          printf("Programming is cool\n");
          Input++;
        } while (Input <= 20);
    
    
        //this has to be a for loop
        for (Input = 1; Input <= 20; Input++) {
          printf("We have nice winters\n");
    
    
        }
    
    
        return 0;
      }
    So this is what its like not but its still stuck on the fact that the second and third loop are giving 20 statements each instead of the amount from the input.

  7. #7
    Banned
    Join Date
    Aug 2017
    Posts
    861
    like Matticus first said you're reassigning the value to the input value,
    therefore you're removing the results wanted. rethink how to do that
    Code:
    // Input could have been 5 but gets reset to 1, then told to 
    // wait until it hits 20, so that is valid code, 
    // because it is doing what it is told to do, but ... 
    // not what you want because Input is being
    // reset to 1, and
    // no longer using the original
    // value input off the command line.
     
    (Input = 1; Input <= 20; Input++)
    try using what Matticus suggested.

    you have all of your constitutionals backwards.
    Code:
    do {
            printf("Programming is cool\n");
            Input++;
        } while (Input <= 20);
    // backwards use that against something else until it reaches whatever value is already inside of Input
    

    Last edited by userxbw; 10-18-2017 at 07:46 PM.

  8. #8
    Registered User
    Join Date
    May 2015
    Posts
    90
    Quote Originally Posted by carancho View Post
    Code:
    
    #define _CRT_SECURE_NO_WARNINGS
    #include < stdio.h > 
    
    
      int main() {
        
        int Input;
        printf("Type in a number from 1-20\n");
        scanf(" %d", &Input);
    
    
        while (Input != 0) {
          
          printf("\nGo Spartan\n");
          Input--;
        }
    
    
        
        do {
          printf("Programming is cool\n");
          Input++;
        } while (Input <= 20);
    
    
        //this has to be a for loop
        for (Input = 1; Input <= 20; Input++) {
          printf("We have nice winters\n");
    
    
        }
    
    
        return 0;
      }
    So this is what its like not but its still stuck on the fact that the second and third loop are giving 20 statements each instead of the amount from the input.
    What do you think is happening at the first loop, the one that does work? (Please explain thoroughly).
    What is the difference between the first one and the other two in your opinion? Are they the same? Why? Why not?

  9. #9
    Banned
    Join Date
    Aug 2017
    Posts
    861
    copy paste this into a separate file, save, compile, then run it
    Code:
    #define _CRT_SECURE_NO_WARNINGS
    #include <stdio.h>
    #include <stdlib.h>
    
    
    
    int main() {
        
         
        int Input;
        printf("Type in a number from 1-20\n");
        scanf(" %d", &Input);
    printf(" I want %d many times three times\n", Input);
    // this will work (once) but when it gets to the 
    // other loop it will have what value inside of it?
        while (Input != 0) {
    //While Loop to show print
            printf("\nGo Spartan\n");
            Input--;
        }
    
    printf("what is left inside of Input %d\n",Input);
    exit(0); // you will not see any more output after execution of 
    // printf 
     do {
          printf("Programming is cool\n");
          Input++;
        } while (Input <= 20);
    
    
    // a for loop just like the ones before
        for (Input = 1; Input <= 20; Input++) {
            printf("We have nice winters\n");
    
    
        }
    
    
    return 0;
    }
    Last edited by userxbw; 10-18-2017 at 07:58 PM.

  10. #10
    Banned
    Join Date
    Aug 2017
    Posts
    861
    sorry @
    Fiskker

    I must have post at the same time. oops.

  11. #11
    Registered User
    Join Date
    Oct 2017
    Posts
    10
    So would I create a new variable and make that = to Input as the condition in the do while loop? I would add the new variable to the main function.

    Code:
    do {
    		printf("Programming is c\n");
    		I--;
    	} while (I <= Input);
    




  12. #12
    Banned
    Join Date
    Aug 2017
    Posts
    861
    Quote Originally Posted by carancho View Post
    So would I create a new variable and make that = to Input as the condition in the do while loop? I would add the new variable to the main function.

    Code:
    do {
            printf("Programming is c\n");
            I--; // correct as long as you declare it and give it a value
                 //of what then do what to it to make it eventually  match the 
                 // Input so you will stop the loop?
        } while (I <= Input);
    



    for all three, you would set that one, and then reset that one again to use it again in your other loops against your Input value . that stays the same. do not mess with its value within it.
    Last edited by userxbw; 10-18-2017 at 09:05 PM.

  13. #13
    Registered User
    Join Date
    Apr 2017
    Location
    Quetzaltenango
    Posts
    82
    You need both a loop counter variable to count down and a constant to compare it to (not 20, but instead the scanf-ed number). So you need to copy your input number into a loop counter variable. One way to copy it is to pass it as an argument to a function.
    Code:
    #include <stdio.h> 
    int main(void) {
      int getinput(void);
      const int Input = getinput();
    
      void gospartans(int);
      gospartans(Input);
    
      void whatscool(int);
      whatscool(Input);
      
     // Input is still the same here because it was COPIED 
     // into remaining because C passes by value.
      void howstheweather(int);
      howstheweather(Input);
    }
    
    void whatscool(int remaining) {
      do
        printf("Programming is cool\n");
      while (--remaining);
    }
    Yes, of course I left the other three FUNctions for you.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Check if input is an integer using only loops
    By mababaan in forum C Programming
    Replies: 10
    Last Post: 11-03-2013, 08:19 PM
  2. Input to array loops forever [C]
    By Quixiotic in forum C Programming
    Replies: 5
    Last Post: 11-08-2011, 12:59 PM
  3. prompt user for number of loops
    By wonderpoop in forum C Programming
    Replies: 26
    Last Post: 11-15-2006, 06:58 AM
  4. Using loops for check a roman number input.
    By eryell in forum C++ Programming
    Replies: 9
    Last Post: 04-12-2006, 11:04 AM
  5. Help / Loops; input redirection from files
    By Frank_Rye in forum C Programming
    Replies: 10
    Last Post: 10-16-2005, 01:15 AM

Tags for this Thread