classes, array of objects, member functions, instantiation. stuck on second input

Good afternoon,

This is totally a homework question and I have the code written and it compiles and runs, but the console hangs when I input the second side.

I suspect the issue is with passing variables between the functions, but at this point, I've been staring at this code and another question for hours and none of it is making sense any more.

I know I don't need fflush(stdin). I was grasping at straws so it's there.

I did try doing breakpoints and looking at the variables. Both side1 and side2 take the inputted value, so it's not garbage.

Is there anything blatantly obviously wrong? Thank you :)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#include<iostream>
#include<iomanip>
using namespace std;

void totVolume(void);

class Box
{
	public:

	float side1, side2, side3;
	float volume;

	void Get_W ();
	void Get_H ();
	void Get_D ();
	float Comp_Volume ();	    	 
	
};									

	void Box::Get_W()	 	 	 	 //member functions
	{
		cout << "Enter the length of side 1: ";
		fflush(stdin);
		cin >> side1;
		cout << endl;
	}
	
	void Box::Get_H()
	{
		cout << "Enter the length of side 2: ";
		fflush(stdin);
		cin >> side2;
		cout << endl;
	}
		
	void Box::Get_D()
	{
		cout << "Enter the length of side 3: ";
		fflush(stdin);
		cin >> side3;
		cout << endl;
	}
		
	float Box::Comp_Volume()
	{
		volume=side1*side2*side3;
		
	}
	
	
int main ()
{
	totVolume();
}

void totVolume(void)
{
	

	int a;
	int i;
	
	float total=0;
	cout << "How many boxes? ";
	cin >> a;
	cout << endl;

	Box *created = new Box[a];			

	for(i=0; i<a; i++);
	{
		created[i].Get_W();
		created[i].Get_H();				
		created[i].Get_D();
		total=total+created[i].Comp_Volume();
	}
	cout << "The total volume of all your boxes is:\n" << total;
	
	delete []created;

}
Last edited on
What do you mean "the console hangs"? Does it crash, or is it just waiting for more input?

I don't see anything blatant or obvious that could be causing the problem (other than those fflushs).
Last edited on
It's a Windows message that says "lab3q4.cpp has stopped working" and gives me an option to quit the app or search online for a solution.

I am using Quincey as a compiler.

I just tried it again (I removed the fflushs) and now it stops working after the first input.

Hmmm.
Last edited on
Comp_Volume does not return a value, although it is required to, resulting in undefined behavior.

[edit: Also, your for loop is equivalent to i=a;. Do you see why?]
Last edited on
cire, you're right. I'm really struggling to wrap my head around which function prototypes/definitions should have passed variables in this program and which inputs are being stored in which object.

I'm ready to pull my hair out! I'm having a hard time wrapping all these concepts together to work properly.

I changed this:

1
2
3
4
5
6
	float Box::Comp_Volume()
	{
		volume=side1*side2*side3;
		return volume;
		
	}


The rest of the code is the same. Since all the variables are part of the same class, nothing needs to be passed among them, right? Just need that volume passed to the non member function.

It's still stopping after the first input. Should I give it a try in another compiler maybe?
See the edit in my previous post.
cire, you called it!

Ran the code in xcode and it gave me a warning on my for loop. Looked closer, yep, I'm a noob.

It fully works now. THANK YOU!
Topic archived. No new replies allowed.