Working on an assignment for a c++ class, and I've been running into some trouble. I'm sure this is probably a very simple issue, but I'm horrible at programming, so bear with me. Here is the exercise guidelines:
You are given a file consisting of students' names in the following form: lastName, firstName midddleName. (Note that a student may not have a middle name.) Write a program that converts each name to the following form: firstName middleName lastName. Your program must read student's entire name in a variable and must consist of a function that takes as input a string, consists of a student's name, and returns the string consisting of the altered name. Use the string function find to find the inex of ,; the function length to find the length of the string; and the function substr to extract the firstName, middleName, and lastName.
The input file looks like this:
Miller, Jason Brian
Blair, Lisa Maria
Gupta, Anil Kumar
Arora, Sumit Sahil
Saleh, Rhonda Beth
I made an attempt to code at least the extraction and printing of the names, but failed miserably. My output was " Miller". I really don't understand the flaws in my logic, which I know is pathetic, but I would really appreciate any assistance that can be offered. Here's my code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
|
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
void getName();
int main()
{
getName();
system("pause");
return 0;
}
void getName()
{
string fullName;
ifstream inData;
do
{
string lastName;
string otherName;
int junk;
int otherJunk;
inData.open("Exercise9Page518.txt");
inData >> fullName;
junk = fullName.find(",");
lastName = fullName.substr(0, junk);
otherJunk = fullName.find("\n");
otherName = fullName.substr(junk + 1, otherJunk);
cout << otherName << " " << lastName << endl;
}
while (fullName != "");
inData.close();
}
|