【发布时间】:2015-01-04 20:25:18
【问题描述】:
我一直在尝试从一本名为 C Programming Absolute Beginner's Guide 的书中学习 C,但我遇到了一个问题,一个不好的事情是你不能向一本书提问!将错误输入谷歌,它把我带到了这个网站。我得到的错误在问题标题中,这是我的代码。
#include <stdio.h>
main(
{
//Set up the variables, as well as define a few
char firstInitial, middleInitial;
int number_of_pencils;
int number_of_notebooks;
float pencils = 0.23;
float notebooks = 2.89;
float lunchbox = 4.99;
//The information for the first child
firstInitial = 'J';
middleInitial = 'R';
number_of_pencils = 7;
number_of_notebooks = 4;
printf("%c%c needs %d pencils, %d notebooks, and 1 lunchbox\n",
firstInitial, middleInitial,number_of_pencils,
number_of_notebooks);
printf("The total cost is £%.2f\n\n", number_of_pencils*pencils + number_of_notebooks*notebooks + lunchbox);
//The information for the second child
firstInitial ='A';
middleInitial = 'J';
number_of_pencils = 10;
number_of_notebooks = 3;
printf("%c%c needs %d pencils, %d notebooks, and 1 lunchbox\n",
firstInitial, middleInitial,number_of_pencils,
number_of_notebooks);
printf("The total cost is £%.2f\n\n", number_of_pencils*pencills
+ number_of_notebooks*notebooks + lunchbox);
//The information for the third child
firstInitial = 'M';
middleInitial = 'T';
number_of_pencils = 9;
number_of_notebooks = 2;
printf("%c%c needs %d pencils, %d notebooks, and 1 lunchbox\n",
firstInitial, middleInitial,number_of_pencils,
number_of_notebooks);
printf("The total cost is £%.2f\n",
number_of_pencils8pencils + number_of_notebooks*notebooks + lunchbox);
return0;
}
)
这段代码有什么问题?
【问题讨论】:
-
欢迎来到 Stack Overflow。请尽快阅读About 页面。当您遇到编译错误时,最好包含确切的错误消息 - 除了如果文件名是一英里长的绝对路径,请将名称缩减为文件的基本名称(因此将
/huge/long/path/with/many/levels/of/directory/src/file.o更改为 @ 987654326@)。确保您显示的源代码和其中的行号与编译器所说的一致。如果文件太长而不方便,请创建一个 MCVE (How to create a Minimal, Complete, and Verifiable Example?) -
我认为最后一个字符
)应该在main(之后所以 ->main()这对你有用吗? (顺便说一句:return0;->return 0;) -
我查看了本书的示例材料,并且(在第 2 章中)示例程序都有
main()而不是int main()(或者,我更喜欢int main(void)) .它们确实包含必要的return 0;,因为它们没有使用允许省略return 0;的C99 表示法(尽管我也不喜欢这样做的代码)。第 3 版的日期为 2014 年;令人担忧的是,第一个程序已经有 15 年没有用当前的 C 语言编写了。我不喜欢将return、while、int、if、float描述为“命令”——它们是关键字!当心! -
@Dave:请将一个答案标记为解决方案。如果可以:删除您的“谢谢回答”,因为这不能回答问题。
标签: c