Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C++ (http://www.go4expert.com/forums/cpp/)
-   -   Need help getting characters from a string (http://www.go4expert.com/forums/help-getting-characters-string-t26679/)

addision 13Sep2011 09:37

Need help getting characters from a string
 
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;

}


poornaMoksha 13Sep2011 10:20

Re: Need help getting characters from a string
 
Code: c++

#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 :
Quote:

paul,joseph,laker,21345
I didn't get any (compile time or run time) errors. Did I miss something? Can you elaborate your problem a bit more?

addision 13Sep2011 10:37

Re: Need help getting characters from a string
 
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 . . .

addision 13Sep2011 10:43

Re: Need help getting characters from a string
 
1 Attachment(s)
If it helps, here is the text file.

poornaMoksha 13Sep2011 10:54

Re: Need help getting characters from a string
 
Have you added the following piece of code to your program?
Code: c++

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.

addision 13Sep2011 12:14

Re: Need help getting characters from a string
 
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

poornaMoksha 13Sep2011 13:56

Re: Need help getting characters from a string
 
try this :

Code: c++

#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...

addision 13Sep2011 14:10

Re: Need help getting characters from a string
 
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::out_of_range at memory location 0x001bf4a8..
Unhandled exception at 0x7693fc56 in Week4 FileIO.exe: Microsoft C++ exception: std::out_of_range at memory location 0x001bf4a8..
The program '[6784] Week4 FileIO.exe: Native' has exited with code 0 (0x0).

poornaMoksha 13Sep2011 14:37

Re: Need help getting characters from a string
 
Can you tell me exactly at which line in the source code this exception is being thrown??

addision 13Sep2011 15:00

Re: Need help getting characters from a string
 
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.


All times are GMT +5.5. The time now is 05:08.