程序主要是接受一个名字,然后在星号框架内显示问候信息 - 即以 * 包围的 Hello!。
在示例程序中,作者使用 const int 来确定问候信息和星号之间的填充(空格)。作为练习的一部分,他们要求读者询问用户输入他们想要的填充大小。
看起来很简单,我已经要求用户输入了两个整数(int)并将它们存储起来,并更改了程序以使用这些整数。但是,当我编译时,却收到以下警告:
Exercise2-3.cpp:46:警告:有符号和无符号整数表达式之间的比较
经过一些研究,似乎是因为代码尝试将其中一个整数(int)与 string::size_type 进行比较,这是可以的。但是我想知道 - 这是否意味着我应该将其中一个整数更改为 unsigned int?明确指定我的整数是有符号还是无符号重要吗?
cout << "Please enter the size of the frame between top and bottom you would like ";
int padtopbottom;
cin >> padtopbottom;
cout << "Please enter size of the frame from each side you would like: ";
unsigned int padsides;
cin >> padsides;
string::size_type c = 0; // definition of c in the program
if (r == padtopbottom + 1 && c == padsides + 1) { // where the error occurs
这是相关的代码,变量 c
是类型为 string::size_type
的字符串类型,因为我们不知道问候语会有多长 - 但是,作者在使用 const int
时没有遇到问题,为什么现在我遇到了这个问题?此外,如果有人完成了《Accelerated C++》 - 这个问题是否会在后面的章节中解释?
我正在使用 Geany 上的 g++ 在 Linux Mint 上进行编译,如果这有帮助或区别(因为我读到确定 string::size_type
是什么时可能会有影响)。