PDA

View Full Version : input overflow crash


Earthsoup
08-07-2003, 12:24 PM
I'm currently teaching myself C++ w/ the Wrox tutorial, that comes w/ the Microsoft Visual Basic 6 package. It uses either "cin" or "cin.getline" for input from the keyboard.
I've found that neither limit the amount of input from the keyboard. In programs I've created to accept input (run in a '98 console window), values which are too large cause a crash. If I tell getline to only read - say, 3 chars - it still accepts up to 255, and crashes if there are too many. I "solved" this problem by telling getline to accept 255 chars.
My question is this - how do I limit the number of keystrokes? Getline does not appear to be working in the way that the Wrox tutorial outlined. I would like it if I could keep input values within a defined field. :o

DNAunion2000
08-07-2003, 07:56 PM
/*DNAunion/* Can you post the code (where you ask getline to get just 3 characters and it gets 255 instead).

Earthsoup
08-08-2003, 11:09 AM
I've included a snippet of my program. In the getline statement below, if I replace 256 with 3 (for now, a more or less arbitrary value), then when I run the program and input a buncha buncha chars, it crashes - even if I set the Board_type array to 3 also. When I say "crashes", I mean that the previous "cout" statement decides to execute an infinite number of times, and I have to <Ctrl+C> out of the program.

#include <iostream>
using namespace std;

double pawn_path (int X_Start, int Y_Start, int X_Dest, int Y_Dest, int Piece_ID);
double knight_path (int X_Start, int Y_Start, int X_Dest, int Y_Dest, int Piece_ID);
double bishop_path (int X_Start, int Y_Start, int X_Dest, int Y_Dest, int Piece_ID);
double rook_path (int X_Start, int Y_Start, int X_Dest, int Y_Dest, int Piece_ID);
double queen_path (int X_Start, int Y_Start, int X_Dest, int Y_Dest, int Piece_ID);
double king_path (int X_Start, int Y_Start, int X_Dest, int Y_Dest, int Piece_ID);

double render_game (int Board_ID, char Piece_type[], int Your_X_ID, int Your_Y_ID, int x_i, int y_i);

main()
{
char Board_type[256]; // Inputs
char Piece_type[256];
char Your_X[256];
char Your_Y[256];
char X_Dest[256];
char Y_Dest[256];

char* p_board_type = "123"; // Game construction tools
char* p_piece_type = "PNBRQKpnbrqk";
char* p_your_x = "abcdefghABCDEFGH";
char* p_your_y = "12345678";
int Board_ID = 0;
int Piece_ID = 0;
int Your_X_ID = 0;
int Your_Y_ID = 0;
int Dest_X_ID = 0;
int Dest_Y_ID = 0;

int X_Start = 0; // Piece move tools
int Y_Start = 0;
bool current_move = 0;
bool last_move = 0;

int i = 0; // Counters
int x_i = 0;
int y_i = 0;

/*
INPUTS
*/

// Board type?

while (Board_ID < 1 || Board_ID > 3)
{
cout << endl;
cout << "Choose your chess board, 1, 2, or 3: ";
cin.getline (Board_type, 256, '\n'); // 256 limit may differ with OS
for (int i = 1; i < 4; i++)
{
if ((Board_type[0] == p_board_type[i - 1]) && (Board_type[1] == 0))
Board_ID = i;
}
}

stuka
08-08-2003, 05:48 PM
Do you have any other cin statements below that one? Are you sure that THAT line is where it's crashing? Remember, the input is buffered - if you don't clear the buffer, your next cin access will be getting that buffered data, so you could be dying there.

My honest opinion on this is a bit more brutal - DON'T USE char ARRAYS IN C++!!!!!! Use std::string!

Earthsoup
08-08-2003, 07:59 PM
Thanks for the suggestion about std::string, I'll give it a try.

Even if I delete the rest of the program, and leave just what I copied, it behaves the same. :rolleyes:

Hopefully, std::string is a better option.

stuka
08-09-2003, 03:40 AM
std::string is much better - and I like to use std::getline(cin, my_string) to read the input. std::string will handle all the memory allocation for you :)