![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Nov 2004
Posts: 6
Rep Power: 0
![]() |
Hi everybody! B)
I'm a beginner in C programming so I'm still learning the language. I decided that to understand the language well I would write simple programmes. So while learning about if-loops I got the idea of doing a simple math programme so I would get familiar to if-loops. But there's one problem - I would like the programme to return to the question if you've been writing the wrong answer . Could anybudy help with that thing? and by the way, I'm also a new member on this forum ![]() |
|
|
|
|
|
#2 |
|
Professional Programmer
Join Date: Nov 2004
Posts: 250
Rep Power: 4
![]() |
Use a loop:
#include <stdio.h>
#include <stdlib.h>
int main ( void )
{
int answer;
do {
printf ( "1 + 1 == " );
fflush ( stdout );
if ( scanf ( "%d", &answer ) != 1 ) {
fprintf ( stderr, "Unexpected input encountered\n" );
return EXIT_FAILURE;
}
} while ( answer != 4 );
printf ( "You got the answer right!\n" );
return EXIT_SUCCESS;
} |
|
|
|
|
|
#3 |
|
I eat cake for breakfast.
![]() ![]() ![]() ![]() Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9
![]() |
No offence Eggy, but that's scary. Try this:
int number = 32;
int n1, n2;
int count = 1;
cout << "*** Try " << count << " ***" << endl;
cout << "Enter two numbers, separated by a space, that multiply to give " << number << ":" << endl;
cin >> n1 >> n2;
while ((n1 * n2) != number) {
cout << "Wrong answer!" << endl << endl;
count++;
cout << "*** Try " << count << " ***" << endl;
cout << "Enter two numbers, separated by a space, that multiply to give " << number << ":" << endl;
cin >> n1 >> n2;
}
cout << "Right answer! You got it!" << endl;Hopefully you'll understand that - it's about as far as I can go without confusing myself when I come back to look at it. |
|
|
|
|
|
#4 |
|
Professional Programmer
Join Date: Nov 2004
Posts: 250
Rep Power: 4
![]() |
>No offence Eggy, but that's scary.
How is it scary (aside from my inability to distinguish 1 from 2[*])? By removing the proper error handling code, and assuming an environment that flushes the output stream automatically, the code can be tightened up to this: #include <stdio.h>
int main ( void )
{
int answer;
do {
printf ( "1 + 1 == " );
scanf ( "%d", &answer );
} while ( answer != 4 );
printf ( "You got the answer right!\n" );
return 0;
}[*] My excuse is that I'm getting senile. |
|
|
|
|
|
#5 |
|
I eat cake for breakfast.
![]() ![]() ![]() ![]() Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9
![]() |
Fine... here's the same thing in C:
int number = 32;
int n1, n2;
int count = 1;
printf("*** Try %i ***\n", count);
printf("Enter two numbers, separated by a space, that multiply to give %i:\n", number);
scanf("%i %i", n1, n2);
while ((n1 * n2) != number) {
printf("Wrong answer!\n\n")
count++;
printf("*** Try %i ***\n", count);
printf("Enter two numbers, separated by a space, that multiply to give %i:\n", number);
scanf("%i %i", n1, n2);
}
printf("Right answer! You got it!\n");Granted, it's nowhere near perfect, but it's a start. As you might have noticed, I'm not exactly a pro. Of course, your solution minus the error checking wins. And about the redundant code: it's either that or use flags, if you want to signal a wrong answer. And I don't like flags ![]() |
|
|
|
|
|
#6 |
|
Professional Programmer
Join Date: Nov 2004
Posts: 250
Rep Power: 4
![]() |
>And I don't like flags
You already have a flag sleeping in count, why not use it to make your code simpler? You could also duplicate the test for a correct answer and use that instead of a flag if you feel the need to avoid flags at all costs.Also, even though they aren't used that often, a do..while loop is best suited to this example because the question is asked at least once. The alternatives are an infinite loop that breaks from the body, initialization of n1 and n2 that guarantees that the condition will be false, or redundant code. #include <stdio.h>
int main ( void )
{
int a, b;
int count = 1;
const int answer = 32;
do {
if ( count > 1 )
printf ( "Wrong answer, try again.\n" );
printf ( "Try %d\n? * ? == %d: ", count++, answer );
scanf ( "%d%d", &a, &b );
} while ( a * b != answer );
printf ( "You have chosen wisely\n" );
return 0;
}#include <stdio.h>
static int count = 1;
static const int answer = 32;
int is_correct()
{
int a, b;
printf ( "Try %d\n? * ? == %d: ", count++, answer );
scanf ( "%d%d", &a, &b );
return a * b == answer;
}
int main ( void )
{
while ( !is_correct() )
printf ( "Wrong answer, try again.\n" );
printf ( "You have chosen wisely\n" );
return 0;
} |
|
|
|
|
|
#7 | |
|
I eat cake for breakfast.
![]() ![]() ![]() ![]() Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9
![]() |
Quote:
|
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|