10 Replies - 806 Views - Last Post: 10 June 2010 - 01:17 PM Rate Topic: -----

#1 Guest_Intekin*


Reputation:

I need some guidance concerning vector stacks

Posted 09 June 2010 - 10:37 AM

So here is my problem, I have this assignment with making a bank management program.

I have an vector for storing class objects (name, balance). and the problem I am having is picking out a specific object to deposit, withdraw or delete.

I was hoping that someone could point or show me the way to make that work.

The code may be a bit haphazard and the comments in the code is in Swedish. I surely hope that i do not need to translate it. It would be a pain.

#include <iostream>
#include <vector>
#include <string>
#include "person.h"
#include "iodos.h"
#include "a_stuff.h"

using std::cout;			//Ingen namespace denna gång. Man har bättre koll på vad man gör om man skriver in standard funktionerna själv. 
using std::cin;
using std::endl;
using std::vector;

int main()
{
	dos_console();					// för ett fungerande å, ä, ö
	vector<person> customer;		//vector för lagring av data.
	const size_t maxlength = 20;	//max längd på text strängar.
	char firstname[maxlength];	
	char lastname[maxlength];
	char balance[maxlength];
	char option=0;					// sätter den till 0 för att den inte skall bråka i början.


	cout << "\t\t**BANKPROGRAMMET**\n\n\n\n";

	cout << "(1)Lägg till kund.\n"
			"(2)Visa kunder.\n\n"
			"(0)Avlsuta program.\n";
	cin >> option;		//den gillar inte getline till denna variabel.
	cin.get();			//för att motverka '\n' tecknet som kommer.
	
	while(option>0)		
	{

		if (option == '1')
		{
			cout << string(50,'\n');						
			cout << "\nSkriv in följande data.\n";
			cout << "Förnamn: ";
			cin.getline(firstname, maxlength, '\n');		
			
			cout << "Efternamn: ";
			cin.getline(lastname, maxlength, '\n');		

			cout << "Insättning kronor: ";
			cin.getline(balance, maxlength, '\n');
			
			customer.push_back(person(firstname, lastname, balance));  
			dl();	//funktion för dubbla rader... 
			
			
		};
		
		if (option == '2')
		{
			cout << string(50,'\n');		//rent fönster
			vector<person>::iterator iter = customer.begin(); 
			while(iter != customer.end())	// Startar loopen för att skriva ut alla stackar.
			iter++->show_person();			// skriver ut stacken och byter till nästa stack.
			cout << "Tryck enter för att komma till menyn.";
			cin.get();

			
		};
		
		if (option == '0')
			return 0;		// avslutar programmet.

		cout << string(50,'\n');
		cout << "\nBANKPROGRAMMET: "<<dl; // den återkommande menyn.

		cout << "(1)Lägg till kund.\n"
				"(2)Visa kunder.\n\n"
				"(0)Avlsuta program.\n";
		cin >> option;			// ger option ett nytt värde.
		std::cin.get();

		
		
	}
	
	


}

/*-------------------------------------------------------------------------------*/



//person.h 

#pragma once			// För att bara ladda player.h en gång.
#include <cstring>
#include <iostream>
#include <iomanip>

using std::cout;
using std::endl;
using std::setw;
using std::string;

class person {
public:
	person(char* first = "John", char* last = "Doe", char* money = "4000") //grund constructor, beskriver player funktionen.
	{
		size_t length = strlen(first)+1;
		firstname = new char [length];
		strcpy_s(firstname, length, first);
		
		length = strlen(last)+1;
		lastname = new char [length];
		strcpy_s(lastname, length, last);
		
		length = strlen(money)+1;
		balance = new char [length];
		strcpy_s(balance, length, money);
		

	};

	person(const person& p) // constuctor kopia, som skall lägga in data med hjälp av pekare.
	{
		size_t length = strlen(p.firstname)+1;
		firstname = new char [length];
		strcpy_s(firstname, length, p.firstname);

		length =strlen(p.lastname)+1;
		lastname = new char [length];
		strcpy_s(lastname, length, p.lastname);

		length = strlen(p.balance)+1;
		balance = new char [length];
		strcpy_s(balance, length, p.balance);
		
	}

	~person()						// destuctor
	{
		delete[] firstname;
		delete[] lastname;
		delete[] balance;
		
	}

	person& operator=(const person& p) // Har något med addressering och peckare att göra.
	{
		if (&p == this)
			return *this;

		delete [] firstname;
		delete [] lastname;
		delete [] balance;

		size_t length = strlen(p.firstname)+1;
		firstname = new char[length];
		strcpy_s(firstname, length, p.firstname);

		length =strlen(p.lastname)+1;
		lastname = new char [length];
		strcpy_s(lastname, length, p.lastname);

		length = strlen(p.balance)+1;
		balance = new char [length];
		strcpy_s(balance, length, p.balance);

		return *this;
	}

	bool operator<(const person& p) const			//jämnför lastname och firstname.
	{
		int result = strcmp(lastname, p.lastname);
		if (result < 0 || result == 0 && strcmp(firstname, p.firstname) < 0)
			return true;
		return false;
	}

	void show_person() const		// skriver ut data med en tabbs mellanrum.
	{	
		cout << firstname << "\t" << lastname << "\t" << balance << "\t" << endl;   
	}

private:	// bra för säkerheten. 
	char* firstname;
	char* lastname;
	char* balance;
};



Is This A Good Question/Topic? 0

Replies To: I need some guidance concerning vector stacks

#2 RedSon  Icon User is offline

  • D.I.C Head

Reputation: 56
  • View blog
  • Posts: 179
  • Joined: 01-June 10

Re: I need some guidance concerning vector stacks

Posted 09 June 2010 - 10:41 AM

Where in the code are you having a problem?

So, you need some kind of loop to iterate through all the elements in the vector? You are searching for what item?

Give me an example use case that you want to work, but that currently doesnt.
Was This Post Helpful? 0
  • +
  • -

#3 Guest_Intekin*


Reputation:

Re: I need some guidance concerning vector stacks

Posted 09 June 2010 - 10:46 AM

Everything th that code is working, yes.

But the thing with withdrawing and depositing and deleting specific objects elude me.
I do not where to begin with that. There is something about that that is not connecting in my brain.
Was This Post Helpful? 0

#4 RedSon  Icon User is offline

  • D.I.C Head

Reputation: 56
  • View blog
  • Posts: 179
  • Joined: 01-June 10

Re: I need some guidance concerning vector stacks

Posted 09 June 2010 - 10:51 AM

Lets talk about a normal user case for withrawing and then depositing:

What are the steps necessary to make a withdrawl?

  • User enters in identification information (Card, acct number, name whatever). This step might be skipped for you.
  • User identifies amount they would like to withdraw.
  • Bank checks user's current balance and subtracts money if the balance is higher than the amount.
  • Define the error case for the previous 3 steps.


Does that match with your program so far? Can we do those steps? How would one go about withdrawing money using your program?
Was This Post Helpful? 1
  • +
  • -

#5 Guest_Intekin*


Reputation:

Re: I need some guidance concerning vector stacks

Posted 09 June 2010 - 11:06 AM

Yes, that is basically it.

And I do know how to implement that with a little trail and error. But for selecting a specific customer from the stack with people and changing his or her money balance. Since my knowledge with stacks end with adding and deleting the top object in a stack.
Was This Post Helpful? 0

#6 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3103
  • View blog
  • Posts: 10,899
  • Joined: 08-August 08

Re: I need some guidance concerning vector stacks

Posted 09 June 2010 - 11:16 AM

It looks to me like you need to keep track of the customer's number as you add them to the vector. That would be analogous to an account number.

Cust_Number = customer.size(); // store/use this to access the appropriate vector later.
customer.push_back(person(firstname, lastname, balance));  


This post has been edited by CTphpnwb: 09 June 2010 - 11:17 AM

Was This Post Helpful? 0
  • +
  • -

#7 RedSon  Icon User is offline

  • D.I.C Head

Reputation: 56
  • View blog
  • Posts: 179
  • Joined: 01-June 10

Re: I need some guidance concerning vector stacks

Posted 09 June 2010 - 11:21 AM

Ahh okay, so you don't know how to iterate through a vector.

Here are the ingredients you will need: http://www.cplusplus.../vector/vector/



You can use the for loop to iterate through the vector elements using the known size and the [] operator. When you find the specific customer using the comparison operators you can then get the information using the [] operator or the at method.

See if you can combine and mix thoroughly then apply a complier and bake until golden brown.
Was This Post Helpful? 1
  • +
  • -

#8 Guest_Intekin*


Reputation:

Re: I need some guidance concerning vector stacks

Posted 09 June 2010 - 11:32 AM

Okey, lets see if I can get an understanding of that process before I need to turn in the little project.

I really appreciate all the help so far. If I do not have an understanding of it by this time tomorrow maybe I'll return with more questions and maybe I'll sign up too. Seams like a good site with good people. =)
Was This Post Helpful? 0

#9 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3103
  • View blog
  • Posts: 10,899
  • Joined: 08-August 08

Re: I need some guidance concerning vector stacks

Posted 09 June 2010 - 11:47 AM

Maybe this will make it clear:
#include <vector>
#include <iostream>

using namespace std;

int main()
{
	vector<int> test;
	test.push_back(33);
	test.push_back(44);
	
	cout<<"size:"<<test.size()<<endl;
	cout<<"contents: " << endl;
	for(int i = 0; i < test.size(); i++)
	{
		cout<<test[i]<<endl;
	}
	cout<<endl;
	return 0;
}

Was This Post Helpful? 0
  • +
  • -

#10 Intekin  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 10-June 10

Re: I need some guidance concerning vector stacks

Posted 10 June 2010 - 12:18 PM

And here I am again, registered and everything.

Thank you CTphpnwb for that little demonstration. Even if I came to understand a little more about vectors, it did not give the mind blowing revelation I needed to solve the problem I have with the iterators, index and the other confusing stuff related to the withdraw and deposit part of my programing assignment.

It seam like I am a terrible slow learner of things I have no experience of.

I have changed the part that deals with showing the customers a little.


if (option == '2')
{
	cout << string(50,'\n');		

	cout << left << setw(8) << "Kund Nr" << setw(10) << "Förnamn" << setw(10) << "Efternamn" << right << setw(10) << "Belopp" << endl
	     << left << setw(8) << "-------" << setw(10) << "-------" << setw(10) << "---------" << right << setw(10) << "------" << endl;
	for (vector<person>::iterator iter = customer.begin(); iter != customer.end(); ++iter)
	{					
		iter->show_person();
	}
	cout << "\n\nVälj en av kunderna för att kolla närmare på.\nSkriv in kundnummret för att välja kund.\n\n\n";    
        //withdrawing and depositing shall be added after this
	cin.get();



Was This Post Helpful? 0
  • +
  • -

#11 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3103
  • View blog
  • Posts: 10,899
  • Joined: 08-August 08

Re: I need some guidance concerning vector stacks

Posted 10 June 2010 - 01:17 PM

I think you want something like:
        for ( iter = 0; iter < customer.size(); ++iter)
        {                                       
                customer[iter].show_person();
        }


or change
                iter->show_person();


to

                iter.show_person();


This post has been edited by CTphpnwb: 10 June 2010 - 01:31 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1