How to avoid buffer overflow at input?

This is a discussion on How to avoid buffer overflow at input? within the C++ Programming forums, part of the General Programming Boards category; I'm trying to figure out how to do input to variables without overflowing them. I have string variables to hold ...

  1. #1
    Registered User
    Join Date
    Nov 2004
    Posts
    20

    How to avoid buffer overflow at input?

    I'm trying to figure out how to do input to variables without overflowing them. I have string variables to hold strings and int for numbers.

    1. Is getchar() the most elegant way of taking care of a string with spaces? (And use arrays instead of string)

    2. Should I use getchar() with integers aswell and convert the characters to integers after verifying that they are numbers?

    3. I would like some kind of window system in my program. What do you suggest I use? (Windows, console)

    Thanks.

  2. #2
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,892
    1) Use std::getline.

    2) I'd say no. You can use the input stream itself to verify the numbers.

    3) Well, what do you want the system for? And I'm not sure what kind of suggestions you want. Windowing libraries?
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  3. #3
    Registered User
    Join Date
    Nov 2004
    Posts
    20
    I've used getline for both 1 and 2. To convert to integer I used:
    http://faq.cprogramming.com/cgi-bin/...&id=1043284385
    It works like a charm.

    I thought of Allegro and Qt to get some kind of window system. The included window framework wasn't that pretty in allegro. And I could'nt find Qt for windows that was free. Perhaps some kind of ascii library would do the job. Anyone know of a neat ascii library?

  4. #4
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,892
    You could use GTK++ or the C++ version GTK--. It is free. (But it's also GNU-licensed, I think.)
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  5. #5
    Registered User
    Join Date
    Nov 2004
    Posts
    20
    I'll take a look at it thanks for the help.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. can someone help me with these errors please code included
    By geekrockergal in forum C Programming
    Replies: 7
    Last Post: 02-10-2009, 01:20 PM
  2. Single Byte Buffer Overflow
    By Azimuth in forum C Programming
    Replies: 5
    Last Post: 02-07-2009, 10:59 AM
  3. Need some help with C program writing
    By The_PC_Gamer in forum C Programming
    Replies: 9
    Last Post: 02-12-2008, 08:12 PM
  4. large program code ,please help
    By Ash1981 in forum C Programming
    Replies: 14
    Last Post: 01-30-2006, 05:16 AM
  5. getline problem
    By Unregistered in forum C++ Programming
    Replies: 4
    Last Post: 10-06-2001, 09:28 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21