splitting a string read from a file/istringstream - c++

I'm currently taking an online C++ class and our current project requires us to read a file, take each student and put it into a vector.
My current problem is splitting the name read and setting it to the right variables.
The professor's pseudo code here:
inputFile.open(sFileName.c_str ());
while(inputFile.fail())
{
cout ERROR OPENING FILE
cout PLEASE REENTER THE PASSWORD OF THE FILE
getline(cin >> wd, sFileName);
inputFile.open(sFileName.c_str());
}
inputFile.clear();
inputFile.seek(0, ios::beg);
while(getline(inputFile, sTemp))
{
istringstream inputSStream(sTemp)
inputSStream >> sFirstName >> sMiddleName >> sLastName >> sID >> sClass;
if(sMiddleName != "|")
sFullName = sFirst name + " " + sMiddleName +" " + sLastName;
else
sFullName = sFirstName + " " + sLastName;
My current code here:
ifstream myFile;
string firstName, middleName, lastName, fullName, studentID, cID;
string inFileName, stringTemp;
cout << "Please enter the name of the file that you want to read in. \n";
cin >> inFileName;
myFile.open(inFileName);
while (myFile.fail())
{
cout << "\n""";
cout << "Error file, Please re-enter file. \n ";
cin >> inFileName;
myFile.open(inFileName);
}
myFile.clear();
myFile.seekg(0, ios::beg);
while (getline(myFile, stringTemp))
{
istringstream inputStream(stringTemp);
inputStream >> firstName >> middleName >> lastName >> studentID >> cID;
if (middleName != "|")
fullName = firstName + " " + middleName + " " + lastName;
else
fullName = firstName + " " + lastName;
//Checking what the input values are
cout << "First name is " << firstName << endl;
cout << "Middle name name is " << middleName << endl;
cout << "Last name is " << lastName << endl;
cout << "studentID is " << studentID << endl;
cout << "CID is " << cID << endl;
Student thisStudent(fullName, studentID, cID);
studentList.push_back(thisStudent);
}
myFile.close();
The current problem is that the line isn't split correctly, first name gets the whole line and mid and last name, studentID and cID all remain empty.
Here is the current file that I'm reading in:
ERIC,ANTHONY,TURNER,1234573,CISC_198
GABRIEL,FEIJO,LOPES,1234574,CISC_199
GEOFFERY,BRYAN,RANSOM,1234575,CISC_200
HANNAH,MAE ,LONGRIE,1234576,CISC_201
HASSAN,ISMAIL,AHMED,1234577,CISC_202
HUNG,B,PHAM,1234578,CISC_203
HUSSEIN,FOUAD,ALJANABI,1234579,CISC_204
JING,XUN,CHEN,1234580,CISC_205
KAJALBEN,CHIMANLAL,MAKWANA,1234581,CISC_206
DANDREA,SHAMAICAH,BUSH,1234570,CISC_195
DANIELLE,MARIE,CORTEZ,1234571,CISC_196
ERDI,T,KIDANE,1234572,CISC_197
AARON,FABIAN,LINGAD,1234567,CISC_192
AARON,T,PATCHIN,1234568,CISC_193
ALI,FOUAD,ALJANABI,1234569,CISC_194
NATHAN,|,NANN,1234585,CISC_210
NEIL,ANDREAS,FRANKA,1234586,CISC_211
OBONE,|,ORIYAVONG,1234587,CISC_212
OLIVIA,JOANNE,MAILANDER,1234588,CISC_213
RALEIGH,|,COSGROVE,1234589,CISC_214
RYAN,PAREDES,PALMARES,1234590,CISC_215
MICHAEL,DUONG,NGUYEN,1234583,CISC_208
MIGLENA,|,CHEMELEKOVA,1234584,CISC_209
STEPHEN,MICHAEL,HOUSE,1234591,CISC_216
MARCUS,D,BUTLER,1234582,CISC_207
Here is the output that I do not understand how to fix.
First name is ERIC,ANTHONY,TURNER,1234573,CISC_198
Middle name name is
Last name is
studentID is
CID is

When you read with operator >> you are splitting on whitespace. Despite the name, getline() can split by other characters than newline:
getline(inputStream, firstName, ',');
getline(inputStream, middleName, ',');
// ...

Related

trouble outputting lines from .txt file using while loop, every word of txt file begins on new line

every word of txt file starts on new line
ex:
first name
last name
first part of phone number
second part of phone number
first part of address
etc..
it should look like:
name
phone number
address
city, state zip
int main()
{
string username, streetAddress, city, state, phoneNum, zip;
ofstream outputFile;
outputFile.open("list.txt");
cout << "Enter your name: ";
getline(cin, username);
outputFile << username << endl;
cout << "Enter your phone number: ";
getline(cin, phoneNum);
outputFile << phoneNum << endl;
cout << "Enter street address: ";
getline(cin, streetAddress);
outputFile << streetAddress << endl;
cout << "Enter your city: ";
getline(cin, city);
outputFile << city << ", ";
cout << "Enter your state: ";
getline(cin, state);
outputFile << state << ", ";
cout << "Enter your zip code: ";
getline(cin, zip);
outputFile << zip;
outputFile.close();
ifstream inputFile;
string list;
inputFile.open("list.txt");
while (inputFile >> list) {
cout << list << endl;
}
inputFile.close();
return 0;
}
Use getline not >>
while (getline(inputFile, list)) {
cout << list << endl;
}
Your version reads a word at a time, and then outputs each word on a separate line.

C++ Spaces are not being read in my input file?

Input txt file:
Joe Smith
Mary Jones
Hamid Namdar
Desired Output Txt file:
Smith Joe
Jones Mary
Namdar Hamid
Output file I receive:
SmitJoeJonesMaryNamdarHamid
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
int main()
{
ofstream output;
ifstream input;
string firstname, lastname;
output.open("LastName.txt");
input.open("FirstName.txt");
cout << "Processing Data..." << endl;
input >> firstname >> lastname;
cout << firstname << lastname << endl;
output << lastname << firstname;
cout << lastname << firstname << endl;
input >> firstname >> lastname;
cout << firstname << lastname << endl;
output << lastname << firstname;
cout << lastname << firstname << endl;
input >> firstname >> lastname;
cout << firstname << lastname << endl;
output << lastname << firstname;
cout << lastname << firstname << endl;
input.close();
output.close();
cin.get();
cin.get();
return 0;
}
My program is required to have spaces between the names, and even though there is a space in my text document, the spaces are not being read. Does anyone have an idea on what I should do in order to have the spaces be read?
I am guessing that you would like to see spaces in your output but you are not getting them. That makes you think that the spaces are not being read. The truth is that the white spaces are being read but are being discarded when you use:
input >> firstname >> lastname;
You need to change the lines that create the output to:
cout << firstname << " " << lastname << endl;
output << lastname << " " << firstname << endl;

C++: Simple fstream not writing to output file (not sure if it's taking the input file)

I have a test file ("InputFile.txt") saved with some random information as well as a blank output file ("output.txt"). When I debug the program and check the output file, it's still blank.
Should "output.out" be "output.txt" (line 25)? Am I having problems because I have "InputFile.txt" as a cstring at line 22 in the if statement?
Is this correct otherwise? I'm sort of lost when it comes to fstream so far, please help.
My code:
#include<fstream>
#include<string>
#include<iomanip>
#include<iostream>
using namespace std;
int main()
{
ifstream InputFile;
ofstream OutputFile;
string lastName, firstName, fileName,
line;
int creditHours, state;
int tuition;
bool valid = false;
do{
cout << "Input file name (.txt): ";
cin >> fileName;
if(fileName == "InputFile.txt")
{
InputFile.open("InputFile.txt");
OutputFile.open("output.out");
valid = true;
}
else{
cout << "Invalid file name/type." << endl;
}
}while(valid == false);
OutputFile << "Last Name: " << ' ' << "First Name: " << ' '
<< "Credit Hours: " << ' ' << "Tuition Amount: " << endl;
while(!InputFile.eof()){
getline(cin, line);
InputFile >> firstName >> lastName >> creditHours >> state;
if(state == 1)
{
tuition = (creditHours * 350);
}
else
{
tuition = (creditHours * 570);
}
OutputFile << lastName << ' ' << firstName << ' '
<< creditHours << ' ' << tuition << endl;
}
InputFile.close();
OutputFile.close();
return 0;
}
Try this
ifstream inFile;
ofstream outFile;
and open your file in and out mode like
inFile.open("InputFile.txt", ios::in);
outFile.open("Output.txt",ios::out);
after that read Inputfile data and write it into output file
while (!inFile.eof()) {
inFile >> firstName >> lastName >> creditHours >> state;
outFile << firstName << " " << lastName<<" "<<creditHours<< " " <<state<<" "<< endl;
}
and apply logic within loop own way

While loop skipping every other input

I am trying to write a program that will open a user input file name and then take the names in that file and arrange them from LastName, FirstName MiddleName to FirstName MiddleName LastName. However I my while loop is skipping every other name in the input file.
Here is my main:
using namespace std;
int main() {
string fullName;
string lastName;
string first_middleName;
size_t pos;
string fileName;
ifstream inData;
cout << "Please type the file name including extension(such as .txt)." << endl;
cout << "If your file is in a different directory please specify the path:";
getline(cin, fileName);
inData.open(fileName.c_str());
if(!inData) {
cout << "Cannot open" << fileName << "." << endl;
return 1;
}
getline(inData, fullName);
while(getline(inData, fullName)) {
pos = fullName.find(',');
lastName = fullName.substr(0, pos);
first_middleName = fullName.substr(pos + 2);
cout << first_middleName << " " << lastName << endl;
getline(inData, fullName);
}
inData.close();
return 0;
}
This is because you read two lines per loop
while(getline(inData,fullName)){
...
getline(inData,fullName);
}

C++: File Processing: matching sequential files

I have a small problem. I have three files, OldMaster, Transaction and NewMaster. If the account numbers match between OldMaster and Transaction, I update the balance and write it to NewMaster. If they don't match, I write the original account info from OldMaster to NewMaster and display an error.
There are 4 accounts in file OldMaster and 4 account transactions in file Transactions. For some reason my program is not processing the fourth (last) account/transaction in each file and NewMaster is not receiving data.
int accountNumber
int accountNum;
string lastName;
string firstName;
float currentBalance;
float dollarAmount;
inOldMaster >> accountNumber >> firstName >> lastName >> currentBalance;
inTransaction >> accountNum >> dollarAmount;
while ( !inOldMaster.eof() && !inTransaction.eof() )
{
if ( accountNumber == accountNum )
{
currentBalance += dollarAmount;
outNewMaster << accountNum << " " << firstName << " " << lastName << " "
<< currentBalance << endl;
}
else if (accountNumber != accountNum)
{
outNewMaster << accountNumber << " " << firstName << " " << lastName << " "
<< currentBalance << endl;
cout << "Unmatched transaction record for account number: " << accountNum
<< endl;
}
inOldMaster >> accountNumber >> firstName >> lastName >> currentBalance;
inTransaction >> accountNum >> dollarAmount;
}
When you read the data from the input files, if end-of-file is reached in the loop, the loop will not continue and so not write out the newly read data.
If your case I would do something like this:
do
{
inOldMaster >> accountNumber >> firstName >> lastName >> currentBalance;
inTransaction >> accountNum >> dollarAmount;
if (inOldMaster.bad() || inTransaction.bad())
break;
// Your old if-statements
} while (inOldMaster.good() && inTransaction.good());