通过多个函数传递变量,而不是获得所需的结果

时间:2015-07-06 14:24:09

标签: c function parameter-passing pass-by-reference

在将其拆分为函数之前,我已将其工作了。我敢肯定这只是一个&或*我在某处失踪,但我似乎无法弄明白。用户输入一个数字,然后在菜单上选择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;
}
}

现在将它转换为汇编!哦,孩子!

4 个答案:

答案 0 :(得分:2)

您的scanf()调用错误:

 scanf("%s",  &i);

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

 scanf(" %c",  &i);

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

答案 1 :(得分:2)

read_int在进行递归调用时不会返回任何内容(这意味着它可以返回任何内容)。

答案 2 :(得分:2)

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

return read_int(number);

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

(我还考虑重新编码以删除递归;如果你有一个特别恶毒的用户,你可能会溢出你的调用堆栈 - 这在某些平台上非常小 - )

答案 3 :(得分:1)

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);
}