【发布时间】:2011-10-12 04:39:22
【问题描述】:
我最近一直在努力学习 C++,直到今天一切顺利。我正在尝试制作一个非常简单的应用程序,基本上只是询问用户一个数字,然后显示该数字的阶乘。
当我尝试在 Cygwin (g++ factorial.cpp -o fact) 中编译文件时,我收到以下警告“警告:文件末尾没有换行符”。按照警告的内容,我在文件末尾添加了一个换行符并再次尝试......结果完全相同。无论我尝试一个换行符还是二十个换行符,它似乎都无法编译。
我在下面附上了一个简单但仍未完成的文件(除非想象它的末尾有一个空行。这个菜鸟无法让它显示在代码视图中):
#include <iostream>
#include <string>
using namespace std;
int getInput();
void displayFactorial(int);
int main()
{
int number = getInput();
displayFactorial(number);
return 0;
}
int getInput()
{
int userNumber;
bool okNumber;
while(okNumber == false){
cout << "Please eneter a number in the range of 1-10";
cin >> userNumber;
if(userNumber >= 1 and userNumber <=10){
okNumber = true;
}
else{
cout << "Incorrect number" << endl;
}
}
return userNumber;
}
void displayFactorial(int number){
string displayString = number + "! =";
int total;
for(int i = 0; i<=number; i++){
//displayString += "
}
cout << displayString;
}
// File ended the line above this (with a new line. This line added by Martin so it shows)
知道如果不是换行符会导致该警告的原因吗?
【问题讨论】:
-
不确定是什么导致了警告,但在您(尝试)进入循环之前,您没有将
okNumber初始化为“假”,因此while循环可能永远不会运行。哦,也许这意味着你不要在displayFactorial()末尾做<< endl -
你在重新编译之前保存了文件吗?
-
@Rudy Velthuis:缺少
<< endl不可能是原因。见This。此外,如果我改用\n会怎样? -
@Mr.T:我胡乱猜测。
我这里没有安装 Cygwin 编译器。 -
FWIW,你有评论
//displayString += "。这也是函数中的注释,还是代码?看起来像一个未终止的字符串文字。
标签: c++ cygwin warnings newline