Write a C++ code to
–Read two test scores from input_file.txt
–compute the average of the scores and
–write the output to output_file.txt.
–Display the result up to two decimal places.
•Your input text file titled input_file.txt must contain below lines:
–95.75 89.50
•Your output should read
–Your average test score is:
I have tried to successfully complete this code but it is not executing. What am I doing wrong?
Please use code tags (the button with <> on the left). It will make the code more readable, and it will show the line numbers, so it's easier to comment on your code.
Also, the program will likely not compile, so it would be useful to paste all error messages that you get.
I suggest you approach this program one step at a time, starting from scratch.
Step one, read a and b from the file. You define them in your program instead, but that's not what you were asked. The line double a = 95.75, b = 89.50, sum, average, string str1 = Your average test score is; does not make sense. You can declare variables only of one type, so the line should be double a , b , sum, average; The string str1 will cause errors. Even if you put it on a separate line, you must use quotes: string str1 = "Your average test score is ";
The rest of the code does not make too much sense. You open infile and that's it. You should read a and be from there, and then you must close infile. You can google examples of reading numbers with ifstream.
Once you have those, you can print them out to cout, just to make sure you got them right.
Step 2 it would be to calculate average. If you have a and b, average=(a+b)*0.5; You don't really need the sum for this simple example. In your current code you declare the variables, but you did not assign any value to them.
Step 3 you open outfile, you output str1 and average to it, and you close it. DONE
What is fout? You declare it as an output file stream, but you don't open it. And you just print the characters "a" and "b", not the values of variables a or b