I got Accelerated C++ (is this book outdated, because I swear std::cout and that kind of stuff is horribly outdated?) and I have a huge issue with making the console stay. I made this program:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
#include "stdafx.h"
#include <iostream>
#include <string>
int main()
{
std::cout << "Please enter your first name: ";
std::string name;
std::cin >> name;
std::cout << "Hello, " << name << "!" << std::endl;
return 0;
}
Basically, the console pops up, I write my name, then the console closes down. Right. Is this std::endl that's the problem then? Because this is really annoying. I haven't even got past the first chapter and it already doesn't work.
Otherwise: std::cout is not outdated. Add the following line to your code before the int main(): usingnamespace std.
now you can drop the std:: tags.
If you don't want the console to close you have one of two options:
1. Open the command prompt (I assume you are using windows), navigate to your exe directory and run the program.
2. Use cin.get() immediately before your return 0 line.
2. Use cin.get() immediately before your return 0 line.
Beware of trailing newlines...
@OP:
I'm not sure who told you that std::cout is outdated, but that's not really a true statement. While it may not be used too frequently in programs that use GUIs, it's not outdated.
My biggest problem with std:: is that it tells me to use std::endl;, something which I've never seen before when programming in C++ (yes, I'm fairly new :P) and it seems to replace cin.get();. Do these two fill the same function, and if not, what are the differences?
I've not heard that cin.get() replaces a std::endl; , so I'll tell you what it does (or at least what I use it for)
Say I have a C array like this: char arr[60];
Now, say I want to get a user input on this and fill in the array, I would do this:
std::cin.get(arr, '\n');
Now, you may be confused about what that means, but it is simple: I want the user to cin.getarr until the user types in: '\n' ('\n' is equal to someone pressing enter)
Though, you do not need cin.get() for the project that you have there.