Error reading from file
Nov 15, 2014 at 2:45am UTC
I'm attempting to read from a file that has two lines of data, a date and a weight. It loads the first data set correctly, but when I try to output vector[1], I get an error. I'm not sure what is going wrong here?
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 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
#include <iostream>
#include <fstream>
#include <stdlib.h>
#include <windows.h>
#include <vector>
using namespace std;
struct weighday {
int weight;
string date;
};
void check_for_existing_file(vector <weighday>& weight_history, const string& document);
char get_action();
void execute_action(const char action, bool & endProgram);
int main(){
SetConsoleTitle("Snake Weigh In" );
vector <weighday> weight_history;
string document = "Snake weight sheet.txt" ;
check_for_existing_file(weight_history, document);//does file exist? if not, create it
cout << weight_history[0].date << " " << weight_history[0].weight << endl;
cout << weight_history[1].date << " " << weight_history[1].weight << endl;
bool endProgram = false ;
while ( !endProgram ){
execute_action(get_action(), endProgram);
}
return 0;
}
void check_for_existing_file(vector <weighday>& weight_history, const string& document){ //THIS IS THE FUNCTION I'M HAVING TROUBLE WITH <<<<<---------------
ifstream findFile;
findFile.open( document.c_str() );
if ( findFile.good() ){ // check if file exists, if it exists, check if it has data in it, if it does, read it into vector
const int emptyFile = 0;
string checkIfEmpty;
int fileSize;
getline(findFile, checkIfEmpty);
fileSize = checkIfEmpty.size();
if ( fileSize != emptyFile ){
while ( !findFile.eof() ){
weighday* load = new weighday;
findFile >> load->date >> load->weight;
weight_history.push_back(*load);
delete load;
}
cout << "File loaded!" << endl;
}
else {
cout << "File found!" << endl;
}
}
else {
ofstream createFile;
createFile.open( document.c_str() );
createFile.close();
cout << "File created!" << endl;
}
}
char get_action(){
bool choiceMade = false ;
char userInput;
char confirmation;
cout << "Please choose from the following choices by typing the corresponding letter." << endl;
cout << endl << "A) View weight history" << endl;
cout << "B) Make a new entry" << endl;
cout << "C) Quit program" << endl;
while ( !choiceMade ){
cin >> userInput;
userInput = toupper(userInput);
cout << "'" << userInput << "' is that correct? (Y/N)" << endl;
cin >> confirmation;
confirmation = toupper(confirmation);
if ( confirmation == 'Y' ){
choiceMade = true ;
return userInput;
}
else {
cout << "Please reenter your choice." << endl;
}
}
}
void execute_action(const char action, bool & endProgram){
switch ( action ){
case 'A' :
break ;
case 'B' :
break ;
case 'C' : endProgram = true ;
default : break ;
}
}
Last edited on Nov 15, 2014 at 2:46am UTC
Nov 15, 2014 at 5:06am UTC
Bump
Nov 15, 2014 at 5:41am UTC
Anyone see whats wrong?
Topic archived. No new replies allowed.