![]() |
|
#1
|
||||
|
||||
How to show a number when it is more than int maxHi,
This is my 7th homework, and I have finish it with no errors. The program should read an integer number and calculates its number of digits. (For example , 123 has 3 digits) However, when I type in a number that is more than the maxmium int value, the program will "collapse". What I want is when I type a number that is more than the int maxmium , it should print "invalid". Here is the code. Thanks -------------------------------------------------------------------------- CPP / C++ / C Code:
__________________
just started to learn C++ :) |
||||
|
#2
|
|||
|
|||
Re: how to show a number when it is more than int maxAn easy way to do this would be to read the input from within a try/catch block. This way, you catch the exception and can print out an error message such as "invalid input".
|
|
#3
|
||||
|
||||
Re: how to show a number when it is more than int maxhow shall i do it?can u show me?
__________________
just started to learn C++ :) |
|
#4
|
||||
|
||||
Re: how to show a number when it is more than int maxI want to do it like
" the number invalid" __________________
just started to learn C++ :) |
|
#5
|
|||
|
|||
Re: how to show a number when it is more than int maxSee this. Basically, you would put the statement that you convert the user input to an integer in a try{} block. Then catch any exception and print to the error to the screen within the catch(){} block.
|
|
#6
|
|||
|
|||
Re: how to show a number when it is more than int maxQuote:
If you read the number into an integer data type and the conversion fails, you have to reset the stream before you can do anything with it. One way might be with a try-catch block, as fakepoo suggested (you clear the error bit and empty out the stream inside the catch block). Or you can simply test the state of cin after the attempted read. If cin is in the "fail" state, you can clear it and empty the input stream before continuing to ask the user for more input. (No exception stuff here.) On the other hand if you want to show the user what the invalid data was, I think the only general way is to read user input into a std::string object and then do the conversion. If the conversion fails, you have the original string to put into the user message. You don't need the try-catch stuff here. One way to convert a "C-Style string" to an integer data type that lets you do some error checking is with the standard library strtol(). (Use the .c_str() member function of the std::string object to get the "C-Style string" corresponding to user input.) Another way that is preferred by many C++ programmers is to use a C++ stringstream object as follows:
Regards, Dave Last edited by davekw7x : 28-Sep-2009 at 15:32.
|
|
#7
|
||||
|
||||
Re: how to show a number when it is more than int maxThx for the reply!
__________________
just started to learn C++ :) |
|
#8
|
||||
|
||||
Re: how to show a number when it is more than int maxQuote:
How about: CPP / C++ / C Code:
Output: Code:
MxB |
|
#9
|
||||
|
||||
Re: How to show a number when it is more than int maxwell, my teacher haven cover this part of program but still thx!
XD~ __________________
just started to learn C++ :) |
Recent GIDBlog
Problems with the Navy (Officers) by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| How to show a 3 digit number with separate digits showing | fomi101 | C++ Forum | 9 | 29-Sep-2007 13:50 |
| String trim problem and wrong node routing agent! | newbie06 | Computer Software Forum - Linux | 8 | 01-Mar-2007 23:51 |
| Knights Tour - Reloaded . | kobi_hikri | C Programming Language | 12 | 03-Oct-2005 12:15 |
| Anyone can write a program code for this??? | chriskan76 | C Programming Language | 1 | 19-Oct-2004 20:25 |
| Apache2 config issues | monev | Apache Web Server Forum | 2 | 28-Jun-2004 06:19 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The