Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

C++ query

Name: Anonymous 2010-05-06 13:08

  
    int listsize, count;
    cout<<"Define size of list: ";
    cin>>listsize;
    cout<<endl;
    int list[listsize];


Are there any problems with this? It works fine but I just want to make sure I'm not committing a faux pas.

Name: Anonymous 2010-05-07 7:25

>>19

Have you read the references I left in my post?1
Let me point you to the right direction, a simple C++ FAQ:
http://faqs.cs.uu.nl/na-dir/C++-faq/part07.html
Go and read section 15.2.
I quote:


 #include <iostream>

 int main()
 {
   std::cout << "Enter numbers separated by whitespace (use -1 to quit): ";
   int i = 0;
   while (i != -1) {
     std::cin >> i;        // BAD FORM -- See comments below
     std::cout << "You entered " << i << '\n';
   }
 }


The problem with this code is that it lacks any checking to see if someone
entered an invalid input character.
In particular, if someone enters something that doesn't look like an integer (such as an 'x'), the stream std::cin goes into a "failed state,"
and all subsequent input attempts return immediately
without doing anything.
In other words, the program enters an infinite loop;
if 42 was the last number that was successfully read, the program will print
the message You entered 42 over and over.


And that's why it's bad to do:2

cin >> MyInt;

_________________________________________________________________
1This right here?
2It will also rape puppies, and your anus but honestly who cares about your anus.

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List