Thread: issue with retrieving values and run time error

  1. #1
    Registered User
    Join Date
    Oct 2006
    Location
    New York
    Posts
    124

    issue with retrieving values and run time error

    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?
    Attached Files Attached Files

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I suggest defining a structure like this:
    Code:
    struct student
    {
        std::string first_name;
        std::string last_name;
        std::vector<int> grades;
    };
    Overload operator>> to read into a student object. Reading into first_name and last_name would then be easy: just use the overloaded operator>> for std::string. Likewise, use the overloaded operator>> to read into an int, then push_back the int into the grades vector. You can either assume that there will be exactly 10 grades, or you can just keep reading grades until an error or end of file.

    Then, you create a std::vector<student>. If you are not assuming that there will be exactly 10 grades, then instead of using operator>> to read student by student, you then use getline to read into a string like what you did. The trick is that after reading into the string, you then create a stringstream from the string, and then read from the stringstream into a student object, then use push_back to append it to the vector of student objects.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Oct 2006
    Location
    New York
    Posts
    124
    Quote Originally Posted by laserlight View Post
    I suggest defining a structure like this:
    Code:
    struct student
    {
        std::string first_name;
        std::string last_name;
        std::vector<int> grades;
    };
    Overload operator>> to read into a student object. Reading into first_name and last_name would then be easy: just use the overloaded operator>> for std::string. Likewise, use the overloaded operator>> to read into an int, then push_back the int into the grades vector. You can either assume that there will be exactly 10 grades, or you can just keep reading grades until an error or end of file.

    Then, you create a std::vector<student>. If you are not assuming that there will be exactly 10 grades, then instead of using operator>> to read student by student, you then use getline to read into a string like what you did. The trick is that after reading into the string, you then create a stringstream from the string, and then read from the stringstream into a student object, then use push_back to append it to the vector of student objects.
    Well, I found out I was getting the values, but
    every third iteration the program would just hang or crash. It gets out of the extract with values, but it hangs after "going to next person"

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You are probably accessing the fullname array out of bounds since you keep incrementing count when you should only do it once at the end of the loop.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting current time issue
    By Lawrence Chua in forum C Programming
    Replies: 8
    Last Post: 12-06-2012, 03:16 PM
  2. Replies: 3
    Last Post: 06-21-2012, 01:16 AM
  3. stat() returning same values all the time
    By pamplemoose in forum C Programming
    Replies: 6
    Last Post: 02-24-2008, 10:24 PM
  4. Storing different values of time
    By stellastarr in forum C Programming
    Replies: 3
    Last Post: 02-08-2006, 10:38 AM