lundi 27 avril 2020

When I receive an average of 60 or 70 the following if statement outputs 'D' and 'F' incorrectly

This is a program to randomly generate values and let the user practice multipliv]cation with those values. If incorrect its stored as array 0 and if correct its stored as array 1. The program then gives you an average and its corresponding letter grade... the letter grade doesn't work for the first 2 if statement however.

In the letterGrade Function the first two if statements are off at the endpoints 60 and 70 ex: 60 will output 'F' and 70 will output 'C'

        #include <iostream>
        #include<time.h>
        #include<cstdlib>
        #include<stdlib.h>
        #include<ctime>
        using namespace std;
//global variables
        int num;
        int numValues=0;
        int a,b,c,d;
        int ans;
        int answer;
        double total=0;
        double avg=0;
        int choice;
//functions
        void menu();
        void hard(int math[], int num);
        void easy(int math[], int num);
        void print(int math[], int num);
        double average(int math[], int num);
        double Average2(int math[], int num);
        void letterGrade(int math[], int num);
        int main()
        {
        srand(time(NULL));//random generator
        int math[numValues];
        cout << "Math Program" << endl;
        cout<<"practice multiplying one or two digit numbers"<<endl;
//menu and user choice
        menu();
        cin>>choice;
        while(choice>0 && choice<3)
        {
        switch (choice)
        {
        case 1://easy math 
            {
                total=0;
                 cout<<"enter the amount of practice trials you wish to complete"<<endl;
                cin>>numValues;
                easy(math,numValues);
                print(math,numValues);
                average(math,numValues);
                avg=average(math,numValues);
                cout<<endl;
                cout<<"Average: "<<avg*100;
                cout<<" ";
                letterGrade(math,numValues);
                break;

            }
            case 2://hard math
            {
                 total=0;
                 cout<<"enter the amount of practice trials you wish to complete"<<endl;
                cin>>numValues;
                hard(math,numValues);
                print(math,numValues);
                average(math,numValues);
                avg=average(math,numValues);
                cout<<endl;
                cout<<"Average: "<<avg*100;
                cout<<" ";
                letterGrade(math,numValues);
                cout<<endl;
                break;
              }
             }
           menu();
           cin>>choice;
             }

            if(choice==3)
           {
           cout<<"goodbye"<<endl;
           }
           return 0;
           }



           void easy(int math[], int num)//generates random A and Random B then user enters answer and it compares it with ab, array then is 0 if incorrect and 1 if correct
          {
          for(int i=0; i<num; i++)
          {
          a=rand()%10+1;
          b=rand()%10+1;
          cout<<"multiply "<<a<<"*"<<b<<""<<endl;
         answer=a*b;
         cin>>ans;
         if(ans==answer)
         {
            math[i]=1;
         }
        else
        {
            math[i]=0;
        }
        }

        }
        void hard(int math[], int num)//similar to above with numbers between 10 and 20 now
        {
        for(int i=0; i<num; i++)
        {
        c=rand()%11+10;
        d=rand()%11+10;
        cout<<"multiply "<<c<<"*"<<d<<""<<endl;
        answer=c*d;
        cin>>ans;
        if(ans==answer)
        {
            math[i]=1;
        }
        else
        {
            math[i]=0;
        }
        }
        }
        void print(int math[], int num)// prints your score as a 1 or 0
        {
       for(int i=0; i<num; i++)
        {
        cout<<math[i]<<" | ";
        total+=math[i];
       }
       }
       double average(int math[], int num)//avergaes your score
       {
       avg=total/numValues;
       return avg;
       }
       void letterGrade(int math[], int num)//displays your score as a letter grade
       {
      int letter;
      letter=avg*100;
      if(letter>=0 && letter<60)
      {
        cout<<"F"<<endl;
      }
      if(letter>=60 && letter<70)
      {
        cout<<"D"<<endl;
      }
      if(letter>=70 && letter<80)
      {
        cout<<"C"<<endl;
      }
      if(letter>=80 && letter<90)
      {
        cout<<"B"<<endl;
      }
      if(letter>=90 && letter<=100)
      {
        cout<<"A"<<endl;
      }
      }


      void menu()
     {
      cout<<"1. Easy math"<<endl;
       cout<<"2. Hard math"<<endl;
       cout<<"3. Exit"<<endl;
       }

Aucun commentaire:

Enregistrer un commentaire