问题描述
对 C++ 相对较新,我在运行代码时遇到了问题。当我编译并运行程序时,终端窗口是黑色的,但我注意到我的 3 个警告可能导致了这个问题,但我不知道如何清除它们。
Line 31 Warning C4018 '<': signed/unsigned mismatch
Line 60 Warning C4018 '<': signed/unsigned mismatch
Line 57 Warning C4018 '>=': signed/unsigned mismatch
这是我程序的头文件:
#ifndef H_JOSEPHUS
#define H_JOSEPHUS
#include <iostream>
#include <sstream>
#include <string>
#include <list>
#include <algorithm>
using namespace std;
#define NO_LETS 26 // no of letters in English alphabet
#define NO_ITEMS 12 // no of items printed on single line
// struct for input arguments
struct args {
unsigned N,// no of initial people
M,// count to eliminate person
K; // frequency of printouts
};
// class to generate name tags for people
class SEQ {
private:
string id; // name tag for person
unsigned size,nd; // no of people,no of digits in name tags
// returns no of digits in name tags
unsigned find_nd ( const double& sz ) {
if ( ( sz / NO_LETS ) <= 1 ) return 2;
else return ( find_nd ( sz / NO_LETS ) + 1 );
}
public:
// constructor for name-tag generator
SEQ ( const unsigned& s = 1 ) : size ( s ) {
double sz = ( double ) size / 9; nd = find_nd ( sz );
id = string ( nd,'A' ); id [ nd - 1 ] = '1';
}
// returns next name tag in sequence
string operator ( ) ( ) {
string tmp = id; int i = nd - 1;
if ( id [ i ] < '9' ) id [ i ]++;
else {
id [ i ] = '1'; bool flag = true;
for ( i--; i >= 0 && flag; i-- )
if ( id [ i ] < 'Z' ) { id [ i ]++; flag = false; }
else id [ i ] = 'A';
}
return tmp;
}
};
// reads and initializes all input arguments
void init_vals(list<string>& L,args& in);
// prints all name tags for remaining people after elimination
void print_list ( const list < string >&,const unsigned& );
#endif
这是我的 .cc 文件:
#include "josephus.h"
#include <iostream>
using namespace std;
int M;
int K;
void prnt(list < string >& L)
{
int c = 1;
int i = 0;
for (list<string>::iterator it = L.begin(); it != L.end(); it++)
{
cout << *it << " ";
if (c == 12)
{
c = 1; cout << "\n";
}
c++;
}
cout << "\n";
}
void init_vals(list<string>& L,args& in)
{
cin >> in.N >> in.M >> in.K;
cout << "\nThe values of n m k are: " << in.N << " " << in.M << " " << in.K;
list<string>::iterator it;
for (int i = 0; i < in.N; i++) // line 31
{
string s;
stringstream ss;
ss << char('A' + i / 9);
ss >> s;
s += char(((i + 1) % 9) + '0');
L.push_back(s);
//cout<<L[i]<<" ";
}
}
void print_list(const list<string>&L,const unsigned&cnt)
{
cout << "\nAt begining of the joseph problem\n";
//bool arr[L.size()]={true};
list < string > l = L;
prnt(l);
int counter = 0;
int sz = L.size() - 1;
int c = cnt;
while (c < sz)
{
counter += M - 1;
if (counter >= l.size()) counter %= l.size(); // line 57
list<string>::iterator it = l.begin();
//it = it
for (int i = 0; i < static_cast<unsigned int>(counter); i++) // line 60
it++;
l.erase(it);
c++;
if ((c) % K == 0)
{
cout << "\n After deleting K name\n";
prnt(l);
}
}
cout << "\nLast remaining person: " << *(l.begin());
}
int main()
{
list<string> L;
struct args in;
init_vals(L,in);
M = in.M;
K = in.K;
print_list(L,0);
}
非常感谢任何指导!
解决方法
in::N
在此处声明为 unsigned
:
struct args {
unsigned N,// no of initial people
M,// count to eliminate person
K; // frequency of printouts
};
您将循环变量声明为普通(即 signed
)int
in
for (int i = 0; i < in.N; i++)
并且 std::list::size
是未签名的,但您将其与已签名的 int
counter
进行比较:
if (counter >= l.size()) counter %= l.size();
,
所有警告都是微不足道的类型不匹配,是您的程序无法输出的原因。
- 在
for (int i = 0; i < in.N; i++)
args::N
是unsigned
,而i
是int
。 - 在
if (counter >= l.size()) counter %= l.size();
counter
是int
而std::list::size()
返回无符号类型。 - 在
for (int i = 0; i < static_cast<unsigned int>(counter); i++)
i
是int
,并且您通过强制转换counter
奇怪地强制类型不一致。
修复警告是一个简单的问题,不比较有符号和无符号类型。我收到一个额外的警告 - i
中的变量 prnt()
未使用。
运行代码,它等待输入:
cin >> in.N >> in.M >> in.K;
我输入了 1 2 3
,它输出:
The values of n m k are: 1 2 3
At begining of the joseph problem
A1
也许唯一的问题是它正在等待您没有提供的输入 - 用户提示可能是有序的。
您也可以使用调试器 - 当您的代码像这样停顿时,您可以中断其执行并查看它在代码中的位置。