fstream

I do have a homework assignment. I have been pulling what little bit of hair that I have out over this, as the textbook that I have does not reference what I am doing wrong. The only errors I am receiving are: c4700 on three counts. That the variables that I have are not initialized.

What I am asking here is what I am doing wrong, and if you're able to point me in the correct direction without giving me the answer, it would be very helpful. I tried to decipher the fstream and what all it said, but to no avail.


#include <fstream> //for file input and output
#include <iostream>
#include <iomanip>
#include <cmath>
#include "string"

using namespace std;

int main()
{
ifstream fileInput; //for file input
ofstream fileOutput; //for file output

fileInput.open("C:\\Users\\Kevin\\Documents\\Westwood College\\V4D1 - Fundimentals of Programming\\Fogg_fstream\\fstream\\Fogg_fstream.txt"); //opens the file to allow editing
fileOutput.open("C:\\Users\\Kevin\\Documents\\Westwood College\\V4D1 - Fundimentals of Programming\\Fogg_fstream\\fstream\\Fogg_fstream.txt"); //opens the file to pull data from

char letter;
fileOutput << "The character from the file is " << letter << ".\n:";
int num1, num2;
int sum=num1+num2;
int prod=num1*num2;

fileOutput << "The sum of " << num1 << " and " << num2 << " = " << sum << ".\n";
fileOutput << "The product of " << num1 << " and " << num2 << " = " << prod << ".\n";

fileInput.close(); //closes the file
fileOutput.close(); //closes the file

system("Pause");

return 0;

}


Also, if I didn't use the correct format for submitting my question, I am sorry. But this is the second time taking the course that I am, and I am supposed to reference a file (which I did), but I cannot get it to display what I need it to.
1.) Are you sure you have the correct path to the files? You have "Fundamentals" misspelled as "Fundimentals", are you sure the file path has it spelled that way?

2.) You haven't initialized the char letter or the int num1, num2. You will just get random values for them;

3.) Are aren't reading anything from the file. You have no fileInput >> letter or fileInput >> num1 or anything else in your code.

Lets say your input file is like this:

a 12 43

then after declaring letter,num1,num2 you do

fileInput >> letter >> num1 >> num2;


4.) Your input and output file are the same. You probably don't want that.

5.) Never seen system("Pause") before, not sure if its a windows thing but i took it out.


This worked on my system for a file Input.txt and output file Output.txt on my system.

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
#include <fstream> //for file input and output
#include <iostream>
#include <iomanip>
#include <cmath>
#include "string"

using namespace std;

int main()
{
ifstream fileInput; //for file input
ofstream fileOutput; //for file output

fileInput.open("Input.txt"); //opens the file to allow editing
fileOutput.open("Output.txt"); //opens the file to pull data from

char letter;
int num1, num2;
fileInput >> letter >> num1 >> num2;

int sum=num1+num2;
int prod=num1*num2;

fileOutput << "The character from the file is " << letter << ".\n:";
fileOutput << "The sum of " << num1 << " and " << num2 << " = " << sum << ".\n";
fileOutput << "The product of " << num1 << " and " << num2 << " = " << prod << ".\n";

fileInput.close(); //closes the file
fileOutput.close(); //closes the file

//system("Pause");

return 0;

}
ac517,

1. I am not certain that I have it correct now that you have pointed out that I misspelled the way. But the general pathing is correct.

2. I am comparing the two of our code together, so, I am seeing the difference.

3. The file input, are you stating that I haven't initialized to write in the file yet?

4. Why would I not want the files in and out to be the same?

5. System("Pause") allows for the console to stop without having to terminate the program itself.

Lastly, thank you for your help. I am going to rewrite your code, duplicate it if you're okay with that and learn from what you showed me here.
You can output to the same file you input to but you will lose the original data.
Also the code he provided means you have to have your input/output file(s) in the same directory as the executable otherwise you will have to give it a direct path.
Never seen system("Pause") before, not sure if its a windows thing but i took it out
this is for windows but there is similar for unix. Though you should avoid using system for anything nonetheless for keeping the console open there are better solutions the best would probably to enable the setting to keep it open in your ide. http://www.cplusplus.com/forum/beginner/1988/
I did some searching of my textbooks and taking some notes on what was said by ac517, it seems that I was doing some over-thinking. I finally satisfied my program project with what I have here:

#include <fstream> //for file input and output
#include <iostream>
#include <iomanip>
#include <cmath>
#include "string"

using namespace std;

int main()
{
ifstream fileIn; //for file input
fileIn.open("list.txt");

if (fileIn.fail()) //just in case there was a problem with loading the file
{
cerr << "Error opening file.\n";
exit(1);
}

string let;
int x , y;


fileIn >> let >> x >> y;

int sum = x+y;
int prod = x*y;

cout << "The character from the file is " << let << "." << endl;
cout << "The sum of " << x << " and " << y << " = " << sum << "." << endl;
cout << "The product of " << x << " and " << y << " = " << prod << "." << endl;

fileIn.close();//closes the file

system("Pause");

return 0;

}


Everyone. Thank you for your help!
FYI you can use
[code]//some code[/code]
== //some code around code instead of
[b]//some code[ /b]
== //some code.
Last edited on
Topic archived. No new replies allowed.