Note: THIS IS THE WHOLE CODE I really need some help with this C++ program I\'m
ID: 3655664 • Letter: N
Question
Note: THIS IS THE WHOLE CODE
I really need some help with this C++ program I'm making. I'm a beginner, so please go easy on me. If you figure it out, please explain what the problem was so I can understand it better.
First off, I want this to be the input data:
This has to do with Classes and the Project is broken up into three files: Implementation, MainLine and Header File.
If you have Bloodshed, this is what it's suppose to look like:
When I ran it, I get this error:
Someone told me that:
Your data file looks incorrect. count is supposed to be the first item in the data file. it looks like you have only the 4 entries. Your code is treating 1111, the first ID, as the count, then everything else is totally screwed up after that since it is trying to read in the first ID and encountering the name, and furthermore it expects there to be 1111 entries when there are only 4 entries. You should edit the data file and add 4 to the beginning and give it a try.
Here's my code:
Header File:
#include<iostream>
#include<string>
using namespace std;
class memberType
{
string memberID;
string firstName;
string lastName;
int booksPurchased;
double amountSpent;
public:
memberType();
memberType(string ID, string fName, string lName,int bPurchased, double amount);
void setMemberInfo(string ID, string fName, string lName,int bPurchased, double amount);
void setMemberID(string);
void setName(string, string);
bool isMemberID(string) const;
int getBooksPurchased() const;
double getTotalAmountSpent() const;
void purchaseBook(double amount);
void resetbooksBoughtAndAmount();
void printMemberID() const;
void printName() const;
void printInfo() const;
};//end of class
#include<iostream>
#include<string>
#include"memberType.h"
#include <fstream>
using namespace std;
void getMembersData(memberType m[],int&);
void printMembersData(memberType m[],int);
int main()
{
int count;
memberType arr[500];
getMembersData(arr, count);
printMembersData(arr, count);
cin.get();
return 0;
}
void getMembersData(memberType m[],int &count)
{
string ID, fName, lName;
int bookCount, i = 0;
double amount;
ifstream infile;
infile >> count;
infile.open("Project9Data.DAT");
infile >> count;
while(i < count)
{
infile >> ID >> fName >> lName >> bookCount >> amount;
m[i].setMemberInfo(ID, fName, lName, bookCount,amount);
i++;
}
infile.close();
return;
}
void printMembersData(memberType m[],int count)
{
int i=0;
while(i<count)
{
m[i].printInfo();
i++;
}
return;
}
Implementation File:
#include<iostream>
#include<string>
#include"memberType.h"
using namespace std;
memberType::memberType()
{
memberID = "0";
firstName = "First";
lastName = "Last";
booksPurchased = 0;
amountSpent = 0;
}
memberType::memberType(string ID, string fName, string lName,int bPurchased, double amount)
{
memberID = ID;
firstName = fName;
lastName = lName;
booksPurchased = bPurchased;
amountSpent = amount;
}
void memberType::setMemberInfo(string ID, string fName, string lName,int bPurchased, double amount)
{
memberID = ID;
firstName = fName;
lastName = lName;
booksPurchased = bPurchased;
amountSpent = amount;
}
void memberType::setMemberID(string ID){memberID = ID;}
void memberType::setName(string fName, string lName){firstName = fName; lastName = lName;}
bool memberType::isMemberID(string ID) const
{
return(memberID==ID);
}
int memberType::getBooksPurchased() const{return booksPurchased;}
double memberType::getTotalAmountSpent() const{return amountSpent;}
void memberType::purchaseBook(double amount)
{
booksPurchased++;
amountSpent+=amount;
}
void memberType::resetbooksBoughtAndAmount()
{
booksPurchased = 0;
amountSpent = 0;
}
void memberType::printMemberID() const
{
cout << memberID;
}
void memberType::printName() const
{
cout << firstName << " "<< lastName;
return;
}
void memberType::printInfo() const
{
cout << " Member ID: ";
printMemberID();
cout << " Name: ";
printName();
cout << " Number of Books Bought: "<< booksPurchased;
cout << " Total Amount Spent: $"<< amountSpent << endl;
return;
}
Explanation / Answer
What they told you is correct. One possible way of implementing this would be to add a "count" entry at the beginning of the file. However, the preferred way of implementing this would be to check for end-of-file at the beginning of each iteration. Here would be your corrected getMembersData() function:
void getMembersData(memberType m[],int &count)
{
string ID, fName, lName;
int bookCount;
double amount;
count = 0;
ifstream infile;
infile.open("Project9Data.DAT");
infile >> ID >> fName >> lName >> bookCount >> amount;
while( !infile.eof() && count < 500)
{
m[count].setMemberInfo(ID, fName, lName, bookCount,amount);
infile >> ID >> fName >> lName >> bookCount >> amount;
count++
}
infile.close();
return;
}
Note that we read a set of fields, check to see if the End-of-File bit has been set during our read, and then use the setMemberInfo() method to assign the information to the next memberType element. Also note the other two significant changes -- the while loop will explicitly stop after reading 500 elements (since you statically allocated an array of 500 elements - attempting to read more would walk you off the end of your array), and the loop automatically calculates count while it continues reading.
Also notice the subtlety that we needed to read from the file and then check to see if we encountered end-of-file during that read.
I hope this helps!
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.