C ++用于不执行指定次数的语句

时间:2018-02-01 23:34:53

标签: c++

我有一个随机字生成器,它应该运行input次,但它并没有这样做。如果我将input设置为10,则只会运行2-3次,在我的for语句中,我也尝试使用while语句。

这是我的代码:

#include <iostream>
#include <fstream>
#include <cstdlib>
#include <string>
#include <time.h>
#include <random>

int random_number(int min, int max) {
    int output = min + (rand() % static_cast<int>(max - min + 1));
    return output;
}

std::string random_syllable() {

    int line_num = random_number(1, 50);
    int x = 0;
    std::string line;
    std::ifstream myfile("sylla.txt");
    if (myfile.is_open()) {
        while (getline(myfile, line)) {
            x += 1;
            if (line_num == x) {
                return line;
            }
        }
        myfile.close();
    }
    return "ERROR";
}

char random_consonant() {
    int x = 0;
    char letter = char(random_number(97, 122));
    while (x == 0) {
        if ((letter == 'x') || (letter == 'q') || (letter == 'f') || (letter == 'w') || (letter == 'd')) {
            char letter = char(random_number(97, 122));
        }
        else {
            char letter = char(random_number(97, 122));
            x = 1;
        }
    }
    return letter;
}

char random_vowel() {
    char letter;
    int x = random_number(1, 5);
    switch (x) {
    case 1:
        letter = 'x';
        break;

    case 2:
        letter = 'q';
        break;

    case 3:
        letter = 'f';
        break;

    case 4:
        letter = 'w';
        break;

    case 5:
        letter = 'd';
        break;
    }
    return letter;
}

char random_letter2() {
    char letter;
    int x = random_number(1, 5);
    switch (x) {
    case 1:
        letter = 'l';
        break;

    case 2:
        letter = 'n';
        break;

    case 3:
        letter = 'r';
        break;

    case 4:
        letter = 's';
        break;

    case 5:
        letter = 'y';
        break;
    }
    return letter;
}

int main() {

    // Consonant - Syllable - (vowel) - (l,n,r,s,y) - ((')-(vowel)-(consonant)-(Syllable))

    srand(time(NULL));
    std::string word;
    int x = 0;
    double input = 0;

    input = 10;

    for (double i = 0; i < input; i = i + 1) {
        word = "";
        word = word + random_consonant();
        word = word + random_syllable();
        x = random_number(1, 2);
        if (x == 1) {
            word = word + random_vowel();
            word = word + random_letter2();
        }
        x = random_number(1, 10);
        if (x <= 3) {
            word = word + "'";
            word = word + random_vowel();
            word = word + random_syllable();
        }
        std::cout << word << std::endl;
    }

    /*
    ifstream file("FILE NAME HERE"); // syllables
    while (getline(file, line))
    count++;
    */

    system("PAUSE");

    return 0;
}

** sylla.txt只是一个充满音节的文件

1 个答案:

答案 0 :(得分:0)

您在letter中重新声明了random_consonant,因此您只需检查外部范围的letter变量,但只分配给未使用的letter变量if或else语句。删除额外的char应该可以防止:

char random_consonant() {
    int x = 0;
    char letter = char(random_number(97, 122));
    while (x == 0) {
        if ((letter == 'x') || (letter == 'q') || (letter == 'f') || (letter == 'w') || (letter == 'd')) {
            letter = char(random_number(97, 122));
        }
        else {
            letter = char(random_number(97, 122));
            x = 1;
        }
    }
    return letter;
}