GP32 Is there a reason this shouldn't work?


Tobriand

Well-Known Member
Joined
Dec 27, 2002
Messages
4,071
Age
38
Location
Croydon (UK)
Website
Visit site
Is there a reason this shouldn't work (assuming I'm typing it in right).
Its supposed to be a simple loop proggy (not for GP32 obviously) to ask for a number, ask for a max for that number, then ask the ammount to increment it by and do so until it hits the max you set. You'll have to forgive my spacing... its not easy to get right in a text-box...

#include <stdio.h> /** and something else, but can't rememebr what the other one was off the top of my head **/

int i, maxi, inci;

main()
{
printf ("Please give a starting value for i: /n");
scanf ("%i", &i);
printf ("Now enter a starting value for maxi: /n");
scanf ("%maxi", &maxi);
printf ("Finally, a value to increment i by: /n");
scanf ("%inci", &inci);

while (i <= maxi) {
printf ("i is %i this time /n", i);
i = i + inci;
}
}

What actually happens when it's compiled is that the moment you enter a value - any value - for maxi and it jumps straight to a loop. Unfortauntely, it goes further and i shoots up to stupidly high numbers (>1000000) very quickly. A long way further than maxi, anyway. Setting all the values to 0 on declaring them doesn't seem to work either. Nor does replacing the while loop with a do {} while() one. My ide is kDevelop at the moment (since it came with Mandrake) - any tips?
 
Well, dunno if you typed it in wrong but.
scanf(const char *pFormat, ...);

Shouldn't you have
scanf("%i", &maxi);
instead of
scanf("%maxi", &maxi);
etc.
?

---
mithris
 
If you're using the standard headers then it should be
scanf("%d", &maxi);

%d for int
%l or %f for longs and floats (I think)
%s for strings
%c for characters

I'm only human, I'm probably wrong ;)
 
%i is a bit more flexible, handles hex and octal etc.
But anyway.. any C/C++ manual should cover that..

---
mithris
 
Well, that'd be the reason. Wasn't coz I typed it wrong, I'm just very very new to trying to program in C (or indeed anything but basic). Thanks for the help everyone... I'll post again if switching to the right wossname (%d, %f etc) doesn't fix it, but I'd be a little surprised if it didn't...
 
Try this...

Code:
#include <stdio.h>

int i, maxi, inci;

main()
{
printf ("Please give a starting value for i: \n");
scanf ("%d", &i);
printf ("Now enter a starting value for maxi: \n");
scanf ("%d", &maxi);
printf ("Finally, a value to increment i by: \n");
scanf ("%d", &inci);

while (i <= maxi) {
printf ("i is %i this time \n", i);
i = i + inci;
}

return 0;
}
 
Back
Top