我的字符串数组一次打印出前两个字符串

时间:2019-12-10 15:28:53

标签: c++ arrays string

我正在编写一个名为TypeBetter的程序,共有3种模式。简单来说,您必须准确输入25个单词,以免分数下降。除了中型句子(10)和硬性段落(5)外,Medium和Hard都一样。

“简易”模式效果很好,但是当我尝试执行“中等”或“困难”模式时,程序会立即打印出数组的第一个和第二个元素。

#include <iostream>
#include <string>
using namespace std;

int main()
{
    bool repeat = true;

    string easy[25] =
    {
        "toast", "finger", "sound", "word", "technology", "pace", "mouse", 
        "family", "laptop", "clock", "banana", "stand", "dinner", "skeleton", "oatmeal","binder",
        "social","peace" , "spoon", "door", "postmates", "freedom", "speech", "example", "solution"
    };

    string medium[10] =
    { 
        "The fox jumped over the fence at a very fast pace.",
        "Sticks and stones may break my bones but words may never hurt me.", 
        "And his name is... John Cena!",         
        "Find a problem, then create a solution using technology.", 
        "Be here on time and ready to learn.",
        "The weather is very nice today, but I wish the wind would die down.",
        "Hi hungry, I am dad!", 
        "What time can you come over?",         
        "My school laptop is running unbearably slow.", 
        "I stared into the distance: Who was he? What did he want?"
    };

    string hard[5] =
    {
        "On July 16, 1969, the Apollo 11 spacecraft launched from the Kennedy 
        Space Center in Florida. Its mission was to go where no human being had gone before—the moon! 
        The crew consisted of Neil Armstrong, Michael Collins, and Buzz Aldrin. The spacecraft landed on 
        the moon in the Sea of Tranquility, a basaltic flood plain, on July 20, 1969. The moonwalk took 
        place the following day. On July 21, 1969, at precisely 10:56 EDT, Commander Neil Armstrong 
        emerged from the Lunar Module and took his famous first step onto the moon’s surface. He 
        declared, That’s one small step for man, one giant leap for mankind. It was a monumental 
        moment in human history!", 

        "Oceans and lakes have much in common, but they are also quite 
        different. Both are bodies of water, but oceans are very large bodies of salt water, while lakes 
        are much smaller bodies of fresh water. Lakes are usually surrounded by land, while oceans are 
        what surround continents. Both have plants and animals living in them. The ocean is home to the 
        largest animals on the planet, whereas lakes support much smaller forms of life. When it is time 
        for a vacation, both will make a great place to visit and enjoy.",

        "Last year was the first time 
        I had ever been the new kid at school. For the first four days, I was completely alone. I don’t 
        think I even spoke to a single person. Finally, at lunch on the fifth day, Karen Watson walked 
        past her usual table and sat down right next to me. Even though I was new, I had already figured 
        out who Karen Watson was. She was popular. Pretty soon, all of Karen’s friends were sitting 
        there right next to me. I never became great friends with Karen, but after lunch that day, it 
        seemed like all sorts of people were happy to be my friend. You cannot convince me that Karen 
        did not know what she was doing. I have a great respect for her, and I learned a great deal 
        about what it means to be a true leader.", 

        "The Blue Whales just played their first baseball 
        game of the new season; I believe there is much to be excited about. Although they lost, it was 
        against an excellent team that had won the championship last year. The Blue Whales fell behind 
        early but showed excellent teamwork and came back to tie the game. The team had 15 hits and 
        scored 8 runs. That’s excellent! Unfortunately, they had 5 fielding errors, which kept the 
        other team in the lead the entire game. The game ended with the umpire making a bad call, and if 
        the call had gone the other way, the Blue Whales might have actually won the game. It wasn’t a 
        victory, but I say the Blue Whales look like they have a shot at the championship, especially if 
        they continue to improve.", 

        "Sunset is the time of day when our sky meets the outer space solar 
        winds. There are blue, pink, and purple swirls, spinning and twisting, like clouds of balloons 
        caught in a whirlwind. The sun moves slowly to hide behind the line of horizon, while the moon 
        races to take its place in prominence atop the night sky. People slow to a crawl, entranced, 
        fully forgetting the deeds that must still be done. There is a coolness, a calmness, when the 
        sun does set." 
    };

    int diff;
    char again;
    double score;
    double count = 0;

    cout << "Welcome to TypeBetter. Please select your difficulty (1=Easy, 2=Medium, 3=Hard):\n";

    cin >> diff;

    cout << "Type in the words to the best of your ability! You are graded on accuracy, not time.\n";

    while (repeat)
    {
        if (diff == 1)
        {
            string easyHold;
            for (int i = 0; i < 25; i++)
            {
                cout << easy[i] << endl;
                cin >> easyHold;
                if (easy[i] == easyHold)
                {
                    count++;
                }
            }
            score = (count / 25)*100;
        }
        else if (diff == 2)
        {
            string medHold;
            for (int i = 0; i < 10; i++)
            {
                cout << medium[i] << endl;
                getline(cin, medHold);
                if (medium[i] == medHold)
                {
                    count++;
                }
            }
            score = (count / 10)*100;
        }
        else if (diff == 3)
        {
            string hardHold;
            for (int i = 0; i < 5; i++)
            {
                cout << hard[i] << endl;
                getline(cin, hardHold);
                if (hard[i] == hardHold)
                {
                    count++;
                }
            }
            score = (count / 5)*100;
        }
        else
        {
            cout << "Unknown value.";
        }

        cout << "Your score was " << score << "%! Would you like to play again? (Y/N)\n";

        cin >> again;

        if (again == 'y' || again == 'Y')
        {
            repeat = true;
        }
        else
        {
            repeat = false;
        }
    }



    system("PAUSE");
    return 0;
}

1 个答案:

答案 0 :(得分:1)

我花了大约15分钟来格式化您的代码。然后我意识到您的错误是由于您没有在变量hard中定义字符串而引起的。将hard的定义更改为以下内容。那应该可以修复您的代码。

    string hard[5];

    hard[0] = "On July 16, 1969, the Apollo 11 spacecraft launched from the Kennedy \
    Space Center in Florida. Its mission was to go where no human being had gone before—the moon! \
    The crew consisted of Neil Armstrong, Michael Collins, and Buzz Aldrin. The spacecraft landed on \
    the moon in the Sea of Tranquility, a basaltic flood plain, on July 20, 1969. The moonwalk took \
    place the following day. On July 21, 1969, at precisely 10:56 EDT, Commander Neil Armstrong \
    emerged from the Lunar Module and took his famous first step onto the moon’s surface. He \
    declared, That’s one small step for man, one giant leap for mankind. It was a monumental \
    moment in human history!";

    hard[1] = "Oceans and lakes have much in common, but they are also quite different. Both are bodies of water,\
    but oceans are very large bodies of salt water, while lakes \
    are much smaller bodies of fresh water. Lakes are usually surrounded by land, while oceans are \
    what surround continents. Both have plants and animals living in them. The ocean is home to the \
    largest animals on the planet, whereas lakes support much smaller forms of life. When it is time \
    for a vacation, both will make a great place to visit and enjoy.";

    hard[2] = "Last year was the first time I had ever been the new kid at school. For the first four days, \
    I was completely alone. I don’t \
    think I even spoke to a single person. Finally, at lunch on the fifth day, Karen Watson walked \
    past her usual table and sat down right next to me. Even though I was new, I had already figured \
    out who Karen Watson was. She was popular. Pretty soon, all of Karen’s friends were sitting \
    there right next to me. I never became great friends with Karen, but after lunch that day, it \
    seemed like all sorts of people were happy to be my friend. You cannot convince me that Karen \
    did not know what she was doing. I have a great respect for her, and I learned a great deal \
    about what it means to be a true leader.";

    hard[3] = "The Blue Whales just played their first baseball game of the new \
    season; I believe there is much \
    to be excited about. Although they lost, it was against an excellent team that had won the \
    championship last year. The Blue Whales fell behind \
    early but showed excellent teamwork and came back to tie the game. The team had 15 hits and \
    scored 8 runs. Thats excellent! Unfortunately, they had 5 fielding errors, which kept the \
    other team in the lead the entire game. The game ended with the umpire making a bad call, and if\
    the call had gone the other way, the Blue Whales might have actually won the game. It wasnt a \
    victory, but I say the Blue Whales look like they have a shot at the championship, especially if \
    they continue to improve.";

    hard[4] = "Sunset is the time of day when our sky meets the outer space solar \
    winds. There are blue, pink, and purple swirls, spinning and twisting, like clouds of balloons \
    caught in a whirlwind. The sun moves slowly to hide behind the line of horizon, while the moon \
    races to take its place in prominence atop the night sky. People slow to a crawl, entranced, \
    fully forgetting the deeds that must still be done. There is a coolness, a calmness, when the \
    sun does set.";