Hello, I have issues for the following piece of code :
Code:
#include <string>
#include <fstream>
#include <iostream>
#include <cstdlib>
#define print( text ) std::cout << text << "\n"
void extract_word( std::string & sentence , std::string & word )
{
print( "extracting word" );
// first, we need to get rid of space if it is there
if( sentence[0] == ' ' )
{
sentence.erase(0, 1 );
}
std::size_t sizeofword = sentence.find_first_of(' ');
word = sentence.substr(0, sizeofword);
sentence.erase( 0 , sizeofword ); // remove each word from the sentence
print(word);
}
void extract_grade( int * array , std::string & sentence )
{
print( "extracting grade");
std::size_t found;
std::size_t bais = 0; // start position
unsigned int count = 0;
int value;
// first, we need to get rid of space if it is there
if( sentence[0] == ' ' )
{
sentence.erase(0, 1 );
}
found = sentence.find(' ', bais );
while(found != std::string::npos) // goes to the end of the string
{
value = std::atoi( sentence.substr( bais , found ).c_str() );
print(value);
array[ count++ ] = value;
bais = found + 1;
found = sentence.find(' ', bais );
}
while( count < 10 )
{
array[ count++ ] = -1;
}
}
void init_program()
{
std::ifstream file("gradebook.txt");
std::string sentence;
std::string fullname[5][2];
int grades[5][10];
unsigned int count = 0;
if( !file.is_open())
{
std::cout << "Problem opening file.\n"<<std::endl;
return;
}
while( std::getline(file, sentence) )
{
sentence.push_back(' ');
print("extracting info");
print("getting firstname ");
// get first name
extract_word(sentence, fullname[ count++ ][ 0 ]);
print("getting lastname ");
// get second name
extract_word(sentence, fullname[ count++ ][ 1 ]);
print(sentence);
print("getting grades ");
// get grades
extract_grade( &grades[ count++][0] , sentence );
print("results");
print( grades[count][0] );
print(" going to next person");
}
std::cout << "this is the check \n";
// will check here
std::cout<< " this is the first name "<< fullname[4][0] << " this is the last name " << fullname[4][1] << "\n";
file.close();
}
int main(void)
{
init_program();
return 0;
}
I get errors retrieving the grade values extracted, and then I get a run time fault.
any suggestions?