so i was doing this exercise wher i need to get the birth year of a person, the atual year and then calculate the age and print something if the age is greater or equal to 18.
however, the part where i put the birth year and the actual year doesn’t actually print the numbers i write.
I’m new to this so i would like some help
#include <stdio.h>
int main() {
int n, a;
char str[25];
int idade = a - n;
/* idade (eng) = age(pt) */
printf("write your birth year, actual year and name:\n");
scanf("%s, %d, %d", &str, &n, &a);
printf("%s, \n %d, \n %d, \n", str, n, a);
/* Btw this is what appears: " Playground file0.c: In function 'main':
Playground file0.c:10:13: warning: format '%s' expects argument of type 'char ', but argument 2 has type 'char (*)[25]' [-Wformat=]
10 | scanf("%s, %d, %d", &str, &n, &a);
| ~^ ~~~~
| | |
| char * char (*)[25]
Playground file0.c:7:19: warning: 'a' is used uninitialized in this function [-Wuninitialized]
7 | int idade = a - n;
| ~~^~~
Playground file0.c:7:19: warning: 'n' is used uninitialized in this function [-Wuninitialized] "
*/
printf("Since you were born in %d and we are in %d, your age is: %d \n", n, a, idade);
if (idade >= 18){
printf("%s, a sua entrada foi permitida.", str);
}
/* your entry was permited/accepted (eng) = a sua entrada foi permitida(pt) */
return 0;
}
So the error messages mean exactly what they say. The trick is understanding what they say.
You are computing idade before getting the values of a and n (it is good practice to use actual words as variable names instead of obscure letters). This means that your subtraction is based upon the junk values that happen to be in your computer memory, not the values from the user.
For the string issue, a variable holding a charcter array is a pointer, no need to use the reference operator &.
In addition to @JeremyLT’s excellent answer, you’re requesting input in the order birth year, actual year, name but reading it in the order name, birth year, actual year. This is guaranteed to cause problems for your users.
Thank you for your explanation! Actually i learnt pointers yesterday but totally forgot to apply them now. The a and n are like that because they’re short for a - atual/actual and n- nascimento/birth, but i’ll just keep the word instead of only the letter.
So i’ve changed it and now there are no warnings. However, it doesnt print the correct birth year nor the actual year and the subtraction doesnt work…
#include <stdio.h>
int main() {
int nascimento;
int atual;
int idade;
char str[25];
printf("write your name, birth year and actual year:\n");
scanf("%s*, %d, %d", str, &nascimento, &atual);
printf("%s, \n %d, \n %d, \n", str, nascimento, atual);
idade = atual-nascimento;
printf("Your age is: %d \n", idade);
if (idade >= 18){
printf("%s, a sua entrada foi permitida.", str);
}
return 0;
}
s, String of characters, Any number of non-whitespace characters, stopping at the first whitespace character found. A terminating null character is automatically added at the end of the stored sequence.
If you adjust for this, then I can read the numbers as integers.