Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Nov 20th, 2004, 3:49 PM   #1
rocking_finn
Newbie
 
Join Date: Nov 2004
Posts: 6
Rep Power: 0 rocking_finn is on a distinguished road
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
rocking_finn is offline   Reply With Quote
Old Nov 20th, 2004, 3:55 PM   #2
Eggbert
Professional Programmer
 
Eggbert's Avatar
 
Join Date: Nov 2004
Posts: 250
Rep Power: 4 Eggbert is on a distinguished road
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;
}
Eggbert is offline   Reply With Quote
Old Nov 20th, 2004, 7:12 PM   #3
Ooble
I eat cake for breakfast.
 
Ooble's Avatar
 
Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9 Ooble is on a distinguished road
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.
__________________
Me :: You :: Them
Ooble is offline   Reply With Quote
Old Nov 20th, 2004, 7:32 PM   #4
Eggbert
Professional Programmer
 
Eggbert's Avatar
 
Join Date: Nov 2004
Posts: 250
Rep Power: 4 Eggbert is on a distinguished road
>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;
}
Which is far from complicated. Compare this with your solution (which is C++ when the original question specified C) that uses redundant code and potentially confusing loop control expressions. Granted, the two programs perform a different test, but I find my code simpler. Though I would be interested to hear your reasoning.
[*] My excuse is that I'm getting senile.
Eggbert is offline   Reply With Quote
Old Nov 21st, 2004, 5:49 AM   #5
Ooble
I eat cake for breakfast.
 
Ooble's Avatar
 
Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9 Ooble is on a distinguished road
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
__________________
Me :: You :: Them
Ooble is offline   Reply With Quote
Old Nov 21st, 2004, 8:01 AM   #6
Eggbert
Professional Programmer
 
Eggbert's Avatar
 
Join Date: Nov 2004
Posts: 250
Rep Power: 4 Eggbert is on a distinguished road
>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;
}
If you throw a function into the mix, a while loop can be used without duplicating statements:
#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;
}
Eggbert is offline   Reply With Quote
Old Nov 21st, 2004, 2:10 PM   #7
Ooble
I eat cake for breakfast.
 
Ooble's Avatar
 
Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9 Ooble is on a distinguished road
Quote:
Originally posted by Eggbert@Nov 21 2004, 02:01 PM
>And I don't like flags
You already have a flag sleeping in count, why not use it to make your code simpler?
Good point... didn't see that one.
__________________
Me :: You :: Them
Ooble is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 5:05 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC