Hi! Please help me debug the following code in C++. Below, you may find the code and the identified error. Please Ide

Business, Finance, Economics, Accounting, Operations Management, Computer Science, Electrical Engineering, Mechanical Engineering, Civil Engineering, Chemical Engineering, Algebra, Precalculus, Statistics and Probabilty, Advanced Math, Physics, Chemistry, Biology, Nursing, Psychology, Certifications, Tests, Prep, and more.
Post Reply
answerhappygod
Site Admin
Posts: 899604
Joined: Mon Aug 02, 2021 8:13 am

Hi! Please help me debug the following code in C++. Below, you may find the code and the identified error. Please Ide

Post by answerhappygod »

Hi! Please help me debug the following code in C++. Below, you may find the code and the identified error. Please Identify what is incorrect, correct the code, and provide comments to explain. Thanks!
1 #include <iostream> // lets me work with cout 2 #include <fstream> // lets me work with files 3 #include <sstream> // lets me work with strings 4 #include <iomanip> // lets me work with input and output 5 #include <string> // lets me create strings 6 7 using namespace std; // lets me use c++ throughout entire program 8 9 void calculate( float &p, float &q, float &r, float a[], float b[], float c[], int x, int y, int z) // void function that calculates overall grade 10 { 11 int st1=0, st2=0, st3=0; // defines 3 variables as integers 12 for(int i=0; i<x; i++) // for loop from a to x value 13 sti += a; // adds a and sti values, sets it to sti 14 for(int i=0; i<y; i++) // for loop from oto y value 15 st2 += b; // adds b and st2 values, sets it to sta 16 for(int i=0; i<z; i++) // for loop from a to z value 17 st3, += c; // adds c[l] and st3 values, sets it to st3 18 19 p = ((float)st1/(x*20))*100.0; // sets p variable to the equation value of sti 20 9 = ((float)st2/(*10))*100.0; // sets q variable to the equation value of st2 21 r = ((float)st3/(x*20))*100.6; // sets r variable to the equation value of 53 22} 23 24 int main() // main function 25 26 float labs [10], quiz[10], assignment (10); // specifies assignment type and point value, as floats 27 float practice, midterms, finals; // defines practice, midterms $ and finals as floats 28 29 itstream f("marks.txt"); // reads marks.txt file 30 char c; // storesc 31 float marks; // defines marks float 32 int x=0, y=0, z=0; / defines integer values

49 34 while(f >> C >> marks) // while loop to determine what type of work (assignment? quiz? etc) 35 v { 36 if(c = 'L') // if c value is L 37 { 38 labs[x] = marks; // assign marks to labs 39 X++; // increment operator, X = x+1 40 } 41 else if(0 == 'Q') // if c value is 0, 42 { 43 quiz[y] = marks; // assign marks to quiz 44 y++; // increment operator, y = y+1 45 } 46 else if(c = 'A') // if c value is A 47 { 48 assignment [2] = marks; // assign marks to assignment Z++; // increment operator, z = 2+1 50 } 51 else if (c = 'P') // if c value is P 52 practice marks; // assign marks to practice 53 else // otherwise 54 midterms = marks; // assign marks to midterms 55 } 56 57 f.close(); // close file 58 labs[x] practice/5.0; // sets labs[x] to the value of practice divided by 59 60 float avgi, avg2, avg3; // defines avi, av2 and av as loats 61 calculatel avgi, avg2, avg3, labs, quiz, assignment,x,y,z); calculates percentage 62 63 cout << "Enter a grade for the final: " << endl; W prints message to console 64 cin >> finals; // puts in user input as finals 65 66 cout << "Assesment type: average" << endl; // prints message to console .

66 cout << "Assesment type: average" << endl; // prints message to console 67 68 cout << "Labs: << avgi << endl; // prints message to console 69 cout << "Quizzes : < avg2 << endl; // prints message to console 70 cout << "Assignments : << avg3 << endl; // prints message to console 71 cout << "Mid : << midterms << endl; // prints message to console 72 cout << "Final : " << finals << endl; // prints message to console 73 74 avg1 = avg1*.10; // defines avi and sets it equal to average 1 value based on the grade scale 75 avg2 = avg2*.05; // defines avg2 and sets it equal to average 2 value based on the grade scale 76 avg3 = avg3*.10; // defines avg3 and sets it equal to average 3 value based on the grade scale 77 midterms = midterms*.35; // defines midterms and sets it equal to midterms value based on the grade scale 78 finals = finals*.40; // defines finals and sets it equal to finals value based on the grade scale 79 80 float current grade avgi + avg2 + avg3 + midterms + finals; defines current grade as a float 81 cout << Current grade: " << current_grade << endl; / prints message to console 82 return of lets computer know everything ran smoothly 843 85 86 83

LAST RUN on 5/9/2022. 10:12:17 PM X Check 1 failed Output: Enter a grade for the final: Assesment type: average Labs : -nan Quizzes: -nan Assignments : -nan Mid : 0 Final : 100 Current grade : -nan Expected: Enter a grade for the final Labs: 87 -Quizzes: 91 Assignments: 96 Midterm exam: 94 Final exam: 100 Current grade: 95.75
Join a community of subject matter experts. Register for FREE to view solutions, replies, and use search function. Request answer by replying!
Post Reply