Thread: Terminal Warning gets()

  1. #1
    Registered User
    Join Date
    Mar 2005
    Posts
    39

    Terminal Warning gets()

    Hi

    Right Now im just experimenting with c++. This basic program has keyboard input..and converts it to uppercase (just for the fun of it):-

    Code:
    //String Experiment and ideal of array understanding
    
    #include <iostream>
    #include <cctype>
    #include <cstring>
    using namespace std;
    
    int main () {
    	
    	char str[60];
    	int i;
    	
    	cout << "\nPlease Enter A String For Conversion: ";
    	gets(str); //Keyboard Input
    	
    	for(i = 0; str[i]; i++) str[i] = toupper(str[i]);
    	
    	cout << "toupper() Conversion: " << str << "\n";
    	
    	
    	
    	
        return 0;
    }
    However when I execute it it gives me the following warning:

    warning: this program uses gets(), which is unsafe.

    Is this normal?..
    So far I havnt experienced any stability issues with the gets() and the compiler doesnt have any warnings or errors

    Is there any alternative to gets()..i know cin..but I want to be able to have spaces and tabs etc

    Thanx For The Heads Up In Advanced!

    Cheers

    Alex

  2. #2
    Let's do some coding! Welshy's Avatar
    Join Date
    Mar 2005
    Location
    Staffordshire University, UK
    Posts
    168
    I think the problem is that your using char, so change the variable:

    Code:
    char str[60];
    to:

    Code:
    string str;
    and then i'd use cin, but it's up to you really
    Last edited by Welshy; 04-09-2005 at 11:20 AM.

  3. #3
    Registered User
    Join Date
    Mar 2005
    Posts
    39
    Hi

    Damn That Was a fast reply..thanks!

    if i was the use cin i wouldnt get the spaces would I?

    e.g

    If I wanted to enter: i like c++

    it would return: I

    cheers

    Alex

  4. #4
    Let's do some coding! Welshy's Avatar
    Join Date
    Mar 2005
    Location
    Staffordshire University, UK
    Posts
    168
    Actually no, what i said is wrong, wait for someone who knows what they're talking about to answer, lol. There is a way to do it using 'streams', but i dont know the syntax so i cant help you.

    I'm only learning C++ myself, so excuse me for making the mistake, hope you solve it

  5. #5
    Registered User
    Join Date
    Mar 2005
    Posts
    39
    Totally Cool.

    Thanx For The Help Welshy..your effort is mucho appriciated!

    Anybody Else Have Any Ideas?

    Cheers

    Alex

  6. #6
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    You get the warning about gets being unsafe because it does not limit how many characters it accepts from the user and you could therefore end up storing more characters than your array could hold. In place of gets you should use fgets which does allow you to specify a limit as to how many characters it should accept:

    Code:
    char str[60];
    
    ...
    
    // Read up to 59 characters (60th is saved for NULL terminator) from stdin and store into str
    fgets(str,sizeof(str),stdin);
    But, since this is C++ and not C, a better alternative would be to use a string container as Welshy stated which can hold any number of characters as needed:

    Code:
    #include <string>
    
    ...
    
    string str;
    
    ...
    
    // Get as many characters from user as they type, stop at first newline by default
    getline(cin,str);
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  7. #7
    Registered User
    Join Date
    Mar 2005
    Posts
    39
    Hi

    Thanx for the reply

    So I can still use the gets() aslong as the character count is in order.
    Thanx for the heads up..and ill look into that string container!


    Cheers

    Alex

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 10
    Last Post: 07-10-2008, 03:45 PM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. <Gulp>
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 01-14-2006, 01:03 PM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM