Write a program that: Prompts user for: (1) Name, (2) Favorite Color, (3) Favori
ID: 3670664 • Letter: W
Question
Write a program that:
Prompts user for: (1) Name, (2) Favorite Color, (3) Favorite Food
Output results to file called "Lastname_favorites.txt"
***Make sure that each execution of the program appends to the file instead of overwriting an existing file.
this was my program but i did not get it right, can you tell me what i did wrong?
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
// Declaring variables
string name, color, food;
ofstream outFile;
// Open files (input first then output)
outFile.open ("Perez_favorites.txt", ios::app);
// prompt user for name, favorite color, and food
cout << "Please enter your name, favorite color, and favorite food:";
cin >> name >> color >> food;
// exports user's name, color, and food to the file
ofstream outFile ("Perez_favorites.txt");
// close files
outFile.close ();
return 0;
}
Explanation / Answer
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
// Declaring variables
string name, color, food;
ofstream outFile;
// Open files (input first then output)
outFile.open ("Perez_favorites.txt", ios::app);
// prompt user for name, favorite color, and food
cout << "Please enter your name, favorite color, and favorite food:";
cin >> name >> color >> food;
// exports user's name, color, and food to the file
outFile << name << " " << color << " " << food << endl;
//ofstream outFile ("Perez_favorites.txt");
// close files
outFile.close ();
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.