where is my indefinite loop here?

This is a discussion on where is my indefinite loop here? within the C Programming forums, part of the General Programming Boards category; this is supposed to be a multiplication program.. it compiles well but when I run it an indefinite loop is ...

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

    Question where is my indefinite loop here?

    this is supposed to be a multiplication program.. it compiles well but when I run it an indefinite loop is caused. can't see where. heeelp! i've been doing this for 2 days!....how I create the responses with a function void message(answer) ????? thanks...

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

    int main()

    {

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

    srand( time( NULL ) );

    printf( "\nLet's learn how to multiply.\n" );
    printf( "Choose 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( "How much is %d times %d? \n", x,y );
    scanf("%d", &answer1);
    }

    if (chosenoption == 2){

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

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

    if (answer1 == x*y )
    response = 1;

    else
    response = 0;

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

    while ( response == 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;

    }
    }

    while ( response == 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;

    }
    }

    printf( "Let's learn how to multiply.\n" );
    printf( "Choose 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;
    }

  2. #2
    Mayor of Awesometown Govtcheez's Avatar
    Join Date
    Aug 2001
    Location
    MI
    Posts
    8,825
    while(chosenoption != -1) {
    if (chosenoption == 1){

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

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

    You only ask for chosenoption before this, so if they put anything in besides -1, that's going to loop infinitely.

  3. #3
    Registered User
    Join Date
    Feb 2002
    Posts
    22
    nop...the loop is when the responses appear...for example if i answer correctly it will loop with excellent! forever......how do i correct that?

  4. #4
    Mayor of Awesometown Govtcheez's Avatar
    Join Date
    Aug 2001
    Location
    MI
    Posts
    8,825
    Whoops - you're right, I counted the braces wrong. Your problem's the same type of thing, though.
    Code:
    while ( response == 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; 
    
    } 
    }
    ::double checks braces::
    There's no way for response to change inside the loop - that's why it loops forever. Use if statements instead.
    Code:
    if ( response == 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; 
    
    } 
    }

  5. #5
    Registered User
    Join Date
    Feb 2002
    Posts
    22
    thanks!!!! .......the program is running properly ...
    well, the grand final question:

    the program ask the following feature which I dont know how to do:


    *use the following function: void message(int answer); function message should accept as parameter an integer value, which should be 1 if the answer is correct, and 0 if the answer is wrong. It should then print the appropiate message..


    how in the hell i do that....the instructor did not explain that well the part of function prototypes and function calls..please this is the last question!

  6. #6
    B26354 Deckard's Avatar
    Join Date
    Jan 2002
    Posts
    633
    Something like this?
    Code:
    void message( int answer )
    {
      if ( answer )
        printf( "Correct\n" );
      else
        printf( "Incorrect\n" );
    }
    Jason Deckard

  7. #7
    Registered User
    Join Date
    Feb 2002
    Posts
    22
    no....with the responses that are in the switch structures above...please! thanks

  8. #8
    B26354 Deckard's Avatar
    Join Date
    Jan 2002
    Posts
    633

    Show a little effort

    Hmm, perhaps you could take a stab at it and post your code if you get stuck. If that doesn't work, maybe you could provide us with directions to your school and the contact information for a good plastic surgeon so that Cheez or I are able to complete the class for you.
    Jason Deckard

  9. #9
    Registered User
    Join Date
    Feb 2002
    Posts
    22
    here is.,...the problem is that it always prints the wrong responses even though i perform the multiplications well....whats wrong?


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

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

    if ( response == 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 ( response == 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;

    }
    }

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

    }

    return 0;
    }

    void Message( int answer )
    {
    int x,y;

    if (answer == x*y )
    answer = 1;

    else
    answer = 0;

    }

  10. #10
    B26354 Deckard's Avatar
    Join Date
    Jan 2002
    Posts
    633
    Originally posted by pancho
    Code:
    void Message( int answer )
    {
      int x,y;
    
      if (answer == x*y )
        answer = 1;
      else
        answer = 0;
    }
    Pancho, x and y are not initialized. I suspect you wanted them to retain the values of the x and y variables from main(). You may want to review your textbook for discussions on variable scope.

    Also, consider using the message() function in a different way. Instead of using message() to determine if the answer was correct, decide if the answer was correct beforehand, and then let message() print the success/failure message as your homework instructs.
    Code:
    /* in main() */
    if ( x * y == answer1 )
      message( 1 ); /* Success */
    else
      message( 0 ); /* Failure */
    
    .
    .
    .
    
    void message( int answer )
    {
      if ( answer )
        printf( "Correct\n" );
      else
        printf( "Incorrect\n" );
    }
    If you are hell-bent on having the pseudo-random success/failure messages, move your rand() statement and switches into message().
    Jason Deckard

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. My loop within loop won't work
    By Ayreon in forum C Programming
    Replies: 3
    Last Post: 03-18-2009, 11:44 AM
  2. While loop misbehaving (or misunderstanding)
    By mattAU in forum C Programming
    Replies: 2
    Last Post: 08-28-2006, 03:14 AM
  3. Personal Program that is making me go wtf?
    By Submeg in forum C Programming
    Replies: 20
    Last Post: 06-27-2006, 01:13 AM
  4. loop issues
    By kristy in forum C Programming
    Replies: 3
    Last Post: 03-05-2005, 09:14 AM
  5. when a while loop will stop ?
    By blue_gene in forum C Programming
    Replies: 13
    Last Post: 04-20-2004, 04:45 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21