我的字符串数组一次打印出前两个字符串
Posted
技术标签:
【中文标题】我的字符串数组一次打印出前两个字符串【英文标题】:My string array is printing out the first two strings at once 【发布时间】:2019-12-10 15:28:53 【问题描述】:我正在编写一个名为 TypeBetter 的程序,它有 3 种模式。简单地说,有 25 个单词,你必须准确地输入这些单词,以免你的分数下降。除了 medium 使用句子 (10) 和 hard 使用段落 (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;
【问题讨论】:
“输入”运算符>>
读取空格分隔的单词。要获得多个单词,我建议您read whole lines。
这最终解决了输入问题,但现在我的程序(中等和硬)一次打印前两个段落/句子。知道为什么吗?
@Samuel DeZube 请格式化您的代码。
我做对了吗,再一次,这是新手。
@SamuelDeZube 如果您使用的是 Visual Studio,那么您的 IDE 中已经有一个内置调试器。尝试查找 youtube 视频或其他如何使用 c++ 的 Visual Studio 调试器
【参考方案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.";
【讨论】:
我试图将我的代码更改为此,但没有任何改变。问题在于中型和硬型阵列。现在将重新格式化。以上是关于我的字符串数组一次打印出前两个字符串的主要内容,如果未能解决你的问题,请参考以下文章