Hi Could anyone help me please
Im only learning C Im surprised Ive got as far as I have! It works once - but it wont loop. So Ive been looking up nested loops and I just cant see where Im going wrong.
and thanks if you're even reading this.
Hi Could anyone help me please
Im only learning C Im surprised Ive got as far as I have! It works once - but it wont loop. So Ive been looking up nested loops and I just cant see where Im going wrong.
and thanks if you're even reading this.
Last edited by chead; 01-04-2005 at 09:30 AM.
Put your program inside CODE tags.
If you understand what you're doing, you're not learning anything.
Well, I think you might want to consider resetting your num variable to 0 when the user chooses to go again.
Other than that, have a look at your variables - if you are using a variable, you should explicity initialise it before you use it - otherwise you are using 'junk' values, and you could get some unexpected results. In particular, your first while loop uses a variable which is not initialised. Also, initialising the variable num to 1 gives some problems as well. Test the program, and see if you can see what is wrong with it. Hint: the numbers are off by 1.
~/
Last edited by kermit; 01-03-2005 at 05:46 PM.
It works, but it is not much better than what you had before. Do you code like that, with no indentation? It makes it much nicer to read if you indent your code.
~/
Yes.is it a mess?
Seriously though, if it's hard to follow then it's not good. You can see that after you start your while() loop everything kind of suddenly slams up against the left margin. Indentation should be used to let you easily see which block of code you're currently looking in. Indentation goes hand in hand with bracing style.
Here's a link for you: http://burks.brighton.ac.uk/burks/foldoc/80/56.htm
If you understand what you're doing, you're not learning anything.
Maybe I should have another variable after each if?
I think I would probably do it something like this:
See how much easier it is with proper indentation to tell which code block you're looking at?Code:#include <stdio.h> int main(void) { int table; int num; int try; int ans; int another; puts("Welcome to Gill's Multiplication loopers tutorial"); do { puts("Please choose a table"); scanf("%d", &table); printf("You have chosen %d times tables\n", table); for(num = 1;num < 12;++num) { for(try = 0;try < 2;++try) { printf("%d * %d = ", table, num); fflush(stdout); scanf("%d", &ans); if(table * num == ans) { printf("well done on %s try!\n", try ? "2nd" : "1st"); break; } else { if(try) { puts("WRONG WRONG WRONG YOU FOOL! on 2nd try"); printf("The answer is %d\n", table * num); } else { puts("WRONG WRONG WRONG YOU FOOL! on 1st try"); puts("Try again"); } } } } puts("would you like to try another? y=1/n=0"); scanf("%d", &another); } while(another); return 0; }
I'm not sure why you were using this crazy test (table==ans/num)&&(ans%num==0) when you can just check table * num == ans.
Last edited by itsme86; 01-04-2005 at 12:54 AM.
If you understand what you're doing, you're not learning anything.