I'm trying to make a program that will read a textfile that contains a few names together with their age. The program should count the total sum of all the persons age.
Mark
56
Andy
27
Samuel
24
I made a program that reads and writes out the textfile. But I have no clue of how to continue from here. How can I read the text file and sort the char from int, Give the age one value that does not include the names? I really need som help on this to get started. This is what I got this far.
#include<fstream>
#include<iostream>
#include<string>
#include<conio.h>
using namespace std;
int summa;
int main ()
{
char vekt[50][30];
ifstream fil;
fil.open("data.txt");
//If file not found
if (!fil)
{
cout << "Cant find file";
exit(1); // terminate with error
}
int num=0;
//Reads the file
while(fil.getline(vekt[num], 30))
{
num++;
}
fil.close();
//The file
cout<<"Varor:"<<endl;
for( int i=0;i<num;i++)
cout<<vekt[i]<<" "<<endl;
getch();
return 0;
}


Sign In
Create Account


Back to top









