Ruisu
Ruisu

Reputation: 1

statement not working in for loop

"The actual issue I am having is that It just stops running after giving out the number of questions. I've corrected the "%d%" to "%d" and its still having that issue"

I'm currently working on a project where I make a small mathematical game. I need to prompt for a difficulty (Easy, Hard, or Quit) level followed by a category (Addition, Subtraction, Multiplication, and Division. I've got my menu's working but I cant seem to get anything bellow

printf (" How many questions would you like to attempt?\n");
scanf ("%d%", &numofquestions);

to work. Could anyone show me what I am doing wrong? Below is my full code so far, there are a few unnecessary items there but they aren't the issue so far to my knowledge.

/* Program Description Goes Here */
#include <stdio.h>
#include <stdlib.h>

 /* function main begins program execution */
int main( void )
{
    /* Main Code Area For Program */
int difficulty = -1;
int category = 0;
int numofquestions;
int ca = 0; /* Correct Answer */
int cc; /* Correcnt Counter */
int ran1;
int ran2;
int random1 = 0;
int random2 = 0;
int usera; 
int i;
/* Welcome mesage */
srand ( time (NULL));

printf ( "Welcome to the math challenge.\n"); 


while ( difficulty != 1 && difficulty != 2 && difficulty !=0){ 

    printf ("Select 1 for Easy\n");
    printf ("Select 2 for Hard\n");
    printf ("Select 0 to Quit\n");
    scanf ( "%d", &difficulty);
}

        while ( category != 1 && category != 2 && category != 3 && category != 4){

    category = getchar();
    switch (category){
        case 'A':
        case 'a':
            category = 1;
            break;
        case 'S':
        case 's':
            category = 2;
            break;
        case 'M':
        case 'm':
            category = 3;
            break;
        case 'D':
        case 'd':
            category = 4;
            break;
        case '/n':
        case '/t':
        case ' ':
            break;
        default:
            printf ("Enter A for Addition\n");
            printf ("Enter S for Subtraction\n");
            printf ("Enter M for Multiplication\n");
            printf ("Enter D for Division\n");
    }
}

        printf (" How many questions would you like to attempt?\n");
        scanf ("%d%", &numofquestions);


        for (i = numofquestions; i<=0; --i){/* start for */

            if ( difficulty == 1){ /* start if */
                random1 = (rand () % 10);
                random2 = (rand () % 10);

                }/* end if */ 

            if ( category == 1){/* start if */
                printf ( "%d + %d = ?" , random1 , random2 );
                ca = random1 + random2;
                printf ( "Your response\n");
                scanf ( "%d", &usera);
                                }/* end if */

            if ( usera == ca ){ /* start if */
                printf (" %d\n ", ca);
            }/* end if */


        /* end for */}


return 0; /* indicate that program ended successfully */
/* end function main */
}

Upvotes: 0

Views: 119

Answers (3)

Patrick W. McMahon
Patrick W. McMahon

Reputation: 3561

getchar() returns an int and your switch statement your checking char. http://www.cplusplus.com/reference/cstdio/getchar/

%d% should be %d. for (i = numofquestions; i<=0; --i){ your getting an int. this will only run if the number is 0 or less and will then run forever.

Upvotes: 0

enigma
enigma

Reputation: 89

Is that extra %d*%* intentional ?

scanf ("%d%", &numofquestions);

for (i = numofquestions; i<=0; --i){/* start for */ this should be for (i = numofquestions; i>=0; --i) I think !

Upvotes: 1

Inisheer
Inisheer

Reputation: 20794

Remove the duplicate % in:

scanf("%d%", &numofquestions);

Should be:

scanf("%d", &numofquestions);

Upvotes: 1

Related Questions