Question

ASU CSE 100 Lab #7 Due date/Time: Friday, Oct. 13th, 2017 at 5:30pm What this Lab Is A Learn to create and use istream object to read data from a text file Learn to create and use ofstream object to write program output into a text file Learn to use a while loop to read a text file line by line · Guidelines for All Labs/ ou will be graded on this .Give identifiers semantic meaning and make them casy to read (examples numStudents, grossPay, etc). . Keep identifiers to a reasonably short length. Use upper case for constants. Use title case (first letter is upper case) for classes. Use lower case with uppercase word separators for all other identifiers (variables, methods, objects) Use tabs or spaces to indent code within blocks (code surrounded by braces). This includes classes, methods, and code associated with ifs, switches and loops. Be consistent with the number of spaces or tabs that you use to indent. . Use white space to make your program more . Use comments properly before or after the ending brace of classes, methods, and blocks to identify to which block it belongs. . Lab Description For this lab, you are given a text file which contains several students four test scores, see the following partial file for sample input data: Mouse Mickey 94.2 88.1 90 .4 82 87.479 83.5 Duck Donald Basically each line represents one students test scores in the following format: lastName EirstNane lastName and firsName are seperated by one empty space firstName to testIScore, and testIScore to test2Score, etc. are seperated by one tab t)You are required to write a program to read in data from the file and compute each students average and save it testiscor test 2Score testascore inside a text file called testAverage txt in the following format. CSE100 Report Name Mickey Nouse Donald Duck Test Average 90.43 82.98 Note: For each students output, theres one empty space between firstName and lasName and one tab t) between lasName and lestAverage. Also, the test average need to be formatted with 2 decimal digits. Similar as what youve done in previous labs, ereate a C++source codclled Lab7cpp, Use the same setup for setting up your class and main method as you did in previous labs and assignments. Be sure to name your file Lab7.cpp (again, no empty spaces and special characters are allowed, folow the naming conventions). For documentation purpose, at the beginning of each programming lablassignment, you must have a comment block with the following information inside FILENAME: Lab7.cpp AUTHOR: your name ASU IDt your 10 digits ASU ID

I wrote some code and it builds but everytime I run it, It shows the error message in the else statement

0 0
Add a comment Improve this question Transcribed image text
Answer #1

NOTE: I have completed the program for your assignment. Please check and let me know if you have any questions. I will acknowledge back with a response within 24 hours. Thanks for your patience.

Program explanation:
1) The program asks user for an input filename which contains student scores to read
2) The program creates an output file testAverage.txt to store the average of student scores
3) Before processing the input file the program validates if it exists.
4) If input file exists the program reads each line in input file and calculates sum and average of the student scores
5) If input file does not exists it displays error message to the user and exits.


Code:

sh-4.4$ cat lab7.cpp
#include <iostream>   
#include <fstream>
#include <string>   
#include <iomanip>
using namespace std;

int main()
{   
// declaring variables
string firstName, lastName, fileName;   
double test1, test2, test3, test4, average, sum=0.0;
ifstream inputFile;   
ofstream outputFile;

// taking input filename from the user
cout <<"Enter the input filename: ";
cin >> fileName;
// opening the input file given   
inputFile.open(fileName.c_str());   

// creating the output file   
outputFile.open("testAverage.txt");   

// if inputFile given exists
if(inputFile){
// writing the header part to output file   
outputFile << "=====CSE100 Report=====";
outputFile << "\n";   
outputFile << "Name" << "\t" << "Test Average" << "\n";   

// Iterating through each line in input file and taking required fields   
while(inputFile >> firstName >> lastName >> test1 >> test2 >> test3 >> test4){
// initialize sum and average on each line
sum = average = 0.0;
// summing the scores   
sum += test1 + test2 + test3 + test4;   
// calculating the average
average = sum / 4.0;
// writing the average and other fields to output fields
outputFile << firstName << " " << lastName << "\t" << fixed << setprecision(2) << average << endl;
}   

}   
// if inputFile does not exists   
else{   
cout << "\n Error opening the file";
}   


return 0;   
}   


Code output screenshot:

Add a comment
Know the answer?
Add Answer to:
I wrote some code and it builds but everytime I run it, It shows the error...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • What this Lab Is About: Given a UML diagram, learn to design a class Learn how...

    What this Lab Is About: Given a UML diagram, learn to design a class Learn how to define constructor, accessor, mutator and toStringOmethods, etc Learn how to create an object and call an instance method. Coding Guidelines for All ments You will be graded on this Give identifiers semantic meaning and make them easy to read (examples numStudents, grossPay, etc) Keep identifiers to a reasonably short length. Use upper case for constants. Use title case (first letter is u case)...

  • Lab #7 CSE110 - Arizona State University Topics • Basic arrays Coding Guidelines • Give identifiers...

    Lab #7 CSE110 - Arizona State University Topics • Basic arrays Coding Guidelines • Give identifiers semantic meaning and make them easy to read (examples numStudents, grossPay, etc). • Keep identifiers to a reasonably short length. • Use upper case for constants. Use title case (first letter is upper case) for classes. Use lower case with uppercase word separators for all other identifiers (variables, methods, objects). • Use tabs or spaces to indent code within blocks (code surrounded by braces)....

  • please answer correctly and follow the code structure given [JavaFX/ Exception handing and text I/O] 1....

    please answer correctly and follow the code structure given [JavaFX/ Exception handing and text I/O] 1. Write a program for the following. NOTE that some of these steps are not dependent on each other. Using methods is mandatory. Make sure to use methods where it makes sense. a. Ask the user for a series of integers entered from the keyboard. Use a sentinel value such as 999 to end the input process. If the entered values are not integers, throw...

  • Please complete the following programming with clear explanations. Thanks! Homework 1 – Programming with Java: What...

    Please complete the following programming with clear explanations. Thanks! Homework 1 – Programming with Java: What This Assignment Is About? Classes (methods and attributes) • Objects Arrays of Primitive Values Arrays of Objects Recursion for and if Statements Selection Sort    Use the following Guidelines: Give identifiers semantic meaning and make them easy to read (examples numStudents, grossPay, etc.) Use upper case for constants. • Use title case (first letter is upper case) for classes. Use lower case with uppercase...

  • For this lab assignment, you will be writing a few classes that can be used by...

    For this lab assignment, you will be writing a few classes that can be used by an educator to grade multiple choice exams. It will give you experience using some Standard Java Classes (Strings, Lists, Maps), which you will need for future projects. The following are the required classes: Student – a Student object has three private instance variables: lastName, a String; firstName, a String; and average, a double. It has accessor methods for lastName and firstName, and an accessor...

  • The following code uses a Scanner object to read a text file called dogYears.txt. Notice that...

    The following code uses a Scanner object to read a text file called dogYears.txt. Notice that each line of this file contains a dog's name followed by an age. The program then outputs this data to the console. The output looks like this: Tippy 2 Rex 7 Desdemona 5 1. Your task is to use the Scanner methods that will initialize the variables name1, name2, name3, age1, age2, age3 so that the execution of the three println statements below will...

  • I keep getting an error code in my C++ program. It says "[Error] 'strlen' was not...

    I keep getting an error code in my C++ program. It says "[Error] 'strlen' was not declared in this scope" in my main.cpp. Here are my codes. main.cpp #include <iostream> #include <string> #include "functions.h" using namespace std; //definition of the main function. //takes arguments from the command-line. int main(int argc, char *argv[]) { //Determine if you have enough arguments. //If not, output a usage message and exit program if (argc<2 || (argc == 2 && argv[1][0] == '-')) { //call...

  • the source code I have is what i'm supposed to fix for the assignment at the...

    the source code I have is what i'm supposed to fix for the assignment at the bottom. cars.cpp source code: /* Program Cars.cpp reads records from file and writes them back to another file with the price member increased by 10%. */ #include <fstream> #include <iostream> #include <string> #include "car.h" // car.h header file is included here using namespace std; /* Reads data from a file and using it to populate a Car struct and then return that Car struct...

  • Please I need help in C language, I am trying to modify the code per the...

    Please I need help in C language, I am trying to modify the code per the below instructions, but I am getting errors. Can't fgure it out. The attempted code modification and data file is privided below, after the instructions. Thank you. Instructions:                  1.      First, add a function named printMsg that displays a message, a greeting, or an introduction to the program for the user. Add a statement that calls that function from the main function when your program starts....

  • ***C++ Code*** What this Assignment Is About: -Learn to define and call void & non-void functions...

    ***C++ Code*** What this Assignment Is About: -Learn to define and call void & non-void functions -Learn to use reference variables Coding Guidelines for All Labs/Assignments (You will be graded on this) -Give identifiers semantic meaning and make them easy to read (examples numStudents, grossPay, etc). -Keep identifiers to a reasonably short length. -Use upper case for constants. Use title case (first letter is upper case) for classes. Use lower case with uppercase word separators for all other identifiers (variables,...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT