如何忽略空/空格字符?

时间:2013-10-17 17:28:41

标签: c++

我的输出略有偏差。现在我的输出看起来像这样:

 Is this correct??
 My Project works!!!!
 *************** I don't know

我需要它看起来像这样:

Is this correct???
My Project works!!!!!
****************I don't know

空字符/空格对我来说搞乱了所有3行,我不知道在这个例子中如何忽略null。

我的输入文件如下所示:

63-27.8 18spaces L Is this correct
33fill 21b5 l My Project works
42??? 28-09ng5 R I don't know

这是我的代码:

#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>


using namespace std;

int main()
{
    string filename;
    int a, b, c; // ASCII values for fill character
    int aw, bw, cw; // width values for setwidth
    char ch1, ch2, ch3; // character for right or left justification
    string s1, s2, s3; // strings

    ifstream inData;

    cout << "please enter the location of the file you wish to input: " << endl;
    getline(cin, filename);

    inData.open(filename.c_str());


    inData >> a;
    inData.ignore(100, ' ');
    inData >> aw;
    inData.ignore(100, ' ');
    inData >> ch1;
    getline(inData, s1);
    inData >> b;
    inData.ignore(100, ' ');
    inData >> bw;
    inData.ignore(100, ' ');
    inData >> ch2;
    getline(inData, s2);
    inData >> c;
    inData.ignore(100, ' ');
    inData >> cw;
    inData.ignore(100, ' ');
    inData >> ch3;
    getline(inData, s3);
    {
        if  (ch1 == 'l' || ch1 == 'L')
            cout << left;
        else if (ch1 == 'r' || ch1 == 'R')
            cout << right;
        else
            cout << "error" << endl;
    }
    cout << setw(aw) << setfill(char(a)) << s1 << endl;
    {
        if  (ch2 == 'l' || ch2 == 'L')
            cout << left;
        else if (ch2 == 'r' || ch2 == 'R')
            cout << right;
        else
            cout << "error" << endl;
    }
    cout << setw(bw) << setfill(char(b)) << s2 << endl;
    {
        if  (ch3 == 'l' || ch3 == 'L')
            cout << left;
        else if (ch3 == 'r' || ch3 == 'R')
            cout << right;
        else
            cout << "error" << endl;
    }
    cout << setw(cw) << setfill(char(c)) << s3 << endl;

    return 0;
}

你会如何纠正这个问题?

0 个答案:

没有答案