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 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...
Write a C++ program that processes orders for our company Widgets and More. Your program will...
Write a C++ program that processes orders for our company Widgets and More. Your program will read the product code and quantity for all the products in an order from a file, order.txt. You will be given a sample order.txt with the starter code. There are two items on each line of the file the first is a letter that represents the product and the second is an integer representing the number bought. Based on the product and quantity, you...
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...
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...
Lab 6    -   Program #2   -   Write one number to a text file. Use the write()...
Lab 6    -   Program #2   -   Write one number to a text file. Use the write() and read() functions with binary                                                        data, where the data is not char type.              (Typecasting is required) Fill in the blanks, then enter the code and run the program. Note:   The data is int type, so typecasting is            required in the write() and read() functions. #include <iostream> #include <fstream> using namespace std; int main() {    const int SIZE = 10;   ...
Write a program that does the following in C++ 1 ) Write the following store data...
Write a program that does the following in C++ 1 ) Write the following store data to a file (should be in main) DC Tourism Expenses 100.20 Revenue 200.50 Maryland Tourism Expenses 150.33 Revenue 210.33 Virginia Tourism Expenses 140.00 Revenue 230.00 2 ) Print the following heading: (should be in heading function) Store name | Profit [Note: use setw to make sure all your columns line up properly] 3 ) Read the store data for one store (should be in...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT