sfuo 111 Practically a Master Poster

This might be your source of error.

class2(int i)
{ 
	class1 objxx(99);
	obj1 = objxx;//constructing object 1
	obj1.variab += i;
	cout << obj1.variab;//using obj1
}

This will actually assign your obj1 variable within class2. Before it was just making a temp variable during your constructor runtime.

sfuo 111 Practically a Master Poster

The reason why its outputting a random number is because it has not been assigned a value. The class2 constructor is making a new variable obj1 in its own scope.

for example

int i = 0;
int x = 0;
if( x >= 0 )
{
	int i = 2;
}
cout << i << endl;

the output to this is 0 because it all has to do with scopes

So to fix your problem you need to make a function to assign a value to obj1 because you only get to run a classes constructor once.

example

void setValue(int i)
{
	obj1.variab = i;
}

not sure if this is what you want to do but it works.

sfuo 111 Practically a Master Poster

Yeah sorry I should have been using your code.

Anyways I made this change to class1 and it worked.

class class1{
	public:
	int variab;
	//constructor
	class1(){} // add the new default constructor
	class1 (int h){variab=h;}
};
sfuo 111 Practically a Master Poster

OK I think I figured it out.

#include <iostream>
#include <string>

using namespace std;
////////////////////////////////////

class CLASS1
{
	string name;
	
	public:
	
	CLASS1();
	CLASS1(string xname);
	
	string getName();
};

CLASS1::CLASS1()
{
}

CLASS1::CLASS1(string xname)
{
	name = xname;
}

string CLASS1::getName()
{
	return name;
}

///////////////////////////////////////
class CLASS2
{
	CLASS1 names;
	
	public:
	
	CLASS2();
	CLASS2(CLASS1 xclass1);
	CLASS1 getName();
};

CLASS2::CLASS2()
{
}

CLASS2::CLASS2(CLASS1 xclass1)
{
	names = xclass1;
}

CLASS1 CLASS2::getName()
{
	return names;
}

/////////////////////////////////

int main()
{
	CLASS1 name("Sean");
	
	CLASS2 names(name);
	
	cout << names.getName().getName();
	
		
	system("PAUSE");
	return 0;	
}

The problem was that when you make your own constructor it deletes the default constructor. All you have to do is remake the default CLASS1() and it should work.

sfuo 111 Practically a Master Poster

Flawed and bigger :( you win.

sfuo 111 Practically a Master Poster

This is the same error that I got when you try to a class inside of another class.

What I did was I used a constructor for class1 and a add() function for class2 instead of a constructor.

sfuo 111 Practically a Master Poster

When I made my "Tile" game I ran into the same problem.

I had a class called Tile and it held information for 1 square. Then I had a class called Board and this had a vector of Tiles within it.

The problem that I had was that if I had a constructor in my Board class it would give me an error saying that I cannot declare Tile or something.

Here is my really quick example with CLASS1 being used in CLASS2. I hope this solves any problems.

#include <iostream>
#include <vector>
#include <string>

using namespace std;
////////////////////////////////////

class CLASS1
{
	string name;
	
	public:
	CLASS1(string xname);
	string getName();
};

CLASS1::CLASS1(string xname)
{
	name = xname;
}

string CLASS1::getName()
{
	return name;
}

///////////////////////////////////////
class CLASS2
{
	vector<CLASS1> names;
	
	public:
	
	void addName(CLASS1 name);
	CLASS1 getName(int x);
};

void CLASS2::addName(CLASS1 name)
{
	names.push_back(name);
}

CLASS1 CLASS2::getName(int x)
{
	return names[x];
}

/////////////////////////////////

int main()
{
	CLASS1 name = CLASS1("Sean");
	
	CLASS2 names;
	names.addName(name);
	
	cout << names.getName(0).getName() << endl;
		
	system("PAUSE");
	return 0;	
}
sfuo 111 Practically a Master Poster

Lets see yours because last time I checked this works and its short.

sfuo 111 Practically a Master Poster

This is some really quick prime number checker that I made since the post above. It checks to see if the number can be divided by 2, 3, 5, 7 evenly and if it is not 1. If it passes through all this then it is prime.

#include <iostream>

using namespace std;

bool isPrime(int x)
{
	if( x == 1 )
		return false;
	if( x % 2 == 0 && x != 2 )
		return false;
	if( x % 3 == 0 && x != 3 )
		return false;
	if( x % 5 == 0 && x != 5 )
		return false;
	if( x % 7 == 0 && x != 7 )
		return false;
	return true;
}

int main()
{
	int max;
	cout << "Max?: ";
	cin >> max;
	for( int i = 1; i <= max; i++ )
	{
		if( isPrime(i) )
		{
			cout << "Prime: " << i << endl;
		}
	}
	system("PAUSE");
	return 0;
}
sfuo 111 Practically a Master Poster

A bool function returns true or false.

sfuo 111 Practically a Master Poster

First of all, you should learn how to click the code button to put code tags around your code.

Second two of us posted fixes for this already but if you really want to use this very first post then read the comments below for the change in code.

#include <iostream>
using namespace std;

void drawShape(int nrP)
{
int s1 = 0,s2 = 0;  //(NEW)

for (int i = 1; i <= nrP; i++)
{
    s1 = i - 1;
    s2 = (2 * nrP - 2 * i - 1); 

    for (int j = 1; j <= s1; j++)
        cout << '^';

    cout << '#';

    for (int j = 1; j <= s2; j++)
        cout << '^';
    if( i != nrP ) //this says if it is on the last line then do not output
        cout << '#';

    for (int j = 1; j <= s1; j++)
        cout << '^';

    cout << endl;
} 
}

int main()
{
   int nr = 0;

    cout << "Number of rows: ";
    cin >> nr;
    drawShape(nr);

    return 0;
}
mrnutty commented: Starting to help out, eh? +5
sfuo 111 Practically a Master Poster

If you are using firstPerson's Pattern() function I'm not sure if it was intentional but it draws like so.
Input 5 for rows:
^***^***^
*^**^**^*
**^*^*^**
***^^^***
****^****

To get rid of the line down the middle I just put

else if(i == Mid && i == Row-1)

since the only place that one '^' will occur is on the last row.

sfuo 111 Practically a Master Poster

Post your code and I'll have a look.

sfuo 111 Practically a Master Poster

It is because all of the upper rows output 2 '#'s and he didn't put in if s1 == s2 then do not output a 2nd '#'.

sfuo 111 Practically a Master Poster

I used the nested for() loops for it and I got it to work fine for any input.

This is just the function:

void drawShape(int nrP)
{
	for(int r = 0; r < nrP; r++)
	{
		for(int c = 0; c < (nrP*2-1); c++)
		{
			if( r == nrP-1 && c == ((nrP*2-1)-1)/2) //last r and middle c
			{
				cout << '#';
			}
			else if( c == r || c == ((nrP*2-1)-1) - r ) //1st and last point on the V shape on the row
			{
				cout << '#';				
			}
			else //not a point on the V shape
			{
				cout << '^'; 
			}
			if( c == (nrP*2-1)-1) //end of the r
			{
				cout << endl;
			}
		}
	}
}
sfuo 111 Practically a Master Poster

I have looked over the code and got it working. However, I guess I cannot post the code or I could lose reputation. The big thing I you should do is reread your code because I found lots of typos and missing statement endings '}'. Also, take a look at your for() loops

for(;i>3||i<1||j>3||j<1||'X'==matrix[i-1][j-1]||'O'++matrix[i-1][j-1];)

and right below your for() loop if you read it it pretty much says unless there is an error on input I'm not going to add the initial input to the matrix.

You also have a line above player 1's turn that says if( sum == 0 ) then its his turn. Sum is only equal to zero on the first turn so that would mean player 1 only gets to have one turn.

It's just a bunch of minor bugs that turn it into a major mess. If after fixing up the things stated above you still have problems just ask I have it all done.

sfuo 111 Practically a Master Poster

Why don't you just remove the counter and the loss thing and then submit it? It was working fine till we added that.

If you have any questions on other assignments feel free to PM me I find this stuff fun to work out.

I'm off to bed good night.

sfuo 111 Practically a Master Poster

OK my fault here I was just talking and not testing.

You need to change

static double counter= 0;
static double loss= 0;

to

double counter;
double loss;

I made up variable names here because I'm getting tired =(.
Now go into the constructors and assign zero to these.

SavingsAccount::SavingsAccount()
{
	balance = 100.0;
	counter = 0;
	loss = 0;
}

SavingsAccount::SavingsAccount( double balance )
{
	balance = balance;
	counter = 0;
	loss = 0;
}

This way when you go counter++ you are not adding 1 to a undefined variable and that would give you some random number.
This fix should work. Get back to me fast if you have any questions because I plan on calling it a night in 30 min or so.

sfuo 111 Practically a Master Poster

That looks error free to me tell me if you still have some in that class tho.

As for outputting the counter and fees you put that right under the other 2 outputs

outData << "Savings Total: " << savingsAccount.getBalance() << endl;
outData << "Savings Total Transactions: " << savingsAccount.getTransactionCounter() << endl;
outData << "Savings Total Lost to Withdraw Fee: " << savingsAccount.getTotalLostToFee() << endl;
outData << "Checking Total: " << checkingAccount.getBalance() << endl;
outData << "Checking Total Transactions: " << checkingAccount.getTransactionCounter() << endl;
outData << "Checking Total Lost to Withdraw Fee: " << checkingAccount.getTotalLostToFee() << endl;
sfuo 111 Practically a Master Poster

It's really hard to tell what is wrong when I can't see what you changed but if it is saying that it is undeclared then that means you misspelled the variable name or you didn't declare it in the class properly.

sfuo 111 Practically a Master Poster

Yeah but the ++ at the end just means increase by 1 so that is not part of the variable name.

Also try going

static double totalLostToFee = 0;
static double transactionCounter = 0;
sfuo 111 Practically a Master Poster

Notice how I am just making savingsAccount and checkingAccount and not trying to run the functions? This is exactly what you want to have for your main.cpp file.

"main.cpp"

#include <fstream>
#include "account.h"

using namespace std;

int main()
{
	SavingsAccount savingsAccount;
	CheckingAccount checkingAccount;
	
	ifstream inData("input.txt");
	ofstream outData("output.txt");
	
	string transactionType, accountType;
	double amount;
	
	while(inData >> accountType >> transactionType >> amount )
	{
		if( accountType == "savings" )
		{
			
			if( transactionType == "deposit" )
			{
				savingsAccount.deposit(amount);
				
			}
			else if( transactionType == "withdraw" )
			{
				savingsAccount.withdraw(amount);
			}
		}
		else if( accountType == "checking" )
		{
			if( transactionType == "deposit" )
			{
				checkingAccount.deposit(amount);
			}
			else if( transactionType == "withdraw" )
			{
				checkingAccount.withdraw(amount);
			}
		}
	}
	
	outData << "Savings Total: " << savingsAccount.getBalance() << endl;
	outData << "Checking Total: " << checkingAccount.getBalance() << endl;
	
	system("PAUSE");
	return 0;
};
sfuo 111 Practically a Master Poster

After looking over this I see you are calling the functions at the start of main() and I have no clue why.

All you have to do is declare them

SavingsAccount savingsAccount;
CheckingAccount checkingAccount;

Do not do their functions and use amount as a parameter when it has not been defined yet.

Also one thing to remember is that variables are case sensitive so transactioncounter is not the same as transactionCounter so pick one and use it.

In the class constructors you need to assign transactionCounter and totalLostToFee a value (zero or something) or try declaring them as static doubles (not constant because you want their value to change).

I see you just put getTransactionCounter() without a return type. You want:

double getTransactionCounter()

do the same for the Fee variable too.

It looks like the #ifndef statement closer is fixed with the #endif but let me know if that is still an error.

sfuo 111 Practically a Master Poster

Yeah, add it to the one with the ifsteam and ofstream in it.

Delete one of the sets of class declarations out of the main() or it will say it is already defined.

sfuo 111 Practically a Master Poster

Yeah put it in there and then right below your getBalance() function add getTotalLostToFee() and getTransactionCounter().

sfuo 111 Practically a Master Poster

I'm not sure what compiler you are using but it should be telling you that you cannot have 2 main() functions declared in the same project.

Also by the looks of it, unless you just cut it off, you did not close the #ifndef "COSC214Lab3.h"
statement with
#endif at the bottom of the file

sfuo 111 Practically a Master Poster

Also you want the transactionCounter++; on deposits too.

That is if they are counted as transactions but I'm pretty sure they are.

And I see you still have single quotes around savings in the input.txt reader

sfuo 111 Practically a Master Poster

I never even asked you about the WITHDRAW_FEE variable but is that supposed to be a percent or just a flat rate?

The way I set it up it is a percentage of what you withdraw so 0.50 would be 50% which is huge. If you want it as a flat $0.50 fee change it to

balance -= amount + WITHDRAW_FEE;
sfuo 111 Practically a Master Poster

Yeah I actually rewrote the whole thing out.

Just post your class structure and I'll take a look at what you have unless it is no different from your last code post.

"Account.h"
"Account.cpp"

sfuo 111 Practically a Master Poster

I understand what you mean but would you want it for a multiple use counter or just how much you lost from the transaction fee on the one run of the program?

For these you could just declare a variable in the class structures and then whenever you withdraw it goes

balance -= amount*(1+WITHDRAW_FEE);
//name the variables whatever you want
totalLostToFee += amount*WITHDRAW_FEE;
transactionCounter++;

This code would go into your withdraw() functions and then you would make getTotalLostToFee() and getTransactionCounter() returning themselves. Use it the same way you output the balances to the text file.

sfuo 111 Practically a Master Poster

Just noticed that when I copy pasted I left some stuff in the main as savingsAccount when it should be checkingAccount.

while(inData >> accountType >> transactionType >> amount )
	{
		if( accountType == "savings" )
		{
			
			if( transactionType == "deposit" )
			{
				savingsAccount.deposit(amount);
				
			}
			else if( transactionType == "withdraw" )
			{
				savingsAccount.withdraw(amount);
			}
		}
		else if( accountType == "checking" )
		{
			if( transactionType == "deposit" )
			{
				savingsAcco2unt.deposit(amount);
			}
			else if( transactionType == "withdraw" )
			{
				savingsAccount.withdraw(amount);
			}
		}
	}

should change to

while(inData >> accountType >> transactionType >> amount )
	{
		if( accountType == "savings" )
		{
			
			if( transactionType == "deposit" )
			{
				savingsAccount.deposit(amount);
				
			}
			else if( transactionType == "withdraw" )
			{
				savingsAccount.withdraw(amount);
			}
		}
		else if( accountType == "checking" )
		{
			if( transactionType == "deposit" )
			{
				//change here
				checkingAccount.deposit(amount);
			}
			else if( transactionType == "withdraw" )
			{
				//change here
				checkingAccount.withdraw(amount);
			}
		}
	}
sfuo 111 Practically a Master Poster

OK the format might be a bit different but it works.

This writes to "output.txt" and reads in from "input.txt"

"account.h"

#ifndef ACCOUNT_H
#define ACCOUNT_H

//SAVINGS ACCOUNT
class SavingsAccount
{
	double balance;
	static const double WITHDRAW_FEE = 0.0;
	
	public:
		
	SavingsAccount();
	SavingsAccount( double balance );
	
	void setBalance( double amount );
	
	void withdraw( double amount );
	void deposit( double amount );
	
	double getBalance();
};

//CHECKING ACCOUNT
class CheckingAccount
{
	double balance;
	static const double WITHDRAW_FEE = 0.5;
	
	public:
		
	CheckingAccount();
	CheckingAccount( double balance );
	
	void setBalance( double amount );
	
	void withdraw( double amount );
	void deposit( double amount );
	
	double getBalance();
};

//SAVINGS ACCOUNT FUNCTIONS
SavingsAccount::SavingsAccount()
{
	balance = 100.0;
}

SavingsAccount::SavingsAccount( double balance )
{
	balance = balance;
}

void SavingsAccount::setBalance( double amount )
{
	balance = amount;
}

void SavingsAccount::withdraw( double amount )
{
	balance -= amount*(1+WITHDRAW_FEE);
}

void SavingsAccount::deposit( double amount )
{
	balance += amount;
}

double SavingsAccount::getBalance()
{
	return balance;
}

//CHECKING ACCOUNT FUNCTIONS

CheckingAccount::CheckingAccount()
{
	balance = 100.0;
}

CheckingAccount::CheckingAccount( double balance )
{
	balance = balance;
}

void CheckingAccount::setBalance( double amount )
{
	balance = amount;
}

void CheckingAccount::withdraw( double amount )
{
	balance -= amount*(1+WITHDRAW_FEE);
}

void CheckingAccount::deposit( double amount )
{
	balance += amount;
}

double CheckingAccount::getBalance()
{
	return balance;
}

#endif

"main.cpp"

#include <fstream>
#include "account.h"

using namespace std;

int main()
{
	SavingsAccount savingsAccount;
	CheckingAccount checkingAccount;
	
	ifstream inData("input.txt");
	ofstream outData("output.txt");
	
	string transactionType, accountType;
	double amount;
	
	/* delete if you want
	format for "input.txt"
	
	accountType transactionType amount
	
	*/
	while(inData >> …
sfuo 111 Practically a Master Poster

Give me about 5 min I'm writing out the whole thing to see what is going on and I will post a working copy in a bit.

sfuo 111 Practically a Master Poster

First off why do you have 2 main() functions declared?

I'm guessing one of your errors is from that.

Also you have 2 constructors in each class but you are only declaring 1.

SavingsAccount::SavingsAccount()
{
	balance = 100.0;
}

SavingsAccount::SavingsAccount( double balance )
{
	balance = balance;
}

is what you want I am assuming

sfuo 111 Practically a Master Poster

Are you sure?

In my quick test code I have a static const double working.

Post your class declarations.

sfuo 111 Practically a Master Poster

Change your "WITHDRAW_FEE" declarations to

static const double WITHDRAW_FEE = 0.0;

Change the value to what you want but in order to declare a constant variable inside a class you need to make it static.

Also, you cannot assign a return type to a constructor.

You have:

void SavingsAccount::SavingsAccount()

and you want to change that to

//remove void
SavingsAccount::SavingsAccount()
sfuo 111 Practically a Master Poster

I'm not 100% sure what you are asking is this correct?

#include <iostream>
#include <string>

using namespace std;

int main()
{

	string arrcon[2];
	
	arrcon[0] = "John";
	arrcon[1] = "56";
	
	for( int i = 0; i < 2; i++ )
	{
		cout << arrcon[i] << endl;
	}
	
	system("PAUSE");
	return 0;
}
sfuo 111 Practically a Master Poster

First off, I think you are missing a semicolon at the end of your SavingsAccount class (line 14, Account.cpp).

For your withdraw and deposit functions you could use:

///savings class


void SavingsAccount::withdraw(double amount)
{
	balance -= amount*(1+WITHDRAW_FEE);
}

void SavingsAccount::deposit(double amount)
{
	balance += amount;	
}


//checking class


void CheckingAccount::withdraw(double amount)
{
	balance -= amount*(1+WITHDRAW_FEE);	
}

void CheckingAccount::deposit(double amount)
{
	balance += amount;	
}

double CheckingAccount::getBalance()
{
	return balance;	
}