Thread: Why this is happening

  1. #1
    Registered User
    Join Date
    May 2019
    Posts
    47

    Exclamation Why this is happening

    hello friends,

    In this quiz code all the quiz score is counting but everytime

    it shows the msg "you did ok" even after answering all answers correct

    Code:
    #include <stdio.h>#include <string.h>
    
    
    
    
    
    
    int main(){
    int rl,count,i;
    int score,score1,score2,score3,score4,score5,score6;
    count = 0;
         for(i=0;i<=6;i++){
         system("cls");
         rl = i;
    
    
         switch(rl){
       case 1:
        printf("\n\nWhich of the following is Palindrome number?");
        printf("\n\nA.42042\t\tB.101010\n\nC.23232\t\tD.01234");
        if(toupper(getch())=='C'){
    
    
           printf("\nCorrect!!!");
           count++;
           score1 = 5;
           getch();
           break;
    
    
        } else {
    
    
          printf("\nWrong!!! .The correct answer is C.23232");
          score1 =0;
          getch();
          break;
        }
    
    
         case 2:
        printf("\n\nThe Country with highest environmental performance index is...?");
        printf("\n\nA.France\t\tB.Denmark\n\nC.Switzerland\t\tD.Finland");
        if(toupper(getch())=='C'){
    
    
           printf("\nCorrect!!!");
           count++;
           score2 = 5;
           getch();
           break;
    
    
        } else {
    
    
          printf("\nWrong!!! .The correct answer is C.Switzerland");
          score2 = 0;
          getch();
          break;
        }
    
    
         case 3:
        printf("\n\nWhich animal laughs like human being?");
        printf("\n\nA.Polar Bear\t\tB.Hyena\n\nC.Donkey\t\tD.Chimpanzee");
        if(toupper(getch())=='B'){
    
    
           printf("\nCorrect!!!");
           count++;
           score3 = 5;
           getch();
           break;
    
    
        } else {
    
    
          printf("\nWrong!!! .The correct answer is B.Hyena");
          score3 = 0;
          getch();
          break;
        }
    
    
    
    
        case 4:
        printf("\n\nWho was awarded the youngest player award in Fifa World Cup 2006?");
        printf("\n\nA.Wayne Rooney\t\tB.Lucas Podolski\n\nC.Lionel Messi\t\tD.Christiano Ronaldo");
        if(toupper(getch())=='B'){
    
    
           printf("\nCorrect!!!");
           score4 = 5;
           count++;
           getch();
           break;
    
    
        } else {
    
    
          printf("\nWrong!!! .The correct answer is B.Lucas Podolski");
          score4 = 0;
          getch();
          break;
        }
    
    
    
    
        case 5:
        printf("\n\nWhich is the third highest mountain in the world?");
        printf("\n\nA.Mt. K2\t\tB.Mt. Kanchanjungha\n\nC.Mt. Makalu\t\tD.Mt. Kilimanjaro");
        if(toupper(getch())=='B'){
    
    
           printf("\nCorrect!!!");
           score5 = 5;
           count++;
           getch();
           break;
    
    
        } else {
    
    
          printf("\nWrong!!! .The correct answer is B.Mt. Kanchanjungha");
          score5 = 0;
          getch();
          break;
        }
    
    
    
    
        case 6:
        printf("\n\nWhat is the group of frogs known as?");
        printf("\n\nA.A Traffic\t\tB.A Toddler\n\nC.A Police\t\tD.An Army");
        if(toupper(getch())=='D'){
    
    
           printf("\nCorrect!!!");
           score6 = 5;
           count++;
           getch();
           break;
    
    
        } else {
    
    
          printf("\nWrong!!! .The correct answer is D.An Army");
          score6 = 0;
          getch();
          break;
        }
    
    
    
    
    
    
         }//switch end
    
    
    
    
         }//for loop end
    
    
         if(count>=2){
    
    
            printf("\nyou did ok");
            score = score1+score2+score3+score4+score5+score6;
            printf("\nyour total score is %d",score);
         }else if (count>=4){
    
    
            printf("\nyou did good");
            score = score1+score2+score3+score4+score5+score6;
            printf("\nyour total score is %d",score);
    
    
         }else if (count==6){
    
    
            printf("you are genius");
            score = score1+score2+score3+score4+score5+score6;
            printf("your total score is %d",score);
    
    
         }else{
    
    
            printf("sorry you need to practice");
    
    
         }
    
    
    
    
    
    
    return 0;
    
    
    }

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Please list all numbers greater than 4 that is not greater than 2.

    Hint: If using > in this type of if/else statement start with the highest condition check first.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  3. #3
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    Quote Originally Posted by stahta01 View Post
    Please list all numbers greater than 4 that is not greater than 2.
    Code:
    if(count>=2) { 
    ...
    } else if (count>=4) {
    ... 
    }

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Break up your main functions into smaller functions that each does one thing and does it well.

    DRY principle: Don't Repeat Yourself. If you do the same thing in multiple places, try to pull it out to do it in one place. See if you can use loops to reduce duplication, if not typically you can write a function or two.

    Generally, you only need one blank line to separate logical sections of code. At top level you might use two or even three blank lines, but four or more blank lines pretty much always merely make the code harder to read.

    If you are numbering your variables, consider using an array.

    You're indenting your code, which is good, but for indentation to be useful it must be consistent, otherwise it becomes just extra whitespace rather than a guide to the code structure.

    Note that getch is non-standard.
    Last edited by laserlight; 07-16-2019 at 10:26 PM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    May 2019
    Posts
    47
    this actually worked as stahta said to keep bigger condition first

    Code:
    #include <stdio.h>#include <string.h>
    
    
    
    
    
    
    int main(){
    int rl,count,i;
    int score,score1,score2,score3,score4,score5,score6;
    count = 0;
    quiz:
         for(i=0;i<=6;i++){
         system("cls");
         rl = i;
    
    
         switch(rl){
       case 1:
        printf("\n\nWhich of the following is Palindrome number?");
        printf("\n\nA.42042\t\tB.101010\n\nC.23232\t\tD.01234");
        if(toupper(getch())=='C'){
    
    
           printf("\nCorrect!!!");
           count++;
           score1 = 5;
           getch();
           break;
    
    
        } else {
    
    
          printf("\nWrong!!! .The correct answer is C.23232");
          score1 =0;
          getch();
          break;
        }
    
    
         case 2:
        printf("\n\nThe Country with highest environmental performance index is...?");
        printf("\n\nA.France\t\tB.Denmark\n\nC.Switzerland\t\tD.Finland");
        if(toupper(getch())=='C'){
    
    
           printf("\nCorrect!!!");
           count++;
           score2 = 5;
           getch();
           break;
    
    
        } else {
    
    
          printf("\nWrong!!! .The correct answer is C.Switzerland");
          score2 = 0;
          getch();
          break;
        }
    
    
         case 3:
        printf("\n\nWhich animal laughs like human being?");
        printf("\n\nA.Polar Bear\t\tB.Hyena\n\nC.Donkey\t\tD.Chimpanzee");
        if(toupper(getch())=='B'){
    
    
           printf("\nCorrect!!!");
           count++;
           score3 = 5;
           getch();
           break;
    
    
        } else {
    
    
          printf("\nWrong!!! .The correct answer is B.Hyena");
          score3 = 0;
          getch();
          break;
        }
    
    
    
    
        case 4:
        printf("\n\nWho was awarded the youngest player award in Fifa World Cup 2006?");
        printf("\n\nA.Wayne Rooney\t\tB.Lucas Podolski\n\nC.Lionel Messi\t\tD.Christiano Ronaldo");
        if(toupper(getch())=='B'){
    
    
           printf("\nCorrect!!!");
           score4 = 5;
           count++;
           getch();
           break;
    
    
        } else {
    
    
          printf("\nWrong!!! .The correct answer is B.Lucas Podolski");
          score4 = 0;
          getch();
          break;
        }
    
    
    
    
        case 5:
        printf("\n\nWhich is the third highest mountain in the world?");
        printf("\n\nA.Mt. K2\t\tB.Mt. Kanchanjungha\n\nC.Mt. Makalu\t\tD.Mt. Kilimanjaro");
        if(toupper(getch())=='B'){
    
    
           printf("\nCorrect!!!");
           score5 = 5;
           count++;
           getch();
           break;
    
    
        } else {
    
    
          printf("\nWrong!!! .The correct answer is B.Mt. Kanchanjungha");
          score5 = 0;
          getch();
          break;
        }
    
    
    
    
        case 6:
        printf("\n\nWhat is the group of frogs known as?");
        printf("\n\nA.A Traffic\t\tB.A Toddler\n\nC.A Police\t\tD.An Army");
        if(toupper(getch())=='D'){
    
    
           printf("\nCorrect!!!");
           score6 = 5;
           count++;
           getch();
           break;
    
    
        } else {
    
    
          printf("\nWrong!!! .The correct answer is D.An Army");
          score6 = 0;
          getch();
          break;
        }
    
    
    
    
    
    
         }//switch end
    
    
    
    
         }//for loop end
    
    
         if(count==6){
    
    
            printf("you are genius");
            score = score1+score2+score3+score4+score5+score6;
            printf("your total score is %d",score);
         }else if (count>=4){
    
    
            printf("\nyou did good");
            score = score1+score2+score3+score4+score5+score6;
            printf("\nyour total score is %d",score);
    
    
         }else if (count>=2){
    
    
            printf("\nyou did ok");
            score = score1+score2+score3+score4+score5+score6;
            printf("\nyour total score is %d",score);
    
    
         }else{
    
    
            printf("\nsorry you need to practice");
            printf("\npress any key to restart");
            getch();
            goto quiz;
    
    
         }
    
    
    
    
    
    
    return 0;
    
    
    }

  6. #6
    Registered User
    Join Date
    May 2019
    Posts
    47
    @lasrelight ya i know arrays would be the way to go for tht but i am still new in coding so numbered those variables

    i will try to keep other points in mind

    getch() is not standard? then what to use in place of tht?
    Last edited by sash_007; 07-17-2019 at 11:27 PM.

  7. #7
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    Quote Originally Posted by sash_007 View Post
    getch() is not standard? then what to use in place of tht?
    By default, input is line-buffered in almost all systems. There is no standard way to get unbuffered input from the user. What you could do instead of asking for any key to be pressed is to ask for enter to be pressed( be careful with stray newlines though ).
    Devoted my life to programming...

  8. #8
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    Quote Originally Posted by sash_007 View Post
    @lasrelight ya i know arrays would be the way to go for tht but i am still new in coding so numbered those variables

    i will try to keep other points in mind

    getch() is not standard? then what to use in place of tht?
    It's better to ask the user what they want to do now.


    This should get your started - Basically loop until the user enters 'r' to reset, or q for quit

    Code:
        printf("\nsorry you need to practice");
    
        char userInput[BUFSIZ] = {0};
    
        do
        {
            printf("\nWould you like to (r)estart or (q)uit?: ");
            fflush(stdout);
    
            fgets(userInput, BUFSIZ, stdin);
        }
        while (!(userInput[1] == '\n' && ((userInput[0] == 'r') || (userInput[0] == 'q'))));
    Fact - Beethoven wrote his first symphony in C

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How come this is happening?
    By Angermanaged in forum C++ Programming
    Replies: 3
    Last Post: 03-21-2009, 07:35 PM
  2. What is happening here?
    By kermit in forum C Programming
    Replies: 4
    Last Post: 02-01-2009, 11:59 AM
  3. What is happening here PLZ help me
    By jayee_spicyguy in forum C Programming
    Replies: 1
    Last Post: 11-15-2008, 06:47 AM
  4. Why does this keep happening?
    By darknite135 in forum C++ Programming
    Replies: 5
    Last Post: 12-23-2007, 12:20 PM
  5. This can't be happening
    By Silvercord in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 03-28-2003, 04:00 PM

Tags for this Thread