Hi, I really need help with understanding how to solve an exercise.
Create a new textfile with the name 'studentinfo.txt' and write following and save:
Marie Anderssen
29
Alexander Lind
35
Gholam Payro
32
Zlatan Ibrahimavic
26
Write a program that read the information from 'studentinfo.txt' and calculates the average age.
#include <fstream>
#include <iostream>
#include <string>
using namespace std;
int main()
{
int age;
ifstream read("studentinfo.txt");
if (! read)
{
cout<<"File not found!";
getch();
return (1);
}
//Average age calculation supposed to be here I guess...
char vekt[50][30];
int a, i;
cout<<"How many new student to you want to add to the list? ";
cin>>a;
cin.get();
for(i=0; i<a; i++)
{
cout<<"Name: ";
cin.getline(vekt[i],30);
//Need help with adding the age part too..
}
ofstream outstream("studentinfo.txt", ios::app);
if(!outstream)
{
cout<<"File could not be found!"<<endl;
return(1);
}
//calculates new average age..
return 0;
}
Example for how it should look:
Marie Anderssen
29
Alexander Lind
35
Gholam Payro
32
Zlatan Ibrahimavic
26
Average age: 30,5
How many new student to you want to add to the list? 2
Name: Reza Bozorg
Age: 40
Name: Stephanie Meyer
Age: 34
New average age: 32,6666
Would really appreciate it if someone could help me with this one =)