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;
}