i am using microsoft visual c++ and cant get this to run. i have the file in the right spot.....any suggestions, or do you need any more info from me.....it always tells me that there is an error opening the file....
#include "stdafx.h"
#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>
using namespace std;
int main()
{
string filename;
cout<< "Enter file name to open:";
cin >> filename;
ifstream fin(filename.c_str());
if (!fin)
{
cout << "Error opening" + filename + "\n";
//We're outa here.
return 0;
}
string firstname,lastname,ssn; //ssn has to be a string because the input
int age; // text has minues (-) in it. So you can't use an int.
int total=0; //We'll use this to add up the ages as we get them.
//Loop until end of file
while (!fin.eof())
{
fin >> firstname;
//If the firstname is blank, we don't have a line. It's probably just
// the carriage return at the very end of the file.
if (firstname != "")
{
fin >> lastname;
fin >> ssn;
fin >> age;
cout << firstname << " " << lastname << " " << ssn << " " << age << "\n";
//Add the age to the total. total = total + age is another way to write it.
total += age;
}
}
fin.close();
cout << "Total= " << total;
return 0;
}



LinkBack URL
About LinkBacks





.