3 Replies - 3208 Views - Last Post: 23 November 2009 - 01:49 PM Rate Topic: -----

#1 Rapidninja  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 03-November 09

gets() command warning.

Posted 23 November 2009 - 01:07 PM

I'm trying making a simple program in C++ and I can't get it to work. I've tried it on Microsoft Visual Studio and it worked but now when I use it on my Mac on XCode it gives me this warning:
warning: this program uses gets(), which is unsafe
Then it skips over that line. Heres my code:
#include <iostream>
using namespace std;

int main() {
char n1[64];
	cout << "Please type: \n";
	scanf( "\n" );
	gets(n1);
cout << n1;
}


I added the "scanf("\n") because someone said it would help. Now it lets me type in something, displays "1", then exits. What am I doing wrong?

Is This A Good Question/Topic? 0
  • +

Replies To: gets() command warning.

#2 Aphex19  Icon User is offline

  • Born again Pastafarian.
  • member icon

Reputation: 615
  • View blog
  • Posts: 1,873
  • Joined: 02-August 09

Re: gets() command warning.

Posted 23 November 2009 - 01:16 PM

How about something like this?

#include <iostream>

using namespace std;

int main() 
{
char input[64];

	cout << "Please type: \n";
	cin.getline(input, 64);
	
	cout << input;
	cin.get();
	return 0;
}


Was This Post Helpful? 1
  • +
  • -

#3 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6063
  • View blog
  • Posts: 23,517
  • Joined: 23-August 08

Re: gets() command warning.

Posted 23 November 2009 - 01:31 PM

Whoa...whatever you do, don't mix and match C and C++ I/O in the same program as you did in the OP. That's a recipe for confusion!

If you want C++, do with Aphex19's solution.

If you want C, you need to look into how to use scanf or better yet, fgets:
#include <stdio.h>
#include <string.h>

int main()
{
    char input[64];
    printf("Please enter your name: ");

    /* Read at the most 63 characters from the command line */
    if (fgets(input, sizeof(input), stdin) != NULL)
    {
        /* fgets keeps the newline in the string (if the number of
            characters entered is less than the size of the buffer),
            so if the newline is there, let's get rid of it. */
        if (input[strlen(input) - 1] == '\n')
            input[strlen(input) - 1] = '\0';

        printf("You entered your name as %s\n", input);   
    }
    return 0;
}

Was This Post Helpful? 0
  • +
  • -

#4 Rapidninja  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 03-November 09

Re: gets() command warning.

Posted 23 November 2009 - 01:49 PM

View PostAphex19, on 23 Nov, 2009 - 12:16 PM, said:

How about something like this?

#include <iostream>

using namespace std;

int main() 
{
char input[64];

	cout << "Please type: \n";
	cin.getline(input, 64);
	
	cout << input;
	cin.get();
	return 0;
}



Thanks Aphex19. This works this is exactly what I needed. And, Jackofalltrades, Thanks for the reply but I think I'll take Aphex19's answer and stick with C++.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1