对c++比较陌生,我在编写代码时遇到了问题。当我编译并运行程序时,终端窗口只是黑色的,但我注意到我可能会导致这个问题的3个警告,但我不确定如何清除它们。
错误如下,都在。cc文件中声明:
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 problemn";
//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 namen";
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
};
在
中将循环变量声明为plain(即signed
)int
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()
返回unsigned类型 - 在
for (int i = 0; i < static_cast<unsigned int>(counter); i++)
i
是int
,你奇怪地通过转换counter
来强制类型不一致。
修复警告很简单,不比较有符号类型和无符号类型。我得到一个额外的警告-prnt()
中的变量i
是未使用的。
运行代码,它等待输入:
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
可能唯一的问题是它正在等待您没有提供的输入-用户提示可能是有序的。
你也可以使用调试器——当你的代码像这样停滞时,你可以中断它的执行,看看它在代码中的位置。