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