Re: XCode, cin and string (C++) => troubles :s
Re: XCode, cin and string (C++) => troubles :s
- Subject: Re: XCode, cin and string (C++) => troubles :s
- From: Andreas Grosam <email@hidden>
- Date: Sun, 14 Feb 2010 14:01:23 +0100
On Feb 14, 2010, at 12:38 PM, Arnaud Schoofs wrote: #include <iostream> using namespace std; int main (int argc, char * const argv[]) { string s; cout << "Type something (using string) : "; cin >> s; cout << s << "\n\n"; char* c; cout << "Type something (using char*) : "; cin >> c; cout << c << "\n\n"; return 0; }
Your pointer to char c is not initialized, nor does it point to a reasonable buffer. So, cin writes *somewhere* into memory. That it crashes is normal ;)
Anyway, using a pointer to a char as input buffer isn't a reasonable way to go.
Furthermore, the pattern
std::cin >> some_variable
is frequently causing troubles, when the details about an istream are not fully understood. For instance,
std::string str; std::cin >> str;
is completely inappropriate to read a "string" (that is input) from the console.
Please read this article for further information:
Regards Andreas |
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Xcode-users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden