Count average c++ from

Hello. i'm creating a c++ windows form application. i don't know how to make to count average from file. Need help. I can input in file also read from file in form, i need to do when i push read button, that in richtextbox it would show average of numbers.

private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) {

FILE *egzas;
String^ tekstas;
int i=0;
tekstas = textBox1->Text;
tekstas = tekstas + "\n" ;
egzas=fopen("vidurkis.txt","a");
marshal_context ^ eilute = gcnew marshal_context();
const char* str = eilute->marshal_as<const char*>(tekstas);
fputs(str,egzas);
fclose(egzas);
}
private: System::Void button2_Click(System::Object^ sender, System::EventArgs^ e) {
FILE *egzas;
richTextBox1->Text="";
char str[500];
egzas=fopen("vidurkis.txt","r");
while (fgets (str, 500, egzas)) {

String^ strNew = gcnew String(str);
richTextBox1 -> AppendText(strNew);
}

fclose(egzas);
}
};
}
For Example:

In textbox1 i write:

Tom Jones 10
Jone Jone 5
Tim Duncan 6

then i push write and it write that text in vidurkis.txt file

then i push read and it shows me:

Tom Jones 10
Jone Jone 5
Tim Duncan 6

Average of numbers:
7

Last edited on
Topic archived. No new replies allowed.