Please help. I have an assignment for a class and I have been working on this for a week. Nearly all of it I finished in two days but I have been stuck with this problem for 5 days now. I am using MS VC++.

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?

I have to complete this project as a blank template and not a win32 template, I have been told to do an #include "stdafx.h", but I guess that did not work unless I am going to use the win32.

So how can I get this to work starting with a blank template? I don't need help with file output, just truncating the StudentID string.

I have attached a sample text file.

#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";
        // Now I need to pare down StudentID to only the last three numbers of the student ID
        //concatonate strings to make email address
        //output to file
                         }

            //myfile.close();
          }

    else cout << "Unable to open file";

    system ("pause");
    return 0;

}
Attachments
Ryan,P,Lee,06698
Jayden,F,Garcia,69425
Sophia,C,Davis,84833
Madison,K,Smith,21582
Sarah,A,Johnson,80226
Mariana,G,Rodriguez,31295
Liam,E,Brown,53869
Michael,J,Williams,72770
Tiffany,F,Nguyen,36606
Ava,L,Torres,77521
William,O,Brown,14077
Ruby,C,Jones,11223
Angelica,A,Wilson,28817
Oliver,L,Miller,47356
Mia,V,Hernandez,22931

Please help. I have an assignment for a class and I have been working on this for a week. Nearly all of it I finished in two days but I have been stuck with this problem for 5 days now. I am using MS VC++.

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?

I have to complete this project as a blank template and not a win32 template, I have been told to do an #include "stdafx.h", but I guess that did not work unless I am going to use the win32.

So how can I get this to work starting with a blank template? I don't need help with file output, just truncating the StudentID string.

I have attached a sample text file.

#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";
        // Now I need to pare down StudentID to only the last three numbers of the student ID
        //concatonate strings to make email address
        //output to file
                         }

            //myfile.close();
          }

    else cout << "Unable to open file";

    system ("pause");
    return 0;

}

Looks good, mostly. The problem is case 1 and case 2, and default. At the end of each loop, you're properly setting start to end + 1. And thus when your loop begins again, you're properly searching for the next comma and giving end that value. So, since start and end are always properly set, you should not have "end - start" anywhere in your code. It should just be substr(start, end).

If end is equal to 10, and start is equal to 8, then what will substr(8, 10-8) do? Probably nothing good.

" NewStudentID=StudentID.substr(2,3) " --There is nothing wrong with this code. If it doesn't work, then StudentID is not set properly.

EDIT: Blank template can be found in the menu when you go to create a project. It's listed under "Other" as "Blank Solution". Create one of these, then you can add your own cpp file or whatever you wish by right clicking on the project in the left margin.

To my understanding, #include stdafx.h is for precompiled header use. You can enable or disable precompiled headers SOMEWHERE in MS VS, if you want, I think again by right clicking on your project in the left margin and going to the properties screen. Look around, you'll find it, eventually. I recommend disabling it for now--you don't need that. Then you never have to worry about that extra include.

Edited 5 Years Ago by Greywolf333: n/a

OK I tried it with just "end" and it garbled my output. Here are my errors when I have "end-start":

'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 0x002bf604..
Unhandled exception at 0x7693fc56 in Week4 FileIO.exe: Microsoft C++ exception: std::out_of_range at memory location 0x002bf604..
The program '[4408] Week4 FileIO.exe: Native' has exited with code -529697949 (0xe06d7363).

And here are my errors when I just have "end":


'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 0x0013f5a0..
Unhandled exception at 0x7693fc56 in Week4 FileIO.exe: Microsoft C++ exception: std::out_of_range at memory location 0x0013f5a0..
The program '[6400] Week4 FileIO.exe: Native' has exited with code -529697949 (0xe06d7363).

Edited 5 Years Ago by addision: n/a

You really have been a lot of help, and I am learning new ways to look at things.

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

Edited 5 Years Ago by addision: n/a

Try placing below code , just after getline (myfile,line);

if(line.empty() == true)
		{
			cout<<"no data in file to read";
			break;
		}

it will help in you getting the garbage data in the end.

OK I tried it with just "end" and it garbled my output. Here are my errors when I have "end-start":

'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 0x002bf604..
Unhandled exception at 0x7693fc56 in Week4 FileIO.exe: Microsoft C++ exception: std::out_of_range at memory location 0x002bf604..
The program '[4408] Week4 FileIO.exe: Native' has exited with code -529697949 (0xe06d7363).

And here are my errors when I just have "end":


'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 0x0013f5a0..
Unhandled exception at 0x7693fc56 in Week4 FileIO.exe: Microsoft C++ exception: std::out_of_range at memory location 0x0013f5a0..
The program '[6400] Week4 FileIO.exe: Native' has exited with code -529697949 (0xe06d7363).

Yeah, I missed that too. Ignoring lines that are empty like tajendra is part of it. Keep in mind that you can also use the >> extraction operator to automatically skip whitespace and empty lines.

end=inputstring.find_first_of(",", start);

If a "," is not found, find will set "end" to a value called string::npos. Which is the highest possible value for an integer. Yes it's strange, but that's what it does. So you have to add some checking for that otherwise you're going to do a substr that's like substr(start, 32738492074320) or some such. And clearly that's no good. So you have to check for that immediately after your find.

if (end == string::npos) end = string.size() - 1;
So if there's no comma, instead have end set to the last position in the string.

You may want to check to make sure "start = end + 1" does not cause start to be larger than the size() - 1 too.

This question has already been answered. Start a new discussion instead.