Jul 26, 2014 at 4:20am UTC
hey guys, FIRST POST! Sorry had to, anywho. I'm having a problem with my programming homework. I have to have the user input some numbers and save them to a file, problem is the data just isn't being saved.
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
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
int east1, east2, east3, east4;
ofstream outFile;
outFile.open("salesList.txt" , ios::out);
if (!outFile.is_open()){
cout << "Error opening sales list.\n\n" ;
}
cout << "Hello this is the accounting department. we need\n" ;
cout << "the sales reports for the eastern Enron branch.\n" ;
cout << "Please enter sales for all four quarters for each branch:\n" ;
cout << "Eastern branch: 1st quarter: $" >> endl;
cin >> east1;
cout << "2nd quarter: $" >> endl;
cin >> east2;
cout << "3rd quarter: $" >> endl;
cin >> east3;
cout << "4th quarter: $" >> endl;
cin >> east4;
outFile.close();
system("PAUSE" );
return 0;
}
Any hope would be greatly appreciated.
Last edited on Jul 26, 2014 at 4:22am UTC
Jul 26, 2014 at 9:17am UTC
What are you trying to save to the file? You are opening the file, printing a message to the console for the user and collecting the data, but I don't see anywhere that you are actually attempting to save anything to the file. "cout" is for the console. You opened a stream called "outfile", and continue to simply print to the console. Well... Sort of. You are using the wrong indirection before endl; so I'm surprised you aren't getting an error.
You need to add something like the following after you've collected the user data:
outfile << "1st quarter: $" << east1 << endl;
Last edited on Jul 26, 2014 at 9:17am UTC
Jul 26, 2014 at 12:16pm UTC
I agree with discofire , it seems you are just opening and closing the file
and doing nothing to it;
But well this is the code you might be needing;
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
int east1, east2, east3, east4;
ofstream outFile;
outFile.open("salesList.txt", ios::out);
if(!outFile.is_open()){
cout << "Error opening sales list.\n\n"; /// can also use
/// if(outfile.fail())
}
cout << "Hello this is the accounting department. we need\n";
cout << "the sales reports for the eastern Enron branch.\n";
cout << "Please enter sales for all four quarters for each branch:\n";
cout << "Eastern branch: 1st quarter: $" << endl;
cin >> east1;
cout << "2nd quarter: $" << endl;
cin >> east2;
cout << "3rd quarter: $" << endl;
cin >> east3;
cout << "4th quarter: $" << endl;
cin >> east4;
///you missed these part;
outFile<<"1st quarter: $"<<east1<<endl;
outFile<<"2nd quarter: $"<<east2<<endl;
outFile<<"3rd quarter: $"<<east3<<endl;
outFile<<"4th quarter: $"<<east4<<endl;
outFile.close();
system("PAUSE");
return 0;
}
Jul 26, 2014 at 12:19pm UTC
I agree with discofire , it seems you are just opening and closing the file
and doing nothing to it;
insert the following part between the cin >> east4 and outFile.close();
///you missed these part;
outFile<<"1st quarter: $"<<east1<<endl;
outFile<<"2nd quarter: $"<<east2<<endl;
outFile<<"3rd quarter: $"<<east3<<endl;
outFile<<"4th quarter: $"<<east4<<endl;