Thread: Simple Logical Problem.. HELP!

  1. #1
    Registered User
    Join Date
    Aug 2012
    Location
    Cagayan de Oro, Philippines, Philippines
    Posts
    7

    Simple Logical Problem.. HELP!

    Code:
     case 1: ///////////////EASY MATH
                       printf("EASY\n");
    if (x1==0){
    printf("1 + 6\n");scanf("%d",&a1[0]);}
    if (x1==1){
    printf("2 + 3\n");scanf("%d",&a1[1]);}
    if (x1==2){
    printf("2 + 7\n");scanf("%d",&a1[2]);}
    if (x1==3){
    printf("7 + 4\n");scanf("%d",&a1[3]);}
    if (x1==4){
    printf("6 + 9\n");scanf("%d",&a1[4]);}
    
    
    
    
    
    
    if (x2==0){
    printf("1 + 2\n");scanf("%d",&a2[0]);}
    if (x2==1){
    printf("1 + 3\n");scanf("%d",&a2[1]);}
    if (x2==2){
    printf("1 + 4\n");scanf("%d",&a2[2]);}
    if (x2==3){
    printf("9 + 4\n");scanf("%d",&a2[3]);}
    if (x2==4){
    printf("3 + 7\n");scanf("%d",&a2[4]);}
    
    
    if (x3==0){
    printf("7 - 5\n");scanf("%d",&a3[0]);}
    if (x3==1){
    printf("9 - 6\n");scanf("%d",&a3[1]);}
    if (x3==2){
    printf("3 - 1\n");scanf("%d",&a3[2]);}
    if (x3==3){
    printf("2 - 2\n");scanf("%d",&a3[3]);}
    if (x3==4){
    printf("9 - 2\n");scanf("%d",&a3[4]);}
    
    
    if (x4==0){
    printf("6 * 6\n");scanf("%d",&a4[0]);}
    if (x4==1){
    printf("9 * 2\n");scanf("%d",&a4[1]);}
    if (x4==2){
    printf("2 * 6 \n");scanf("%d",&a4[2]);}
    if (x4==3){
    printf("4 * 8\n");scanf("%d",&a4[3]);}
    if (x4==4){
    printf("7 * 6\n");scanf("%d",&a4[4]);}
    
    
    if (x5==0){
    printf("6 / 6\n");scanf("%d",&a5[0]);}
    if (x5==1){
    printf("9 / 3\n");scanf("%d",&a5[1]);}
    if (x5==2){
    printf("30 / 6 \n");scanf("%d",&a5[2]);}
    if (x5==3){
    printf("100 / 10\n");scanf("%d",&a5[3]);}
    if (x5==4){
    printf("54/ 9\n");scanf("%d",&a5[4]);}
    
    
    //ans EASY MATH
    /////1
    if (a1[0]==7){
    score++;}
    if (a1[1]==5){
    score++;}
    if (a1[2]==9){
    score++;}
    if (a1[3]==11){
    score++;}
    if (a1[4]==15){
    score++;}
    ////2
    if (a2[0]==3){
    score++;}
    if (a2[1]==4){
    score++;}
    if (a2[2]==5){
    score++;}
    if (a2[3]==13){
    score++;}
    if (a2[4]==10){
    score++;}
    ////3
    if (a3[0]==2){
    score++;}
    if (a3[1]==3){
    score++;}
    if (a3[2]==2){
    score++;}
    if (a3[3]==0){
    score++;}
    if (a3[4]==7){
    score++;}
    //////////////4
    if (a4[0]==36){
    score++;}
    if (a4[1]==18){
    score++;}
    if (a4[2]==12){
    score++;}
    if (a4[3]==32){
    score++;}
    if (a4[4]==42){
    score++;}
    ///////5
    if (a5[0]==1){
    score++;}
    if (a5[1]==3){
    score++;}
    if (a5[2]==5){
    score++;}
    if (a5[3]==10){
    score++;}
    if (a5[4]==6){
    score++;}
    
    
    
    
     printf("you got %d out of 5\n",score);
      if (add==0){printf("Study more about Addition\n");}
      if (sub==0){printf("Study more about Subtraction\n");}
      if (mul==0){printf("Study more about Multiplication\n");}
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
                       break;//EASY MATH
    just a piece of the code

    This is a 5 item quiz program...

    the problem is that when you got a perfect score.. it will display you got 6 out 5...which is I cant tell why... please help.. thanks

    note: already set the score to 0.
    Last edited by Jimboynoob; 09-09-2012 at 10:38 PM. Reason: wrong title...

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    To help you debug this, start by formatting your code properly. This way, you will be better able to trace the flow of control and hence figure out why score ends up as 6 instead of 5.
    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

  3. #3
    Registered User
    Join Date
    Aug 2012
    Location
    Cagayan de Oro, Philippines, Philippines
    Posts
    7
    Quote Originally Posted by laserlight View Post
    To help you debug this, start by formatting your code properly. This way, you will be better able to trace the flow of control and hence figure out why score ends up as 6 instead of 5.
    How do you format it properly?

  4. #4
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Something like this:
    Code:
    case 1: ///////////////EASY MATH
        printf("EASY\n");
        if (x1 == 0) {
            printf("1 + 6\n");
            scanf("%d", &a1[0]);
        }
        if (x1 == 1) {
            printf("2 + 3\n");
            scanf("%d", &a1[1]);
        }
        if (x1 == 2) {
            printf("2 + 7\n");
            scanf("%d", &a1[2]);
        }
        if (x1 == 3) {
            printf("7 + 4\n");
            scanf("%d", &a1[3]);
        }
        if (x1 == 4) {
            printf("6 + 9\n");
            scanf("%d", &a1[4]);
        }
    Also, you have too much duplication of code. It isn't outright duplication, but there are repeated structures that could be simplified. For example, a very minimal simplication of the above code might be:
    Code:
    case 1: ///////////////EASY MATH
        printf("EASY\n");
        if (x1 == 0) {
            printf("1 + 6\n");
        } else if (x1 == 1) {
            printf("2 + 3\n");
        } else if (x1 == 2) {
            printf("2 + 7\n");
        } else if (x1 == 3) {
            printf("7 + 4\n");
        } else if (x1 == 4) {
            printf("6 + 9\n");
        }
        if (0 <= x1 && x1 < 5) {
            scanf("%d", &a1[x1]);
        }
    If you placed those strings used in the printf calls in an array, you might even just write:
    Code:
    case 1: ///////////////EASY MATH
        printf("EASY\n");
        if (0 <= x1 && x1 < 5) {
            printf("%s\n", al_texts[x1]);
            scanf("%d", &a1[x1]);
        }
    Then if instead of variables x1, x2, etc, you used an array x:
    Code:
    case 1: ///////////////EASY MATH
        printf("EASY\n");
        for (i = 0; i < 5; ++i) {
            if (0 <= x[i] && x[i] < 5) {
                printf("%s\n", texts[i][x[i]]);
                scanf("%d", &a[i][x[i]]);
            }
        }
    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

  6. #6
    Registered User
    Join Date
    Dec 2011
    Posts
    795
    There's no need to have five different arrays with (at least) five different elements just to store one value in each array. And, checking these arrays can be problematic:

    Code:
    /* let's say that x1 is 0 */
    
    if (x1==0) {
    
        printf("1 + 6\n");
        scanf("%d",&a1[0]);
    }
    
    /* now a1[0] is (hopefully) 7 */
    /* and a1[1], a1[2], ...a1[4] aren't initialized to anything */
    
    
    if (a1[0]==7){
    score++;}
    /* score should be 1 */
    
    
    if (a1[1]==5){
    score++;}
    /* because the array isn't initialized, if a1[1] happens to have a 5, the score will wrongly increase */
    
    if (a3[3]==0){
    score++;}
    /* BUT, even if it is initialized, it won't help when the program checks for zero here */
    If x3 isn't 3, this value will have never been changed, and there's a good chance it's already zero. Hence the comparison evaluates to true, and the score increases without a correct answer.

    Also, I see
    Code:
    score++;
    way too many times. You should figure out a way to check answers in a loop similar to what Laserlight posted.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple program, simple problem
    By KAUFMANN in forum C Programming
    Replies: 5
    Last Post: 02-16-2011, 01:16 PM
  2. simple program, simple error? HELP!
    By colonelhogan44 in forum C Programming
    Replies: 4
    Last Post: 03-21-2009, 11:21 AM
  3. Simple program...simple problem?
    By deadherorising in forum C Programming
    Replies: 2
    Last Post: 03-12-2009, 08:37 PM
  4. Simple program, not so simple problem
    By nolsen in forum C++ Programming
    Replies: 2
    Last Post: 01-18-2008, 10:28 AM
  5. Need help with simple, simple program.
    By LightsOut06 in forum C Programming
    Replies: 5
    Last Post: 09-01-2005, 08:31 PM