7 Replies - 1654 Views - Last Post: 14 October 2009 - 03:46 PM Rate Topic: -----

#1 Blackbass99   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 9
  • Joined: 13-October 09

Problem Initializing Variables

Post icon  Posted 13 October 2009 - 09:14 PM

#include <string>
using namespace std;

class memberType
{
public:
  void setMemberInfo(string ID, string firstName, string lastName, 
					 int booksPurchasedRecently, double amountSpentRecently,
					 int booksPurchased, double amountSpent);
  void printInfo();

  bool isID(string ID);

  string getID();
  string getFirstName();
  string getLastName();
  int getBooksPurchasedRecently();
  double getAmountSpentRecently();
  int getBooksPurchased();
  double getAmountSpent();

  void purchaseBook(double amount);
  void resetbooksPurchasedAndAmount();
private:
  string ID;
  string firstName;
  string lastName;
  int booksPurchasedRecently; // used for 11th book discount
  double amountSpentRecently; // used for 11th book discount
  int booksPurchased; // total number of books purchased (never be reset to 0)
  double amountSpent; // total amount spent (this will never be reset to 0)
};



Using the .h file I'm supposed to access the classes and create a new program. I know everything Im supposed to do besides actually accessing the classes correctly. The instructor always gives us a pre-made lab and we are to finish it. That's easy. Starting from scratch is the hard part. Just a few hints would be great

Here's What I've Tried:

#include "bookType.h"
#include "memberType.h"
#include <iostream>
#include <string>
#include <fstream>

using namespace std;

const int N = 2000;

int main()
{
	 memberType list;
	 list.setMemberInfo();
	 list.printInfo();
	 {
	 ifstream infile;
	 string fname;
	 string x;

	 cout << "Enter file name: ";
	 cin >> fname;
	 infile.open(fname.c_str());
	 if(!infile){
	   cerr << "Error: Can't open data file." << endl;
	   return 1;
	 }
	 else{
	   while(!infile.eof){
	   infile >> x;
	   cout << x << endl;
	   infile >> x;
	   }
	  }
	 infile.close();


  /*
  bookType();
  memberType();
  ifstream infile;
 string fname;
  int index;
  int n = 0;
  string ntitle;
  string title;

  int x;
  char c;
  cout << "Enter file name: ";
  cin >> fname;
  infile.open(fname.c_str());
  if(!infile){
  cerr << "Error: Can't open data file." << endl;
  return 1;
  }
  else{
	while(!infile.eof){
	infile >> x;
  cout << x << endl;
	infile >> x;
	}
  }
  infile.close();


*/
  return 0;
}}



The commented part is trying to see if I could get something out of the .dat file and it got one line so I know to tweak it a little to get all lines and store it to the array. Now my question is am I declaring it right by saying
memberType();
Should it be
memberType.setMemberInfo();
. Last question is how would I get the array to store strings and integers? I can get the integer but an error for the strings of the .dat file.

Thanks for any help :D!

Is This A Good Question/Topic? 0
  • +

Replies To: Problem Initializing Variables

#2 seeP+   User is offline

  • D.I.C Addict

Reputation: 55
  • View blog
  • Posts: 601
  • Joined: 20-July 09

Re: Problem Initializing Variables

Posted 13 October 2009 - 09:54 PM

You have one class with a bunch of members. Your object for the class is fine, but when you call the a member from a class you need to make sure it receives the same amount of parameters as declared in the class.

Your first call:
 list.setMemberInfo();



You created it like this in the class:
void setMemberInfo(string ID, string firstName, string lastName,
					 int booksPurchasedRecently, double amountSpentRecently,
					 int booksPurchased, double amountSpent);



Your call should have 7 parameters.

This post has been edited by seeP+: 13 October 2009 - 09:56 PM

Was This Post Helpful? 0
  • +
  • -

#3 Ancient Dragon   User is offline

  • D.I.C Addict
  • member icon

Reputation: 82
  • View blog
  • Posts: 679
  • Joined: 19-July 09

Re: Problem Initializing Variables

Posted 13 October 2009 - 10:00 PM

>> list.setMemberInfo();
That won't work because the methd needs several parameters -- see the class description in the *.h file. That line should go after all the data for that class has been read from the file. You can't set the class info before you know what it is. Its like putting the cart before the horse.

You need to post the data file before we can comment on how it should be read.
Was This Post Helpful? 0
  • +
  • -

#4 Blackbass99   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 9
  • Joined: 13-October 09

Re: Problem Initializing Variables

Posted 14 October 2009 - 10:41 AM

100 // Member ID
John Williams // Member Name (First and Last)
5 // Number of Books Recently Purchased (towards a discount)
159.50 //total amount spent for recently purchased books
16 // number of books purchased since he/she became a member
536.00 // total amount spent since becoming a member. 
200
Lisa Berry
35.50
2
35.50
300
Ron Brown
10
255.68
22
610.42
400
Jessey Smith
0
0.00
0
0.00



It repeats itself for the next 3 members.

Heres the member.cpp file which I hope will also help in you guys explaining how to get each of those values into an array without the error I'm getting involving conversion.

#include "memberType.h"
#include <iostream>
#include <string>
using namespace std;

void memberType::setMemberInfo(string newID, string newFirstName, 
							   string newLastName,
							   int newBooksPurchasedRecently, 
							   double newAmountSpentRecently,
							   int newBooksPurchased, double newAmountSpent)
{
  ID = newID;
  firstName = newFirstName;
  lastName = newLastName;
  booksPurchasedRecently = newBooksPurchasedRecently;
  amountSpentRecently = newAmountSpentRecently;
  booksPurchased= newBooksPurchased;
  amountSpent= newAmountSpent;
}

void memberType::printInfo()
{
  cout << "Member ID: " << ID << endl
	   << "Name: " << firstName << " " << lastName << endl
	   << "Number of Books Purchased Recently: " 
		   << booksPurchasedRecently << endl
	   << "Total Amount Spent Recently: " << amountSpentRecently << endl
	   << "Number of Books Purchased: " << booksPurchased << endl
	   << "Total Amount Spent: " << amountSpent << endl;
}

bool memberType::isID(string id)
{
  return ID == id;
}

string memberType::getID()
{
  return ID;
}

string memberType::getFirstName()
{
  return firstName;
}

string memberType::getLastName()
{
  return lastName;
}

int memberType::getBooksPurchasedRecently()
{
  return booksPurchasedRecently;
}

double memberType::getAmountSpentRecently()
{
  return amountSpentRecently;
}

int memberType::getBooksPurchased()
{
  return booksPurchased;
}

double memberType::getAmountSpent()
{
  return amountSpent;
}

void memberType::purchaseBook(double amount)
{
  ++booksPurchasedRecently;
  ++booksPurchased;
  amountSpentRecently = amountSpentRecently + amount;
  amountSpent = amountSpent + amount;
}

void memberType::resetbooksPurchasedAndAmount()
{
  booksPurchasedRecently = 0;
  amountSpentRecently = 0;
}



Thanks once again for any further help!
Was This Post Helpful? 0
  • +
  • -

#5 Blackbass99   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 9
  • Joined: 13-October 09

Re: Problem Initializing Variables

Posted 14 October 2009 - 02:29 PM

I Figured Out How To Call It And Print Values On Separate Lines From The .dat File. Is There An Easier Way To Do This Though... How can I get it into an array?

#include "bookType.h"
#include "memberType.h"
#include <iostream>
#include <string>
#include <fstream>

using namespace std;
string ID;
string firstName;
string lastName;
int booksPurchasedRecently;
double amountSpentRecently;
int booksPurchased;
double amountSpent;

int main()
{
	memberType mType;
	mType.setMemberInfo(ID, firstName, lastName, booksPurchasedRecently, amountSpentRecently, booksPurchased, amountSpent);{
	   ifstream infile;
	   string fname;
	   cout << "Enter file name: ";
	   cin >> fname;
	   infile.open(fname.c_str());
	   if(!infile) {
	   cerr << "Error: Can't open file." << endl;
	return 1;
}
 else
   {
	for(int i = 0; i < 4; ++i){
	 //Reading data items from input file and storing into an array
	 infile>>ID;
	 infile>>firstName;
	 infile>>lastName;
	 infile>>booksPurchasedRecently;
	 infile>>amountSpentRecently;
	 infile>>booksPurchased;
	 infile>>amountSpent;
	 cout << "ID: " << ID << endl;
	 cout << "Name: " << firstName << " " << lastName << endl;
	 cout << "Books Purchased Recently: " << booksPurchasedRecently << endl;
	 cout << "Amount Spent Recently: $" << amountSpentRecently << endl;
	 cout << "Books Purchased Total: " << booksPurchased << endl;
	 cout << "Amount Spent: $" << amountSpent << endl;
		 cout << " " << endl;
   }
}
   infile.close(); 
}
  return 0;
}
								 



Figured Out The Loop :D

This post has been edited by redemption15: 14 October 2009 - 03:28 PM

Was This Post Helpful? 0
  • +
  • -

#6 Ancient Dragon   User is offline

  • D.I.C Addict
  • member icon

Reputation: 82
  • View blog
  • Posts: 679
  • Joined: 19-July 09

Re: Problem Initializing Variables

Posted 14 October 2009 - 03:31 PM

Here is one way to do it. Note that I added another method -- read() -- which reads the data for one memberType class.
#include <fstream>
#include <vector>
#include <string>
using namespace std;

class memberType
{
public:
	bool read(ifstream& in);
  void setMemberInfo(string ID, string firstName, string lastName,
					 int booksPurchasedRecently, double amountSpentRecently,
					 int booksPurchased, double amountSpent);
  void printInfo();

  bool isID(string ID);

  string getID();
  string getFirstName();
  string getLastName();
  int getBooksPurchasedRecently();
  double getAmountSpentRecently();
  int getBooksPurchased();
  double getAmountSpent();

  void purchaseBook(double amount);
  void resetbooksPurchasedAndAmount();
private:
  string ID;
  string firstName;
  string lastName;
  int booksPurchasedRecently; // used for 11th book discount
  double amountSpentRecently; // used for 11th book discount
  int booksPurchased; // total number of books purchased (never be reset to 0)
  double amountSpent; // total amount spent (this will never be reset to 0)
};

bool memberType::read(ifstream& in)
{
	if( getline(in, ID) )
	{
		in >> firstName >> lastName
			>>booksPurchasedRecently >> amountSpentRecently
		  >>booksPurchased >> amountSpent;
		return true;

	}
	else
	{
		// end-of-file reached or error
		return false;
	}

}


int main()
{
	vector<memberType> list;
	ifstream in("TextFile.txt");
	if( in.is_open() )
	{
		memberType t;
		while( t.read(in) )
			list.push_back(t);
	}
	return 0;
}

This post has been edited by Ancient Dragon: 14 October 2009 - 03:32 PM

Was This Post Helpful? 1
  • +
  • -

#7 Blackbass99   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 9
  • Joined: 13-October 09

Re: Problem Initializing Variables

Posted 14 October 2009 - 03:34 PM

That would be great! The teacher has restricted us from using code we haven't learned such as vectors, and push_back, although I know what they are, I'm not allowed To use them.
Was This Post Helpful? 0
  • +
  • -

#8 Ancient Dragon   User is offline

  • D.I.C Addict
  • member icon

Reputation: 82
  • View blog
  • Posts: 679
  • Joined: 19-July 09

Re: Problem Initializing Variables

Posted 14 October 2009 - 03:46 PM

Then use a simple array or linked list.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1