ok...i'm new to this c language and i was wondering what am i supposed to do if the user enters a string that is longer than my array size. for our assignment, we're supposed to ask for the user's name and our array size is supposed to be 20 characters. if the user enter something more than 20, we're supposed to give an error message saying that only the first 20 characters will be stored. for my code, if the user enters more than 20 characters, the additional character would be assigned to another variable. what am i supposed to do.
i'm using fscanf to accept the string but i read that it's better to use gets.
teteret 0 Newbie Poster
Recommended Answers
Jump to Postfgets() is always better than scanf() because fgets() will not accept more characters than will fit in the buffer. But neither scanf() nor fgets() will meet the requirements of your assignment. You need a function that will get only one key at a time from the keyboard so that your …
Jump to Postyou're trying to put too much at the top of that loop. Break it down, pub the getchar() inside the loop and on each loop iteration check if the value of i exceeds 19 (need one byte for the string's null terminator, or make the size of the buffer 21.
…
Jump to Postthe baseline behavior of fgets() is that it will read in the newline char in addition to the alphanumeric char and other whitespace char if there's enough room in the array. So I here's what I would try. Declare the target string to include two more than the max number …
All 10 Replies
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster
teteret 0 Newbie Poster
teteret 0 Newbie Poster
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster
teteret 0 Newbie Poster
Aia 1,977 Nearly a Posting Maven
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster
WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague
teteret 0 Newbie Poster
Lerner 582 Nearly a Posting Maven
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.