Thread: new to programming. can't get this if else statement to work properly. please help!

  1. #1
    Registered User
    Join Date
    Jan 2006
    Posts
    3

    new to programming. can't get this if else statement to work properly. please help!

    The printf statement is not working.

    Code:
    #include <stdio.h>
    
    main()
    
    {
    
    
    int num;  /* the number for the table */
    int answer;   /* the answered number */
    int correctans; /* the correct answer */
    int table_counter; /* counts the number of tables attempted */
    int counter;   /* increments the question! */
    int counter2;  /* increments 1 errors */
    int counter3;  /* increments 2 errors */
    
    table_counter=0; /* sets number of tables attempted to zero */
    counter=2;       /* sets table number to zero */
    counter2=0;      /* sets '1 errors' to zero */
    counter3=0;	     /* sets '2 errors' to zero */
    
    		
    
    		printf("\tWelcome to the Multiplication Tutorial.\n\n");
    
    		do
    		{
    			printf("\t\tPlease select a table: ");
    				scanf("%d",&num);
    					table_counter++; 
    			printf("\n\n\tYou have selected the %d times able.",num);
    
    			do
    			{				
    				printf("\n\tWhat is %d times %d ?: ",num,counter);
    					scanf("%d",&answer);
    					    correctans=num*counter;
    					{
    					if (correctans==answer)
    						{
    							printf("\t\t\t\tCorrect.");
    						}
    						else
    							{
    							if	(correctans!=answer)
    								{
    									printf("\t\t\t\tThat is not correct, please try again.");
    									printf("\n\tWhat is %d times %d ?: ",num,counter);
    										scanf("%d",&answer);
    											correctans=num*counter;
    								}
    								else
    									{	
    									if	(correctans==answer)
    										{
    											printf("\t\t\t\tCorrect.");
    												counter2++;
    										}
    										else 
    											{
    											if  (correctans!=answer)
    												{
    													printf("\t\t\t\tThat is not correct. The answer is %d.",correctans);
    														counter3++;
    												}
    											}
    									}
    							}
    					}
    					counter++;		
    			}
    			while (counter <= 12);
    			counter=2;
    		}
    		while (num > 0);
    						
    return 0;		
    }
    The code compiles, however, the printf staements highlighted in blue won't print.

    Why?

  2. #2
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Yout program basically looks like this
    Code:
    if  (correctans==answer) { // this block if answer is correct
        ..... 
    }
    else { // the next if is not necessary because it's already shure that the answer is not correct
        if ( correctans!=answer) { // this block will be entered 
        
        }
        else { // cannot be entered 
        
        }
    }

  3. #3
    Cached User mako's Avatar
    Join Date
    Dec 2005
    Location
    Germany.Stuttgart
    Posts
    69
    line 37 and 68 have pointless {}'s

    apart from that I can't help but wonder why you don't use a looping function as opose to the if statement for the whole determining whether an answer is correct or not. You could just write it much shorter...

    you have code like this:

    Code:
    if(TRUE)
    {
    }
    else
    {
       if(FALSE)
       {
       }
       else
       {
        }
    }
    this could be simplified to

    Code:
    if(TRUE)
    {}
    else
    {}   //code to execute if false
    and if you wanna use another function just say:

    [code]
    Code:
    if(Statement1==TRUE)
    {}
    else
    {
       if(Statement2==TRUE)
       {}
       else
       {}
    }
    also, I don't understand what you mean with 1errors and 2errors. Please explain what you want the program to do. I gather you take in a number and then check the answers to that number * n adding n untill 10. then asking the next table.

  4. #4
    Registered User
    Join Date
    Jan 2006
    Posts
    3
    This code is still a work in progress. The counters are for a table to display overall information. This table will be entered once the loop works properly!

  5. #5
    Cached User mako's Avatar
    Join Date
    Dec 2005
    Location
    Germany.Stuttgart
    Posts
    69
    Code:
    scanf("%d",&answer);  //say we enter 2
    					    correctans=num*counter;  //but the correctanswer is 1
    					{
    					if (correctans==answer)  // 1 != 2 so answer is FALSE
    						{
    							printf("\t\t\t\tCorrect.");
    						}
    						else  //so we go into the else loop
    							{
    							if	(correctans!=answer)  /* if (1!=2) execute this code. Our answer is 2 so this if loop is entered*/
    								{
    									printf("\t\t\t\tThat is not correct, please try again.");
    									printf("\n\tWhat is %d times %d ?: ",num,counter);
    										scanf("%d",&answer);
    											correctans=num*counter;
    								}
    								else /* this else would need a correct answer (1) to be entered, but we only got this far by entering a wrong number*/
    									{	
    									if	(correctans==answer)
    										{
    this should work:

    Code:
    do
    			{				
    					printf("\n\tWhat is %d times %d ?: ",num,counter);
    					scanf("%d",&answer);
    					correctans=num*counter;
    					
    					if (correctans==answer)
    						{
    							printf("\t\t\t\tCorrect.");
    						}
    						else
    						{
    							printf("\t\t\t\tThat is not correct, please try again.");
    							printf("\n\tWhat is %d times %d ?: ",num,counter);
    							scanf("%d",&answer);
    											
    							if	(correctans==answer)
    							{
    								printf("\t\t\t\tCorrect.");
    								counter2++;
    							}
    							else
    							{
    								printf("\t\t\t\tThat is not correct. The answer is %d.",correctans);
    								counter3++;
    							}
    						}					
    					counter++;		
    			}
    			while (counter <= 12);

  6. #6
    Cached User mako's Avatar
    Join Date
    Dec 2005
    Location
    Germany.Stuttgart
    Posts
    69
    or you could replace the do while counter<12 with:

    Code:
    			//int try2;
    			
    			for(counter=2; counter<=12; counter++)
    			{
    				
    				
    					printf("\n\tWhat is %d times %d ?: ",num,counter);
    					scanf("%d",&answer);
    					correctans=num*counter;
    					
    					try2=0;
    					
    					while(correctans!=answer&&try2==0)
    					{					
    						printf("\t\t\t\tThat is not correct, please try again.");
    						printf("\n\tWhat is %d times %d ?: ",num,counter);
    						scanf("%d",&answer);
    						try2++;
    					}
    					
    					if(correctans==answer)
    					{
    						printf("\t\t\t\tCorrect.");
    						if(try2==1) counter2++;
    					}
    					else
    					{
    						printf("\t\t\t\tThat is not correct. The answer is %d.",correctans);
    						counter3++;
    					}
    			}

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How properly inherit from template?
    By 6tr6tr in forum C++ Programming
    Replies: 118
    Last Post: 04-25-2008, 04:30 AM
  2. Program doesnt work properly.
    By +Azazel+ in forum C Programming
    Replies: 4
    Last Post: 10-12-2007, 06:57 AM
  3. Usefulness of the "else if" statement
    By gn17 in forum C Programming
    Replies: 7
    Last Post: 08-12-2007, 05:19 AM
  4. My monitor doesn't work with Ubuntu Linux.
    By indigo0086 in forum Tech Board
    Replies: 1
    Last Post: 07-12-2007, 10:59 AM
  5. loops in a main statement....
    By xero18 in forum C Programming
    Replies: 8
    Last Post: 10-30-2005, 09:36 PM