2014-11-05 29 views
0

我工作的一个验证问题,如果环路检查在开始和结束的管道,并确保有32个有效的字符(字符有效是:和|)与字符串是否确认循环

我想知道为什么我的程序没有正确读取32个字符输入的if语句。这是我到目前为止。

void checkitout(string validate) 
{ 
    string check; 
    check = validate; 

    if ((check.length() == 31) && 
     (check.substr(0,1) == "|") && 
     (check.substr(31,1) == "|")) 
    { 
    cout << "is this running?"; 

    for (int i = 0; i < 31; i++) 
    { 
     cout << "for loop running"; 

     if (!(check.substr(i, 1) == ":") || !(check.substr(i, 1) == "|")) 
     { 
     cout << "Please enter acceptable barcode."; 
     return; 
     } 
    } 
    } 
    else 
    { 
    cout << "else Please enter acceptable barcode"; 
    } 
} 

我是新来的,但我认为我走在正确的轨道上。 couts将测试以查看循环是否正常工作。它正好处于其他状态。以下是样本输入

||:| ::: |:|:|| :::::: ||:| :: | ::: |||

一如既往,任何想法如何更好地做到这一点非常感谢。

+0

你的问题是什么? – user657267 2014-11-05 08:42:09

+0

@JoachimPileborg它用C++ 11 iirc定义好了吗?应该返回一个不可修改的空字符。 – user657267 2014-11-05 08:43:30

+0

这是总共32个字符,所以应该是0-31的权利? – Buttons 2014-11-05 08:44:29

回答

1

你串有32 lenght,因此如果条件是因为check.length()== 31 另外在你的循环中,如果条件需要的“& &”而不是假的“| |“,因为你希望它既不是”|“也不是“:”是不可接受的条形码。

更改以粗体标出。

void checkitout(string validate) 
{ 
    string check; 
    check = validate; 
    string one = check.substr(4,1); 
    cout << (check.substr(4,1) == one) << endl; 

    if ((check.length() == **32**) && 
     (check.substr(0,1) == "|") && 
     (check.substr(31,1) == "|")) 
    { 
    cout << "is this running?"; 

    for (int i = 0; i < 31; i++) 
    { 
     cout << "for loop running"; 

     if (!(check.substr(i, 1) == ":") **&&** !(check.substr(i, 1) == "|")) 
     { 
     cout << "Please enter acceptable barcode."; 
     return; 
     } 
    } 
    } 
    else 
    { 
    cout << "else Please enter acceptable barcode"; 
    } 
}