![]() |
| | #1 |
| Registered User Join Date: Sep 2009
Posts: 70
| Here is the code: Code: #include <stdio.h>
#include <stdlib.h>
#include "addition.h"
/* This function will create random exercises of addition */
int addition()
{
int first, second, answerout, answerin;
char repeat;
printf("Addition:\n");
do{
first = 1 + rand() % 10;
second = 1 + rand() % 10;
answerout = first + second;
printf("\n");
printf(" What is %d + %d = ? ", first, second);
scanf("%d", &answerin);
printf("\n");
while ( answerin != answerout)
{
printf("No. Try Again. What is %d + %d = ? ", first, second);
scanf("%d", &answerin);
printf("\n");
}
printf("Correct!\n");
printf("\n");
printf("Play Again? ");
repeat = getchar();
while ( getchar() != '\n');
} while(repeat == 'Y' || repeat == 'y');
return EXIT_SUCCESS;
}
Thank you. |
| georgio777 is offline | |
| | #2 |
| Registered User Join Date: Sep 2007
Posts: 372
| What does “doesn't work” mean in this context? |
| cas is offline | |
| | #3 |
| Registered User Join Date: Sep 2009
Posts: 70
| |
| georgio777 is offline | |
| | #4 |
| Registered User Join Date: Sep 2007
Posts: 372
| If I understand correctly, what the user types is not being stored into repeat, and so repeat is never equal to 'y' or 'Y'? If so, the problem is that you have code that is roughly the following: Code: scanf("%d", &whatever);
repeat = getchar();
while ( getchar() != '\n');
User input in C is an annoying thing. For a quick fix you can replace the second two lines in the excerpt above with: Code: while( (repeat = getchar()) == '\n' ); You'll also want to make repeat an int, not a char. Despite its name, getchar() returns an int, and for good reason: otherwise, you would not be able to reliably detect EOF—something you'll want to do, at least at some point in your future C endeavors. |
| cas is offline | |
| | #5 |
| Registered User Join Date: Feb 2009
Posts: 35
| this works. ive bolded the changes despite its name, getchar works with integers, not chars. I think this program will still work if you have char repeat, but it should be an integer. I believe the reason relates to EOF. i changed the do while to a while loop. Code: int addition(void) {
int first, second, answerout, answerin;
int repeat = 'y';
printf("Addition:\n");
while(repeat == 'Y' || repeat == 'y') {
first = 1 + rand() % 10;
second = 1 + rand() % 10;
answerout = first + second;
printf("\n");
printf(" What is %d + %d = ? ", first, second);
scanf("%d", &answerin);
printf("\n");
while ( answerin != answerout){
printf("No. Try Again. What is %d + %d = ? ", first, second);
scanf("%d", &answerin);
printf("\n");
}
printf("Correct!\n");
printf("\n");
printf("Play Again? ");
repeat = getchar(); // this eats up the '\n' leftover from the scanf()
repeat = getchar(); // this gets their decision, y or n
}
return EXIT_SUCCESS;
}
Code: #include <time.h> .... srand(time(NULL)); // anywhere before the rand(); |
| Brain_Child is offline | |
| | #6 | |
| Registered User Join Date: Sep 2009
Posts: 70
| Quote:
Good to know! | |
| georgio777 is offline | |
| | #7 | |
| Registered User Join Date: Sep 2009
Posts: 70
| Quote:
Have a nice day and happy programming! | |
| georgio777 is offline | |
![]() |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Laptop Problem | Boomba | Tech Board | 1 | 03-07-2006 06:24 PM |
| getchar() problem | jlharrison | C Programming | 6 | 01-25-2006 02:49 PM |
| problem with parser code | ssharish2005 | C Programming | 2 | 12-02-2005 07:38 AM |
| Sorting problem.. well actually more of a string problem | fatdunky | C Programming | 5 | 11-07-2005 11:34 PM |