Thread: I need help again. I've gotten better =)

  1. #1
    Registered User
    Join Date
    Oct 2012
    Posts
    5

    I need help again. I've gotten better =)

    ok I'm really sleepy and coding as fast as i can to finish. this is what i got. I got to create a program to help kids develop math skills and encourage them. what I'm trying to do is create a loop and then break it. loop will make the child input another answer to the previously given question unless he doesn't want to---which is where the break comes in. sorry if its a little messy, i'm new

    OK

    Code:
    #include <stdio.h>#include <stdlib.h>
    #include <time.h>
    #include <math.h>
    #include <ctype.h>
    
    
    int main(void);
    void correctMessege(void);
    void incorrectMessege(void);
    void again(void);
    
    
    int main(void)
    {
    	int num1, num2, operation, answer;
    	
    	num1 = rand() % 4;
    	num2 = rand() % 4;
    	
    	printf("...................................... HELLO!! .................................\n");
    	printf("................ LET'S ...................................... PLAY .............\n\n");
    	printf("          PRESS>> 1 to ADDING            PRESS>>  2 to SUBTRACT\n\n");
    	printf("          PRESS>> 3 to MULTIPLY          PRESS>>  4 to DIVIDE\n\n");
    	printf("                     PRESS>> 5 if your really SMART \n");
    	scanf("%d", &operation );
    	if(operation == 1) 
    	{
    	 
    	        answer = num1 + num2;
    	        printf("How much is %d + %d?\n", num1, num2);
    	do{	                                 /*here i need help*/
    		scanf(" %d",&answer);
    		if(num1+num2 !=answer)
    		{
    			incorrectMessege();
    			continue;
    		}
    		else if(num1+num2 ==answer)
    		{
    			correctMessege();
    			again();  /*trying to make this string break the loop, string is further down*/
    		}}while(0==0);
    	} 
    	else if(operation == 2) 
    	{
    	        answer = num1 - num2;
    	        printf("How much is %d - %d?\n", num1, num2);
    	do {	
    		scanf(" %d",&answer);
    		if( num1-num2 !=answer )
    		{
    			incorrectMessege();
    			continue;
    		}
    		else if( num1-num2 ==answer )
    		{
    			correctMessege();
    			again();
    		}}while(0==0);
    	} 
    	else if(operation == 3) 
    	{
    	        answer = num1 * num2;
                    printf("How much is %d * %d?\n", num1, num2);
    	while(){	                    /*ignore this i was messing around i'll fix it once i get it down on the first one*/
    		scanf(" %d",&answer);
    		if( num1*num2 !=answer )
    		{
    			incorrectMessege();
    			continue;
    		}
    		else if( num1*num2 ==answer )
    		{
    			correctMessege();
    			again();
    		}};
    	}
    	else if(operation == 4) 
    	{
    		answer = num1 / num2;
                    printf("How much is %d / %d?\n", num1, num2);
    	do {	
    		scanf(" %d",&answer);
    		if( num1/num2 !=answer )
    		{
    			incorrectMessege();
    			continue;
    		}
    		else if( num1/num2 ==answer )
    		{
    			correctMessege();
    			again();
    		}}while(0==0);
    	}
    	return 0;
    }
    
    
    
    
    void correctMessege(void)
    {
    	int select;
    	select = 1 +rand() % 5;
    
    
    switch (select)
    {
    case 0:
    printf( "Very good!");
    break;
    case 1:
    printf( "Excellent!");
    break;
    case 2:
    printf( "Nice work!");
    break;
    case 3:
    printf( "Keep up the good work!");
    break;
    case 4:
    printf( "Your awesome!");
    break;
    } 
    }
    
    
    void incorrectMessege(void)
    {
    	int select;
    	select = 1 +rand() % 5;
    switch (select)
    {
    case 0:
    printf( "No. Please try again.");
    break;
    case 1:
    printf( "Wrong. Try once more.");
    break;
    case 2:
    printf( "Don't give up!");
    break;
    case 3:
    printf( "No. Keep trying.");
    break;
    case 4:
    printf( "I believe in you!");
    break;
    }
    }
    
    
    void again(void)              /*string is  here*/
    {
    	char pop;
    printf("Another one? (y/n):\n");
    scanf("%c",&pop);
    	if(pop=='y')
    	{
    	continue;
    	}
    	else if(pop=='n')
    	 {
    	 break;
    	 }
    	 }

  2. #2
    Registered User
    Join Date
    Jan 2012
    Location
    Chennai, Tamil nadu, India
    Posts
    30
    For making infinite loops, give a non-zero value inside that while() statement. example, while(1)... etc...

    And, inside that void again() function, you're using continue and break statements which should be used only inside loops..

    You better replace those statements with a return returning boolean values 1 or 0.
    And give these statements "If it is 1 continue.. Else, break..." to the place where again function is called..

  3. #3
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Here's what I get when I compile the program you posted:
    Code:
    $ make foo
    gcc -g -Wall -std=c99 -o foo foo.c -lpthread -lm -lpq
    foo.c: In function ‘main’:
    foo.c:66:15: error: expected expression before ‘)’ token
    foo.c: In function ‘again’:
    foo.c:161:9: error: continue statement not within a loop
    foo.c:165:9: error: break statement not within loop or switch
    make: *** [foo] Error 1
    Line 66: This is a while loop with no condition, but you note that you're aware of the problem and will fix it later.
    Lines 161 & 165: The continue keyword only works in a loop, and the break keyword in a loop or a case statement. You have them in an if/else.

    Also, you use
    Code:
    do {
    ...
    } while (0==0);
    for an infinite loop. It's far more common, and easier to read, if you just put a 1 in there instead of 0==0, like
    Code:
    do {
    ...
    } while (1);
    But you don't need the infinite loops with continue/break statements. You can just use the loop condition:
    Code:
    do {
    ...
    } while (user gave the wrong answer);
    You will need two do-while loops, one in the other. Though since you use the same loop that checks for the right answer, in each problem type (addition, subtraction, etc), I would move that into a function. My recommendation would be something like the following:
    Code:
    void check_answer(int correct)
    {
        do {
            get the answer from user
            if answer != correct
                print wrong message
            else  // note, no need for else-if, it's either wrong or right
                print correct message
        } while answer != correct
    }
    
    int main(void)
    {
        ...
        do {
            ask a problem
            check_answer(num1 + num2);  // pass in the right answer, num1 - num2 for subtraction, etc
        } while (again())  // if again returns 1, meaning the user wants to play again, the loop will repeat
        ...
    }
    
    // this function returns 1 if the user wants to play again, or 0 otherwise
    // so it can be used in loop condition
    int again(void)
    {
        ask user if they want to play again
        read response
        if (toupper(response) == 'Y')  // that way you handle upper and lower case 'y'
            return 1;
        else
            return 0;
    }
    Remember, you need to be careful about any leftover newlines in your input buffer, just like we talked about in your other thread.

    Last thing, you seem to be a little over-zealous with the "else if" construct. Not every if needs to be followed by an else-if. An else-if is only needed when you have more than two possibilities. In the above example, the answer is either right or wrong, there is no third option, so you only need an if/else, not an if/else-if. If it's wrong, make the user guess again, otherwise, congratulate them. A good example of when to use the else if is your main menu choice, which could be one of 5 different values. I would add a 6th option to quit the program, add that to my if-else, and also add an else at the end to handle errors:
    Code:
    if choice == 1
        add
    else if choice == 2
        subtract
    ...
    else if choice == 5
        smart stuff
    else if choice == 6
        quit
    else  // this covers every other possibility except 1..5
        error

  4. #4
    Matt Conway bobthebullet990's Avatar
    Join Date
    Nov 2005
    Location
    Cambridge
    Posts
    122
    In my opinion:
    Code:
    while (1)
    {
        /* SOME CODE */
    }
    Is far easier to read than
    Code:
    do
    {
    } while(1);
    Many junglists take pride in their belongin to what may be referred to as a globalised drum & bass subculture, as a subculture though, it is not nearly as distinct at gothic or punk!

Popular pages Recent additions subscribe to a feed