Hello there, i have a programming assignment i need help with. here is the assig
ID: 675465 • Letter: H
Question
Hello there, i have a programming assignment i need help with. here is the assignment followed by my code:
Write a program that prompts the user for a file path to a file that contains data. The data should be several lines where each line is a name followed by two numbers, such as:
Tom 4.4 3.3
Larry 6 9.32
Mary 22.3 55.65
Example #1
Please enter the data file path: C:Tempdata.txt
The file contents are:
Tom 4.4 3.3
Larry 6 9.32
Mary 22.3 55.65
Here is my code so far:
#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>
using namespace std;
int main()
{
string filename;
string line;
cout << "Please enter the data file path :";
cin >> filename;
ifstream file(filename);
string name;
double num1;
double num2;
if (file.is_open())
{
while (file >> name >> num1 >> num2)
{
cout << name << " " << num1 << " " << num2;
}
file.close();
}
system("pause");
}
it isnt displaying any files so I'm a little lost.
Explanation / Answer
#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>
using namespace std;
int main()
{
string filename;
cout << "Please enter the data file path :";
cin >> filename;
ifstream file(filename.c_str());
string name;
double num1;
double num2;
while (file >> name >> num1 >> num2)
{
cout << name << " " << num1 << " " << num2<<' ';
}
file.close();
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.