Thread: almost done...help with small detail...

  1. #1
    Registered User
    Join Date
    Feb 2002
    Posts
    22

    Talking almost done...help with small detail...

    its almost done...it compiles and works almost perfect....well when I write a good answer after doing it wrong it doesnt print a positive response..instead it displays the menu...what i have to do in order it to display a positive response like "Good job" before displaying the menu?? thanks



    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>

    void message(int);
    int main()

    {

    int x, y, chosenoption, answer1, response, option;

    srand( time( NULL ) );

    printf( "\nLet's learn how to multiply.\n" );
    printf( "\nChoose an option from below:\n" );
    printf( "Type 1 for a single digit multiplication\n" );
    printf( "Type 2 for a two digit multiplication\n" );
    printf( "Type -1 to end program.\n \n");
    scanf("%d", &chosenoption);

    while(chosenoption != -1) {
    if (chosenoption == 1){

    x = ( rand() % 10 );
    y = ( rand() % 10 );

    printf( "\nHow much is %d times %d? \n", x,y );
    scanf("%d", &answer1);
    }

    if (chosenoption == 2){

    x = 10 + ( rand() % 90 );
    y = 10 + ( rand() % 90 );

    printf( "\nHow much is %d times %d? \n", x,y );
    scanf("%d", &answer1);
    }

    if ( x * y == answer1 )
    message( 1 ); /* Success */

    else
    message( 0 ); /* Failure */

    while ( x * y != answer1 ) {

    printf( "\nHow much is %d times %d? \n", x,y );
    scanf("%d", &answer1);
    message (0);


    }



    printf( "\n\nLet's learn how to multiply.\n" );
    printf( "\nChoose an option from below:\n" );
    printf( "Type 1 for a single digit multiplication\n" );
    printf( "Type 2 for a two digit multiplication\n" );
    printf( "Type -1 to end program.\n \n");
    scanf("%d", &chosenoption);

    }

    return 0;
    }



    void message( int answer )
    {

    int option;

    option = 1 + ( rand() % 4 );

    if ( answer == 1 ) {
    switch (option) {

    case 1:
    printf( "Very good!\n" );
    break;

    case 2:
    printf( "Excellent!\n" );
    break;

    case 3:
    printf( "Nice work!\n" );
    break;

    case 4:
    printf( "Keep up the good work!\n" );
    break;

    }
    }

    if ( answer == 0 ) {
    switch (option) {

    case 1:
    printf( "No. Please try again.\n" );
    break;

    case 2:
    printf( "Wrong. Try one more time.\n" );
    break;

    case 3:
    printf( "Don't give up.\n" );
    break;

    case 4:
    printf( "No. Keep trying.\n" );
    break;

    }
    }

    }

  2. #2
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680
    Yes indeed, almost done.

    Change the following code:

    Code:
    if ( x * y == answer1 ) 
        message( 1 ); /* Success */ 
    else 
        message( 0 ); /* Failure */ 
    
    while ( x * y != answer1 ) 
    { 
        printf( "\nHow much is %d times %d? \n", x,y ); 
        scanf("%d", &answer1); 
        message (0); 
    }
    To:

    Code:
    while ( x * y != answer1 ) 
    { 
        message (0); 
        printf( "\nHow much is %d times %d? \n", x,y ); 
        scanf("%d", &answer1); 
    } 
    
    message(1);
    This should work!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how to split long programe in small files
    By umeshjaviya in forum C Programming
    Replies: 11
    Last Post: 04-15-2008, 02:45 AM
  2. get keyboard and mouse events
    By ratte in forum Linux Programming
    Replies: 10
    Last Post: 11-17-2007, 05:42 PM
  3. want to make this small program...
    By psycho88 in forum C++ Programming
    Replies: 8
    Last Post: 11-30-2005, 02:05 AM
  4. Replies: 16
    Last Post: 09-21-2004, 11:08 PM
  5. FILES in WinAPI
    By Garfield in forum Windows Programming
    Replies: 46
    Last Post: 10-02-2003, 06:51 PM