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. #1
    Registered User
    Join Date
    Apr 2009
    Location
    ...creepy
    Posts
    75

    Structures, will take a minute to answer!

    I have a problem with one of my data pieces within my structure. Here, I have my structure:

    Code:
    struct StudentAccount
    {
      string name;
      string major;
      int admission_year;
      int number;
      string admission_semester;
      string expected_graduation_year;
      string expected_graduation_month;
    };
    and here I have my declaration of a variable:

    Code:
    account.expected_graduation_year = (account.admission_year + 4);
    so why does this give me an invalid answer when I run my program? Thanks for the help in advance!

  2. #2
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,183
    You can't assign an int to a string. One option is to use stringstream.

    Post the error message next time.

  3. #3
    Registered User
    Join Date
    Apr 2009
    Location
    ...creepy
    Posts
    75
    there was no error message. when i ran the program, it just gave an invalid character and continued through the program. thank you though.

  4. #4
    Registered User
    Join Date
    Jul 2004
    Posts
    222
    You may want to convert the admission_year member from a string (or stringstream) to an int or float by using atoi or atof. Just a suggestion.

  5. #5
    Registered User
    Join Date
    Apr 2009
    Location
    ...creepy
    Posts
    75
    well now i have two problems:

    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?!

    Code:
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    struct StudentAccount
    {
      string name;
      string major;
      int admission_year;
      int number;
      string admission_semester;
      string expected_graduation_year;
      string expected_graduation_month;
    };
    
    int main()
    {
       StudentAccount account; //Accessing the member variables
       
       cout << "Please enter the following information: " << endl;
       cout << "Student Name: ";
       getline(cin, account.name); //Receives the entire line
       cout << "Student number: ";
       cin >> account.number;
       cout << "Year admitted to school: ";
       cin >> account.admission_year;
       cout << "Semester admitted to school: ";
       getline(cin, account.admission_semester);
       cout << "Major: ";
       getline(cin, account.major);
       
       account.expected_graduation_year = (account.admission_year + 4);
       if (account.admission_semester == "Fall")
           account.expected_graduation_month == "May";
       else
           account.expected_graduation_month == "December"; 
       
       cout << "Your personal information: " << endl;
       cout << "Student Name: " << account.name << endl;
       cout << "Student Number: " << account.number << endl;
       cout << "Major: " << account.major << endl;
       cout << "Expected Graduation Year: " << account.expected_graduation_year;
       cout << "Expected Graduation Month: " << account.expected_graduation_month;
    }

  6. #6
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,183
    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).

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,734
    Quote Originally Posted by porsche911nfs
    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.

    Quote 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.,
    Code:
    std::stringstream ss;
    ss << (account.admission_year + 4);
    account.expected_graduation_year = ss.str();
    Quote Originally Posted by porsche911nfs
    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.)
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Registered User
    Join Date
    Apr 2009
    Location
    ...creepy
    Posts
    75
    thanks for the help, i got to run as expected.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Looking for constructive criticism
    By wd_kendrick in forum C Programming
    Replies: 16
    Last Post: 05-28-2008, 09:42 AM
  2. Filling an Array of Structures
    By Zildjian in forum C Programming
    Replies: 5
    Last Post: 11-12-2003, 04:54 PM
  3. Structures? *brain melts*
    By MrDoomMaster in forum C++ Programming
    Replies: 119
    Last Post: 11-01-2003, 11:31 PM
  4. pointers to arrays of structures
    By terryrmcgowan in forum C Programming
    Replies: 1
    Last Post: 06-25-2003, 09:04 AM
  5. Replies: 22
    Last Post: 11-08-2001, 10:01 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21