why wont these perecentages come out?

This is a discussion on why wont these perecentages come out? within the C++ Programming forums, part of the General Programming Boards category; this is a program that "Grades" a test (user inputs key and student answers and it's supposed to give a ...

  1. #1
    Registered User
    Join Date
    Mar 2003
    Posts
    7

    why wont these perecentages come out?

    this is a program that "Grades" a test (user inputs key and student answers and it's supposed to give a % of the amount right and wrong for the entire class). for some reason, this code isnt working (pay attention to pcntwrong, pcntcorrect...the numbers arent coming out correctly)

    #include <iostream.h>
    #include <conio.h>
    #include <stdlib.h>

    const int size=16;
    void main()
    {

    char answers[size];
    char quiz[size];
    int classize;

    cout << " Enter the class size: " << endl;
    cin >> classize;
    for (int x=0; x<size-1; x++)
    {
    cout << "Enter answer key for # " << (x+1) << endl;
    cin >> answers[x];
    }

    for (int x=0; x<size-1; x++)
    cout << " Question # " << (x+1) << " = " << answers[x] << endl;

    cout << endl << endl << "Press any key to continue... " << endl;


    int cnt=0;
    int correct=0;
    int wrong=0;
    float pcntcorrect = 0;
    float pcntwrong = 0;
    while (cnt < classize)
    {
    for (int x=0; x<size-1; x++)
    {
    cout << " Question # " << (x+1) << " is T or F? " << endl;
    cin >> quiz[x];
    }
    cout << endl << endl << "Press any key to continue...."<< endl;



    for (int x=0; x<size-1; x++)
    cout << " Question # " << (x+1) << " is: " << endl;

    for (int x=0; x<size-1; x++)
    if ( quiz[x] == answers[x])
    correct++;
    else
    wrong++;

    pcntcorrect = (correct / wrong) * 100;
    pcntwrong = 100 - pcntcorrect;

    cout << "The # correct: " << pcntcorrect << endl;
    cout << "The # wrong : " << pcntwrong << endl;

    cnt++;
    }


    system("pause");
    }

  2. #2
    Registered User
    Join Date
    Jan 2003
    Posts
    16
    I see a few problems with your code. There are no code tags, "void main()" is incorrect, as well as include statements.

    Anyway, to answer you question on the percentage, you are just calculating it incorrectly. I also added casting because and int divided by an int is just another int not a float.

    I changed:
    Code:
    pcntcorrect = (correct / wrong) * 100;
    to:
    Code:
    pcntcorrect = ((float)correct / (float)(size-1)) * 100;
    I am not an expierienced programmer by any means so take my advice for what it's worth. I'm sure others may have something more to add.

    James

  3. #3
    Disturbed Boy gustavosserra's Avatar
    Join Date
    Apr 2003
    Posts
    244
    use code tags!!!!!!!
    in red the things that I modified... Hope that helps
    Code:
    #include <iostream.h>
    #include <conio.h>
    #include <stdlib.h>
    
    const int size=16;
    void main()
    {
    
    char answers[size];
    char quiz[size];
    int classize;
    
    cout << " Enter the class size: " << endl;
    cin >> classize;
    for (int x=0; x<size; x++) 
    {
       cout << "Enter answer key for # " << (x+1) << endl;
       cin >> answers[x];
    }
    
    for (int x=0;  x<size; x++)
    cout << " Question # " << (x+1) << " = " << answers[x] << endl;
    
    cout << endl << endl << "Press any key to continue... " << endl;
    
    
    int cnt=0;
    int correct=0;
    int wrong=0;
    float pcntcorrect = 0;
    float pcntwrong = 0;
    while (cnt < classize)
    {
       for (int x=0;  x<size; x++)
       {
          cout << " Question # " << (x+1) << " is T or F? " << endl;
          cin >> quiz[x];
       }
       cout << endl << endl << "Press any key to continue...."<< endl;
    
    
    
       for (int x=0;  x<size; x++)
          cout << " Question # " << (x+1) << " is: " << endl;
       for (int x=0; x<size-1; x++){
          if ( quiz[x] == answers[x])
             correct++;
          else
             wrong++;
          }
    
       pcntcorrect = (correct / wrong) * 100;
       pcntwrong = 100 - pcntcorrect;
       cout << "The # correct: " << pcntcorrect << endl;
       cout << "The # wrong : " << pcntwrong << endl;
    
       cnt++;
    }
    
    
       system("pause");
    }
    Nothing more to tell about me...
    Happy day =)

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21