Structures, will take a minute to answer!

This is a discussion on Structures, will take a minute to answer! within the C++ Programming forums, part of the General Programming Boards category; I have a problem with one of my data pieces within my structure. Here, I have my structure:
Code:
struct ...

1.) When I output 'account.expected_graduation_year,' I get a very square white box. I would assume this has to do with a casting problem, but I do not really know how to fix this and I do not know how to utilize streamstring like cyberfish said. Is there a way besides streamstring that just uses strings?

2.) When the user is inputting data (in this case, ME), I can enter in the name, number, and the year admitted, but then the program displays "Semester admitted to school" but doesn't let me input anything and goes straight to major...why is this?!

What's happening is, assigning an int to a string uses string's overloaded assignment operator for char. Meaning the int is converted to a char (remember that chars are just 8-bit ints that get converted to their ASCII (if your computer uses ASCII) symbol when printed).

That obviously won't make sense in this case.

You need some way to convert an int to a string, using either atoi (C way) or stringstream (C++ way).

there was no error message. when i ran the program, it just gave an invalid character and continued through the program. thank you though.

std::string has an overloaded assignment operator that has a char parameter. As such, your int was converted to a char, and this assignment operator was invoked, thus leading to a bug.

Originally Posted by porsche911nfs

1.) When I output 'account.expected_graduation_year,' I get a very square white box. I would assume this has to do with a casting problem, but I do not really know how to fix this and I do not know how to utilize streamstring like cyberfish said. Is there a way besides streamstring that just uses strings?

A solution is to #include <sstream> and use a stringstream for conversion, e.g.,

2.) When the user is inputting data (in this case, ME), I can enter in the name, number, and the year admitted, but then the program displays "Semester admitted to school" but doesn't let me input anything and goes straight to major...why is this?!

The problem is that formatted input with operator>> leaves trailing whitespace in the input stream buffer. As such, you should ignore the whitespace, either by using:

Code:

cin.ignore(1000, '\n');

where 1000 is selected arbitrarily, or by changing to:

Code:

cin >> account.admission_year >> ws;

where std::ws is an input stream manipulator that reads and discards consecutive whitespace. (If you are not familiar with manipulators, recall your own use of the std::endl manipulator.)