prompt user for number of loops

Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last
• 11-11-2006
wonderpoop
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?
• 11-11-2006
noodles
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*/;```
• 11-11-2006
Dave_Sinkula
• 11-11-2006
wonderpoop
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);     }```
• 11-11-2006
Dave_Sinkula
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?
• 11-11-2006
noodles
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);     }```

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);   } }```
• 11-12-2006
Dave_Sinkula
Quote:

Originally Posted by noodles
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.
• 11-12-2006
Quote:

Originally Posted by noodles
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.

• 11-12-2006
quzah
Quote:

Nope, sorry. This dog will never hunt. :)

Quote:

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:

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.
• 11-12-2006
Quote:

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. :)

• 11-12-2006
quzah
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:

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.
• 11-12-2006
ssharish2005
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
• 11-12-2006
noodles
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?
• 11-12-2006
noodles
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);   } }```
• 11-12-2006
CornedBee
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.
Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last