Question

Write a C++ program. 1) Write a program that writes the grades for 3 students. Declare...

Write a C++ program.

1) Write a program that writes the grades for 3 students.

Declare a variable of type ofstream which is used to output a stream into a file:

ofstream output; // output is the name of the variable

Prompt the user to input values for the grades of 3 students.

Use the output operator (<<) to write the grades into grades.txt:

output << grade1 << " " << grade2 << " " << grade3 << endl;

You may use IO manipulation commands (e.g. setw) in the above statement.

Close your file:

output.close();

After running the code, locate the grades.txt file on your computer. It must show the data you entered.

2) Write a second program reads the grades.

Declare a variable of type ifstream which is used to input a stream from a file:

ifstream input; // input is the name of the variable

Use the input operator (>>) to read data from the above file:

input >> score1 >> score2 << score3;

// the values in the file will be stored in the above variables

Use cout to display the above values.

Close your file:

input.close();

Homework Answers

Answer #1
  • Below is the detailed solution of the above problem in C++ with code and comments in it.
  • Please read the comment mentioned in the code for better understanding.
  • And create a file named grades.txt in the same directory as your c++ file.
  • CODE1:

#include<iostream>
#include<fstream>
using namespace std;

//driver function
int main(){
//variable to output stream into file
ofstream output;

//three integer variables to input grades of students.
int grade1, grade2, grade3;

//open file with file name-grades.txt.
output.open("grades.txt");

//prompt user to enter the grades
cout<<"Enter the grades of three students: ";

//take input
cin>>grade1>>grade2>>grade3;

//write grades of students into file.
output<<grade1<<" "<<grade2<<" "<<grade3<<endl;

//close the file.
output.close();

return 0;
}

  • INPUT/OUTPUT1:

  • CODE2:

#include<iostream>
#include<fstream>
using namespace std;

//driver function
int main(){
//variable to input stream from file
ifstream input;

//three integer variables to input grades of students.
int grade1, grade2, grade3;

//open file with file name-grades.txt.
input.open("grades.txt");

//read from file.
input>>grade1>>grade2>>grade3;

//print grades of students.
cout<<grade1<<" "<<grade2<<" "<<grade3<<endl;

//close the file.
input.close();

return 0;
}

  • INPUT/OUTPUT2:

  • For better indentation and clarity below are the attached screenshots of the code.

CODE1

CODE2

So if you have any doubt regarding this solution please feel free to ask in the comment section below and if it is helpful then please upvote the solution, THANK YOU.

Know the answer?
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
Similar Questions
C++ Write a program that writes the grades for 3 students. Declare a variable of type...
C++ Write a program that writes the grades for 3 students. Declare a variable of type ofstream which is used to output a stream into a file: ofstream output; // output is the name of the variable Prompt the user to input values for the grades of 3 students. Use the output operator (<<) to write the grades into grades.txt: output << grade1 << " " << grade2 << " " << grade3 << endl; You may use IO manipulation...
Write a C++ program reads the grades. Declare a variable of type ifstream which is used...
Write a C++ program reads the grades. Declare a variable of type ifstream which is used to input a stream from a file: ifstream input; // input is the name of the variable Use the input operator (>>) to read data from the above file: input >> score1 >> score2 << score3; //
Write a program that accepts as input the mass, in grams, and density, in grams per...
Write a program that accepts as input the mass, in grams, and density, in grams per cubic centimeters, and outputs the volume of the object using the formula: volume = mass / density. Format your output to two decimal places. ** Add Comments ** Print Name and Assignment on screen ** Date ** Submit .cpp file. Demo // This program uses a type cast to avoid an integer division. #include <iostream> // input - output stream #include <fstream> //working file...
create a complete C++ program that will read from a file, "studentInfo.txt", the user ID for...
create a complete C++ program that will read from a file, "studentInfo.txt", the user ID for a student (first letter of their first name connected to their last name i.e. jSmith). Next it will need to read three integer values that will represent the 3 e xam scores the student got for the semester. Once the values are read and stored in descriptive variables it will then need to calculate a weighted course average for that student. Below is an...
Write a C++ Program Consider the following incomplete C++ program: #include <iostream> using namespace std; int...
Write a C++ Program Consider the following incomplete C++ program: #include <iostream> using namespace std; int main() {    …. } Rewrite the program include the following statements. Include the header file fstream, string, and iomanip in this program. Declare inFile to be an ifstream variable and outFile to be an ofstream variable. The program will read data from the file inData.txt and write output to the file outData.txt. Include statements to open both of these files, associate inFile with...
Create a program that generates a file of random numbers, and then prints them in neat...
Create a program that generates a file of random numbers, and then prints them in neat fashion to another file, and also saves to that file the average and standard deviation of those numbers. I) First, you would need to generate a file of random numbers that consists of N random numbers (100 < N < 1000). Each random digit should be a real number (type double) between 0 and 50. This file and its digits would now serve as...
You will write a program that loops until the user selects 0 to exit. In the...
You will write a program that loops until the user selects 0 to exit. In the loop the user interactively selects a menu choice to compress or decompress a file. There are three menu options: Option 0: allows the user to exit the program. Option 1: allows the user to compress the specified input file and store the result in an output file. Option 2: allows the user to decompress the specified input file and store the result in an...
C++ programming Write a program that reads a comma-separated file (CSV) with integer values and prints...
C++ programming Write a program that reads a comma-separated file (CSV) with integer values and prints the number of integer values in the file. Your program's input will be a string with the name of the file. If the file does not exist, then the program must print: Error opening the file For example, given the following CSV file input1.csv: 1,10,20,30,40 The output of your program must be: 5 You can safely assume that the input file will be a...
In c++ Write a program that creates a dynamically allocated array to store students’ grades. The...
In c++ Write a program that creates a dynamically allocated array to store students’ grades. The user should be able to decide the size of the array during run time. Write code to fill this array from the keyboard and then print out the values of your array,
we will be taking data in as a file. you cannot use the data values in...
we will be taking data in as a file. you cannot use the data values in your source file but you must read in everything into variables and use it from there. First we need to include <fstream> at the top of our file. We will need to create an input file stream to work and ifstream. ifstream is a datatype. Create a variable with the datatype being ifstream. Variable is drfine by using the member accessor operator to call...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT