How to read a file line by line without the getline function?

Pages: 12
I did this
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
if (inputFile)
	{
		while (true)
		{
			// read the file line by line and store it into appropriate variables 
			
      		        getline (inputFile, name);
			getline(inputFile, street);
			getline(inputFile, city);
			getline(inputFile, state);
			getline(inputFile, zipcode);
			getline(inputFile, num);
			getline(inputFile, weight);
			if (!inputFile) break;
			
			getline(inputFile, space);

but you need to string zipcode, num, and weight. Then you need to convert it back to an integer or double using
1
2
3
		hello = atoi(zipcode.c_str());
			hello2 = atoi(num.c_str());
			hello3 = atof(weight.c_str());

I had to go back and change every zipcode, num, and weight into its appropriate variable. With this I was able to see everyones' names in the correct format. I'm still having trouble figuring out how to calculate the total cost of all the postage for choice2.
Last edited on
The total is easy enough. I put a variable double total = 0.0; and then in the block where I calculated each person's postage put a line at the endtotal += postage; then you display the total at the end.

I had it where I could get everyone's name (excluding Ann, hers is still fully recorded) except for the first letter of their name. So close.
Are all of your values in the getline function? whenever I had it as:
1
2
3
inputFile >> zipcode; 
inputFile >> num;
inputFile >> weight;
I wasn't able to display everyone's name. I put everything in the getline function like the others did, but like I said in my previous comment, you have to put everything into a string. To convert the values back into a integer for math algorithms use atoi and atof.
http://www.cplusplus.com/reference/cstdlib/atoi/
You're going to also have to change all your values into the new value. For example:
1
2
3
4
5
6
	string zipcode;
	string num;
	string weight;
	int hello;
	int hello2;
	double hello3;
These are your variables.
1
2
3
	hello = atoi(zipcode.c_str());
	hello2 = atoi(num.c_str());
	hello3 = atof(weight.c_str());	
Converting it back into an integer
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
n1 = hello % 10;
	r1 = hello / 10;
	n2 = r1 % 10;
	r2 = r1 / 10;
	n3 = r2 % 10;
	r3 = r2 / 10;
	n4 = r3 % 10;
	r4 = r3 / 10;
	
	total = n1 + n2 + n3 + n4 + r4;
	checkDigit = 10 - (total % 10);     		 
	
	// if/else statements for multiple scenarios
	if (hello2 == 1)
	{
		if (hello3 > 1)
		{
			new1 = price1 + (charge1 * (hello3-1));
			cout << "\n\n***********************************$" << setprecision(2) << fixed << new1 << endl << endl << name << endl << street << endl << city; 
			cout << "   " << state << " " << zipcode << endl << endl;
		}
		else
		{
			cout << "\n\n***********************************$" << price1 << endl << endl << name << endl << street << endl << city; 
			cout << "   " << state << " " << zipcode << endl << endl;
		}
Replace stringed values with the new value. You don't have to change it in the cout statement because it doesn't have to be an integer.
I hope this helps! I'd like to thank everyone for helping us out. I finally got my code to work correctly.
Topic archived. No new replies allowed.
Pages: 12