Thread: Question about multi-word strings

  1. #1
    Registered User
    Join Date
    Dec 2011
    Posts
    34

    Question about multi-word strings

    Hey I am learning C++ from the book "C++ Primer Plus" and I am on an exercise that has me write a program to write a program to ask from a persons full name(has to be able to handle names with 2 words), age, and what grade they think they earned. The program then will print out the name, age, and the grade that is one letter lower. My entire code is this.
    Code:
    #include <iostream>
    #include <string>
    
    
    struct info
    {
    
    
        std::string first;
        std::string last;
        int age;
        char grade;
    };
    
    
    int main()
    {
    
    
        using namespace std;
        info student;
    
    
        cout<< "What is your first name? ";
        getline(cin, student.first);
        cout<< "What is your last name? ";
        getline(cin, student.last);
        cout<< "What letter grade do you deserve(A-C only)? ";
        cin >> student.grade;
        cout<< "What is your age? ";
        cin >> student.age;
    
    
        student.grade = char(int(student.grade) + 1);
    
    
        cout<< "Name: " << student.last << ", " << student.first << endl;
        cout<< "Age: " << student.age << endl;
        cout<< "Grade: " << student.grade << endl;
        return 0;
    }
    What I was wondering is there a "more correct" way to get a multi word string as input besides this?
    Code:
     getline(cin, student.first);
    It is working for what I need to but I just do not want to develop any habits that later I will have to change.

    Also another thing that I was wondering is there an better way to firgure out the final grade is besides this?
    Code:
    student.grade = char(int(student.grade) + 1);
    If there is anything else you notice that is worth mentioning, I would be extremly thankful for anyone who would point it out.

  2. #2
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    getline is the proper way to get a line of input (that may contain spaces).

    Since a char is just a small number, you should be able to just do student.grade++;

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Heck,
    student.grade = student.grade + 1
    or
    student.grade += 1
    should also work.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Registered User
    Join Date
    Dec 2011
    Posts
    34
    Thanks for answering those two questions but I have one more if you have a char variable will always be treated as an interger value if its on the right side of the "="?

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Depends on context, but it is an integer and can be treated as such.

  6. #6
    Registered User
    Join Date
    Dec 2011
    Posts
    34
    ok so basically what I did is handled automatically by the compiler in this case? Think I understand this better now. Thanks.

  7. #7
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    A char is just an integer, usually one byte, possibly signed. It is only treated specially on input and output.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  8. #8
    Registered User
    Join Date
    Dec 2011
    Posts
    34
    Does the compiler also store things like strings as a number or is this just something for characters?

  9. #9
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    A C-style string is an array of chars, i.e. numbers, ending in a zero. The C++ string class also contains an array of chars.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  10. #10
    Registered User
    Join Date
    Dec 2011
    Posts
    34
    Oh ok Thanks for all the help. I have been learning from a book so its nice to be able to ask someone the questions about stuff the book either didnt cover or concepts I just didnt get(like pointers..... grr dont like those).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 28
    Last Post: 10-23-2011, 07:17 PM
  2. grabbing a multi-word query from stdin
    By drshmoo in forum C Programming
    Replies: 3
    Last Post: 04-10-2011, 04:01 PM
  3. Reading Multiple Multi-Part Strings From a Text File
    By bengreenwood in forum C++ Programming
    Replies: 2
    Last Post: 06-02-2009, 10:43 AM
  4. Replies: 3
    Last Post: 07-07-2006, 08:53 PM
  5. extracting word-per-word from strings
    By tygernoot in forum C++ Programming
    Replies: 4
    Last Post: 12-27-2004, 02:39 PM