Progamming Principles and Practice Using C++ Book and Question

I am currently using the book, Programming Principles and Practice Using C++ and it is great. It is easy for me to understand, and his real world examples and knowledge are thorough and I would reccomend it to anybody beginning. I have a question about the code he puts. The code he uses doesn't go too well with g++ in Linux. For example, he puts this in the book for us to put in the compiler:

int main()
{
cout << "Please enter your first name and age\n";
string first_name;
int age;
cin >> first_name;
cin >> age;
cout << "Hello, " << first_name << " (age " << age << ")\n";
}

Now, I had to edit that code, because I had to put stb:: in front of string, cout, and cin. I got the program to run, but it is coming out all wrong. I would put Rehan and 26 for my name and age. It would come back, Hello,Rehanage26...I was looking around the web, and I was reading about "getline" and I really do not understand it. I wonder why the book does not prepare me for this. Any help would be greatly appreciated.

Hey man, glad you're enjoying it.

In the future, try and use the code tags. Not just because it makes it easier to read, but also we can run it in Shell here, which is quite helpful! [ code ] [/ code ] (no spaces)

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
using namespace std;

int main()
{
     cout << "Please enter your first name and age\n";
     string first_name;
     int age;
     cin >> first_name;
     cin >> age;
     cout << "Hello, " << first_name << " (age " << age << ")\n";
     return 0;
}


Just glancing over it, it SHOULD work, unless I'm missing something, which is entirely possible. It's possibly a g++ issue.

EDIT: Ran it in Shell, and I got:

1
2
3
4
5
Please enter your first name and age
Rehan 26
Hello, Rehan (age 26)
 
Exit code: 0 (normal program termination)


I was reading about "getline" and I really do not understand it.


getline() doesn't really apply here. Stroustrup actually does discuss that soon after (if I recall correctly), but getline() reads until it receives a newline and ignores other whitespace, whereas string reads until whitespace. In this case, you're reading a string and then an int directly after, so this:

Rehan 26

Will read Rehan into the string, and 26 into the int, because after the 'n' is preceded by a space. Does that make sense? For getline(), if you wanted, you could do something like this:

1
2
3
4
5
6
7
8
9
10
#include <iostream>
using namespace std;
int main()
{
     cout << "Enter your name: ";
     string name;
     getline(cin, name);
     cout << "Hello, " << name << "!\n";
     return 0;
}


And you could type in Rehan Anderson Smith, the Greatest Ever and the output would be:

 
Hello, Rehan Anderson Smith, the Greatest Ever!" 
Last edited on
Oh man. Thank you so much. I am going to change compilers and keep on studying! :D
If I recall correctly he used his header "std_lib_facilities.h" and you probably didnt copy that from his website and include it in your program.
http://www.stroustrup.com/Programming/PPP2code/std_lib_facilities.h

There he includes a lot of headers and also is using using namespace std; Thats why in his code he doesnt have to use std::

So its not a compilers fault but its just that next time you have to use
1
2
3
4
5
6
7
8
9
10
11
#include "std_lib_facilities.h"

int main()
{
    cout << "Please enter your first name and age\n";
    string first_name; 
    int age; 
    cin >> first_name;
    cin >> age; 
    cout << "Hello, " << first_name << " (age " << age << ")\n";
}


By the way std_lib_facilities.h file has to be in the same directory as you main.cpp file or whatever you named yours
Last edited on
Topic archived. No new replies allowed.