GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 23-May-2004, 20:07
aaroncohn's Avatar
aaroncohn aaroncohn is offline
Regular Member
 
Join Date: Feb 2004
Location: Bay Area, CA.
Posts: 564
aaroncohn is a jewel in the roughaaroncohn is a jewel in the roughaaroncohn is a jewel in the rough

Limiting Input


Hey, 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:
cin.getline( tempPhrase, MAX_LENGTH-1, '\n' );
That's how I was using it before... I thought the int in the middle was a delimiter on how much input to take, but apparently it isn't, because if I input more than MAX_LENGTH-1, the program crashes (because the program continues writing data outside of the array). So, anyone have a function or a quick algorithm to limit the amount of characters I receive?
__________________
-Aaron
  #2  
Old 23-May-2004, 20:45
Max Payne's Avatar
Max Payne Max Payne is offline
Regular Member
 
Join Date: Apr 2004
Location: 3° 08 North 101° 42 East
Posts: 332
Max Payne is a jewel in the roughMax Payne is a jewel in the roughMax Payne is a jewel in the rough
You can try using the setw() manipulator....
CPP / C++ / C Code:
#include <iostream>
#include <string>
#include <iomanip>

int main()
{
    char foo[5];
    std::cout << "Enter some text :\n";

    std::cin >> std::setw(5) >> foo;

    return 0;
}
__________________
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  
Old 23-May-2004, 22:51
dabigmooish's Avatar
dabigmooish dabigmooish is offline
Member
 
Join Date: May 2004
Location: Baltimore (middle of Canton)
Posts: 165
dabigmooish will become famous soon enough
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  
Old 24-May-2004, 11:25
aaroncohn's Avatar
aaroncohn aaroncohn is offline
Regular Member
 
Join Date: Feb 2004
Location: Bay Area, CA.
Posts: 564
aaroncohn is a jewel in the roughaaroncohn is a jewel in the roughaaroncohn is a jewel in the rough
Quote:
Originally Posted by Max Payne
You can try using the setw() manipulator....
Didn't work... entering too many characters still crashes the program.
__________________
-Aaron
  #5  
Old 24-May-2004, 14:46
aaroncohn's Avatar
aaroncohn aaroncohn is offline
Regular Member
 
Join Date: Feb 2004
Location: Bay Area, CA.
Posts: 564
aaroncohn is a jewel in the roughaaroncohn is a jewel in the roughaaroncohn is a jewel in the rough
Quote:
Originally Posted by dabigmooish
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.
This didn't work either... I tried doing a loop where the user types out the phrase to be entered, then the loop reads it one character at a time from the keyboard buffer and the loop stops if it reaches MAX_LENGTH... but couldn't stop the loop if there was no more input. I have an idea now that I'm going to try. If anyone comes up with something... let me know!
__________________
-Aaron
  #6  
Old 24-May-2004, 15:15
aaroncohn's Avatar
aaroncohn aaroncohn is offline
Regular Member
 
Join Date: Feb 2004
Location: Bay Area, CA.
Posts: 564
aaroncohn is a jewel in the roughaaroncohn is a jewel in the roughaaroncohn is a jewel in the rough
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:
while ( (ch = cin.get()) != '\n' && index != MAX_LENGTH-1)
  {
    tempPhrase[index] = ch;
    index++;
  }
__________________
-Aaron
  #7  
Old 24-May-2004, 19:19
Max Payne's Avatar
Max Payne Max Payne is offline
Regular Member
 
Join Date: Apr 2004
Location: 3° 08 North 101° 42 East
Posts: 332
Max Payne is a jewel in the roughMax Payne is a jewel in the roughMax Payne is a jewel in the rough
Quote:
Originally Posted by aaroncohn
Didn't work... entering too many characters still crashes the program.

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  
Old 24-May-2004, 19:23
Max Payne's Avatar
Max Payne Max Payne is offline
Regular Member
 
Join Date: Apr 2004
Location: 3° 08 North 101° 42 East
Posts: 332
Max Payne is a jewel in the roughMax Payne is a jewel in the roughMax Payne is a jewel in the rough
Quote:
Originally Posted by aaroncohn
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:
while ( (ch = cin.get()) != '\n' && index != MAX_LENGTH-1)
  {
    tempPhrase[index] = ch;
    index++;
  }

find out when it crashes, put a cout in your code, which statement causing the crash??


CPP / C++ / C Code:
cout<<"check 1"<<endl;
while ( (ch = cin.get()) != '\n' && index != MAX_LENGTH-1)
  {
cout<<"check 2"<<endl;
    tempPhrase[index] = ch;
cout<<"check 3"<<endl;
    index++;
  }
__________________
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  
Old 25-May-2004, 01:42
aaroncohn's Avatar
aaroncohn aaroncohn is offline
Regular Member
 
Join Date: Feb 2004
Location: Bay Area, CA.
Posts: 564
aaroncohn is a jewel in the roughaaroncohn is a jewel in the roughaaroncohn is a jewel in the rough
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 GIDBlogHalfway done! by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

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

All times are GMT -6. The time now is 15:42.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.