【问题标题】:How do I make a Diary in C language?如何用C语言写日记?
【发布时间】:2020-11-30 19:58:51
【问题描述】:

这里是第一个计时器;我正在尝试按照cs50参考在c中制作一个日记程序

#include <cs50.h>
#include <stdio.h>
#include <unistd.h>

int main(void)

{

//Program ask user for their name
string name = GetString("What is your name?\n");
printf("Hello there %s, name\n");

//Program ask user for their age
int age = GetInt("What is your age?\n");
printf("You are %i, old\n");

}

当我使用 clang 运行代码时,它会返回此错误消息

Diarytest.c:10:25: error: too many arguments to function call, expected 0,
      have 1
string name = GetString("What is your name?\n");
              ~~~~~~~~~ ^~~~~~~~~~~~~~~~~~~~~~
/usr/local/include/cs50.h:106:1: note: 'GetString' declared here
string GetString(void);
^
Diarytest.c:11:22: warning: more '%' conversions than data arguments
      [-Wformat]
printf("Hello there %s, name\n");
                    ~^
Diarytest.c:14:18: error: too many arguments to function call, expected 0,
      have 1
int age = GetInt("What is your age?\n");
          ~~~~~~ ^~~~~~~~~~~~~~~~~~~~~
/usr/local/include/cs50.h:87:1: note: 'GetInt' declared here
int GetInt(void);
^
Diarytest.c:15:18: warning: more '%' conversions than data arguments
      [-Wformat]
printf("You are %i, old\n");
                ~^
2 warnings and 2 errors generated.

这里有什么我遗漏的吗?我在我的操作系统中安装了 cs50 库,我什至尝试链接它,它返回了相同的错误消息。谢谢你的帮助!干杯。

更新:代码在下方用户的帮助下进行了更新(感谢 David Cullen),现在代码如下所示。

#include <cs50.h>
#include <stdio.h>
#include <unistd.h>

int main(void)

{

//Program ask user for their name
string name = GetString();
printf("Hello there, %s\n", name);

//Program ask user for their age
int age = GetInt();
printf("You are, %i\n", age);

}

但是,当我使用 clang 运行程序时,我收到一条新的错误消息。

/usr/bin/ld: /tmp/Diarytest-862733.o: in function `main':
Diarytest.c:(.text+0x9): undefined reference to `GetString'
/usr/bin/ld: Diarytest.c:(.text+0x2a): undefined reference to `GetInt'
clang: error: linker command failed with exit code 1 (use -v to see invocation)

我被难住了

【问题讨论】:

标签: c linux debian cs50


【解决方案1】:
Diarytest.c:10:25: error: too many arguments to function call, expected 0,
      have 1
string name = GetString("What is your name?\n");
              ~~~~~~~~~ ^~~~~~~~~~~~~~~~~~~~~~
/usr/local/include/cs50.h:106:1: note: 'GetString' declared here
string GetString(void);
^

此错误表示您无法将"What is your name?\n" 传递给GetString

Diarytest.c:11:22: warning: more '%' conversions than data arguments
      [-Wformat]
printf("Hello there %s, name\n");
                    ~^

此错误意味着您必须在此处向printf 提供另一个参数(可能是name)。

Diarytest.c:14:18: error: too many arguments to function call, expected 0,
      have 1
int age = GetInt("What is your age?\n");
          ~~~~~~ ^~~~~~~~~~~~~~~~~~~~~
/usr/local/include/cs50.h:87:1: note: 'GetInt' declared here
int GetInt(void);
^

与上述问题类似,不能将"What is your age?\n" 传递给GetInt

Diarytest.c:15:18: warning: more '%' conversions than data arguments
      [-Wformat]
printf("You are %i, old\n");
                ~^
2 warnings and 2 errors generated.

再一次,您必须在此处向printf 提供另一个参数(可能是age)。

【讨论】:

  • 您好,感谢您的帮助。很抱歉打扰,但我有什么可能的解决方案来让它工作?
猜你喜欢
  • 1970-01-01
  • 2020-03-26
  • 2013-09-16
  • 1970-01-01
  • 2020-03-26
  • 2011-05-17
  • 1970-01-01
  • 2022-01-21
  • 2012-05-28
相关资源
最近更新 更多