-
1 Attachment(s)
Questions from a noob
My program is attached. It's not done yet, but its goal is to take a user's name, store it in an array. Take the name and store it as a variable. If the age is under 67 it'll calculate how many years the user has until they can retire. If it's over 67, it'll say they should've retired by then.
And here's the output.
Welcome to nameage
Input name: abc
Input age: Name: a╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠t¡ΓΣ°OPress any key to continue . . .
I have two problems right now.
1. Once you put in the name, it skips the input age scanf and goes immediately to printing out the name (which is printing some crazy stuff). The only way this can be avoided is by typing in only one character. I'm still new to this but I have read up on it and apparently it has something to do with converting char values into a string. Which leads me to question 2...
2. Converting the Char array into a string completely messes up the output. On this, I think it might be a formatting problem using %s, but I'm not sure.
This is probably such an easy answer some of you are cringing but any advice is welcome
Edit: It'll probably easier for people to read the code since it's so small so here it is instead of having to download it
#include <stdio.h>
void main() {
printf("Welcome to nameage\n");
char name[10];
int age;
printf("Input name: ");
scanf_s("%c", &name); // Why does it skip the age?
printf("Input age: ");
scanf_s("%d", &age);
printf("Name: %s", name); //Why does it print weird characters?
system("pause");
return 0;
}
-
It is good that you also posted your code within your post to make it more accessible than as an attachment, but you should post it within [code][/code] bbcode tags:
Code:
#include <stdio.h>
void main() {
printf("Welcome to nameage\n");
char name[10];
int age;
printf("Input name: ");
scanf_s("%c", &name); // Why does it skip the age?
printf("Input age: ");
scanf_s("%d", &age);
printf("Name: %s", name); //Why does it print weird characters?
system("pause");
return 0;
}
The main problem is that %c is the format specification to read a character with the scanf family of functions, whereas you wish to read a string. Therefore, you should have written:
Code:
scanf("%9s", name);
Notice that I used "%9s" instead of just "%s" because the field width should be specified so as to prevent buffer overflow. Furthermore, because in this context an array would be converted to a pointer to its first element, I passed name rather than &name as an argument. If you absolutely insist on using scanf_s rather than scanf, then you must make use of the "security enhancement" for reading strings and hence pass the buffer size too:
Code:
scanf_s("%9s", name, (unsigned)_countof(name));
(Example derived from the online MSDN entry for scanf_s, _scanf_s_l, wscanf_s, _wscanf_s_l, so if it doesn't work, not my problem as I would have just used scanf.)
By the way, the main function returns int, not void, and even where your compiler might permit it to have a void return type, you are explicitly returning 0 so it should be int return type anyway.
Also, you should consider checking the return value of scanf (or scanf_s) to see if the input was successfully read.