首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >C函数调用问题

C函数调用问题
EN

Stack Overflow用户
提问于 2014-05-05 21:21:27
回答 3查看 86关注 0票数 0

我在让函数在不同的变量上执行时遇到了问题。我不知道是不是因为我对它的定义不正确,但如果能得到帮助,我将不胜感激。

代码语言:javascript
运行
复制
#include <stdio.h>

int dmv(int x);

int main()
{
    printf("Welcome to DMV simulator 2014! \n");
    printf("Come forward first client");
    int z;
    int d;
    int f;
    int g;
    int h;
    int j;
    int e;
    int q;
    int s;
    dmv(int z);
    printf("Are there anymore people?");
    printf("If 'yes' say so otherwise say 'no'");
    int option;
    if(option == "yes")
    {
        dmv(int f);
    }
    else
    {
            printf("We're closing");     
    }
    printf("Are there anymore people?");
    printf("If 'yes' say so otherwise say 'no'");
    if(option == "yes")
    {
        dmv(int d);
    }
    else
    {
        printf("We're closing");
    }
    printf("Are there anymore people?");
    printf("If 'yes' say so otherwise say 'no'");
    if(option == "yes")
    {
        dmv(int g);
    }
    else
    {
        printf("We're closing");
    }
    printf("Are there anymore people?");
    printf("If 'yes' say so otherwise say 'no'");
    if(option == "yes")
    {
        dmv(int h);
    }
    else
    {
        printf("We're closing");
    }
    printf("Are there anymore people?");
    printf("If 'yes' say so otherwise say 'no'");
    if(option == "yes")
    {
        dmv(int j);
    }
    else
    {
        printf("We're closing");
    }
    printf("Are there anymore people?");
    printf("If 'yes' say so otherwise say 'no'");
    if(option == "yes")
    {
        dmv(int e);
    }
    else
    {
        printf("We're closing");
    }
    printf("Are there anymore people?");
    printf("If 'yes' say so otherwise say 'no'");
    if(option == "yes")
    {
        dmv(int q);
    }
    else
    {
        printf("We're closing");
    }
    printf("Are there anymore people?");
    printf("If 'yes' say so otherwise say 'no'");
    if(option == "yes")
    {
        dmv(int s);
    }
    else
    {
        printf("We're closing");
    }
    printf("Alright we're closing!");

    return(0);
}

int dmv(int x)
{
    pritnf("Enter your name \n");
    char name;
    char Birthday;
    printf("Enter your DOB");
    scanf("%c" , name);
    scanf("%c" , Birthday);
    printf("Here is your lisence %d" , name);
}

以下是我在cygwin中遇到的错误。

代码语言:javascript
运行
复制
Functionography.c:19: error: parse error before "int"
Functionography.c:23: warning: comparison between pointer and integer
Functionography.c:25: error: parse error before "int"
Functionography.c:34: warning: comparison between pointer and integer
Functionography.c:36: error: parse error before "int"
Functionography.c:45: warning: comparison between pointer and integer
Functionography.c:47: error: parse error before "int"
Functionography.c:56: warning: comparison between pointer and integer
Functionography.c:58: error: parse error before "int"
Functionography.c:67: warning: comparison between pointer and integer
Functionography.c:69: error: parse error before "int"
Functionography.c:77: warning: comparison between pointer and integer
Functionography.c:79: error: parse error before "int"
Functionography.c:88: warning: comparison between pointer and integer
Functionography.c:90: error: parse error before "int"
Functionography.c:99: warning: comparison between pointer and integer
Functionography.c:101: error: parse error before "int"
EN

回答 3

Stack Overflow用户

发布于 2014-05-05 21:25:01

应该像这样调用函数:

代码语言:javascript
运行
复制
dmv(z);
票数 2
EN

Stack Overflow用户

发布于 2014-05-05 21:30:43

由于dmv函数的类型为int,因此它应该返回某个整数值。否则,对dmv函数使用void返回类型。

票数 0
EN

Stack Overflow用户

发布于 2014-05-05 21:42:40

好吧,这里有很多问题:

  • 您可以复制/粘贴代码。你永远不应该这么做。这就是为什么函数和循环exists!
  • dmv(int z)是一个声明,而不是一个调用。一旦你的函数被声明了,你只需要放入正确的变量类型。(dmv(z))
  • Your扫描不是很好。要么使用字符数组(char birthday[5]),要么将指针发送到您的变量dmv,不要捕获dmv返回的内容。要么不返回任何内容(void dmv),要么对结果做一些处理。
  • 您尝试使用scanf("%c")获取字符串,它将返回字符,而不是字符串。
  • 您尝试使用printf("%d")打印字符串,这将打印一个十进制整数。
  • 您可以声明多个相同类型的变量,如下所示:

当做unreadable.

  • char时,别忘了在末尾加上\n,否则输出的将是一个简单的字符,它可以处理"h","i",或" a ",或ascii表中的任何内容,但不能处理"Hello“或其他多个character.

  • As,你的代码不会在意你是否回答

  • 。首先是因为您不要求任何用户输入,其次是因为您不能==字符串,第三是因为您的if分支指向...下一个"iteration".

下面是改进后的代码:

代码语言:javascript
运行
复制
#include <stdio.h>

int dmv(int x);

int main()
{
    printf("Welcome to DMV simulator 2014! \n");
    printf("Come forward first client");
    int i = 0,
        end = 0;
    int clients[5];
    char option[4] = "";
    do
    {
        dmv(int z);
        printf("Are there anymore people?");
        printf("If 'yes' say so otherwise say 'no'");
        scanf("%s", cache);
        if(strcmp(cache, "yes"))
        {
            client[i] = dmv(f);
        }
        else
        {
                printf("We're closing");
                end = 1;
        }
    }while(i < 5 || end = 1);

    return(0);
}

int dmv(int x)
{
    pritnf("Enter your name \n");
    char name[30];
    char Birthday[11];
    printf("Enter your DOB");
    scanf("%s" , name);
    scanf("%s" , Birthday);
    printf("Here is your lisence %s" , name);
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23473591

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档