• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: XCode, cin and string (C++) => troubles :s
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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:

<http://www.cplusplus.com/forum/articles/6046/>



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

References: 
 >XCode, cin and string (C++) => troubles :s (From: Arnaud Schoofs <email@hidden>)

  • Prev by Date: coredata in instruments (iPhone)
  • Next by Date: Re: XCode, cin and string (C++) => troubles :s
  • Previous by thread: XCode, cin and string (C++) => troubles :s
  • Next by thread: Re: XCode, cin and string (C++) => troubles :s
  • Index(es):
    • Date
    • Thread