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?
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?
Do you mean like this?Originally Posted by wonderpoop
Code:int i, n; scanf("%d", &n); /*accept loop limit from user*/ for (i=0; i<n; i++) /*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.*
no, the way i have it isOriginally Posted by noodles
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); }
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.*
How about:Originally Posted by wonderpoop
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.Originally Posted by noodles
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.*
Nope, sorry. This dog will never hunt.Originally Posted by noodles
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
Too bad you're wrong.Originally Posted by Adak
Which means it will work.Originally Posted by AdakIt 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.Originally Posted by Adak
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.
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.
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.
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
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.
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.Originally Posted by Adak
Quzah.
Hope is the first step on the road to disappointment.
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.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); } }
now again when u uncomment and run the program it will work as specified, which means the value n is vissible.
ssharish2005
Perhaps it is. But isn't that a good way to solve the OP's problem given the restrictions?Originally Posted by Dave_Sinkula
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); } }
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