and read data from the file into suitable arrays
output the mean of the each exam
my code is
#include<iostream>
#include<conio.h>
#include<string>
#include<fstream>
using namespace std;
int main () {
ifstream dosya("stu.txt");
string name[20];
int exam1;
int exam2;
int exam3;
int total;
for(int i=0;i<20;i++) {
dosya>>name[i]>>exam1[i]>>exam2[i]>>exam3[i];
cout<<name[i]<<" "<<exam1[i]<<exam2[i]<<exam3[i]<<endl;
}
dosya.close();
ifstream dosya("stu.txt");
for(int i=0;i<20;i++) {
total[i]=exam1[i]+exam2[i]+exam3[i];
}
getch ();
}
it is not running can anybody help me?
PLEASE ALWAYS USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post. http://www.cplusplus.com/articles/jEywvCM9/ http://www.cplusplus.com/articles/z13hAqkS/
Hint: You can edit your post, highlight your code and press the <> formatting button.
You can use the preview button at the bottom to see how it looks.
Your program has some problems as the comments in the following code will show. The program will not work the way you have it. Closing the file and reopening it for the second for loop will not pick up the numbers because of the name you did not account for.
After setting the file stream you should check to see if it is open. I added some for this to help you out.
The program would benefit from a struct and an array or vector of this struct. It makes it easier to keep track of all the information. And it would eliminate the need for the second for loop.
#include<iostream>
#include<conio.h>
#include<string>
#include<fstream>
#include <chrono>
#include <thread>
//using namespace std; // <--- Best not to use.
int main()
{
std::ifstream dosya("stu.txt");
std::string name[20];
// <--- Should initialize all these vriables.
int exam1{};
int exam2{};
int exam3{};
int total{};
std::string iFileName{ "stu.txt" };
if (dosya.is_open())
{
std::cout << "\n File " << iFileName << " is open" << std::endl;
std::this_thread::sleep_for(std::chrono::seconds(2)); // <--- Needs header files chrono" and "thread".
}
else
{
std::cout << "\n File " << iFileName << " did not open" << std::endl;
std::this_thread::sleep_for(std::chrono::seconds(3)); // <--- Needs header files chrono" and "thread".
exit(1);
}
for (int i = 0; i < 20; i++)
{
dosya >> name[i] /*>> exam1[i] >> exam2[i] >> exam3[i]*/;
std::cout << name[i] << " " << /*exam1[i] << exam2[i] << exam3[i] <<*/ std::endl; // <--- exam? is a single vriable not an arry.
}
dosya.close();
//std::ifstream dosya("stu.txt"); // <--- Duplicate, already defied.
// <--- Need to reopen the file here sine it was closed or close the stream later and reset the file pointer.
// <--- This for loop will not read the file properly.
for (int i = 0; i < 20; i++)
{
//total[i] = exam1[i] + exam2[i] + exam3[i]; // <--- Total is a single variable not an array same for exam?.
}
// <--- Need a second close file here. If used.
std::cout << "\n\n\n\n Press anykey to continue";
_getch(); // <--- Had to change for my compiler.
}
int exam1{};
int exam2{};
int exam3{};
int total{};
\Users\USER\Desktop\Çalışma\Untitled7.cpp C:\Users\USER\Desktop\Çalışma\C chrono: No such file or directory.
\Users\USER\Desktop\Çalışma\Untitled7.cpp C:\Users\USER\Desktop\Çalışma\C thread: No such file or directory.