2 Replies - 950 Views - Last Post: 07 November 2007 - 06:50 AM Rate Topic: -----

#1 ShadoWraitH  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 05-September 07

Data Flow through Classes

Posted 07 November 2007 - 02:18 AM

I'm having some trouble understanding the flow of data thru classes, and constructs this is the code i have so far
#include <iostream>

using namespace std;

class addData //class to declare addData construct
{
public:
	addData(int inputAdd[2]);
	void writeData(int result); //intializes function to cout result to user
};
class subData //class to declare subData construct
{
public:
	subData(int inputSub[2]);
};
int main()
{
	int i; //counter
	subData Sub(int inputSub[2]); //adds data obtained from loop into the construct
	addData Add(int inputAdd[2]); //adds data obtained from loop into the construct
	int addSub;  //variable to determine to run thru add or subtract method
	int inputAdd[2];  //variables for add data
	int inputSub[2];  //variables for subtract data

	cout << "Press 1 to add 2 input numbers" << endl;
	cout << "Press 2 to subtract 2 input numbers" << endl;
	cin >> addSub;

	if(addSub == 1) //if option 1 is choosen
	{
		for(i = 0; i < 1; i++)//loop to obtain data and stores data on an array
		{
			cout << "Please enter number: " << i + 1 << endl; 
			cin >> inputAdd[i];
		}
	}
	if(addSub == 2) //if option 2 is chossen
	{
		for(i = 0; i < 1; i++) //loop to obtain data and stores data on an array
		{
			cout << "Please enter number: " << i + 1 << endl;
			cin >> inputSub[i];
		}
	}
}
subData::subData(int inputSub[2])  //construct to subtract data
{
	system("pause"); //only added so i could see if program was reaching this point or crashing before it
	int result;
	result = inputSub[0] - inputSub[1];
	void writeData(int result);
}
addData::addData(int inputAdd[2]) //construct to add data
{
	system("pause"); //only added so i could see if program was reaching this point or crashing before it
	int result;
	result = inputAdd[0] - inputAdd[1];
	void writeData(int result);
}
void writeData(int result)
{
	cout << "The result is: " << result << endl;
	system("pause"); //pauses so user can read result
}


the program will compile and run, but it crashes after it ask for first number input, project was due 2 days ago, been trying to work thru it myself but having little success, thx for your help.

Is This A Good Question/Topic? 0
  • +

Replies To: Data Flow through Classes

#2 William_Wilson  Icon User is offline

  • lost in compilation
  • member icon

Reputation: 205
  • View blog
  • Posts: 4,807
  • Joined: 23-December 05

Re: Data Flow through Classes

Posted 07 November 2007 - 05:46 AM

your code compiles and seems to work fine. The only advice i could offer was I added a return statement to your main method, since it is of type int, you really should return one. return 0;
Was This Post Helpful? 0
  • +
  • -

#3 ShadoWraitH  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 05-September 07

Re: Data Flow through Classes

Posted 07 November 2007 - 06:50 AM

View PostWilliam_Wilson, on 7 Nov, 2007 - 05:46 AM, said:

your code compiles and seems to work fine. The only advice i could offer was I added a return statement to your main method, since it is of type int, you really should return one. return 0;


I compiled it and ran it again just to make sure, and as soon as i entered the first number the program crashed, i added the "return 0;" statement ^.^, knew i was forgetting something... thx
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1