141k views
0 votes
Suppose we wish to process survey results that are stored in a file. This exercise requires two separate programs. First, create a program that prompts the user for survey responses and outputs each response to a file. Use an ofstream to create a file called "numbers.txt". Then create a program to read the survey responses from "numbers.txt". The responses should be read from the file by using an ifstream. Input one integer at a time from the file. The program should continue to read responses until it reaches the end of file. The results should be output to the text file "output.txt". Hint: ■ The second program will use both ifstream and ofstream objects, the first for reading responses from numbers.txt and the second for writing frequency counts to output.txt. 16 Contents of numbers.txt 5372869542 12 8 10 4 5 2 7 10 4 98213756843821 Contents of output.txt Number of 1 responses: 3 Number of 2 responses: 6 Number of 3 responses: 3 Number of 4 responses: 4 Number of 5 responses: 4 Number of 6 responses: 2 Number of 7 responses: 3 Number of 8 responses: 5 Number of 9 responses: 2 Number of 10 responses: 2

User Katie H
by
7.6k points

1 Answer

6 votes
To complete this exercise, we need to create two separate programs. The first program will ask the user for survey responses and save them in a file called "numbers.txt". The second program will read the responses from "numbers.txt", count how many times each response occurs, and save the results in a file called "output.txt".

First Program: Saving Survey Responses to "numbers.txt"

1. Create a program that asks the user for survey responses.
2. Open a file called "numbers.txt" to save the responses.
3. Inside a loop, ask the user for a response and write it to the file.
4. Once the user finishes entering responses, close the file.

Second Program: Reading Survey Responses from "numbers.txt" and Saving Frequency Counts to "output.txt"

1. Create another program that reads survey responses from "numbers.txt".
2. Open the "numbers.txt" file for reading and create a file called "output.txt" to save the frequency counts.
3. Create a data structure to store the frequency counts for each response.
4. Read each response from the file and update the corresponding frequency count.
5. After reading all responses, write the frequency counts to "output.txt".
6. Close both the input and output files.

By running the second program, you will get an "output.txt" file that shows how many times each response appeared in the survey.
User Aren Hovsepyan
by
7.8k points

No related questions found