c - c编程输出信息错误

标签 c output scanf

这个程序按照我想要的方式工作,但只有一个错误。它不会从欧元转换为美元,它只是给我

0.0 euro = 0.0 dollars;

你们能帮帮我吗?

#include <stdio.h>
#include <stdlib.h>

void dollar(float dollar);

int main()
{
    char what;
    int howmany;
    int i;
    float dollar2;
    float euro2;

    printf("enter how many times do you want to convert\n");
    scanf(" %d", &howmany);
    printf("enter U if you want to convert from USD to Euro\n");
    printf("enter E if you want to convert from Euro to USD\n");

    for(i=0; i<=howmany-1; i++)
    {
        scanf(" %c", &what);
        if(what == 'E')
        {
            printf(" Enter how many euros do you want to convert\n");
            scanf(" %f", &euro2);
            euro(euro2);
        }
        if(what == 'U' ){
            printf(" Enter how many dollars do you want to convert\n");
            scanf(" %f", &dollar2);
            dollar(dollar2);
        }
    }
    return 0;
}

void dollar(float dollar)
{
    float euro = 0.94 * dollar;
    printf("%0.2f dollar = %0.2f euro\n", dollar, euro);
    return;
}

void euro(float euro)
{
    float dollar = 1.37 * euro;
    printf("%0.2f EURO = %0.2f DOLLAR\n", euro, dollar);
    return;
}

最佳答案

“在计算机编程中,函数原型(prototype)或函数接口(interface)是函数的声明,它指定函数的名称和类型签名(元数、参数类型和返回类型),但省略函数体。” https://en.wikipedia.org/wiki/Function_prototype

基本上,由于 euro 是在 main 之后声明的,因此当您在 main 中调用 euro 时,它不知道该函数在哪里。通过在 main 之上声明函数或使用函数原型(prototype)可以避免这种情况。

函数原型(prototype)的结构是这样的

your_return_type function_name(arg_type,arg_type,arg_type....);

关于c - c编程输出信息错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29591660/

上一篇:c - 与 cygwin 链接

下一篇:C编程: Strings

相关文章:

javascript - 如何将函数输出发送到 JS/HTML 中的表格单元格?

java - 单击时获取 JButton 的名称

C++ 快速 cin 输入流

c - 为嵌套链表分配内存+使用scanf

c - 变量在读取文件的循环中不递增

C 编程。字符段错误

c - 如何在C中计算无符号长数中的奇数位

c - 为什么我得到错误的输出,我该如何解决这个问题?

c - 重新分配三重指针

c++ - 在 c/c++ 中输入比 scanf for int 更快