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>
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 usingnamespace 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.
@texagg
Close but cin, cout, endl, string is all in ONE namespace called std. So only usingnamespace 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 usingnamespace <name of namespace>
Then you can use cin, cout, string, endl without std:: in front. But it's a good idea to not use usingnamespace std; and just put std:: in front of everything that uses the std namespace to avoid name collisions.
// 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.
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 usingstatement 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.
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.