RE: Newbie Questions

So I am back, learning to write C+ 25 years after I said I hated coding..but that's what life does..

So right off the bat, I get book that's hard to read as a textbook and a compiler that doesn't work with the example programs...

The compiler I am required to use is Dev-C++ 4.9.9.2

The assignment is to get the name, address, city, state, zip, phone and major and then output it neatly in standard format using only one cout function.

As of 4:00CST on 8-29 I have the following, which doesn't work...It's a discouraging start to a (for me) difficult class.

// Input and then Display Personal Information program
#include <iostream>
#include <string>


{
int main()

string name;
string address;
string city;
string state;
string zip;
string phone;
string major;


std::cout << "What is your name? ";
getline (std::cin, name);

std::cout << "What is your street address? (number and street only please)";
getline (std::cin, address);

std::cout << "What is your city?";
getline (std::cin, city);

std::cout << "What is your state? (two-letter abbreivation please)";
getline (std::cin, state);

std::cout << "What is your ZIP code?";
getline (std::cin, zip);

std::cout<< "Just a bit more information please...";

std::cout << "What is your nine-digit phone number?";
getline (std::cin, phone);

std::cout << "What is your major?";
getline (std::cin, major);

std::cout<< "Thank you very much, your information is displayed below!";


std::cout << name << endl;
std::cout << address << endl;
std::cout << city << endl;
std::cout << phone << endl;
std::cout << "Your major is: " << major << endl;

return 0;
}
Hey and welcome. Please edit your post and use code tags for your code, it makes it much easier to read - http://www.cplusplus.com/articles/jEywvCM9/

I ran this, it works perfectly fine for me, Im using VS13. The only "problem" is that the formatting needs a little bit fixing when displaying all the info.

What exactly is your problem here? please explain it, are you getting errors? if so, copy and paste them here.
Hey @texagg!
Since you aren't using using namespace std; you have to put std:: in front of cout, cin, AND string. Like this:

std::string name;

But when you're using those strings like with cout then you don't have to put std:: and can do this:

std::cout << name;

Cout, cin, string are all in the std namespace. And so is endl
that means you put std:: in front of endl too like std::endl

Also your starting bracket { goes under the main() function. And you might want to add cin.ignore(); right before return 0; so it pauses the program which allows you to read the output. Unless your using an IDE that automatically pauses for you.

Your welcome!

P.S. Use cplusplus.com tutorials, learncpp.com, tutorialspoint.com while learning. It'll help.
Last edited on
@gentleguy
I meant it as a light joke.
Side note: They aren't grown up if they can't handle a little teasing.

It sucks they have him use Dev-C++ though.
Last edited on
Ok so with you guys advice, I got the following working..but I still need to figure out the single cout requirement.

// Input and then Display Personal Information program
#include <iostream>
#include <string>

using std::cin;
using std::cout;
using std::endl;
using std::string;





int main()
{

string name;
string address;
string city;
string state;
string zip;
string phone;
string major;


cout << "What is your name? ";
getline (cin, name);

cout << "What is your street address? (number and street only please) ";
getline (cin, address);

cout << "What is your city? ";
getline (cin, city);

cout << "What is your state? (two-letter abbreivation please) ";
getline (cin, state);

cout << "What is your ZIP code? ";
getline (cin, zip);

cout << "Just a bit more information please..." <<endl;

cout << "What is your nine-digit phone number? ";
getline (cin, phone);

cout << "What is your major? ";
getline (cin, major);

cout << "Thank you very much, your information is displayed below!" << endl;

cout << name << endl;
cout << address << endl;
cout << city << endl;
cout << phone << endl;
cout << "Your major is: " << major << endl;

cin.ignore();

return 0;
}
@texagg
Close but cin, cout, endl, string is all in ONE namespace called std. So only using namespace std; instead of
1
2
3
4
using std::cin;
using std::cout;
using std::endl;
using std::string;

is needed.
And the syntax for using namespaces is using namespace <name of namespace>
Then you can use cin, cout, string, endl without std:: in front. But it's a good idea to not use using namespace std; and just put std:: in front of everything that uses the std namespace to avoid name collisions.

Your code edited:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
// Input and then Display Personal Information program
#include <iostream>
#include <string>

int main()
{
std::string name;
std::string address;
std::string city;
std::string state;
std::string zip;
std::string phone;
std::string major;


std::cout << "What is your name? ";
getline (std::cin, name);

std::cout << "What is your street address? (number and street only please)";
getline (std::cin, address);

std::cout << "What is your city?";
getline (std::cin, city);

std::cout << "What is your state? (two-letter abbreivation please)";
getline (std::cin, state); 

std::cout << "What is your ZIP code?";
getline (std::cin, zip); 

std::cout<< "Just a bit more information please...";

std::cout << "What is your nine-digit phone number?";
getline (std::cin, phone);

std::cout << "What is your major?";
getline (std::cin, major);

std::cout<< "Thank you very much, your information is displayed below!"; 


std::cout << name << std::endl;
std::cout << address << std::endl;
std::cout << city << std::endl;
std::cout << phone << std::endl;
std::cout << "Your major is: " << major << std::endl;

return 0;
}

You can also take out all of the "std::"s and put "using namespace std;" after the #includes. And an alternative to endl is to use the newline character "\n".
So instead of std::cout << "Hello World" << std::endl; you can do std::cout << "Hello World\n"; or std::cout << "Hello World" << '\n';

Tip: When you post, on the right side under "Format:" there are options. The first "<>" one will create these // Code in here and you put your code between to make it look like how I post code here.
Last edited on
I still need to figure out the single cout requirement.

That one's easy, you can chain the << operators together like this:
1
2
cout << name << endl << address << endl << city << endl
        << phone << endl << "Your major is: " << major << endl;

Note that this is one statement, even though it appears on multiple lines.

But it's a good idea to not use using namespace std; and just put std:: in front of everything that uses the std namespace

I prefer what you have: adding a using statement for each symbol in std that you're going to use. That way you don't incur the tedium of adding std:: all over the place.
an alternative to endl is to use the newline character "\n".

You should prefer '\n' to endl. The difference is that endl flushes the stream (pushes the data out to the hardware) and that can take a long time. Use one endl when writing a prompt to the screen, just before you try to read input, but almost everywhere else it's better to use '\n'.

You won't notice the difference between endl and '\n' if you're just printing a few lines of text, but if you print 100,000 lines with endl, it will take forever.
Thanks, I got it!

That one's easy, you can chain the << operators together like this:


I am still looking for where in the book it tells you this. This should be conveyed to the reader immediately, not buried somewhere. Either that, or I am just blind.
Last edited on
It looks like I wasn't much help. :(

@dhayden
Use one endl when writing a prompt to the screen, just before you try to read input, but almost everywhere else it's better to use '\n'.


When writing a prompt do you mean cin or cout? And do you use endl to make a newline AFTER or BEFORE you do this?
I mean like this:
1
2
3
int num;
cout << "Please enter a number." << endl;
cin >> num;

Topic archived. No new replies allowed.