Arrays and Searching Them - c++

I am trying to finish my final debugging tonight. My problem is that I have been writing this code for a couple days and it has a few problems. Previous Post
It now compiles and does not crash, but there are a few issues with my functions not working properly (or at all).
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
string bookTitle [50];
string bookAuthor [50];
int loadData (string pathname);
int showall (int counter);
int authorSearch (string bookAuthor [50]);
int main ()
{
string pathname;
int counter=0;
char choice;
cout<<"Input the name of the file to be accessed: ";
cin>>pathname;
loadData (pathname);
showall (counter);
cout<<"\n\n\n\n What would you like to do \n (A for Author Search , T for Title Search, Q to quit):";
cin>>choice;
while (choice != 'Q' , choice != 'q')
{
if (choice == 'A', choice == 'a')
{
int authorSearch (string bookAuthor [50], char choice);
}
if (choice == 'T', choice == 't')
{
int titleSearch (string bookTitle [50], char choice);
}
}
cout<<"Press <Enter> to Exit";
cin.ignore();
cin.get();
return 0;
cout<<"Press <Enter> to Exit";
cin.ignore();
cin.get();
return 0;
}
int loadData (string pathname) // Loads data from infile into arrays
{
fstream infile;
int counter = 0;
infile.open(pathname.c_str()); //Opens file from user input in main
if( infile.fail() )
{
cout << "File failed to open";
return 0;
}
while (!infile.eof())
{
infile >> bookTitle [counter] ; //takes input and puts into parallel arrays
infile >> bookAuthor [counter];
counter++;
}
infile.close();
}
int showall (int counter) // shows input in title(author) format
{
cout<<bookTitle<<"("<<bookAuthor<<")";
}
void authorSearch (string bookAuthor [50], char choice) // Function to search Author Array
{
string target = "";
cout<<"Which author would you like to search for: "<<target; //input
for (int count = 0; count++;)
{
if(bookAuthor[count] == target) //tests input against array and outputs result
{
cout<<bookTitle[count]<<bookAuthor[count];
}
}
}
void titleSearch (string bookTitle [50], char choice) // Function to Serch Title Array
{
string target = "";
cout<<"Which author would you like to search for: "<<target; //input
for (int count = 0; count++;)
{
if(bookAuthor[count] == target) //tests input against array and outputs result
{
cout<<bookTitle[count]<<bookAuthor[count];
}
}
}
Latest Version , no major improvements. I am having trouble getting the functions to work after the menu selection. ShowAll seems to work but outputs hex. Thanks again everyone!
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
string bookTitle [50];
string bookAuthor [50];
int loadData (string pathname);
int showall (int counter);
void authorSearch (string bookAuthor [50]);
void titleSearch (string bookTitle [50]);
int main ()
{
string pathname;
int counter=0;
char choice;
cout<<"Input the name of the file to be accessed: ";
cin>>pathname;
loadData (pathname);
showall (counter);
cout<<"\n\n\n\n What would you like to do \n (A for Author Search , T for Title Search, Q to quit):";
cin>>choice;
while (choice != 'Q'|| choice != 'q')
{
if (choice == 'A'|| choice == 'a')
{
void authorSearch (string bookAuthor [50], char choice);
}
if (choice == 'T'|| choice == 't')
{
void titleSearch (string bookTitle [50], char choice);
}
}
cout<<"Press <Enter> to Exit";
cin.ignore();
cin.get();
return 0;
}
int loadData (string pathname) // Loads data from infile into arrays
{
fstream infile;
int counter = 0;
infile.open(pathname.c_str()); //Opens file from user input in main
if( infile.fail() )
{
cout << "File failed to open";
return 0;
}
while (!infile.eof())
{
infile >> bookTitle [counter] ; //takes input and puts into parallel arrays
infile >> bookAuthor [counter];
counter++;
}
infile.close();
}
int showall (int counter) // shows input in title(author) format
{
cout<<bookTitle<<"("<<bookAuthor<<")";
}
void authorSearch (string bookAuthor [50], char choice) // Function to search Author Array
{
string target = "";
cout<<"Which author would you like to search for: "<<target; //input
for (int count = 0; count++;)
{
if(bookAuthor[count] == target)
{
cout<<bookTitle[count]<<bookAuthor[count];
}
}
}
void titleSearch (string bookTitle [50], char choice) // Function to Serch Title Array
{
string target = "";
cout<<"Which title would you like to search for: "<<target; //input
for (int count = 0; count++;)
{
if(bookAuthor[count] == target) //tests input against array and outputs reults
{
cout<<bookTitle[count]<<bookAuthor[count];
}
}
}

The comma operator should be replaced with logical and or or, && and || respectively. See uses of the comma operator. Also, authorSearch is a void function. If you want to call authorSearch, simple write authorSearch(...) instead of int authorSearch(...).
Additionally, you need to make sure your prototypes are consistent with your implementations. int authorSearch (string bookAuthor [50]) is not the same as void authorSearch (string bookAuthor [50], char choice). You've mismatched their types and their parameters.

1) The showall() function outputs hex because you can't display arrays that way, you need some kind of loop. It's just printing the starting address of each array.
2) In your search functions you never read the target string from the user.
3) These for() loops will never execute:
for (int count = 0; count++;)
{
...
}
You set count to 0 and then test the value before incrementing. The test fails and the loop body isn't executed.
4) Take care when you fix the for() loops. I don't see any tests to prevent using an invalid index past the (hardcoded) size of your arrays.

Related

Problem while reading objects from file in c++

I am doing a small college project where I have to add, edit and search records in/from file using OOP concept. Adding into file is working fine but whenever I try to read from file it is printing in unreadable texts.
Here is full code and output.
main.cpp
#include <iostream>
#include <cstdlib>
#include <fstream>
#define MIN 20
#define MAX 100
#include "student.h"
using namespace std;
void add_student();
void edit_student();
void search_student();
void addToFile(const Student&);
Student* fetchFromFile();
int getFileSize();
void updateFile(Student*);
// Student stud[MAX];
int main()
{
int choice;
system("cls");
system("Color B0");
while(1)
{
cout<<"\n\t\tWhat do you want to do?"<<endl;
cout<<"\t\t----------------------"<<endl;
cout<<"\t\t1-Add student"<<endl;
cout<<"\t\t2-Edit student"<<endl;
cout<<"\t\t3-Search student"<<endl;
cout<<"\t\t4-Quit Program"<<endl;
cout<<"\t\t----------------------"<<endl;
cout<<"Enter your choice: ";
cin>>choice;
switch(choice)
{
case 1:
add_student(); //calling add_student function to add records.
break;
case 2:
edit_student();
break;
case 3:
search_student();
break;
case 4:
return 0;
break;
default:
cout<<"Invalid choice";
break;
}
}
return 0;
}
int Student::id = getFileSize() - 1; //Initialize id equals to size of file
// setData function of class Student definition
void Student :: setData()
{
// taking input from user
cout<<"Enter student roll no in format(1XXX): ";
cin>>roll;
cout<<"Enter student name: ";
cin>>name;
cout<<"Enter stduent date of birth(dd/mm/yy): ";
cin>>dob;
cout<<"Enter stduent phone no: ";
cin>>phone;
cout<<"Enter student address: ";
cin>>address;
stdId = Student::id;
}
void Student :: showData()
{
cout<<stdId<<" ";
cout<<roll<<" ";
cout<<name<<" ";
cout<<dob<<"\t";
cout<<phone<<" ";
cout<<address<<"\n\n";
}
const int Student :: getRoll()
{
return roll;
}
Student& Student::operator = (const Student& newObj)
{
stdId = newObj.stdId;
roll = newObj.roll;
name = newObj.name;
dob = newObj.dob;
phone = newObj.phone;
address = newObj.address;
return *this;
}
void add_student()
{
Student stud;
Student::incrementId();
stud.setData();
addToFile(stud); //adding records to file
system("CLS");
cout<<endl;
cout<<"--------------------------------------------------------------------------------"<<endl;
cout<<"---------------------------Student updated record Table---------------------------------"<<endl;
cout<<"--------------------------------------------------------------------------------"<<endl;
cout<<"ID "<<"Roll "<<"Name "<<"DOB "<<"Phone no "<<"Address\n\n";
cout<<"--------------------------------------------------------------------------------"<<endl;
Student* student = fetchFromFile(); //getting records from file in array of objects
int length = getFileSize(); //getting length of array of objects
for(int i=0; i<(length-1); i++)
{
student[i].showData(); //showing all the data
}
cout<<"--------------------------------------------------------------------------------"<<endl;
cout<<"---------------------------------FINISH-----------------------------------------"<<endl;
cout<<"--------------------------------------------------------------------------------"<<endl;
cout<<"You want to add more?(Y/n): ";
char c;
cin>>c;
if(c=='y' || c=='Y')
{
add_student();
}
else{
system("pause");
}
}
void edit_student(){
//Showing existing record first before editing
cout<<endl;
cout<<"--------------------------------------------------------------------------------"<<endl;
cout<<"---------------------------Student Existing record Table---------------------------------"<<endl;
cout<<"--------------------------------------------------------------------------------"<<endl;
cout<<"ID "<<"Roll "<<"Name "<<"DOB "<<"Phone no "<<"Address\n\n";
cout<<"--------------------------------------------------------------------------------"<<endl;
Student* student = fetchFromFile(); //fetching all records from file
int length = getFileSize();
for(int i=0; i<(length-1); i++)
{
student[i].showData();
}
int idnumber;
cout<<"--------------------------------------------------------------------------------"<<endl;
cout<<"Which ID number your want to edit: ";
cin>>idnumber; //Asking the user at which ID he wants to make a change.
//checking for valid id number
if(idnumber>length || idnumber<0)
{
cout<<"\nInvalid ID Number."<<endl;
}
//showing existing information about that specific record
cout<<"\nExisted information about this record.\n\n";
cout<<"--------------------------------------------------------------------------------"<<endl;
cout<<"ID "<<"Roll "<<"Name "<<"Father\tCell no. "<<"DOB "<<"Address\n\n";
cout<<"--------------------------------------------------------------------------------"<<endl;
student[idnumber].showData();
cout<<"\n\nEnter new data for above shown record.\n\n";
student[idnumber].setData(); //Inputting data for that specific record.
updateFile(student);
cout<<"\n\nRecord updated successfully."<<endl;
cout<<endl;
cout<<"--------------------------------------------------------------------------------"<<endl;
cout<<"---------------------------Updated record Table---------------------------------"<<endl;
cout<<"--------------------------------------------------------------------------------"<<endl;
cout<<"ID "<<"Roll "<<"Name "<<"DOB "<<"Phone no "<<"Address\n\n";
cout<<"--------------------------------------------------------------------------------"<<endl;
for(int i=0; i<(length-1); i++) //Showing updated record Table
{
student[i].showData();
}
}
void search_student(){
Student* student = fetchFromFile();
int fileLenth = getFileSize() - 1;
int searchkey;
cout<<"Enter roll_no of student you want to search: ";
cin>>searchkey; //roll_no as the search key can be entered by user.
for(int i=1; i<fileLenth; i++)
{
if(searchkey==student[i].getRoll()) //checking for roll no
{
student[i].showData();
}
}
cout<<"--------------------------------------------------------------------------------"<<endl;
cout<<"---------------------------------FINISH-----------------------------------------"<<endl;
cout<<"--------------------------------------------------------------------------------"<<endl;
system("pause");
}
//FILE HANDLING
void addToFile(const Student& obj)
{
ofstream fout;
fout.open("records.txt", std::ofstream::app | std::ofstream::binary);
fout.write((char*)&obj, sizeof(obj));
cout<<"Added to file successfully!"<<endl;
fout.close();
}
Student* fetchFromFile()
{
int i=0;
Student obj;
Student* returnObj = new Student[MAX];
ifstream fin;
fin.open("records.txt", std::ifstream::binary);
while(!fin.eof())
{
fin.read((char*)&obj, sizeof(obj));
returnObj[i] = obj;
i++;
}
fin.close();
delete[] returnObj;
return returnObj;
}
int getFileSize()
{
int i=0;
Student obj;
ifstream fin;
fin.open("records.txt", std::ifstream::binary);
while(!fin.eof())
{
fin.read((char*)&obj, sizeof(obj));
i++;
}
fin.close();
return i;
}
void updateFile(Student* student)
{
ofstream fout;
fout.open("records.txt", std::ofstream::binary);
fout.write((char*)&student, sizeof(student));
fout.close();
}
student.h header file
// A student class that hold students attributes like id, name, address and class
// Object of this class will be craeted to store student details
class Student
{
int stdId;
int roll;
std::string name;
std::string dob;
std::string phone;
std::string address;
public:
static int id; //we will increase 'id' whenever student is added to the record
//Member functions declaration
void setData(); //this function will take input from user and set the data to attributes of class
void showData(); //This function will give student data to user when called
static void incrementId()
{
id++;
}
const int getRoll();
Student& operator = (const Student&);
};
Sample output 1
When I add a student object to file
Sample output 2
Reading all records from file
Problem1: Showing garbage value of id.
Sample output 3
Adding another object to file
Sample output 4
Reading all objects from file
Sample output 5
Now went back and chose to edit record
Problem2: See how the records are printing in unreadable form.
Why is this happening. Now if I close program and run it again then it is still showing in unreadable text.
Hope you get my issue. I want to have detailed explanation about this. Also, if I have done other miscellaneous mistakes, please let me know.
Thank you!
Student is too complicated to read with unformatted IO like read and write. In technical terms, it is not Trivially Copyable because it contain std::strings, and string is not Trivially Copyable.
The easiest thing to do is to abandon read and write. Most of your data is string data, so write << and >> overloads for Student and store everything as text.
friend std::ostream & operator<<(std::ostream & out,
const Student & stu)
{
out << stu.stdId << ',' <<stu.roll << ','<< stu.name... <<'\n';
return out;
}
Reading the data back in is a bit trickier
friend std::istream & operator>>(std::istream & in,
Student & stu)
{
std::string line;
if (std::getline(in, line))
{
std::stringstream strm(line);
if (!(strm >> stu.stdId >> comma >> stu.roll >> comma) ||
!std::getline(strm, stu.name, ',') ||
!std::getline(strm, stu.dob, ',') ||
...))
{ // If any read failed, mark the stream as failed so the caller knows.
out.setstate(std::ios_base::failbit);
}
}
return out;
}
First, a minor trick, add a empty constructor for class to initailial all members, integers{0}, and string{}, which will elminiate some unwanted gabages.
Student::Student() :stdId(0), roll(0), name{}, dob{}, phone{}, address{} {;}
Your major problem arised from the function fetchfomrfile() where you delete the fetched Student array, therefore the caller received a undefined data array:
Student* fetchFromFile()
{
int i=0;
Student obj;
Student* returnObj = new Student[MAX];
//reading records from file
fin.close();
delete[] returnObj;// <<<< youe deleted
return returnObj; // and return it as undefined
}
Since you are able to calculate the size of file, I suggest in the caller function:
void search_student(){
// Student* student = fetchFromFile();
Student *student = new Stduent [getFileSize()];
fecchFromFile(student); // use this array in fetch_file
// other things
}
Rewrite fetchFromFile as :
viod fetchFromFile(Stduent *ss)
{
// read file data to array ss[i];
}

How to read specific data from a file that looks like this?

I want to make a program that inputs data of participants in a txt file through the input function of the class. The output function is then used to extract the information of a single participant at a time by keying in their ID.
In this code of mine, my while loop runs infinitely as soon as I enter an ID. I suspect that it is unable to find the eof(). Any help would be greatly appreciated. I am new to C++.
#include <fstream>
#include <sstream>
#include <iostream>
#include <string>
using namespace std;
class Participant{
private:
int id, score;
string name;
public:
Participant(){
id = 0; score = 0; name = "";
}
void input(){
char choice;
ofstream in;
in.open("Participant.txt", ios::app);
do{
cout<<"Enter your ID: \t";
cin>>id;
cout<<"Enter your name: \t";
cin>>name;
cout<<"Enter your Score:\t";
cin>>score;
in<<name<<" ";
in<<id<<" ";
in<<score<<endl;
cout<<"Add another entry? (Y/N)\n";
cin>>choice;
}while(choice == 'y' || choice == 'Y');
in.close();
}
void output(){
int idout, holderID, holderS;
string holder, output;
cout<<"Enter the ID for more information related to the person:";
cin>>idout;
fstream out;
out.open("Participant.txt");
while(!out.eof()){
out>>holderID;
cout<<"looping...\n";
if(idout == holderID){
out>>holder;
cout<<"Name: \t"<<holder<<endl;
out>>holderS;
cout<<"Score:\t"<<holderS<<endl;
holder ="";
holderS=0;
break;
}
else continue;
}
out.close();
}
void max(){
}
};
int main(){
char choice;
Participant player;
cout<<"Asking for Input: \n";
player.input();
system("pause");
system("cls");
cout<<"Data Viewing: \n";
do{
player.output();
cout<<"\nDo you wish to extract information on other players?\n";
cout<<"Y - Yes."<<endl;
cout<<"N - No."<<endl;
cout<<"Choice: ";
cin>>choice;
}while (choice == 'y' || choice == 'Y');
cout<<"\n\nEnd of Data Viewing.\n";
}
I want it to, at first, read just the ID, in the first line its 1037. If the ID matches, it should display the next 2 members in the file; the name and the score.
The Problem is that you tried to use holderID (int) Directly from the out stream. Try to use string to read the same out value and use stoi() to convert the same to int.
Also note as you write the first is name followed by id and score.
Also use the below as a reference. I have used std::map to store the value of id, name and score.
#include <string>
#include <fstream>
#include <map>
#include <iostream>
#include <algorithm>
#include <sstream>
class Participants
{
int id;
int score;
std::string name;
public:
Participants(): id(0), score(0)
{}
Participants(int id, int score, std::string name): id(id), score(score), name(name)
{}
~Participants()
{}
int GetId()
{
return id;
}
std::string encode()
{
auto strRet = std::string( name + " " + std::to_string(id) + " " + std::to_string(score) + "\n");
return strRet;
}
void decode(std::string text)
{
std::stringstream ss(text);
std::string buf;
//Read Name
std::getline( ss, buf , ' ');
name = buf;
//Read id
std::getline( ss, buf , ' ');
id = std::stoi( buf );
//Read Score
std::getline( ss, buf , '\n');
score = std::stoi( buf );
}
};
class DataReader
{
std::string fileName;
std::fstream myfile;
public:
DataReader(std::string fileName): fileName(fileName)
{
}
~DataReader()
{
}
void ReadParticipants(std::map<int, Participants> &MapParticipants)
{
myfile.open(fileName, std::ios::in);
MapParticipants.clear();
if ( myfile.is_open() )
{
std::string line;
while ( std::getline(myfile, line) )
{
Participants oParticipants;
//Decode and Add to map
oParticipants.decode(line);
//Add to map
MapParticipants[ oParticipants.GetId() ] = oParticipants;
}
}
myfile.close();
}
void WriteParticipants(std::map<int, Participants> &MapParticipants)
{
//Load Map to find Duplicates
std::map<int, Participants> MapParticipants_exist;
ReadParticipants(MapParticipants_exist);
myfile.open(fileName, std::ios::app);
if ( myfile.is_open() )
{
for ( auto oParticipants : MapParticipants)
{
//Check for Duplicates (to Write or not)
if ( MapParticipants_exist.find(oParticipants.first) == MapParticipants_exist.end() )
{
auto text = oParticipants.second.encode();
myfile << text.c_str();
}
}
}
myfile.close();
}
};
int _tmain(int argc, _TCHAR* argv[])
{
DataReader oReader("File.txt");
std::map<int, Participants> MapParticipants;
//Make Some Participants
Participants p1(1, 50, "TOM");
Participants p2(2, 40, "TIM");
Participants p3(3, 80, "JERRY");
//Add them to map
MapParticipants[p1.GetId()] = p1;
MapParticipants[p2.GetId()] = p2;
MapParticipants[p3.GetId()] = p3;
oReader.WriteParticipants(MapParticipants);
oReader.ReadParticipants(MapParticipants);
//Find and Display
int id = 2;
auto it = MapParticipants.find(id);
if ( it != MapParticipants.end() )
{
//Show/Print
...
}
return 0;
}

Output file overwriting incorrectly

This is just to go with my last question, someone asked me to upload the file so I'll cut out all the useless functions and have the main code especially that having to do with the output, this is my 3rd question on stack overflow so I still don't know much about how it works, if you want to answer please check my last (2nd) question for details. Here is the code:
#include <iostream>
#include <iomanip>
#include <fstream>
using namespace std;
const int MAX_SIZE = 1000;
struct Student
{
string studentID;
string firstName;
char midInitial;
string lastName;
};
void menu()
{
cout<<" CLASS ROSTER APPLICATION"<<endl;
cout<<"========================================="<<endl;
cout<<"ADD a student record..................[A]"<<endl;
cout<<"DELETE a student......................[D]"<<endl;
cout<<"EDIT a student record.................[E]"<<endl;
cout<<"INQUIRY about a student...............[I]"<<endl;
cout<<"LIST all students.....................[L]"<<endl;
cout<<"QUIT the system.......................[Q]"<<endl;
}
int linearSearch(const Student roster[], int numStuds, string sID)
{
int i;
for (i=0; i<numStuds; i++)
{
if (roster[i].studentID == sID)
return i;
}
return -1;
}
void selSort(Student roster[], int numStuds)
{
int minIDPos;
Student tempStudRecord;
int i,j;
for (i=0; i<numStuds-1; i++)
{
minIDPos = i;
for (j=i+1; j<numStuds; j++)
{
if (roster[j].studentID < roster[minIDPos].studentID)
minIDPos = j;
}
tempStudRecord = roster[i];
roster[i] = roster[minIDPos];
roster[minIDPos] = tempStudRecord;
}
}
void listStudents(const Student roster[], int numStuds)
{
cout<<"C L A S S R O S T E R"<<endl;
cout<<"Student ID # First Name M.I. Last Name"<<endl;
cout<<"---------------------------------------------"<<endl;
int i2=0; //just a counter.
while (i2<numStuds)
{
cout<<left;
cout<<setw(14)<<roster[i2].studentID<<setw(13)<<roster[i2].firstName;
cout<<roster[i2].midInitial<<". "<<roster[i2].lastName;
cout<<endl;
i2++;
}
cout<<right;
cout<<"---------------------------------------------"<<endl;
cout<<"Enrollment: "<<i2<<endl;
}
int main()
{
Student roster[MAX_SIZE];
fstream inFile;
fstream outFile;
string filename, sID;
Student newStudent;
int numStuds = 0;
char choice;
int i;
cout<<"Enter the name of the data file> ";
cin>>filename;
/** 7. open the data file for input **/
inFile.open(filename.c_str(), ios::in);
if (inFile)
{
/** 8. write a while loop to read the data from the file
into the roster array; the numStuds (number of Students)
must be updated as the records are read from the file.
Also, close the file after its contents are read into the
roster array.
**/
while (!inFile.eof() && numStuds < MAX_SIZE)
{
inFile>>roster[numStuds].studentID>>roster[numStuds].firstName;
inFile>>roster[numStuds].midInitial>>roster[numStuds].lastName;
numStuds++;
}
inFile.close();
}
do
{
cout<<endl;
menu();
cout<<endl;
cout<<"Select an option-> ";
cin>>choice;
cout<<endl;
switch(toupper(choice))
{
case 'A': cout<<"Enter the student ID #> ";
cin>>newStudent.studentID;
cout<<"Enter the student's first name> ";
cin>>newStudent.firstName;
cout<<"Enter the student's middle initial> ";
cin>>newStudent.midInitial;
cout<<"Enter the student's last name> ";
cin>>newStudent.lastName;
addStudent(roster,numStuds,newStudent);
break;
case 'D': /** 9. write code here to remove a student from the roster **/
cout<<"Enter the student ID Number: ";
cin>>sID;
deleteStudent(roster, numStuds, sID);
break;
case 'E': /** 10. write code to edit the record for a student with a specified ID # **/
cout<<"Enter the student ID Number: ";
cin>>sID;
editStudent(roster, numStuds, sID);
break;
case 'I': /** 11. write code to perform an inquiry (obtain full name) on a student with
with a specified ID # **/
cout<<"Enter the student ID Number: ";
cin>>sID;
studentInquiry(roster, numStuds, sID);
break;
case 'L': /** 12. write code to sort and then generate class roster **/
selSort(roster, numStuds);
listStudents(roster, numStuds);
break;
case 'Q': break;
default: cout<<"Invalid menu choice...try again!"<<endl;
}
}while(toupper(choice) != 'Q');
/** 13. open the data file in output mode and error-check to ensure
that it was successfully opened.
**/
outFile.open(filename.c_str(), ios::out);
if (!outFile)
{
cout<<"Unable to open "<<filename<<" for output. "<<endl;
return -1;
}
/** 14. overwrite the data file in the order:
student ID, firstname, middle initial and last name, one record per line.
make sure that you have spaces between the fields.
**/
for (i=0;i<numStuds;i++)
{
outFile<<roster[i].studentID<<" "<<roster[i].firstName<<" ";
outFile<<roster[i].midInitial<<" "<<roster[i].lastName<<endl;
}
/** 15. close the output file **/
outFile.close();
return 0;
}
This here
while (!inFile.eof() && numStuds < MAX_SIZE) {
inFile >> roster[numStuds].studentID >> roster[numStuds].firstName;
inFile >> roster[numStuds].midInitial >> roster[numStuds].lastName;
numStuds++;
}
Will leave the value of numStuds one higher than it should be, as inFile.eof() will only return true once the end of file has been reached, which won't occur until you have actually tried to read past the end of the file.
One way of fixing it would be to change it to
while (numStuds < MAX_SIZE &&
(inFile >> roster[numStuds].studentID >> roster[numStuds].firstName >>
roster[numStuds].midInitial >> roster[numStuds].lastName)
) {
numStuds++;
}
Although you're better off defining a function that reads the struct for you
std::istream& operator>>(std::istream& stream, Student& student)
{
stream >> student.studentID >> student.firstName
>> student.midInitial >> student.lastName;
return stream;
}
Which will work as follows:
while (inFile >> roster[numStuds]) {
numStuds++;
}
As suggested by #JoachimPileborg the following would be even better (once operator>> is defined for your struct):
std::vector<Student> roster;
//...
std::copy(
std::istream_iterator<Student>(inFile),
std::istream_iterator<Student>(),
std::back_inserter(roster)
);

Infile incomplete type error

I am building a program that takes an input file in this format:
title author
title author
etc
and outputs to screen
title (author)
title (author)
etc
The Problem I am currently getting is a error:
"ifstream infile has incomplete type and cannot be defined"
Following is the program:
#include <iostream>
#include <string>
#include <ifstream>
using namespace std;
string bookTitle [14];
string bookAuthor [14];
int loadData (string pathname);
void showall (int counter);
int main ()
{
int counter;
string pathname;
cout<<"Input the name of the file to be accessed: ";
cin>>pathname;
loadData (pathname);
showall (counter);
}
int loadData (string pathname) // Loads data from infile into arrays
{
ifstream infile;
int counter = 0;
infile.open(pathname); //Opens file from user input in main
if( infile.fail() )
{
cout << "File failed to open";
return 0;
}
while (!infile.eof())
{
infile >> bookTitle [14]; //takes input and puts into parallel arrays
infile >> bookAuthor [14];
counter++;
}
infile.close;
}
void showall (int counter) // shows input in title(author) format
{
cout<<bookTitle<<"("<<bookAuthor<<")";
}
File streams are defined in the header <fstream> and you are not including it.
You should add:
#include <fstream>
Here is my code with the previous error fixed Now I get a problem of the program crashing after I input the name of the text file.
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
string bookTitle [14];
string bookAuthor [14];
int loadData (string pathname);
void showall (int counter);
int main ()
{
int counter;
string pathname;
cout<<"Input the name of the file to be accessed: ";
cin>>pathname;
loadData (pathname);
showall (counter);
}
int loadData (string pathname) // Loads data from infile into arrays
{
fstream infile;
int counter = 0;
infile.open(pathname.c_str()); //Opens file from user input in main
if( infile.fail() )
{
cout << "File failed to open";
return 0;
}
while (!infile.eof())
{
infile >> bookTitle [14]; //takes input and puts into parallel arrays
infile >> bookAuthor [14];
counter++;
}
infile.close();
}
void showall (int counter) // shows input in title(author) format
{
cout<<bookTitle<<"("<<bookAuthor<<")";
}

How to fix Error that leads to program Crash

For some reason or another I cannot figure out why the data when opened does not get put into arrays and the program crashes. I have searched around for a answer to no avail. I get the feeling I will be posting a here a few more times before I finish this program!
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
string bookTitle [14];
string bookAuthor [14];
int loadData (string pathname);
void showall (int counter);
int main ()
{
int counter;
string pathname;
cout<<"Input the name of the file to be accessed: ";
cin>>pathname;
loadData (pathname);
showall (counter);
cout<<"Press <Enter> to Exit";
cin.ignore();
cin.get();
return 0;
}
int loadData (string pathname) // Loads data from infile into arrays
{
fstream infile;
int counter = 0;
infile.open(pathname.c_str()); //Opens file from user input in main
if( infile.fail() )
{
cout << "File failed to open";
return 0;
}
while (!infile.eof())
{
cout<<"File Opened"; // I get the "File Opened" text and then a crash
infile >> bookTitle [14] ; //takes input and puts into parallel arrays
infile >> bookAuthor [14];
cout<<"Data Put in Arrays";
counter++;
}
infile.close();
}
void showall (int counter) // shows input in title(author) format
{
cout<<bookTitle<<"("<<bookAuthor<<")";
}
Edit: The assignment is to take a file with names in a format such as title
author
title
author
Then it must output the arrays to screen in a title(author) format.
After this I must make it a looping program so a search can be chosen of one of the arrays and returns the entry in a title(author) format.
Here is how the code stands now;
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
string bookTitle [50];
string bookAuthor [50];
int loadData (string pathname);
int showall (int counter);
int main ()
{
string pathname;
int counter=0;
cout<<"Input the name of the file to be accessed: ";
cin>>pathname;
loadData (pathname);
showall (counter);
cout<<"Press <Enter> to Exit";
cin.ignore();
cin.get();
return 0;
}
int loadData (string pathname) // Loads data from infile into arrays
{
fstream infile;
int counter = 0;
infile.open(pathname.c_str()); //Opens file from user input in main
if( infile.fail() )
{
cout << "File failed to open";
return 0;
}
while (!infile.eof())
{
infile >> bookTitle [counter] ; //takes input and puts into parallel arrays
infile >> bookAuthor [counter];
counter++;
}
infile.close();
}
int showall (int counter) // shows input in title(author) format
{
cout<<bookTitle<<"("<<bookAuthor<<")";
return 0;
}
A Quick problem I see is:
infile >> bookTitle [14] ;
infile >> bookAuthor [14];
Is incorrect, What you need is:
infile >> bookTitle[counter];
infile >> bookAuthor [counter];
Also,
The counter variable declared in main() and in loadData() are two different variables. The one declared in main()is never iniitalized at all.
There are many problems with this code.
Main problem
These lines write only into 14th elements of the arrays
infile >> bookTitle [14] ; //takes input and puts into parallel arrays
infile >> bookAuthor [14];
In order to fill the entire array you need to use the counter variable for index:
infile >> bookTitle [counter] ; //takes input and puts into parallel arrays
infile >> bookAuthor [counter];
BTW: if array's size is 14 then the maximum index is 13. So you can't access an element with index 14. And that's probably causes the crash.
Other problems I noticed
counter in main() is not initialized
showall() function takes an argument but does not use it, which is probably the index.
showall() call in main() probably needs to be in a loop, to show all elements.