reading a txt file
Nov 7, 2016 at 8:18pm UTC
I'm working on two programs, one that continuously asks you to choose a item from its menu until you press x, which ends the loop, and saves the total cost of all the items ordered into a .txt file and the other program, that displays whats in the file.
I'm having trouble with my second program. What I want the program to do, is read the numbers in the .txt file, add them all up and display the sum. When I run my second program all I get is 0.
text file example :
my first progam :
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
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 71 72 73 74 75
int main()
{
ofstream testfile1;
double sum = 0;
double teacount = 0;
double coffeecount = 0;
char lastorder = ' ' ;
double teasum = 0;
double coffeesum = 0;
string name = "" ;
testfile1.open("testname.txt" , ios::app);
do {
cout << "C- coffee ($2) \n " ;
cout << "T- TEA ($3) \n " ;
cin >> lastorder;
switch (lastorder) {
case 'C' : {
coffeecount++;
cout << "Coffee " << "!" << coffeecount << "!" << endl;
break ;
}
case 'c' : {
coffeecount++;
cout << "Coffee" << "!" << coffeecount << "!" << endl;
break ;
}
case 'T' : {
teacount++;
cout << "TEA " << "!" << teacount << "!" << endl;
break ;
}
case 't' : {
teacount++;
cout << "TEA " << "!" << teacount << "!" << endl;
break ;
}
default : {
//cout << " wrong order try again ! \n";
}
}
} while (lastorder != 'X' && lastorder != 'x' );
coffeesum = coffeecount * 2;
teasum = teacount * 3;
sum = coffeesum + teasum;
testfile1 << sum<<endl;
testfile1.close();
cout << "Coffe: " << coffeecount << endl;
cout << "Tea: " << teacount << endl;
cout << "Total cost: " << sum << endl;
My second program
#include <iostream>
#include <string>
# include <fstream>
using namespace std;
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
int main()
{
ifstream testfile2;
double sum = 0;
double filenum =0;
filenum = testfile2.get();
testfile2.open("testname.txt" , ios::in);
testfile2 >> filenum;
while (!testfile2.eof()) {
sum = filenum = 0;
cout << "File is open \n" ;
sum = sum + filenum;
testfile2 >> filenum;
}
testfile2.close();
cout << "total: " << sum<< endl;
return 0;
}
Nov 7, 2016 at 9:04pm UTC
That is because line 14 of 2nd:
sum = filenum = 0;//sets both to zero for every iteration of the loop
.
Nov 7, 2016 at 9:15pm UTC
Okay, I commented out that line and I'm still getting zero for my output.
Nov 7, 2016 at 9:30pm UTC
I removed line 8 and line 14, and ran with test inputs that you provided in "testname.txt" with VS2015. I added in a cout for the sum.It's output was
File is open
SUM:50
File is open
SUM:110
File is open
SUM:122.5
File is open
SUM:203.2
total: 203.2
Press any key to continue . . .
Nov 7, 2016 at 10:03pm UTC
It's not even doing the while loop anymore.
Nov 7, 2016 at 10:16pm UTC
If you do not post your changed code, it won't help us with debugging it. Here is what I have that is working which is a copy paste of your above code with those two line deletes.
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
#include <iostream>
#include <string>
# include <fstream>
using namespace std;
int main()
{
ifstream testfile2;
double sum = 0;
double filenum = 0;
testfile2.open("testname.txt" , ios::in);
testfile2 >> filenum;
while (!testfile2.eof()) {
//sum = filenum = 0;
cout << "File is open \n" ;
sum = sum + filenum;
cout << "SUM:" << sum << endl;
testfile2 >> filenum;
}
testfile2.close();
cout << "total: " << sum << endl;
return 0;
}
Nov 8, 2016 at 12:10am UTC
I tried it as you typed, but now when I run the program, it runs the code inside the wile loop infinitely
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
#include <iostream>
#include <string>
# include <fstream>
using namespace std;
int main()
{
ifstream testfile2;
double sum = 0;
double filenum = 0;
testfile2.open("testname.txt" , ios::in);
testfile2 >> filenum;
while (!testfile2.eof()) {
//sum = filenum = 0;
cout << "File is open \n" ;
sum = sum + filenum;
cout << "SUM:" << sum << endl;
testfile2 >> filenum;
}
testfile2.close();
cout << "total: " << sum << endl;
return 0;
}
Last edited on Nov 8, 2016 at 12:11am UTC
Nov 8, 2016 at 12:19am UTC
Well that is pretty odd, it may have to deal with using the
eof()
.
Let's try using:
1 2 3 4 5 6 7 8 9 10 11 12
//testfile2 >> filenum;
while (testfile2 >> filenum) {
//sum = filenum = 0;
cout << "File is open \n" ;
sum = sum + filenum;
cout << "SUM:" << sum << endl;
}
Nov 8, 2016 at 2:00am UTC
Wow Thank you it works
Topic archived. No new replies allowed.