c++ forums

My younger brother is away at school, and having a bit of trouble with a CS project. Can anyone recommend an active forum for help with C++?

There’s a “General Programming” board. Post it there, it is an active community. Or post it here, I am a C++ programmer.

Thanks for the link, I’ll let him know about that site.

heres the main program he’s trying to compile with cygwin.

#include <fstream>
#include "Bag.h"
using namespace std;

int main () {
  ofstream output1 ("output1.txt");
  if (output1.is_open())
  {
	  Bag example1(), example2(5);
	  output1 << example1.print() << "
";
	  output1 << example2.print() << "
";
	  example2.add(5);
	  example2.add(3);
	  example2.add(7);
	  output1 << example2.print() << "
";
	  example2.take(1);
	  example2.take(1);
	  output1 << example2.print() << "
";
	  output1.close();
  }
  return 0;
}

I’m not sure if you need to see the Bag files, but in case

Bag.h

#ifndef BAG_H
#define BAG_H
#include <iostream>
using namespace std;

class Bag
{		
		public:
			Bag();
			Bag(int);
			Bag(const Bag&);
			Bag& operator =(const Bag&);
			Bag& operator +(const Bag&);
			~Bag();
			void add(int);
			void take(int);
			void print();
			



		private:
			int *p;
			int maxsize;
			int currentloc;

Bag.cpp


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

// This program simulates a Bag using int
	

	// Default constructor creating an empty bag with room for ten int's
	Bag::Bag()
	{
		p = new int[10];
		maxsize = 10;
		currentloc = 0;
		cout<<"CONSTRUCTOR";
	}
	
	// Constructor that creates a bag with the size input into the parameter
	Bag::Bag(int size)
	{
		p = new int[size];
		maxsize = size;
		currentloc = 0;
		cout<<"CONSTRUCTOR";
	}

	// Copy constructor that copies a bag and makes itself exactly like it
	Bag::Bag(const Bag& anotherBag)
	{
		maxsize = anotherBag.maxsize;
		currentloc = anotherBag.currentloc;
		p = anotherBag.p;
		cout<<"COPY CONSTRUCTOR";
	}

	// Overrides the = funtion and checks to see if two bags are the same in size
	Bag& Bag::operator=(const Bag& anotherBag)
	{
		cout<<"ASSIGNMENT OP";
		int other_maxsize = anotherBag.maxsize;
		if (maxsize == other_maxsize)
		{
			cout << "These two bags sizes are equal.";
		}
		else
		{
			cout << "These two bags sizes are not equal. ";
		}
	}

	// Overrides the + function and adds the contents of one bag and puts it in another
	Bag& Bag::operator+(const Bag& anotherBag)
	{
		cout<<"ADDITION OP";
		int another_maxsize = anotherBag.maxsize;
		int *temp_Bag = anotherBag.p;
		int new_maxsize = maxsize + another_maxsize;
		int *temp_array = new int[new_maxsize];
		for (int index = 0; index < currentloc; index++)
			{
				temp_array[index] = p[index];
			}
		int index2 = 0;
		for (int index = currentloc; index < (currentloc + anotherBag.currentloc); index++)
			{
				temp_array[index] = temp_Bag[index2];
				index2++;
			}

		maxsize = new_maxsize;
		p = temp_array;
		currentloc = currentloc + anotherBag.currentloc;

		return *this;
	}

	// Destructor that frees memory allocated for p back to system
	Bag::~Bag()
	{
		delete [] p;
		p = NULL;
		cout<<"DESTRUCTOR";
	}

	// Method that adds an int to the bag making it one size bigger
	void Bag::add(int number)
	{
		cout<<"ADDING";
		if (currentloc < maxsize)
		{
			p[currentloc] = number;
			currentloc++;
		}
		else
		{
			maxsize++;
			currentloc++;
			int *temp_array = new int[maxsize];
			for (int index = 0; index < (maxsize-1); index++)
			{
				temp_array[index] = p[index];
			}
			temp_array[currentloc] = number;
			p = temp_array;
		}
	}


	// Method that takes an int from the bag making it one size smaller
	// int refers to its location in memory using a zero index
	void Bag::take(int location)
	{
		cout<<"TAKING";
		int new_maxsize = maxsize - 1;
		int *temp_array = new int[new_maxsize];
		int first_half = location - 1;
		int second_half = location + 1;
		for (int index = 0; index < location; index++)
		{
			temp_array[index] = p[index];
		}
		for (int index = location; index < new_maxsize; index++)
		{
			temp_array[index] = p[second_half];
			second_half++;
		}
		
		maxsize = new_maxsize;
		p = temp_array;
		currentloc = new_maxsize;
		
	}

	// Method that prints out the bag to the console window
	void Bag::print()
	{
		for (int index = 0; index < currentloc; index++)
		{
			int number = p[index];
			cout << number << "
";
		}

	}

I’m honestly not sure what this is supposed to do, I’m no programmer. :stuck_out_tongue:

I appreciate any help you can offer.

actually he said: its a simple test program that supposed to test add take and print and send data to a file

if that helps

Well, I do a bit of programming and I couldn’t figure out what it was meant to do either. Well, I didn’t bother figuring it out is more accurate but the problem is that it wasn’t compiling and I got it to compile:

You missed a }; from the end of class declaration in Bag.h - I reckon that was a typo on your part and not his mistake.

What his main problem is that he is calling a void function from the main function. Print() has no return value so when he calls it from main, it doesn’t return anything.

There was an #endif statement missing from the bag.h header too.

He was also missing a return *this from one of the Bag functions.

Also, my compiler doesn’t let me use the default constructor using example1(), I have to use example on its own.

Anyway, I get output in the console that looks like:

CONSTRUCTORCONSTRUCTORADDINGADDINGADDING5
3
7
TAKINGTAKING5
0
0
DESTRUCTORDESTRUCTOR
test has exited with status 0.

and file output:

-1073743216
0
7
0

I suspect the file output’s wrong but I don’t know what it’s supposed to output.

Anyway, the updated files look like this:

main.cpp:

#include <fstream>
#include "Bag.h"
using namespace std;

int main () {
  ofstream output1 ("output1.txt");
  if (output1.is_open())
  {
     Bag example1, example2(5);
     output1 << example1.print() << "
";
     output1 << example2.print() << "
";
     example2.add(5);
     example2.add(3);
     example2.add(7);
     output1 << example2.print() << "
";
     example2.take(1);
     example2.take(1);
     output1 << example2.print() << "
";
     output1.close();
  }
  return 0;
}

Bag.h:

#ifndef BAG_H
#define BAG_H
#endif
#include <iostream>
using namespace std;

class Bag
{      
      public:
         Bag();
         Bag(int);
         Bag(const Bag&);
         Bag& operator =(const Bag&);
         Bag& operator +(const Bag&);
         ~Bag();
         void add(int);
         void take(int);
         int print();
         
      private:
         int *p;
         int maxsize;
         int currentloc;

};

Bag.cpp:

#include <iostream>
#include "Bag.h"

using namespace std;

// This program simulates a Bag using int
   

   // Default constructor creating an empty bag with room for ten int's
   Bag::Bag()
   {
      p = new int[10];
      maxsize = 10;
      currentloc = 0;
      cout<<"CONSTRUCTOR";
   }
   
   // Constructor that creates a bag with the size input into the parameter
   Bag::Bag(int size)
   {
      p = new int[size];
      maxsize = size;
      currentloc = 0;
      cout<<"CONSTRUCTOR";
   }

   // Copy constructor that copies a bag and makes itself exactly like it
   Bag::Bag(const Bag& anotherBag)
   {
      maxsize = anotherBag.maxsize;
      currentloc = anotherBag.currentloc;
      p = anotherBag.p;
      cout<<"COPY CONSTRUCTOR";
   }

   // Overrides the = funtion and checks to see if two bags are the same in size
   Bag& Bag::operator=(const Bag& anotherBag)
   {
      cout<<"ASSIGNMENT OP";
      int other_maxsize = anotherBag.maxsize;
      if (maxsize == other_maxsize)
      {
         cout << "These two bags sizes are equal.";
      }
      else
      {
         cout << "These two bags sizes are not equal. ";
      }
	  return *this;
   }

   // Overrides the + function and adds the contents of one bag and puts it in another
   Bag& Bag::operator+(const Bag& anotherBag)
   {
      cout<<"ADDITION OP";
      int another_maxsize = anotherBag.maxsize;
      int *temp_Bag = anotherBag.p;
      int new_maxsize = maxsize + another_maxsize;
      int *temp_array = new int[new_maxsize];
      for (int index = 0; index < currentloc; index++)
         {
            temp_array[index] = p[index];
         }
      int index2 = 0;
      for (int index = currentloc; index < (currentloc + anotherBag.currentloc); index++)
         {
            temp_array[index] = temp_Bag[index2];
            index2++;
         }

      maxsize = new_maxsize;
      p = temp_array;
      currentloc = currentloc + anotherBag.currentloc;

      return *this;
   }

   // Destructor that frees memory allocated for p back to system
   Bag::~Bag()
   {
      delete [] p;
      p = NULL;
      cout<<"DESTRUCTOR";
   }

   // Method that adds an int to the bag making it one size bigger
   void Bag::add(int number)
   {
      cout<<"ADDING";
      if (currentloc < maxsize)
      {
         p[currentloc] = number;
         currentloc++;
      }
      else
      {
         maxsize++;
         currentloc++;
         int *temp_array = new int[maxsize];
         for (int index = 0; index < (maxsize-1); index++)
         {
            temp_array[index] = p[index];
         }
         temp_array[currentloc] = number;
         p = temp_array;
      }
   }


   // Method that takes an int from the bag making it one size smaller
   // int refers to its location in memory using a zero index
   void Bag::take(int location)
   {
      cout<<"TAKING";
      int new_maxsize = maxsize - 1;
      int *temp_array = new int[new_maxsize];
      int first_half = location - 1;
      int second_half = location + 1;
      for (int index = 0; index < location; index++)
      {
         temp_array[index] = p[index];
      }
      for (int index = location; index < new_maxsize; index++)
      {
         temp_array[index] = p[second_half];
         second_half++;
      }
      
      maxsize = new_maxsize;
      p = temp_array;
      currentloc = new_maxsize;
      
   }

   // Method that prints out the bag to the console window
   int Bag::print()
   {
	int number;
      for (int index = 0; index < currentloc; index++)
      {
		number = p[index];
         cout << number << "
";
      }
	return number;
   }

GERR I read the “wrong line” on the posted corrected post and edited out my reply. . .sigh

Well, uh whoever just posted is right, the print() function should either return a standard type (e.g. that supports the ostream “<<” operator) or should itself be an overloaded operator<< (implemented as a friend function) that is of the following form:

ostream operator&lt;&lt;(ostream stream_obj, Bag &bag)
{
      stream_obj &lt;&lt; "This is an example blah blah" &lt;&lt; endl;
      return stream_obj;
}

. . .And he should be using endl, not "
". endl is a C++ standard global variable that has the system-dependant end line chars in it.

Thanks for the help guys. I’ll pass along this info to my bro! :slight_smile:

endl also does an implied flush

cout is buffered, so merely printing a "
" [which should be converted to native anyway] doesn’t mean the line is actually put on the screen

cerr though, isn’t buffered [which can cause some weird things if you mix cout and cerr, but it makes sense that it wouldn’t be buffered because your program can crash before a flush]

I used to frequent http://www.cprogramming.com

Thanks for all your help folks! My brother was able to compile the program. He asked me to relay another question if you don’t mind.

He modified the main code to:

#include "Bag.h"
using namespace std;

int main (void) {
Bag example1, example2(5);
example1.print();
example2.print();
example2.add(5);
example2.add(3);
example2.add(7);
example2.print();
example2.take(0);
example2.print();
return 0;
}

He also asked me to ask “where the 24 comes from”. I’m not sure if he means 24 is one of the numbers or what. Maybe if one of you guys compile this you’ll see a 24, I don’t know.

Again, thanks for the help.