Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   C++ (http://www.programmingforums.org/forum15.html)
-   -   A Beginner Needing Some Little Help... (http://www.programmingforums.org/showthread.php?t=1223)

rocking_finn Nov 20th, 2004 4:49 PM

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 :D

Eggbert Nov 20th, 2004 4:55 PM

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;
}


Ooble Nov 20th, 2004 8:12 PM

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.

Eggbert Nov 20th, 2004 8:32 PM

>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.

Ooble Nov 21st, 2004 6:49 AM

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 :P

Eggbert Nov 21st, 2004 9:01 AM

>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;
}


Ooble Nov 21st, 2004 3:10 PM

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.


All times are GMT -5. The time now is 1:18 AM.

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