通过多个函数传递一个变量,没有得到想要的结果

Passing a variable through multiple functions, not getting desired result

在将它拆分为函数之前,我已经开始工作了。我确定它只是一个 & 或 * 我在某处丢失了,但我似乎无法弄清楚。用户输入一个数字,然后他们在菜单上选择 F 以让它打印 0 数字。我在 display() 函数中遇到了垃圾。

#include <stdio.h>

char menu();
int read_int(int number);
void display(int number);

int main(int argc, char ** argv) {
int number = 0;     
char choice = 'O';
while (choice != 'X'){


    choice = menu();    

    if (choice != 'N' && choice != 'F' && choice != 'X'){
        printf("Invalid Input. Enter N, F, or X\n");
    }

    else if (choice == 'N'){
        number = read_int(number);

    }

    else if (choice == 'F'){        
        display(number);            
    }




}

}


char menu ()
{
char i;
printf("\nEnter N to enter an integer from 0 to 20\nEnter F to display the first N+1 numbers (beginning with zero) on the console \nEnter X to quit the program \n");

printf("Your Choice: ");
scanf("%s",  &i);
return i;
}   

int read_int(number)
{
//int number = 0;
printf("\nEnter an integer 0-20: ");
scanf("%d", &number);
if (number >=0 && number <=20)
    return number;
else{
    printf("Enter a valid number between 0 and 20");    
    read_int(number);
}
}

void display(answer)
{
int count = 0;
printf("\nNumber equals: ");
printf("%d", answer);   
//while (count <= number){
//  printf("%d",count);
//  printf(" , ");
//  count++;
//}
}

它似乎正在使用此代码:

#include <stdio.h>

char menu();
int read_int(int number);
void display(int answer);

int main(int argc, char ** argv) {
    int number = 0;     
    char choice = 'O';
    while (choice != 'X'){


    choice = menu();    

    if (choice != 'N' && choice != 'F' && choice != 'X'){
        printf("Invalid Input. Enter N, F, or X\n");
    }

    else if (choice == 'N'){
        number = read_int(number);
        printf("\nNumber equals: ");
        printf("%d", number);   

    }

    else if (choice == 'F'){
        //printf("\nNumber equals: ");
        //printf("%d", number);         
        display(number);            
    }




}

}


char menu ()
{
    char i; 
    printf("\nEnter N to enter an integer from 0 to 20\nEnter F to display the first N+1 numbers (beginning with zero) on the console \nEnter X to quit the program \n");

    printf("Your Choice: ");
    scanf("%s",  &i);
    return i;
}   

int read_int(int number)
{
number = 0;
    printf("\nEnter an integer 0-20: ");
    scanf("%d", &number);
    if (number >=0 && number <=20)
        return number;
    else{
        printf("Enter a valid number between 0 and 20");    
        return read_int(number);
    }
}

void display(int answer)
{
int count = 0;
//printf("\nNumber equals: ");
//printf("%d", answer); 
//int toprint = answer;
while (count <= answer){            
    printf("%d",count);
    printf(" , ");
    count=count+1;
}
}

现在将其转换为程序集!天哪!

您的 scanf() 调用错误:

 scanf("%s",  &i);

您只读了一个字符。应该是:

 scanf(" %c",  &i);

我添加的前导 space(或任何白色 space 字符)字符将确保输入流中剩余的任何白色space 字符都将被丢弃。

read_int 在进行递归调用时不会 return 任何东西(这意味着它可以 return 任何东西)。

您的程序行为在从自身调用 read_int 的控制路径上未定义。您需要 return 该值:

return read_int(number);

我很惊讶你的编译器没有警告你。你有关闭警告吗?

(我也会考虑重新编码以删除递归;如果您有特别恶意的用户,您可能会溢出您的调用堆栈 - 在某些平台上它非常小。)

char menu (void){
    char i[2];
    printf("\nEnter N to enter an integer from 0 to 20\nEnter F to display the first N+1 numbers (beginning with zero) on the console \nEnter X to quit the program \n");
    printf("Your Choice: ");
    scanf("%1s", i);
    return *i;
}   

int read_int(int number)//add int
{
    printf("\nEnter an integer 0-20: ");
    scanf("%d", &number);
    if (number >=0 && number <=20)
        return number;
    else{
        printf("Enter a valid number between 0 and 20");    
        return read_int(number);//add return
    }
}

void display(int answer)//add int
{
    int count = 0;
    printf("\nNumber equals: ");
    printf("%d\n", answer);
}