如何修复IF / ELSE语句中重复cout的输出

时间:2019-04-06 00:35:01

标签: c++

我有一个完整的班级代码;它根据用户要求的字符数创建一个随机字符串,然后允许用户指定他们是否要在字符串中查找一对特定的字符。最后一部分基于if / else语句,该语句给出位置,或者假定告诉他们字符串中没有对。

我的问题是,当给一对查找时,如果它在字符串中,则给出更正的语句,但是,它也给出else语句,重复几次。如果该对不在字符串中,则它给出正确的else语句,但是重复cout几次。我不知道如何解决这个问题。

这是我的代码和输出的屏幕截图。

image

image

#include <iostream>
#include <stdlib.h>
#include <time.h>
using namespace std;

int main() {

    int i=0, n;
    char alphabet[26];
    char RandomStringArray [100];
    char Ltr1, Ltr2;
    srand(time(0));

    cout <<"How many letters do you want in your random string (no less than 0, no more than 100): ";
    cin >> n;

    for (int i=0; i<=25; i++)
            alphabet[i] = 'a' + i;

    while(i<n) {
        int temp = rand() % 26;
        RandomStringArray[i] = alphabet[temp];
        i++;
    }

    for(i=0; i<n; i++)
        cout<<RandomStringArray[i];
    cout<<"\n";

    cout<<"What letter pair would you like to find? ";
    cin>>Ltr1>>Ltr2;

    for (i=0; i<n; i++)
        if (Ltr1==RandomStringArray[i] && Ltr2== RandomStringArray[i+1]){
            cout<<"The pair is in the string starting at character number "<<i+1<<" in the string. \n";
        }
        else if (Ltr1!=RandomStringArray[i] && Ltr2!= RandomStringArray[i+1])
            cout<<"no";

    return 0;
}

2 个答案:

答案 0 :(得分:0)

由于您将if...else构造放置在for循环中,因此每次都会对其进行评估。这意味着对于第一个条件不成立的每个实例,都会执行else子句,从而导致重复“否”消息。

答案 1 :(得分:0)

您的最终for循环从头到尾循环遍历随机字符串中的每个字符,这对于搜索是很好的,但它输出的是比较每个 个字符的结果,不是你想要的。直到循环结束,才显示任何输出,例如:

for (i = 0; i < (n-1); i++)
{
    if (RandomStringArray[i] == Ltr1 && RandomStringArray[i+1] == Ltr2)
        break;
}

if (i < (n-1))
    cout << "The pair is in the string starting at character number " << i+1 << " in the string.\n";
else
    cout << "The pair is not found in the string.\n";

我建议将搜索结果包装在一个函数中,例如:

int findPair(const char *str, int len, char Letter1, char Letter2)
{
    for (i = 0; i < (len-1); i++)
    {
        if (str[i] == Letter1 && str[i+1] == Letter2)
            return i;
    }
    return -1;
}

...

i = findPair(RandomStringArray, n, Ltr1, Ltr2);
if (i != -1)
    cout << "The pair is in the string starting at character number " << i+1 << " in the string.\n";
else
    cout << "The pair is not found in the string.\n";

话虽这么说,因为无论如何您都在使用C ++,所以应该使用完整的C ++,而不是C和C ++的混合,例如:

#include <iostream>
#include <string>
#include <random>
#include <algorithm>

int main() {

    int n;
    char alphabet[26];
    std::string RandomString;
    char LtrPair[2];

    std::generate(std:begin(alphabet), std::end(alphabet), [ch = 'a'] () mutable { return ch++; } );

    std::cout << "How many letters do you want in your random string: ";
    std::cin >> n;

    std::random_device rd;
    std::mt19937 gen(rd());
    std::uniform_int_distribution<> dis(0, 25);

    RandomString.resize(n);
    std::generate(RandomString.begin(), RandomString.end(), [&](){ return alphabet[dis(gen)]; };

    std::cout << RandomString << "\n";

    std::cout << "What letter pair would you like to find? ";
    cin >> LtrPair[0] >> LtrPair[1];

    auto pos = RandomString.find(LtrPair, 0, 2);
    if (pos != std::string::npos)
        std::cout << "The pair is in the string starting at character number " << pos+1 << " in the string.\n";
    else
        std::cout << "The pair is not found in the string.\n";

    return 0;
}