Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

gets

Status
Not open for further replies.

Mungovan

Programmer
Oct 24, 2002
94
IE
Hi.
Basically I need to input a string of numbers into an array. The user will input a string of < 20 numbers on one line such as &quot;1263444545&quot;, and I want the program to save these as:

myArray[0] = 1;
myArray[1] = 2;
....
....

With the empty ones containing -1.

I think it uses the gets function. How can I implememnt thins?

Thanks for any help.
D
 
Something like this?

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main(void);

int main(void)
{
int myArray[20];
int a=0;
char InputNumbers[21];

/* Init myArray with all -1 */
printf(&quot;please enter string of numbers... &quot;);
for (a=0;a < 20;a++){
myArray[a]=-1;
}

/* Get the numbers */
gets(InputNumbers);

/* Print numbers entered */
printf(&quot;Numbers entered %s\n&quot;,InputNumbers);

/* Populate the numbers entered in to myArray */
for (a = 0; a < strlen(InputNumbers); a++){
myArray[a] = InputNumbers[a] - '0';
}

/* Print the values in myArray */
for (a=0;a < 20; a++){
printf(&quot;myArray %d = %d\n&quot;,a,myArray[a]);
}
exit(0);

}
 
gets() is deprecated, and for good reason.
It does no bounds checking.
fgets() is what should be used.

 
Status
Not open for further replies.

Similar threads

Part and Inventory Search

Sponsor

Back
Top