Need help getting characters from a string

Discussion in 'C++' started by addision, Sep 13, 2011.

  1. addision

    addision New Member

    Joined:
    Sep 13, 2011
    Messages:
    7
    Likes Received:
    0
    Trophy Points:
    0
    I am having quite a bit of trouble. I can parse a string that I have read from a line of a text file using substr, however, when I want to further extract characters from one component of the parsed string "StudentID" (using another substr) I always get errors.

    I have attempted the following to get the last three characters of a 5 digit student ID; NewStudentID=StudentID.substr(2,3). This does not work.

    Also, when I output to the screen, the loop appears to continue to run for one or two cycles and outputs blank characters. I don't understand why. Can anyone help me out with this?

    Code:
    #include <iostream>
    #include <string>
    #include <fstream>
    using namespace std;
    
    int main(int argc, char* argv [])
    {
    string inputstring;
     string line;
     ifstream myfile ("W04In.txt");
      if (myfile.is_open())
              {
            while ( myfile.good() )
                {
            getline (myfile,line);
                             
            string inputstring (line);
    
            string FirstName,MiddleInitial,LastName,StudentID;
            size_t start, end;
            start = 0; //initial start value
    
            for (int counter=0;counter<4;counter++)
            {
            end=inputstring.find_first_of(",", start);
            switch(counter)
                    {
            case(0): FirstName=inputstring.substr(start,end);
                    break;
            case(1): MiddleInitial=inputstring.substr(start,end-start);
                    break;
            case(2): LastName=inputstring.substr(start,end-start);
                    break;
            default: StudentID=inputstring.substr(start,end-start);
                    }
                start=end+1;
            }
                                
    cout << LastName << ", " << FirstName<< ",  "<< FirstName  <<LastName<<StudentID<<"@student.college.net\n\n";
            //pare down StudentID to the last three numbers
            //concatonate strings to make email address
            //output to file
                             }
    
                //myfile.close();
              }
    
        else cout << "Unable to open file";
    
        system ("pause");
        return 0;
    
    }
     
    Last edited by a moderator: Sep 13, 2011
  2. poornaMoksha

    poornaMoksha New Member

    Joined:
    Jan 29, 2011
    Messages:
    150
    Likes Received:
    33
    Trophy Points:
    0
    Occupation:
    Software developer
    Location:
    India
    Code:
    #include "stdafx.h"
    #include <iostream>
    #include <string>
    #include <fstream>
    using namespace std;
    
    
    int _tmain(int argc, _TCHAR* argv[])
    {
    	string inputstring;
    string line;
    ifstream myfile ("C:\\W04In.txt");
    if (myfile.is_open())
    {
    while ( myfile.good() )
    {
    getline (myfile,line);
    
    string inputstring (line);
    
    string FirstName,MiddleInitial,LastName,StudentID;
    size_t start, end;
    start = 0; //initial start value
    
    for (int counter=0;counter<4;counter++)
    {
    end=inputstring.find_first_of(",", start);
    switch(counter)
    {
    case(0): FirstName=inputstring.substr(start,end);
    break;
    case(1): MiddleInitial=inputstring.substr(start,end-start);
    break;
    case(2): LastName=inputstring.substr(start,end-start);
    break;
    default: StudentID=inputstring.substr(start,end-start);
    }
    start=end+1;
    }
    string NewStudentID=StudentID.substr(2,3);
    cout << LastName << ", " << FirstName<< ", "<< FirstName <<LastName<<StudentID<<"@student.college.net\n\n ";
    cout << LastName << ", " << FirstName<< ", "<< FirstName <<LastName<<NewStudentID<<"@student.college.net\n\n ";
    //pare down StudentID to the last three numbers
    //concatonate strings to make email address
    //output to file
    }
    
    //myfile.close();
    }
    
    else cout << "Unable to open file";
    
    system ("pause");
    return 0;
    }
    This is what I compiled and executed on my Visual Studio 2005 and I got the following results :
    The text file that I used contained :
    I didn't get any (compile time or run time) errors. Did I miss something? Can you elaborate your problem a bit more?
     
  3. addision

    addision New Member

    Joined:
    Sep 13, 2011
    Messages:
    7
    Likes Received:
    0
    Trophy Points:
    0
    Yes, thank you for working on this. When I run the file in VC++ 2010 This is the result I receive from my text file. As you can see, I only get the full 5 digits of the student ID and not the truncated 3 digits. If you can also see, at the end of the screen it gives me an empty set for the output "
    , , @student.college.net".

    I am really excited it works for you. I Just don't know why the 3 digits won't truncate on my machine.


    I copied this from the output screen:


    Lee, Ryan, RyanLee06698@student.college.net

    Garcia, Jayden, JaydenGarcia69425@student.college.net

    Davis, Sophia, SophiaDavis84833@student.college.net

    Smith, Madison, MadisonSmith21582@student.college.net

    Johnson, Sarah, SarahJohnson80226@student.college.net

    Rodriguez, Mariana, MarianaRodriguez31295@student.college.net

    Brown, Liam, LiamBrown53869@student.college.net

    Williams, Michael, MichaelWilliams72770@student.college.net

    Nguyen, Tiffany, TiffanyNguyen36606@student.college.net

    Torres, Ava, AvaTorres77521@student.college.net

    Brown, William, WilliamBrown14077@student.college.net

    Jones, Ruby, RubyJones11223@student.college.net

    Wilson, Angelica, AngelicaWilson28817@student.college.net

    Miller, Oliver, OliverMiller47356@student.college.net

    Hernandez, Mia, MiaHernandez22931@student.college.net

    , , @student.college.net

    Press any key to continue . . .
     
  4. addision

    addision New Member

    Joined:
    Sep 13, 2011
    Messages:
    7
    Likes Received:
    0
    Trophy Points:
    0
    If it helps, here is the text file.
     

    Attached Files:

  5. poornaMoksha

    poornaMoksha New Member

    Joined:
    Jan 29, 2011
    Messages:
    150
    Likes Received:
    33
    Trophy Points:
    0
    Occupation:
    Software developer
    Location:
    India
    Have you added the following piece of code to your program?
    Code:
    string NewStudentID=StudentID.substr(2,3);
    cout << LastName << ", " << FirstName<< ", "<< FirstName <<LastName<<NewStudentID<<"@student.college.net\n\n ";
    You can take reference from the running program that i posted in my first reply.
     
    Last edited: Sep 13, 2011
  6. addision

    addision New Member

    Joined:
    Sep 13, 2011
    Messages:
    7
    Likes Received:
    0
    Trophy Points:
    0
    Ok, first let me say that you are a life saver. I have been working on this for five days now. I added the new lines of code:

    #include "stdafx.h" // I added this as the first line of code



    string NewStudentID=StudentID.substr(2,3);
    cout << LastName << ", " << FirstName<< ", "<< FirstName <<LastName<<NewStudentID<<"@student.college.net\n\n";


    I ran the code and it says it can't find the file ""stdafx.h". I googled the problem and I guess I need to start my project as a win32 console or such. The problem is...I have to use a blank project as this is an assignment for a class.

    How can I get "stdafx.h" to work when I am using a blank project as a template?

    Thank you
     
  7. poornaMoksha

    poornaMoksha New Member

    Joined:
    Jan 29, 2011
    Messages:
    150
    Likes Received:
    33
    Trophy Points:
    0
    Occupation:
    Software developer
    Location:
    India
    try this :

    Code:
    #include <iostream>
    #include <string>
    #include <fstream>
    using namespace std;
    
    
    int main(int argc, char* argv[])
    {
    	string inputstring;
    string line;
    ifstream myfile ("C:\\W04In.txt");
    if (myfile.is_open())
    {
    while ( myfile.good() )
    {
    getline (myfile,line);
    
    string inputstring (line);
    
    string FirstName,MiddleInitial,LastName,StudentID;
    size_t start, end;
    start = 0; //initial start value
    
    for (int counter=0;counter<4;counter++)
    {
    end=inputstring.find_first_of(",", start);
    switch(counter)
    {
    case(0): FirstName=inputstring.substr(start,end);
    break;
    case(1): MiddleInitial=inputstring.substr(start,end-start);
    break;
    case(2): LastName=inputstring.substr(start,end-start);
    break;
    default: StudentID=inputstring.substr(start,end-start);
    }
    start=end+1;
    }
    string NewStudentID=StudentID.substr(2,3);
    cout << LastName << ", " << FirstName<< ", "<< FirstName <<LastName<<StudentID<<"@student.college.net\n\n ";
    cout << LastName << ", " << FirstName<< ", "<< FirstName <<LastName<<NewStudentID<<"@student.college.net\n\n ";
    //pare down StudentID to the last three numbers
    //concatonate strings to make email address
    //output to file
    }
    
    //myfile.close();
    }
    
    else cout << "Unable to open file";
    
    system ("pause");
    return 0;
    }
    and please share the output...
     
    Last edited: Sep 13, 2011
  8. addision

    addision New Member

    Joined:
    Sep 13, 2011
    Messages:
    7
    Likes Received:
    0
    Trophy Points:
    0
    copied your code into my window and here are my debug errors, the program gets the error after the las line of good text from the .txt file:


    'Week4 FileIO.exe': Loaded 'C:\Users\School\Documents\Visual Studio 2010\Projects\Week4 FileIO\Debug\Week4 FileIO.exe', Symbols loaded.
    'Week4 FileIO.exe': Loaded 'C:\Windows\System32\ntdll.dll', Cannot find or open the PDB file
    'Week4 FileIO.exe': Loaded 'C:\Windows\System32\kernel32.dll', Cannot find or open the PDB file
    'Week4 FileIO.exe': Loaded 'C:\Program Files\AVAST Software\Avast\snxhk.dll', Cannot find or open the PDB file
    'Week4 FileIO.exe': Loaded 'C:\Windows\System32\msvcp100d.dll', Symbols loaded.
    'Week4 FileIO.exe': Loaded 'C:\Windows\System32\msvcr100d.dll', Symbols loaded.
    First-chance exception at 0x7693fc56 in Week4 FileIO.exe: Microsoft C++ exception: std::eek:ut_of_range at memory location 0x001bf4a8..
    Unhandled exception at 0x7693fc56 in Week4 FileIO.exe: Microsoft C++ exception: std::eek:ut_of_range at memory location 0x001bf4a8..
    The program '[6784] Week4 FileIO.exe: Native' has exited with code 0 (0x0).
     
  9. poornaMoksha

    poornaMoksha New Member

    Joined:
    Jan 29, 2011
    Messages:
    150
    Likes Received:
    33
    Trophy Points:
    0
    Occupation:
    Software developer
    Location:
    India
    Can you tell me exactly at which line in the source code this exception is being thrown??
     
  10. addision

    addision New Member

    Joined:
    Sep 13, 2011
    Messages:
    7
    Likes Received:
    0
    Trophy Points:
    0
    Maybe, I am not that familiar enough to know where that is but I will try. It errors out to a file xthrow.cpp, and this is the line of code in xtrow.cpp:

    _CRTIMP2_PURE __declspec(noreturn) void __CLRCALL_PURE_OR_CDECL _Xout_of_range(_In_z_ const char * _Message)
    { // report an out_of_range error
    _THROW_NCEE(out_of_range, _Message);
    }


    But I am not sure that is what you want. To me it appears that something is going wrong after it reads the last line of text from the file and then wants to start executing "start=end+1" for the last time. I think it wants to continue, but there are return no values and gets a memory error.
     
  11. poornaMoksha

    poornaMoksha New Member

    Joined:
    Jan 29, 2011
    Messages:
    150
    Likes Received:
    33
    Trophy Points:
    0
    Occupation:
    Software developer
    Location:
    India
    Ok, if thats the case, just change the 'default' case to :

    Code:
    default: StudentID=inputstring.substr(start,inputstring.length()-start);
    and the try once again
     
    Last edited: Sep 13, 2011
  12. addision

    addision New Member

    Joined:
    Sep 13, 2011
    Messages:
    7
    Likes Received:
    0
    Trophy Points:
    0
    Nope that didn't work either.

    I think I found a problem, when the program reaches the end of my .txt file list it appears to go back to my text file one more time and it returns an empty string right here:


    getline (myfile,line);

    string inputstring (line);


    What I would like to do is test this each time to determine if the string is empty and if so then

    system ("pause");
    return 0;

    I have tried both of these and my program completely disregards them:

    if (inputstring.empty ()); AND
    if (inputstring == "");

    Neither appear to work.
     
  13. poornaMoksha

    poornaMoksha New Member

    Joined:
    Jan 29, 2011
    Messages:
    150
    Likes Received:
    33
    Trophy Points:
    0
    Occupation:
    Software developer
    Location:
    India
    give me the logs(upto the point at which crash occurs) after running the following :
    Code:
    #include <iostream>
    #include <string>
    #include <fstream>
    using namespace std;
    
    
    int main(int argc, char* argv[])
    {
    string inputstring;
    string line;
    ifstream myfile ("C:\\W04In.txt");
    if (myfile.is_open())
    {
    while ( myfile.good() )
    {
    getline (myfile,line);
    cout<<"DEBUG::"<<line;
    cout<<"\n";
    string inputstring (line);
    
    string FirstName,MiddleInitial,LastName,StudentID;
    size_t start, end;
    start = 0; //initial start value
    
    for (int counter=0;counter<4;counter++)
    {
    end=inputstring.find_first_of(",", start);
    switch(counter)
    {
    case(0): {FirstName=inputstring.substr(start,end); cout<<"\n DEBUG:: Firstname done\n";}
    break;
    case(1): MiddleInitial=inputstring.substr(start,end-start); cout<<"\n DEBUG:: MiddleInitial done\n";}
    break;
    case(2): LastName=inputstring.substr(start,end-start); cout<<"\n DEBUG:: LastName done\n";}
    break;
    default: StudentID=inputstring.substr(start,inputstring.length()-start); cout<<"\n DEBUG:: studentID done\n";}
    }
    cout<<"DEBUG::"<<"start="<<start<<"end="<<end<<"strLen="<<inputstring.length();
    cout<<"\n\n";
    start=end+1;
    }
    string NewStudentID=StudentID.substr(2,3);
    cout << LastName << ", " << FirstName<< ", "<< FirstName <<LastName<<StudentID<<"@student.college.net\n\n ";
    cout << LastName << ", " << FirstName<< ", "<< FirstName <<LastName<<NewStudentID<<"@student.college.net\n\n ";
    //pare down StudentID to the last three numbers
    //concatonate strings to make email address
    //output to file
    }
    
    //myfile.close();
    }
    
    else cout << "Unable to open file";
    
    system ("pause");
    return 0;
    }
    Sorry as Windows PC was unavailable at my home so could not compile and chk the changes, please correct some compile time errors (if any) and share me the logs.
     
    Last edited: Sep 14, 2011

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice