![]() |
|
#1
|
||||
|
||||
Limiting InputHey, this may seem like a silly question to you all, but I guess my head isn't screwed on as tight as it should be right now, because it seems easy enough to me, yet I can't quite fix it. I want to limit user input into an array of chars to a maximum of 100 characters. Right now I'm using cin.getline() for input, but It doesn't work the way I thought it did.
CPP / C++ / C Code:
__________________
-Aaron |
|
#2
|
||||
|
||||
|
You can try using the setw() manipulator....
CPP / C++ / C Code:
__________________
When you say "I wrote a program that crashed Windows," people just stare at you blankly and say "Hey, I got those with the system, for free." Linus Torvalds |
|
#3
|
||||
|
||||
|
have you tried it with out the character delim "/n"? Thats the only thing I can think of because getline should work the way you have it written.
|
|
#4
|
||||
|
||||
|
Quote:
__________________
-Aaron |
|
#5
|
||||
|
||||
|
Quote:
__________________
-Aaron |
|
#6
|
||||
|
||||
|
Ok, I'm a little peeved now. This loop SHOULD work the way it was intended to work, yet the program STILL crashes when too many characters are received.
CPP / C++ / C Code:
__________________
-Aaron |
|
#7
|
||||
|
||||
|
Quote:
Weird, I tried using getline() and cin>>setw(), both works fine with me!! __________________
When you say "I wrote a program that crashed Windows," people just stare at you blankly and say "Hey, I got those with the system, for free." Linus Torvalds |
|
#8
|
||||
|
||||
|
Quote:
find out when it crashes, put a cout in your code, which statement causing the crash?? CPP / C++ / C Code:
__________________
When you say "I wrote a program that crashed Windows," people just stare at you blankly and say "Hey, I got those with the system, for free." Linus Torvalds |
|
#9
|
||||
|
||||
|
Hah, the problem wasn't even with the stupid input. It was with displaying it. When I display the input, I move it from the list to a new array of chars of size MAX_LENGTH, but I add a space after each character, effectively doubling the amount of space the string takes up. So, when I tried to add a space after each character, it would run out of room and write outside of the array causing an access violation. All I did to fix it was instead of using MAX_LENGTH for the size of the char array, I used MAX_LENGTH*2 : ) Thanks for your input, guys. I guess the limiting worked all along. Now I don't have to use that stupid loop.
__________________
-Aaron |
Recent GIDBlog
Halfway done! by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| scanf / clearing input stream | zone4 | C Programming Language | 8 | 08-May-2004 14:42 |
| IP tables | rogermark100 | C Programming Language | 6 | 18-Apr-2004 07:22 |
| Need Help on checking user input | hihellochao | C Programming Language | 5 | 27-Feb-2004 13:30 |
| a C input question | tmike | C Programming Language | 1 | 16-Sep-2003 02:31 |
| Script needed for letting user input a few days of data for tracking and analysis. | tradertt | MySQL / PHP Forum | 3 | 06-Mar-2003 02:54 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The