helping!c++ formatted i o and file

this is my input
UGUR 67 38 55
KADIR 72 45 60
SEMIH 00 71 45
GOZDE 73 70 61
FIRAT 30 69 35
EDA 51 58 67
FULYA 41 65 73
GUL 67 69 70
EMRE 30 40 84
AHMET 76 41 67
ISMAIL 67 15 59
MELIKE 71 65 61
TANER 59 50 58
SEHER 79 51 65
ALI 90 88 72
EMIN 88 92 78
HAVVA 76 85 80
FERDA 10 25 50
MUKRIME 54 15 12
TUGBA 61 70 66


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?
Last edited on
Hello imren,

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#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.
}



Any questions just ask.

Hope that helps,

Andy
thank you so much but this code is not running
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.
Maybe something like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include <iostream>
#include <iomanip>
#include <conio.h>
#include <string>
#include <fstream>

using namespace std;

int main ()
{
    ifstream dosya("stu.txt");
    const int size = 20;
    string name[size];
    int exam1[size];
    int exam2[size];
    int exam3[size];
    int total[size];
    double average[size];

    // 1. Read data from file
    int n = 0;

    for (n=0; n<size; n++)
    {
        dosya >> name[n] >> exam1[n] >> exam2[n] >> exam3[n];
        if (!dosya)
            break;
    }

    cout << "\nNumber of records read from file: " << n << "\n\n";

    // 2. do calculations
    for (int i=0; i<n; i++)
    {
        total [i] = exam1[i] + exam2[i] + exam3[i];
        average[i] = total[i] / 3.0;
    }

    // 3. output results
    cout << fixed << setprecision(1);

    for (int i=0; i<n; i++)
    {
        cout << setw(10) << name[i]
             << setw(4) << exam1[i]
             << setw(4) << exam2[i]
             << setw(4) << exam3[i]
             << setw(8) << average[i] << '\n';
    }

    getch ();
}


Note, in the above code, I used no less then six separate arrays.

But it really depends what you want to do. You can read the file and find the averages with no arrays at all:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include <iostream>
#include <iomanip>
#include <conio.h>
#include <string>
#include <fstream>

using namespace std;

int main ()
{
    ifstream dosya("stu.txt");
    
    string name;
    int exam1;
    int exam2;
    int exam3;
    double total1 = 0;
    double total2 = 0;
    double total3 = 0;
        
    cout << fixed << setprecision(1);
    
    int count = 0;
    while (dosya >> name >> exam1 >> exam2 >> exam3)
    {
        count++;
        int total = exam1 + exam2 + exam3;
        double average = total / 3.0;
        cout << setw(10) << name
             << setw(4) << exam1
             << setw(4) << exam2
             << setw(4) << exam3
             << setw(8) << average << '\n';
        total1 += exam1;
        total2 += exam2;
        total3 += exam3;
    }
    
    cout << "\nNumber of records read from file: " << count << "\n\n";
    
    cout << "Average for Exam 1 = " << setw(6) << total1/count << '\n';
    cout << "Average for Exam 2 = " << setw(6) << total2/count << '\n';
    cout << "Average for Exam 3 = " << setw(6) << total3/count << '\n';

    getch ();
}


Also, it depends whether you want to find the averages horizontally (per student) or vertically (per exam).
Topic archived. No new replies allowed.