![]() |
|
#1
|
|||
|
|||
C++ console programming using cbuilder4I have a problem with C++ console programming using cbuilde4.
I input data through overloaded operators of istream and write to a file through ofstream. Its working alright without validations. I have a problem of how to validate inputs of numeric fields and get back to the istream and write to the file. See below a part of code where I have tried this and relavent coding. If any body can help on this thats great. Thanks in advance. CPP / C++ / C Code:
Last edited by LuciWiz : 04-Dec-2005 at 07:21.
Reason: Please insert your C++ code between [c++] & [/c++] tags
|
|||
|
#2
|
||||
|
||||
Re: C++ console programming using cbuilder4Hi fryup,
Welcome to GIDForums. If you can provide your class Details, that would be nice. I cant guess what you are doing now. This: CPP / C++ / C Code:
To validate postalCode, you can use fail, clear and ignore. Like this: CPP / C++ / C Code:
Else, it will break the loop. Regards, Paramesh. __________________
Don't walk in front of me, I may not follow. Don't walk behind me, I may not lead. Just walk beside me and be my friend. |
|
#3
|
|||
|
|||
Re: C++ console programming using cbuilder4Hi Paramesh,
Thanks for your advice. I tried your coding. stream.fail() works, as clear() and ignor() not working it gives an endless loop. Anyway I feel you can help me. Have to find a way to go back to the original cursor position to input postal code. I will send the class below. If you need futher details I am ready to provide. Thanks CPP / C++ / C Code:
Last edited by LuciWiz : 04-Dec-2005 at 07:22.
Reason: Please insert your C++ code between [c++] & [/c++] tags
|
|
#4
|
||||
|
||||
Re: C++ console programming using cbuilder4Hi,
clear() and ignore didnt give me any error, nor any endless loop. But it has a disadvantage: If the input is "abc", it works. But if the input is "123adf", it treats 123 as postal code and adf as city. Do you understand? So, lets forget clear, ignore and all that things. Lets create our own validation code. First, we'll create a string, as you had earlier. say: CPP / C++ / C Code:
The former is not initialised, and just a pointer, whereas the latter is initialized to 30 characters. Next, we'll have a status variable, say : CPP / C++ / C Code:
Initially, the status is false, indicating that there is no error in input. Next, we'll have a do-while loop, to get the input again if it fails. So, here is the format: CPP / C++ / C Code:
Then, inside the do while, loop we are getting the input by using getline function, like this: CPP / C++ / C Code:
We should now check whether all the characters in the string are numbers. So, in a loop, if inputString[i] is less than '0' or if inputString[i] is greater than '1', put the status as true, and break the inner loop. So, this will take care of invalid input. But how to convert the string to a long? we can use the atol function, which is readily available. Like this: CPP / C++ / C Code:
So, here is the prototype: CPP / C++ / C Code:
Ah! I forgot to tell you one thing: You should not just use the class variables like this, inside the function: CPP / C++ / C Code:
CPP / C++ / C Code:
And, when you are including iostream, there is no need to include istream and ostream separately. iostream is the combination of istream and ostream. And, instead of using another method to get the input, like this: CPP / C++ / C Code:
There is no need for the inputData function! Just remove it. BTW, which compiler are you using? Regards, Paramesh. __________________
Don't walk in front of me, I may not follow. Don't walk behind me, I may not lead. Just walk beside me and be my friend. |
|
#5
|
|||
|
|||
Re: C++ console programming using cbuilder4Hi dear,
Thats great your explanations. I could do the problem well. I appreciate your comments regarding my codings. Thank you so much. |
Recent GIDBlog
Toyota - 2009 May Promotion by Nihal
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Read/ Write EXCEL files using C/C++ programming | confused_pig | C++ Forum | 4 | 25-Nov-2005 01:27 |
| Bloodshed Dev C++ Project Options | JdS | C++ Forum | 6 | 11-Nov-2005 18:23 |
| [Tutorial] GUI programming with FLTK | dsmith | FLTK Forum | 10 | 03-Oct-2005 16:41 |
| Network programming | pointer | C++ Forum | 2 | 20-May-2005 15:36 |
| GUI programming | crystalattice | C++ Forum | 5 | 14-Sep-2004 13:17 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The