Reputation: 173
I'm trying to shorten a string length to be displayed (printf
). For example I ask user to enter a name, then the program will only display back 15 characters of it.
This is snippet of my code:
int score1=0,score2=0,foul1=0,foul2=0,rebound1=0,rebound2=0,assist1=0,assist2=0,missed1=0,missed2=0,choice1,choice2,choice4,choice5=0;
char choice3,home[15]="HOME",away[15]="AWAY";
printf("\t\t\t==================================\n\t\t\t|| NETBALL SCOREKEEPER\t||\n\t\t\t==================================\n\n");
do
{
printf("Do you want to enter teams' names?\n1-Yes\n2-No\n:>>");
scanf("%d",&choice1);
switch(choice1)
{
case 1:
{
do
{
printf("\nPlease enter HOME team's name(max 15 characters including space)\n:>>");
scanf(" %[^\n]s",&home);
printf("\nPlease enter AWAY team's name(max 15 characters including space)\n:>>");
scanf(" %[^\n]s",&away);
do
{
printf("\n\n%s VS %s\n\n1.Confirm\n2.Edit\n:>>",home,away);
scanf("%d",&choice2);
if(choice2!=1&&choice2!=2)
printf("\n***ERROR. INVALID INPUT***\n\n");
}
while (choice2!=1&&choice2!=2);
}
while(choice2==2);
break;
}
case 2:
{
printf("\nSet up to default:\n%s VS %s\n\n",home,away);
break;
}
default:
{
printf("\n***ERROR. INVALID SELECTION***\n\n");
break;
}
}
}
Upvotes: 1
Views: 1153
Reputation: 154280
Code has input and output issues;
char home[15]="HOME";
...
printf("Do you want to enter teams' names?\n1-Yes\n2-No\n:>>");
scanf("%d",&choice1);
...
printf("\nPlease enter HOME team's name(max 15 characters including space)\n:>>");
scanf(" %[^\n]s",&home);
...
Input1: The 's'
in " %[^\n]s"
is certainly not needed. 's'
is not part of the "%[^\n]"
format specifier.
Input2: If "max 15 characters", then home[15]
is too small to save 15 char
plus the terminating null character '\0'
. Should be home[15+1]
.
Input3: Code does not limit user input to 15, it only request user to limit input. Users are evil - do not trust them to limit input as requested. Using "%15[^\n]");
limits the number of save char
to 15.
Input4: Far better to use fgets()
for user input. Read user input into a reasonable size buffer and then scan it.
Input5: Coding both scanf()
with fgets()
is a problem. scanf("%d",&choice1);
typically leaves a '\n'
in stdin
, that fgets()
will consume only that.
[Edit] Input6: (@Cool Guy) &home
should be home
.
Input solution #1: quick, but not robust:
char home[15+1]="HOME";
...
printf("\nPlease enter HOME team's name(max 15 characters including space)\n:>>");
scanf(" %15[^\n]", home);
...
Input solution #2: more robust:
char home[15+1]="HOME";
char buf[50];
...
printf("Do you want to enter teams' names?\n1-Yes\n2-No\n:>>");
fgets(buf, sizeof buf, stdin);
if (sscanf(buf, "%d",&choice1) != 1 || choice2 < 1 || choice2 > 2) {
printf("\n***ERROR. INVALID INPUT***\n\n");
break;
}
...
printf("\nPlease enter HOME team's name(max 15 characters including space)\n:>>");
fgets(buf, sizeof buf, stdin);
if (sscanf(buf, " %15[^\n]", home) != 1) {
Handle_BadNameInput(); // all spaces
}
...
Output:
To print a string with a maximum number of char
uses "%.*s"
printf("%.*s", max_Length, str);
More checks could be added:
1. Insure user input does not have additional data on the line.
2. Check for EOF.
Upvotes: 2
Reputation: 19874
Going by your question Shortening strings in C
You can do it by
char str[20];
fgets(str, sizeof(str), stdin); /* scanf is nasty for reading strings, use fgets instead (see buffer overflow) */
printf("%.ns",str); /* n = number of characters to be printed out */
prints out n characters in the string left aligned.
If string is str = "someString"
and you need to print out just 4 characters in it you can do
printf("%10.4s",str);
Where 10 specifies the field length and 4 specifies the number of characters to be printed out.
Upvotes: 1