Program does not take in the value the user input

hydra89 (3)
The program is not taking in the value the user input for 1 of the attribute.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#ifndef SQUARE_H
#define SQUARE_H

using namespace std;

class square : virtual public shapeTwoD
{
	private:
	int x[3];
	int y[3];

	public:

	square (string, bool, int, int, int, int, int, int, int, int);
	string toString();
}
#endif 

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
#include "square.h"

square::square (string shapeName, bool shapeContainsWarpSpace, int newX1, int newX2, int newX3, int newX4, int newY1, int newY2, int newY3, int newY4):shapeTwoD(shapeName,shapeContainsWarpSpace)
{
	x[0] = newX1;
	x[1] = newX2;
	x[2] = newX3;
	x[3] = newX4;
	y[0] = newY1;
	y[1] = newY2;
	y[2] = newY3;
	y[3] = newY4;	
	
}

string square::toString()
{
	stringstream oss;
	string toString;

	oss << "name = "<<shapeTwoD::getName()<<"\ncontainsWarpSpace = "<<shapeTwoD::getContainsWarpSpace()<<"\nx1 = "<<x[0]<<"\nx2 = "<<x[1]<<"\nx3 = "<<x[2]<<"\nx4 = "<<x[3]<<"\ny1 = "<<y[0]<<"\ny2 = "<<y[1]<<"\ny3 = "<<y[2]<<"\ny4 = "<<y[3]<<endl;
	toString=oss.str();
	return toString;
}



int main()
{
	square s("square",false,1,2,3,4,1,2,3,4);
	cout<<"\nx4 = "<<s.getX4()<<endl<<endl;
	cout<<s.toString()<<endl;
	return 0;
};


However, my program takes in x4 with the values of y1.

name = square
containsWarpSpace = 0
x1 = 1
x2 = 2
x3 = 3
x4 = 1
y1 = 1
y2 = 2
y3 = 3
y4 = 4


If I change the value of y1 to another value, x4 is changed automatically.
May I know why is wrong with my codes?
Last edited on
pogrady (411)
Can we see your output code?
hydra89 (3)
Thank you for the reply.

This is the output code for

square s("square",false,1,2,3,4,1,2,3,4);

name = square
containsWarpSpace = 0
x1 = 1
x2 = 2
x3 = 3
x4 = 1
y1 = 1
y2 = 2
y3 = 3
y4 = 4


Everything works perfectly when i change the array size to x[4] but i cannot get the reason why.
nixer526 (17)
The problem is with this
1
2
int x[3];
int y[3];


Although computers read from 0, but when declaring, it doesn't start from 0.

What I mean is that arrays are like x[0] ~ x[3], but when declaring int x[4] because there will be 4 memory being used.
hydra89 (3)
Okay! I get it now.

Thanks nixer526 for the explaination :)
Registered users can post here. Sign in or register to post.