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

问题描述 投票:0回答:2

我有一个完整的代码供我上课;它根据用户要求的字符数创建一个随机字符串,然后允许用户指定是否要在字符串中查找特定字符对。最后一部分基于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;
}
c++
2个回答
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;
}

0
投票

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

© www.soinside.com 2019 - 2024. All rights reserved.