scanf("%1c%2c %d %d %d %d %d %d %d %d %d %d",
&x, &y, &arr[0], &arr[1], &arr[2], &arr[3], &arr[4],
&arr[5], &arr[6], &arr[7], &arr[8], &arr[9]);
strcpy(string, x);
value1 = atoi(string);
strcpy(string, y);
value2 = atoi(string);
value_final = value1 + value2;
I'm trying to get the ascii values for the -l
or -s
then pass them through the switch
with the ascii values added but I'm getting errors when I use atoi
and I'm not sure as to if you are supposed to be adding the ascii values when the user enters the -l
or -s
or if their is another way to do this?
Your code has several problems:
switch
cases do not have a break;
clause. Control falls into the next clause and finally into the default
statement. char
arrays and single char variables: strcpy(string, x);
should not even compile. -l
or -s
is very convoluted and probably erroneous. You should use character literals. printf
statement should be outside the loop. max
and min
are uninitialized, the loops invoke undefined behavior. atoi()
: if this was your assignment, you should not use scanf()
. Here is a simplified version:
int main(void) {
char option[3], buffer[32];
int i, min, max, value;
min = max = 0;
if (scanf("%2s", option) == 1) {
for (int i = 0; i < 10 && scanf("%31s", buffer) == 1; i++) {
value = atoi(buffer);
if (i == 0) {
min = max = value;
} else {
if (min > value)
min = value;
if (max < value)
max = value;
}
}
if (!strcmp(option, "-l")) {
printf("output: The largest number is %d\n", max);
return 0;
} else
if (!strcmp(option, "-s")) {
printf("output: The smallest number is %d\n", min);
return 0;
}
}
printf("You have entered an invalid option, try again next time.\n");
return 0;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.