无限循环,同时打印表

时间:2017-02-22 08:11:21

标签: c++ printing ascii

我目前正在尝试打印一个包含ID,用户名和密码的用户表。我完成了让董事会看起来像这样:

ID|Username|Password
--+--------+-----------
1 |jtar    |123456
2 |mordecai|11111111111

正如您所看到的,我希望能够将表格的单元格缩放到每列的内容的最大大小。问题是,由于某种原因,我得到了无限循环的打印空间。我在下面的代码中找到了循环的位置。我还添加了评论以进一步澄清。

目前,我计算在打印每个用户数据后应添加多少空格。如果数据字符串大小小于相应的矩阵列标记(即,如果用户名的大小小于字符串的大小"用户名"),则打印大小不等的空格。 如果现在数据的大小超过相应标记的大小,则打印每个列大小的最大数据与当前用户的数据大小的差异。

我在每个for循环中添加了一个或者一个操作,以避免产生if-else语句和两个for循环。

#include <iostream>
#include <vector>
#include <sstream>
#include <string>
using namespace std;

class User {
  int id;
  string username;
  string password;
  public:
  //Constructor-Destructor
  User(const int _id, const string _username, const string _password) : id(_id),
  username(_username), password(_password) { }
  ~User(){}

  //Accessors- Mutators
  int getId() {return id;}
  string getUsername() {return username;}
  string getPassword() {return password;}
};

const string intToStr(const int target)
{
    stringstream stream;
    stream << target;
    return stream.str();
}

int main() {
    vector<User*> userList;
    int maxIdLength = 0, maxUsernameLength = 0, maxPasswordLength = 0;
    //create users
    for(int i = 0; i < 20; i++) {
        userList.push_back(new User(i, "user" + i, "genericpass"));
    }

    //find maximum lengths
    for (vector<User*>::iterator it = userList.begin(); (it != userList.end()); it++) {

        if (maxIdLength < intToStr( (*it)->getId() ).size())
            maxIdLength = intToStr((*it)->getId()).size();

        if (maxUsernameLength < (*it)->getUsername().size())
            maxUsernameLength = (*it)->getUsername().size();

        if (maxPasswordLength < (*it)->getPassword().size())
            maxPasswordLength = (*it)->getPassword().size();
    }

    //print headlines
    cout << "ID";
    for (int i = 0; i < maxIdLength - 2; i++)
        cout << " ";
    cout << "|Username";
    for (int i = 0; i < maxUsernameLength - 8; i++)
        cout << " ";
    cout << "|Password";
    for (int i = 0; i < maxPasswordLength - 8; i++)
        cout << " ";
    cout<<endl;

    //print cosmetic line
    cout << "--";
    for (int i = 0; i < maxIdLength - 2; i++)
        cout << "-";
    cout << "+--------";
    for (int i = 0; i < maxUsernameLength - 8; i++)
        cout << "-";
    cout << "+--------";
    for (int i = 0; i < maxPasswordLength - 8; i++)
        cout << "-";
    cout << endl;

    for (vector<User*>::iterator it = userList.begin(); (it != userList.end()); it++) {//start printing the users
    //Infinite Loop bug in the second conditional argument of the for loops
            cout << (*it)->getId();//id

            for (int i = 0; ( i < maxIdLength - intToStr((*it)->getId()).size() ) || ( i < ( 2 - intToStr((*it)->getId()).size() ) ); i++)
                cout << " ";
            cout << "|" << (*it)->getUsername();//username

            for (int i = 0; ( i < ( maxUsernameLength - ((*it)->getUsername()).size() ) ) || ( i < (8 - ( (*it)->getUsername() ).size()) ); i++)
                cout << " ";
            cout << "|" << (*it)->getPassword();//password
            for (int i = 0; i < maxPasswordLength - (*it)->getPassword().size() || i < 8 - (*it)->getPassword().size(); i++)
                cout << " ";
        cout<<endl;
    }

    while(!userList.empty()) {
        delete userList.back();
        userList.pop_back();
    }
    return 0;
}

2 个答案:

答案 0 :(得分:4)

通常,编译器应该抱怨,因为您要将int与未签名的size_t进行比较,因此2 - xxx.size()是无符号的,如果大小大于2,则会变大号。

请参阅example

可能的解决方案是static_cast,其大小为int。

答案 1 :(得分:1)

我可以检测到的一个问题是您要将int i添加到右值string user

for(int i = 0; i < 20; i++) {
    userList.push_back(new User(i, "user" + i, "genericpass"));
}

您应该使用itoa,将i转换为string