Hello programmers!
The task I have at hand right now is the following:
I need to have the user input a file name for a .txt document. I feel that my code is good up until the validation. (up until line 12) but then i feel like i'm all over the place otherwise.
Sample data that our teacher gave us: (f for female, m for male, the number is a GPA)
f2.5
f4.0
m1.4
m3.4
m2.9
f0.9
m1.2
m3.3
I need to validate the file name and the file data. I also need the data to be read correctly. I'm at a standstill.. Please Help :).
void openFiles(string filenameIn, string filenameOut, double& maleGrade, ostream& outfile, double& femaleGrade, char gender){
ifstream inFile;
ofstream outFile;
cout << "Please enter the name of the file: " <<endl; //asks user for the file name
getline (cin, filenameIn);
filenameIn += ".txt"; //user doesn't need to put .txt at the end of the file name.
inFile.open(filenameIn.c_str()); //opens input file name
outFile << fixed << showpoint;
outFile << setprecision(2); // setprecision to 2
inFile >> gender;
if (gender = 'f')
inFile >> femaleGrade;
elseif (gender = 'm')
inFile >> maleGrade;
elseif (gender =!'f' || !'m')
cout << "Sorry, that file has invalid data" <<endl;
system("pause");
bool dataOk=false;
for (unsignedint nIndex=0; nIndex < filenameIn.length() && !dataOk; nIndex++)
{
// If the current character is an alpha character, that's fine
if (isalpha(filenameIn[nIndex]))
continue;
// If it's a space, that's fine too
if (filenameIn[nIndex]==' ')
continue;
// Otherwise we're rejecting this input
dataOk = true;
}
}
Well, you have input (for example, asking the user for a file name), output (printing errors), and logic (actually checking if the file is ok), and system calls (like system("pause")) - start by removing the system's, you don't need them. Next move the in and output to another function (for simplicities sake, main will do), and inside the function only work with the passed parameters.
void openFiles(string filenameIn, string filenameOut){
ifstream inFile;
ofstream outFile;
cout << "Please enter the name of the file: " <<endl; //asks user for the file name
getline (cin, filenameIn);
filenameIn += ".txt"; //user doesn't need to put .txt at the end of the file name.
inFile.open(filenameIn.c_str()); //opens input file name
if (inFile.fail()) { //can't find file, it will fail
cout << "Sorry, I was unable to open file." << endl;
exit(1);
}
}
Okay then, this is what I have, the output is suppose to be in another function called printResults anyways. This leaves me with a few questions.
How can I get the program to read the input file?
How can I validate that the character in the input file is an f or m?
How can i validate that the GPA in the input file is a number?
How do i call this function into main?
You call FROM main, not INTO. Calling a function means basically, to jump into the function, do whatever it says inside of the function, and once the function is done return to wherever the function was called from.
Look, it seems you don't understand functions at all right now. You do probably have a text book, right? You should read what it says about functions. Also, the language tutorial on this site might help you: http://cplusplus.com/doc/tutorial/
I'm no teacher, I can't really help you grasp the basics that are required for communication between us.
didn't exactly need the attitude sir, and your right I'm struggling alot.
Yes i do have a textbook.
Sorry I'm not a pro like you.
Thanks though, you did better than my teacher does teaching, she's bad.