Hallo everyone, I have some problems with my assignment. I asked to provide some error-handling in my program. What I want to ask abou how we handle an input that isn't its type. For example i define a variable int a but we "accidentally" input a string or other type. How we can handle that error type ? Thank you before
akira_shinizaki 0 Newbie Poster
Recommended Answers
Jump to Post— ArkM 1,090You must detect bad input condition then handle it. If possible, make recovery. For example:
int x; cout << "Type integer: "; if (cin >> x) { cout << "Thank you..." << endl; } else if (cin.eof()) { // end of stream cout << "cin closed." << …
Jump to Post— Lerner 582One of the other approaches is to never accept input as a numerical type. Only accept input as a string. Then parse the string after input for it's validity as a numeric value and convert the string to the numerical type as desired if it's valid.
All 8 Replies
ArkM 1,090 Postaholic
Lerner 582 Nearly a Posting Maven
akira_shinizaki 0 Newbie Poster
ArkM 1,090 Postaholic
Lerner 582 Nearly a Posting Maven
akira_shinizaki 0 Newbie Poster
Lerner 582 Nearly a Posting Maven
ArkM 1,090 Postaholic
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.