Struct and Class questions.... Plus one kind of class-related question.

1
2
3
4
5
6
struct PersonRec
{
    int age;
    double height;
    int weight;
};


PresonRec Me = (19, 66.5, 140) is an incorrect way to assign values to me. Is this becuase it uses () instead of {}, or will this way not work regardless?

1
2
3
4
5
6
7
8
9
10
11
struct SupplierType
{
   int idNumber;
   string name;
};
struct PartType
{
   string partName;
   SupplierType supplier;
};
PartType partsList[10000];


partList[1].supplier.name[1] is of type Char, not String... correct? And if so, is this becuase the [1] is referring to a single character in the string? (I got this from name being a string, so I feel the [1] does such).

1
2
3
4
5
6
7
8
9
10
class Complex {
   public:
   double re, im;
};
Complex x, y;
x.re = 4.0;
x.im = 5.0;
y = x;
x.re = 5.0;
cout << y.re << endl;


A. 4.0
B. 4
C. 5.0
D. 5
E. None of the above.

Why is this B? I picked A. I don't see any sig-fig related issue that would take 4.0 to 4.... Could someone explain to me why this is 4 not 4.0?
1) Yes you're correct.
2) Yes. operator [] on a string will give you a char.
3) I would have picked A as well...but apparently when using cout doubles with nothing after the decimal point are printed without it (4 in this case, not 4.0).
Thanks!
3) You can control the way cout handles the output using manipulators. Thus if you don't like the default way you can change it according to your flavour.

I wiil give you an example of what I mean (I have used this in another answer):

1
2
3
f = 1.2;
cout << f;
cout << left << setfill('0') << setw(4) << f << "\n";


displays

1.2
1.20


You need to include <iomanip> for this also.

To put it shortly you define that at least 4 characters will be present (setw(4)), set the fill character (the character used when less character are available) to be '0' (setfill('0')) and set the place where to insert these characters (left). So you have some of the functionality of the old printf supported

see
http://www.cplusplus.com/reference/iostream/manipulators/setw/
http://www.cplusplus.com/reference/iostream/manipulators/setfill/
http://www.cplusplus.com/reference/iostream/manipulators/left/
for more information over this
Topic archived. No new replies allowed.