Help with reading from file

Here is file:

meal.dat \\ I have create the file using Sources Files (visual studio)
B 100
E 515
S 45
D 520

and here is the code

#include <iostream>
#include <string>
#include <fstream>

using namespace std;

int main()
{
ifstream Meal;
char Type;

int Callories;

Meal.open("Meal.dat");
Meal>> Type >> Callories;
cout<< "Type" << Type <<endl;
Meal.close();



return 0;
}

Here is the output screen:
TypeB

My question is : how can I read all the lines and add them up (Sum); Thank you
Try a while loop.

Hint: read until the end of file, once end of file exit the 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
// here is an example to point you in the right direction

// include statments

int main()
{
ifstream in;// to acces the file with the data information

char ch; // to get the letter
int i = 0; // to eat the calories
int sum = 0; // total calories


in.open("filename");

// check if the file is open
// if not print a message that say id did not open

// if file opened
// while loop

while (!in.eof()) // this will run until it reaches the end of file
{
     // read the charachter;
     // read the calories;

     in.ignore(100, '\n'); // this is to ignore everything after the calories all the way to new line

     // add the calories to sum 

}

// output the sum
return 0;



}


this is all you should need, so put an effort and if you get stuck or have errors post the effort and someone will help you.
Last edited on
How can I call the second and the third line from the File ??
@Muly

Here is your program that first asks which entry to show, prints it along with total calorie count, then shows the list as a whole, with the sum of all the calories.
I made meal.dat larger. Added also. as first entry, how many entries there are.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
15
A 100
B 515
C 45
D 520
E 128
F 421
G 79
H 170
I 625
J 99
K 395
L 135
M 466
N 649
O 199


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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
// Meals.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include <string>
#include <fstream>

using namespace std;

int main()
{
	ifstream Meal;
	char Type;
	int x = 0;
	int sum = 0;
	int entries = 0;
	int Calories = 0;
	int entry = 0;

	Meal.open("Meals.dat");
	Meal >> entries;

	do
	{
		cout << "Which entry do you wish to see? ( 1 to " << entries << " ) ";
		cin >> entry;
		if (entry <=0 || entry >entries)
			cout << "No entry in that slot.." << endl;
	} while (entry <=0 || entry > entries );


	for ( x=0; x < entry; x++)
	{
		Meal >> Type >> Calories;
	}
	sum = Calories;
	cout << endl << "Type : " << Type << "  Calories : " << Calories << " Sum of calories = " << sum << endl << endl;
	Meal.close();	

	cout << "The list of entries are : " << endl << endl;

	Meal.open("Meals.dat");
	entry = 1;
	sum = 0;
	Meal >> entries;
	while (Meal)
	{
		Meal >> Type >> Calories;
		if (Meal)	
		{
			cout << "Entry # ";
			if (entry < 10 )
				cout << " ";
		cout << entry << " : Type : " << Type << "  Calories : ";
		
		if ( Calories < 10)
			cout << " ";
		if ( Calories < 100 )
			cout << " ";
		cout << Calories << endl;
		sum +=Calories;
		entry++;
		}
	}
	cout << endl << "Total Calories of the " << entries << " entries is : " << sum << "." << endl << endl;
	
	Meal.close();
	return 0;
}

Last edited on
Topic archived. No new replies allowed.