I'm trying to allow the user to modify values from a data file. I'm using class members to hold the value as well as an array
#include <iostream>
#include <fstream>
using namespace std;
class student
{
private:
string stuID;
string lastN;
string firstN;
float GPA;
int numb_of_enrolled;
public:
void displayAll();
void setAll(string StuID, string LastN, string FirstN, float gpa, int nof);
};
const int SIZE = 30;
int main()
{
ifstream inFile;
int option, count, scount = 0, cIndex;
student stuFile[SIZE];
string sID, sLN, sFN;
float sgpa;
int snof;
char repeat = 'y';
inFile.open("Students.dat", ios::in);
cout << "MENU\n";
cout << "3. Change an Existing Record's Fields\n";
cout << "What would you like to do?: ";
cin >> option;
cin.ignore();
switch(option) {
case 3:
inFile >> sID >> sLN >> sFN >> sgpa >> snof;
cout << "There are 19 Students in this data file\n";
cout << "Enter an Index from 0 - 19: ";
cin >> cIndex;
cin.ignore();
stuFile[cIndex].setAll(sID, sLN, sFN, sgpa, snof);
stuFile[cIndex].displayAll();
}
}
void student::displayAll()
{
cout << "Student ID: " << stuID << endl;
cout << lastN << ", " << firstN << endl;
cout << numb_of_enrolled << " Classes taken this Semester\n";
cout << "Current GPA is: " << GPA << "\n\n";
}
void student::setAll(string StuID, string LastN, string FirstN, float gpa, int nof)
{
stuID = StuID;
lastN = LastN;
firstN = FirstN;
GPA = gpa;
numb_of_enrolled = nof;
}
So the data file has 19 records inside of it. Case 3 works but not how it should. If I enter 3, it doesn't go to the third record inside of it but it stays at 0. Even if I use .eof(), it would still be stuck at element 0. How can I make it so that I'm able to get to the record I want?
You need to loop for N lines for the cIndex, declare int i and do this code
case 3:
cout << "There are 19 Students in this data file\n";
cout << "Enter an Index from 0 - 19: ";
cin >> cIndex;
cin.ignore();
for(i=0;i<cIndex;i++) {
inFile >> sID >> sLN >> sFN >> sgpa >> snof;
}
stuFile[cIndex].setAll(sID, sLN, sFN, sgpa, snof);
stuFile[cIndex].displayAll();
inFile.clear();
inFile.seekg(0);
It could not work if I type the Name with the space such as Peter Stroll. How to use cin.ignore() in this case?
Thank you in advance.
int main()
{
ofstream File("player.txt");
cout << "Enter player ID, Name, and Money:" << endl;
cout << "Press Ctrl + Z to quit \n" << endl;
int id;
string name;
int age;
while(cin >> id >> name >> age)
{
File << id << " " << name << " " << age << endl;
}
return 0;
}
cin won't accept any more character if it encounters tab or space key.
use getline instead
char name[64] = {0};
cin.getline(name, 64);
#include <iostream>
#include <string>
using namespace std;
struct Student
{
int ID;
long phno;
string name;
string depart;
string email;
};
int main ()
{
Student S1 ;
cout << "\n=======================================================\n" ;
cout << "Enter ID no. of student 1 : " ; cin >> S1.ID ;
cout << "Enter name of student 1 : " ; getline(cin, S1.name) ; cin.ignore();
cout << "Enter department of student 1 : " ; getline(cin, S1.depart) ; cin.ignore();
cout << "Enter email adress of student 1 : " ; getline(cin, S1.email) ; cin.ignore();
cout << "Enter phone number of student 1 : " ; cin >> S1.phno ;
return 0;
}
The problem is that it's not taking input after email adress the program is ignoring to take input in phno, directly exit after emailadress.
I made some slight modifications to your code.
Note that I only call cin.ignore() after I use cin directly on a variable (cin >> S1.ID or cin >> S1.phno).
This is because when you use cin on an int, it leaves the \n in the buffer. When you later call getline(cin,...) you just suck up that leftover \n, and that's considered your whole "line."
A working example is available here.
#include <iostream>
#include <string>
using namespace std;
struct Student
{
int ID;
long phno;
string name;
string depart;
string email;
};
int main ()
{
Student S1 ;
cout << "\n=======================================================\n" ;
cout << "Enter ID no. of student 1 :\n" ;
cin >> S1.ID ;
cin.ignore();
cout << "Enter name of student 1 :\n" ;
getline(cin, S1.name) ;
cout << "Enter department of student 1 :\n" ;
getline(cin, S1.depart) ;
cout << "Enter phone number of student 1 :\n" ;
cin >> S1.phno ;
cin.ignore();
cout << "Enter email adress of student 1 :\n" ;
getline(cin, S1.email) ;
cout << endl << endl;
cout << S1.ID << endl << S1.name << endl << S1.depart << endl << S1.phno << endl << S1.email << endl;
return 0;
}
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, ',');
// ...
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());