I'm writing this program for my homework and i keep getting type errors. To my knowledge %d reads integer which is in this case the variable x and %lf reads double " variable f"
i've tried to remove "\n" in scanf() function as it was requested in other Questions
int x=0;
int l=0;
double f=0;
printf("Geben Sie eine ganze Zahl");
scanf("%d",x);
printf("Geben Sie eine reele Zahl");
scanf("%lf",f);
l=-1;
char r[1]="";
char s[1]="";
while(l !=1){
printf("Geben Sie ein Zeichen");
scanf("%s",r);
l=strlen(r);
}
return 0;
Run Code Online (Sandbox Code Playgroud)
Errors:
C:/Users/---(9): warning in format string of scanf(): the conversion %d expects type int* but given type is int (argument 1).
C:/Users/---(11): warning in format string of scanf(): the conversion %lf expects type double* but given type is double (argument 1).
Run Code Online (Sandbox Code Playgroud)
It's like the warning says: The function is expecting pointers to the types that you're giving it. Fix it by putting a & before the variable, which makes it pass the address instead:
printf("Geben Sie eine ganze Zahl");
scanf("%d", &x);
printf("Geben Sie eine reele Zahl");
scanf("%lf", &f);
Run Code Online (Sandbox Code Playgroud)
There's also an issue with how you're reading in characters. This here
char r[1] = "";
char s[1] = "";
Run Code Online (Sandbox Code Playgroud)
Makes two arrays that contain nothing else than a null terminator. It doesn't have any capacity to read in a non-empty string, which you're doing here:
scanf("%s", r);
Run Code Online (Sandbox Code Playgroud)
It's unclear whether you're trying to read in a whole string or just a character, as your output implies. For just one character, your code should look like this:
char r;
scanf("%c", &r);
Run Code Online (Sandbox Code Playgroud)
And for a whole string:
char r[20]; // can hold 19 chars plus a null terminator
scanf("%19s", r);
Run Code Online (Sandbox Code Playgroud)
Adjust those sizes to match the length of the string that you need to read.
| 归档时间: |
|
| 查看次数: |
65 次 |
| 最近记录: |