sfuo 111 Practically a Master Poster

I would make the checkGuess() take in 2 integers that the user inputs with cin ( int num, denom ) and then compare it to the reduced fraction in f3.

int num, denom;
cout << "num: ";
cin >> num;
cout << "denom: ";
cin >> denom;
f3.checkGuess(num, denom);

The checkGuess() checks by just going

void checkGuess(int xnum, int xdenom)
{
	if( xnum == num && xdenom == denom )
	{
		cout << "Correct!\n";
		print();
	}
	else
		cout << "Wrong, try again!\n;
}

Is this what you are trying for?

sfuo 111 Practically a Master Poster

Everything seems fine except your prime number checker.

Check this thread here

Check out firstPerson's function within his post.

sfuo 111 Practically a Master Poster

Look at where you are calculating their grades.

You have a for() loop that checks through the students to see if they have 10 absences or not then after that loop it goes to another loop that calculates their grade.

You want to take out the 2nd loop because it overwrites the final grade = 0 done by the 10 absences.

I see no problem with your modif() function unless you changed it in any way other than setting the indexes from your first post.

sfuo 111 Practically a Master Poster

In your testset.cpp source file it says:
case 'I' : cout << " Enter int element " << endl;

If it wants you to put in a int and expects to get that int back with dump then the vector has to be a int type because there is no way you are going to get a number other than 1 and 0 outta bool.

sfuo 111 Practically a Master Poster

Yeah you would think that its an int array but he wants it to work with a bool array.

I'm pretty sure making it int is the only way to go.

sfuo 111 Practically a Master Poster

Your welcome.

I learned lots out of this too so its win win.

sfuo 111 Practically a Master Poster

Well I was right that the source of the problem wasn't being posted.

vector <bool> setlist;  // indicates set elements as true

bool type translates to 0s and 1s so anything that is not false is a true meaning everything that is not 0 becomes 1.
If you change this to <int> this should fix your problem.

sfuo 111 Practically a Master Poster

First of all what scope is your gradebook[][] array declared in?

Try putting this for your Modif() function:

void *Modif (double gradebook[][9], int Students)

Not 100% sure if that will even do anything but I think it might change your array.

sfuo 111 Practically a Master Poster

Any chance you could attach your source files so I can see exactly what you have and not just sections?
Click the Reply to Thread button and right below the input box is Additional Options where you can attach your .h and .cpp files.

Because after looking at your screen shot I have a feeling you aren't showing everything that plays a role in this.

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

I'm not sure if you have solved your problem in this but when I used the code from your first post with my modifications from the seconds post I got it working fine.

#include <iostream>
#include <vector>
#include <iterator>

using namespace std;

class set
{
	int cardinality;
	vector<int> setlist;
	const static int DEFAULTSIZE = 100;
	public :
	set();
	set(int n);
	bool empty() const;
	bool isIn(int x) const; 
	void dump() const;
	int getCardinality() const;
	void insert(int x);
	int getSmallest() const;
	int getLargest() const;  
};

set::set()
{
     cardinality = DEFAULTSIZE;
     setlist.resize(DEFAULTSIZE, false);
}

set::set(int n)
{
     cardinality = n;
     setlist.resize(n, false);
}

bool set::empty() const
{
     if ( setlist.empty() )
     {
       return true;
     }
     return false;
}

void set::insert(int x)
{
     if (x >= 0 && x <= DEFAULTSIZE)
     {
           setlist.insert(setlist.end(), 1, x);
           cardinality++;
     }
}

int set::getLargest() const
{
    if ( !setlist.empty() )
    {
    	return *( max_element( setlist.begin(), setlist.end() ) );
    }
} 

int set::getSmallest() const
{
    if ( !setlist.empty() )
    {
    	return *( min_element( setlist.begin(), setlist.end() ) );
    }
}

void set::dump() const
{
     copy (setlist.begin(), setlist.end(), ostream_iterator<int>(cout," "));
     cout << endl;
}

bool set::isIn(int x) const
{
     return *( find( setlist.begin(), setlist.end(), x) );
}


int main()
{
	set test(0);
	test.insert(5);
	test.insert(10);
	test.dump();
	
	system("PAUSE");
	return 0;
}

The way you have it set up for your set() constructor it will output one-hundred 0s and then what you put in with your insert() function. No idea where you are getting the 1s and 0s from.

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 off, when you post in the future you should use the code tags to make it way easier to read.

Last time I checked your main() function has to return an int and you have it as a void function.

void main (void)
{
	mainProgram();
}

I don't know if its just because I use Dev-C++ and you have Borland.


You have a line in your mainMenu() function

cin >> "choice"; //this is wrong "choice" is not a variable
cin >> choice; //change to this

In mainProgram() function

char userChoice;
userchoice = mainMenu ();
switch (userchoice)

userchoice and userChoice are two different variables so watch out for typos

and

case 'E'
case 'e'

case 'E': //add a colon to both cases like you did above for all the other case statements
case 'e':

I'm not too sure why you made a function called mainProgram() when if you stuck it's contents into your main() function.

sfuo 111 Practically a Master Poster

This seems like a pretty bad homework assignment seeing how you can only use a few coins to get 10000 dongs.

This is what I put together really quick. It's not done you need to come up with an output and you might wanna double check to see if the input limits are right.

int main()
{
	int input;
	
	int values[4] = {5000,2000,1000,500};
	
	int amounts[4] = {0,0,0,0};
		
	cout << "How many dongs?" << endl;
	cin >> input;
	
	if( input > 10000 || input < 0 || input % 500 != 0 )
	{
		return 1;
	}
		
	for( int i = 0; i < 4; i++ )
	{
		if( input >= values[i] )
		{
			amounts[i] = input / values[i];
			input -= values[i] * amounts[i];
		}
		
		if( input == 0 )
		{
			break;
		}
	}
	system("PAUSE");
	return 0;
}
sfuo 111 Practically a Master Poster

Hi, I was wondering if anyone could help me figure out how to see who is connected to you when you are hosting a game on a specific port via c++. I know you can use netstat in cmd prompt and it shows a bunch of info of who you are connected to and stuff.

What I want to do is make a program with C++/win32 api and winsock that detects people connecting to me and I would like to be able to ping to that IP or disconnect them.

Thanks for those that take the time to read this.

sfuo 111 Practically a Master Poster

Thanks so much I knew it had something to do with operators but I didn't know what one.

sfuo 111 Practically a Master Poster

You can use this to encrypt your string.

void EncryptableString::encrypt()
{
	for( int i = 0; i < content.length(); i++ )
	{
		if( (content[i] >= 'a' && content[i] < 'z') || (content[i] >= 'A' && content[i] < 'Z'))
		{
			content[i] += 1;
		}
		else if( content[i] == 'z' || content[i] == 'Z' )
		{
			content[i] -= 25;
		}
	}	
}

I made a class that has a private variable content and has two public functions: print() and encrypt().

Based on some of your start code you showed I have a feeling you are going to run into a few problems while doing this project.

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

int main()
{
EncryptableString.encrypt();

	cin.get();
	return 0;
}

EncryptableString.encrypt() means nothing because it is a variable type not a variable its self.

Coming across this thread made me wonder how to output a variable within a class without having to call a function. I made a new thread for that but not knowing how to do that forced me to use a print() function, which I think is messy unless this is going to be a closed source submission.

sfuo 111 Practically a Master Poster

Hey I was wondering how the string class is able to output its self the way it does.
For example:

#include <iostream>
#include <string>

using namespace std;

int main()
{
	string testString;
	
	testString = "Hello";
	
	cout << testString << endl;	
	
	system("PAUSE");
	return 0;
}

If I were to try to make a mimic class such as

#include <iostream>
#include <string>

using namespace std;

class myClass
{
	string content;
	
	
	public:
		
	void operator=(string newContent)
	{
		content = newContent;
	}
	
	string getContent()
	{
		return content;
	}
};


int main()
{
	myClass testString;
	
	testString = "Hello";
	
	cout << testString.getContent() << endl;	
	
	system("PAUSE");
	return 0;
}

I have to call my getContent() function to return the content of the class.

How does the string class output its contents without needing to call a function?

Thank you to those who take the time to read this.

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

1. When I insert elements into the vector, I cannot insert an element higher than 1.. only 0's and 1's.

What is DEFAULTSIZE set to because if it is 1 then yeah 0s and 1s are within the range you set in your if() statement.

2. When I dump the elements all I see are 0's and 1's

Your dump is showing you what you have inside of the vector so if you put 1s and 0s in you are going to get 1s and 0s out.

3. I don't understand if set(int n) is correct or not? Can anyone clarify?

You are not setting it to n you have the number 50000 in the resize parameter which would make the vector have 50000 elements and fill them with 0s. I think you want it to be resize(n); putting false in there is just redundant.

4. Do all the other functions look right?

As for the rest it looks fine aside from the fact that your getLargest(), getSmallest() and isIn() functions have no return value. Just put

return *( min_element( setlist.begin(), setlist.end() ) );

and it will return the smallest value and do the same for the others.

Also, you have this cardinality variable. Is this supposed to track how big the vector is? If so you want your set(int n) function to be

set::set(int n)
{
     if( n >= 0 )
     {
          cardinality = n;
          setlist.resize(n, false);
     }
}

Again I have no idea what DEFAULTSIZE …

kvprajapati commented: I like your way. +17
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

OK so I wrote out all your code into a new project and since this is just a short example I am going to give you what I got.

#include <iostream>
#include <string>

using namespace std;


struct weapons
{
	string name;
	int str;	
};

struct user
{
	string name;
	int str;
	weapons weapon;  //changed type of variable from string to your structure weapons
};

weapons sword_01 =
{
	"Nub Sword", //put comma not semicolon
	5
};

user player_01 =
{
	"CrAzD", //put in quotes (")
	0,
	sword_01
};

int main()
{
	player_01.str = player_01.weapon.str; //variable "player" does not exist

	cout << player_01.str << endl; //included to see if above line worked
	
	system("PAUSE");
	return 0;
}

Based on what you said you were looking for it looks like you want a weapons type variable in player instead of it being a string for what weapon you have.

I'm not sure if you were in a rush typing out your short example code or something but you made a bunch of small mistakes (see above code comments).

sfuo 111 Practically a Master Poster

What does your maze look like? You just give the start and finish points.

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

Last post was March 15th, 2006. In the before posting thread it says don't post on dead threads.

sfuo 111 Practically a Master Poster

Most programs do their actions on key up. A way around this is to make a bool variable and make a KEY_UP if() statement.

For example:

bool upKey;
if( KEY_DOWN(VK_UP) && !upKey )
{
	upKey = true;
	//actions	
}

if( KEY_UP(VK_UP) )
{
	upKey = false;
}

I'm not sure if this is what you tried with a bool variable.

sfuo 111 Practically a Master Poster

The whole reason why you would use the getline() function is to pick up spaces. If I were to input the name "Jim" with just cin, this would work but if I were to give him a last name "Jim McTavish" then it would only take in "Jim" and ignore the space and everything after. With getline() you would get the whole name.

By using cin.ignore() I was able to get it working fine with your code and this adjustment.

sfuo 111 Practically a Master Poster

Just add

cin.ignore();

at the bottom of your for() loop for input and this will ignore the '\n' character from the last cin.

Also it looks like you do not have

#include <iostream>

at the top of your file for cin and cout usage.

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

I would put a while() loop with a bool variable in it

while(!done)

and every time you win a point you add 1 to your score and 1 to the computers score when he wins. When one of the scores reach 2 then just make done = true and the loop will stop.

sfuo 111 Practically a Master Poster

I am using <time.h> for the srand() function and <stdlib.h> for the time variable.

As for the swap function I knew the way I did it was bad but I am not too great at passing stuff by pointers. I self taught myself C++ and when reading about pointers and references I didn't fully understand =(.

Here is my change to that.

void *swap( int A[] )
{
	for( int i = 0; i < 10; i++ )
	{
		int temp = A[i];
		int ran = random(10);
		A[i] = A[ran];
		A[ran] = temp;		
	}
}
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

I know what you wanna do but I don't know how to change numbers from say 7 to binary. If I knew how I could come up with a way to split it into the 3 columns. [0][0], [0][1], [0][2]

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

/