Skip to content

Instantly share code, notes, and snippets.

@chrisdel101
Created June 10, 2019 22:44
Show Gist options
  • Save chrisdel101/de2607645c37176707b07815cff446d2 to your computer and use it in GitHub Desktop.
Save chrisdel101/de2607645c37176707b07815cff446d2 to your computer and use it in GitHub Desktop.
Entering inputs
int main(void){
//enter number of times input with propmpt for string
int count;
puts("Enter count: ");
scanf("%i", &count);
// hack so it does not shallow fget
getchar();
// 1000 is arbitraty
char arr[count][1000];
for (size_t i = 0; i < count; i++)
{
char storeStrTemp[256];
// reset arr
memset(storeStrTemp, 0, sizeof(storeStrTemp));
puts("enter str:");
// NEED STR LEN HERE
fgets(storeStrTemp, 256, stdin);
// NEED STR LEN HERE TOO TO LOOP OVER IT ALL - it stops at 255
for (size_t j = 0; j < strlen(storeStrTemp); j++)
{
arr[i][j] = storeStrTemp[j];
}
}
}
@hawkinsw
Copy link

Line 12 is the reason that you cannot take a string longer than 256 bytes. if you want to take a string longer than that, you'll have to allocate a buffer of memory to store it that is bigger. You can also do it dynamically -- ie, get their input one character at a time and then realloc() the buffer every time until there are no more characters left.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment