prompt user for number of loops

This is a discussion on prompt user for number of loops within the C Programming forums, part of the General Programming Boards category; how do i prompt a user to choose the number of loops they want. for example, I know that if ...

  1. #1
    Registered User
    Join Date
    Sep 2006
    Posts
    41

    prompt user for number of loops

    how do i prompt a user to choose the number of loops they want.

    for example, I know that if I set #define MAXCOUNT 100, it will loop 100 times. What would i put for the user to select the number of times they want it to loop?

  2. #2
    Registered User
    Join Date
    Jun 2006
    Posts
    75
    Quote Originally Posted by wonderpoop
    What would i put for the user to select the number of times they want it to loop?
    Do you mean like this?
    Code:
    int i, n;
    scanf("%d", &n); /*accept loop limit from user*/
    for (i=0; i<n; i++)
        /*loop*/;

  3. #3
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    41
    Quote Originally Posted by noodles
    Do you mean like this?
    Code:
    int i, n;
    scanf("%d", &n); /*accept loop limit from user*/
    for (i=0; i<n; i++)
        /*loop*/;
    no, the way i have it is

    Code:
    #include <stdio.h>
    #include <math.h>
    #define MAXCOUNT 5 <- I want this number to be chosen by the user
    int main ()
    {
    
      int calc;
      float num, total, Gtotal, number;
    
    
      for (calc = 0; calc <  MAXCOUNT; calc ++)
        {
          printf ("Enter a number of times to repeat\n");
          scanf ("%f", &num);
        }

  5. #5
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    You expect two and a third inputs to be a possibility?

    Look at what has been presented and think a little bit. Why would you ask 5 times how many times you want to loop?
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  6. #6
    Registered User
    Join Date
    Jun 2006
    Posts
    75
    Quote Originally Posted by wonderpoop
    no, the way i have it is

    Code:
    #include <stdio.h>
    #include <math.h>
    #define MAXCOUNT 5 <- I want this number to be chosen by the user
    int main ()
    {
    
      int calc;
      float num, total, Gtotal, number;
    
    
      for (calc = 0; calc <  MAXCOUNT; calc ++)
        {
          printf ("Enter a number of times to repeat\n");
          scanf ("%f", &num);
        }
    How about:
    Code:
    #include <stdio.h>
    #include <math.h>
    #define MAXCOUNT n /*<- I want this number to be chosen by the user*/
    int main ()
    {
    
      int calc, n;
      float num, total, Gtotal, number;
    
      printf("How many times to loop?\n");
      scanf("%d", &n);
    
      for (calc = 0; calc <  MAXCOUNT; calc ++)
      {
          printf("Loop %d\n", calc+1);
      }
    }

  7. #7
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Quote Originally Posted by noodles
    How about:
    Code:
    #include <stdio.h>
    #include <math.h>
    #define MAXCOUNT n /*<- I want this number to be chosen by the user*/
    int main ()
    {
    
      int calc, n;
      float num, total, Gtotal, number;
    
      printf("How many times to loop?\n");
      scanf("%d", &n);
    
      for (calc = 0; calc <  MAXCOUNT; calc ++)
      {
          printf("Loop %d\n", calc+1);
      }
    }
    Pretty ugly, IMO.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  8. #8
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by noodles
    How about:
    Code:
    #include <stdio.h>
    #include <math.h>
    #define MAXCOUNT n /*<- I want this number to be chosen by the user*/
    int main ()
    {
    
      int calc, n;
      float num, total, Gtotal, number;
    
      printf("How many times to loop?\n");
      scanf("%d", &n);
    
      for (calc = 0; calc <  MAXCOUNT; calc ++)
      {
          printf("Loop %d\n", calc+1);
      }
    }
    Nope, sorry. This dog will never hunt.

    Defines are processed by the pre-processor, and "#define Maxcount n", would amount to a literal replacement of 'n' every time the word "Maxcount" appears in your code. Like a "Search and replace" function in a text editor.

    The value of n, is not known at this time, (the program hasn't even been compiled yet), and it could never be inserted into your code.

    What you want is what variables are used for.

    Adak

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by Adak
    Nope, sorry. This dog will never hunt.
    Too bad you're wrong.

    Quote Originally Posted by Adak
    Defines are processed by the pre-processor, and "#define Maxcount n", would amount to a literal replacement of 'n' every time the word "Maxcount" appears in your code. Like a "Search and replace" function in a text editor.
    Which means it will work.
    Quote Originally Posted by Adak
    The value of n, is not known at this time, (the program hasn't even been compiled yet), and it could never be inserted into your code.
    It most definately can be inserted, and will! It isn't insterting a "value", it is as you yourself said, a textual replacement. Since n is in fact defined before MAXVALUE is ever seen, it will simply replace the text MAXVALUE with the text n.

    Pull out your compiler and try it yourself. If you weren't in such a hurry to prove someone wrong, you might not have end up being so wrong yourself.


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

  10. #10
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868

    It most definately can be inserted, and will! It isn't insterting a "value", it is as you yourself said, a textual replacement. Since n is in fact defined before MAXVALUE is ever seen, it will simply replace the text MAXVALUE with the text n.
    The OP wants to insert a VALUE from the user, (not the literal text n), into a defined variable which is handled by the pre-processor. That can't be done, simply.

    I'm not trying to "prove him wrong". I'm not trying to "prove me right". If you'd lose your attitude, you'd see that you are in fact, agreeing completely with what I posted.

    I know you know more about C than I do, Quzah. That doesn't mean we're always going to agree on everything. When we disagree, I expect you to provide a persuasive argument for your point of view, if you believe it warrants it - and I'll do the same.

    I believe you're a great resource for this forum. Your attitude is not such a great resource for the forum. I'm sure this is not news to you or anyone else on the board. In fact, a couple of posters were joking about just that, in another part of the forum. They discussed an idea for a new game, where the noobs would be harassed by somebody like Quzah. So you're getting a rep.

    Adak

  11. #11
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    But you're wrong. His sample code does work. You said it didn't. You're wrong. They're still filling n, they've just used a #define for part of the middle loop condition. The point is, you're wrong. Your description of what the #define does in their code is wrong. You were wrong. They were right. I pointed it out.

    Quote Originally Posted by Adak
    So you're getting a rep.
    I'm not "getting" a rep. I already have one. I've been here for eight years or something like that. I'm well past the "getting a rep" stage, and were anyone to pay attention, they'd notice I quite clearly don't care.


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

  12. #12
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    Code:
    #include <stdio.h>
    #include <math.h>
    #define MAXCOUNT n /*<- I want this number to be chosen by the user*/
    int main ()
    {
    
      int calc, n;   // The value of n is seen by the compiler before the value is read from the user. 
                             //Which meains the value is known to the compiler. Which could have some junk value till the value is 
                             //read from the user. 
    
      float num, total, Gtotal, number;
    
      printf("How many times to loop?\n");
      scanf("%d", &n);
    
      for (calc = 0; calc <  MAXCOUNT; calc ++)
      {
          printf("Loop %d\n", calc+1);
      }
    }
    try commenting the two statments which actually read the data for value n. run the program it will loop on the junk value which the n had.

    now again when u uncomment and run the program it will work as specified, which means the value n is vissible.

    ssharish2005

  13. #13
    Registered User
    Join Date
    Jun 2006
    Posts
    75
    Quote Originally Posted by Dave_Sinkula
    Pretty ugly, IMO.
    Perhaps it is. But isn't that a good way to solve the OP's problem given the restrictions?

  14. #14
    Registered User
    Join Date
    Jun 2006
    Posts
    75
    This may be better:
    Code:
    #include <stdio.h>
    
    #define MAXCOUNT n /*<- I want this number to be chosen by the user*/
    int main ()
    {
    
      int calc, MAXCOUNT;
    
      printf("How many times to loop?\n");
      scanf("%d", &MAXCOUNT);
    
      for (calc = 0; calc <  MAXCOUNT; calc ++)
      {
          printf("Loop %d\n", calc+1);
      }
    }

  15. #15
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Why isn't the first solution given feasible? I think there's a misunderstaning here.

    OP: the code in noodles' first post was a solution, not what he thought your code looks like.

    Others: I don't think there is any requirement to use a #define or MAX_VALUE or anything.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Loops Trouble
    By rlframpton in forum C Programming
    Replies: 2
    Last Post: 04-17-2009, 02:08 AM
  2. timed user input
    By sainiabhishek in forum C Programming
    Replies: 4
    Last Post: 04-01-2009, 12:59 PM
  3. Control characters to emulate a prompt?
    By xconspirisist in forum Windows Programming
    Replies: 3
    Last Post: 02-08-2006, 04:16 PM
  4. Changing this to user prompt help.
    By Infuriate in forum C Programming
    Replies: 5
    Last Post: 12-04-2005, 05:01 PM
  5. Newbie Help: Currency Converter
    By Ashfury in forum C Programming
    Replies: 10
    Last Post: 11-06-2005, 01:21 PM

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