4
\$\begingroup\$

I've just learned the basics of c-strings and how to read line oriented input. I noticed that fail bits are set for each function. However, not every problem sends a fail bit, so I thought this would be a good exercise to try and target two user input errors:

  1. The user enters no input
  2. The user enters more input than the allocated number of elements

/*Simple Error handle using get() and getline() *with too much input or no input on c-style strings */ #include <iostream> #include <string> #include <limits> int main() { const int size = 10; std::cout << "Please enter your name and hit enter. Then have your friend do the same\n\n"; //Using get() std::cout << "Your name: "; char name1[size]; while (!std::cin.get(name1, size) || std::cin.get() != '\n') { if (!std::cin) { std::cout << "No input\n"; std::cin.clear(); std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); } else { std::cout << "Name entered was too large for allocated memory...\n"; } std::cout << "Try again: "; } //using getline() std::cout << "Your friends name: "; char name2[size]; while (!std::cin.getline(name2, size) || name2[0] == NULL) { if(name2[0] == NULL){ std::cout << "No input\n"; } else { std::cout << "Name entered was too large for allocated memory...\n"; std::cin.clear(); std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); } std::cout << "Try Again: "; } std::cout << "\nNAME1: " << name1 << " NAME2: " << name2 << "\n"; std::cin.get(); return 0; } 
\$\endgroup\$
4
  • 3
    \$\begingroup\$C style strings and istream don't go well with each other. I'm afraid you probably spent your time for nothing. C style char arrays are effective in solving some problems, but getting user input is the worst case scenario for them.\$\endgroup\$CommentedAug 16, 2017 at 3:08
  • \$\begingroup\$Yea, I agree, input into objects of type string is incredibly smooth and generally more ideal, c-strings lack the simplicity string objects do. However this was simply an exercise to do just a little bit of error handling on the c-string input!\$\endgroup\$CommentedAug 16, 2017 at 3:39
  • \$\begingroup\$@Incomputable Thank you though for pointing that out in the case I didn't know it!\$\endgroup\$CommentedAug 16, 2017 at 3:41
  • \$\begingroup\$You're welcome. iostreams are generally rarely used in C++. Usually people read from sockets, and libraries have dedicated API for reading only certain amount of information from it. I believe that writing good generic data structures and writing algorithms (generic or not) would be the best route of learning C++. Those probably cover most parts of C++ as well, at least most used ones.\$\endgroup\$CommentedAug 16, 2017 at 3:44

1 Answer 1

2
\$\begingroup\$

Writing all the code in a single huge main() is not only hard to read, but also a poor use of your skills. Write a re-usable function to prompt and read input from a supplied std::istream&. That will make it easier to test using a std::istringstream, too.

name2[0] == NULL looks like a pointer comparison. You probably want to compare against '\0' so that it's clear we're dealing with characters.

Both loops cycle indefinitely when we reach end of input stream. We probably should abort if std::cin.eof().

The final std::cin.get() seems pointless, as we just discard the result.

return 0; can be omitted - just reaching the end of main() is equivalent.

\$\endgroup\$

    Start asking to get answers

    Find the answer to your question by asking.

    Ask question

    Explore related questions

    See similar questions with these tags.