读取可能包含空格的字符串

时间:2018-01-07 14:55:25

标签: c++

//============================================================================
// Name        : Lab.cpp
// Author      : Neil Shah
// Version     : 001
// Copyright   : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================

#include <iostream>
#include <string>

using namespace std;

int main() {
string stateCapital[50][50] = {
        {"Alabama" , "Montgomery"},
        {"Alaska" , "Juneau"},
        {"Arizona" , "Phoenix"},
        {"Arkansas" , "Little Rock"},
        {"California" , "Sacramento"},
        {"Colorado" , "Denver"},
        {"Connecticut" , "Hartford"},
        {"Delware" , "Dover"},
        {"Florida" , "Tallahessee"},
        {"Georgia" , "Atlanta"},
        {"Hawaii" , "Honolulu"},
        {"Idaho" , "Boise"},
        {"Illinois" , "Springfield"},
        {"Indiana" , "Indianapolis"},
        {"Iowa" , "Des Moines"},
        {"Kansas" , "Topeka"},
        {"Kentucky" , "Frankfurt"},
        {"Louisiana" , "Baton Rouge"},
        {"Maryland" , "Annapolis"},
        {"Massachusetts" , "Boston"},
        {"Michigan" , "Lansing"},
        {"Minnesota" , "Saint Paul"},
        {"Mississippi" , "Jackson"},
        {"Missouri" , "Jefferson City"},
        {"Montana" , "Helena"},
        {"Nebraska" , "Lincoln"},
        {"Nevada" , "Carson City"},
        {"New Hampshire", "Concord"},
        {"New Jersey" , "Trenton"},
        {"New Mexico" , "Santa Fe"},
        {"New York" , "Albany"},
        {"North Carolina" , "Raleigh"},
        {"North Dakota" , "Bismarck"},
        {"Ohio" , "Columbus"},
        {"Oklahoma" , "Oklahoma City"},
        {"Oregon" , "Salem"},
        {"Pennsylvania" , "Harrisburg"},
        {"Rhode Island" , "Providence"},
        {"South Carolina" , "Columbia"},
        {"South Dakota" , "Pierre"},
        {"Tennessee" , "Nashville"},
        {"Texas" , "Austin"},
        {"Utah" , "Salt Lake City"},
        {"Vermont" , "Montpelier"},
        {"Virginia" , "Richmond"},
        {"Washington" , "Olympia"},
        {"West Virginia" , "Charleston"},
        {"Wisconsin" , "Madison"},
        {"Wyoming" , "Cheyenne"}
};

int correctCount = 0;
string capital;

for (int i = 0; i < 50; i++){
    cout << "What is the capital of " << stateCapital[i][0] << "?" << flush;
    cin >> capital;

    if (capital==stateCapital[i][1]){
        cout << "Your answer is correct" << endl;
    }
    else {
        cout << "The correct answer should be " << stateCapital[i][1] << endl;
    }
};

cout << "The correct count is " << correctCount << endl;
return 0;

}

我在JAVA中尝试了这个程序,但同样的错误来了。谈到C ++,项目运行正常,但在我的代码中某处有一些我无法弄清楚的错误。以下是程序的输出(注意:不是整个输出)

What is the capital of Alabama?Montgomery
Your answer is correct
What is the capital of Alaska?Juneau
Your answer is correct
What is the capital of Arizona?Phoenix
Your answer is correct
What is the capital of Arkansas?Little Rock
The correct answer should be Little Rock
What is the capital of California?The correct answer should be Sacramento
What is the capital of Colorado?

由于某些名字中的空格,是否有问题?我编写这个程序的方式有问题吗?我在JAVA版本中也遇到了完全相同的问题。

2 个答案:

答案 0 :(得分:2)

通过删除,cin.operator>>()在空格(空格,制表符,换行符)处切断。要阅读整行,请使用std::getline()

// Replacement for "cin >> capital"
std::getline(std::cin, capital);

或者,既然您已声明using namespace std;,则可以省略std::前缀:

getline(cin, capital);

这可以解决您的问题。

答案 1 :(得分:0)

这是有效的,将cin替换为getline

#include <iostream>
#include <string>

using namespace std;

int main() {
string stateCapital[50][50] = {
        {"Alabama" , "Montgomery"},
        {"Alaska" , "Juneau"},
        {"Arizona" , "Phoenix"},
        {"Arkansas" , "Little Rock"},
        {"California" , "Sacramento"},
        {"Colorado" , "Denver"},
        {"Connecticut" , "Hartford"},
        {"Delware" , "Dover"},
        {"Florida" , "Tallahessee"},
        {"Georgia" , "Atlanta"},
        {"Hawaii" , "Honolulu"},
        {"Idaho" , "Boise"},
        {"Illinois" , "Springfield"},
        {"Indiana" , "Indianapolis"},
        {"Iowa" , "Des Moines"},
        {"Kansas" , "Topeka"},
        {"Kentucky" , "Frankfurt"},
        {"Louisiana" , "Baton Rouge"},
        {"Maryland" , "Annapolis"},
        {"Massachusetts" , "Boston"},
        {"Michigan" , "Lansing"},
        {"Minnesota" , "Saint Paul"},
        {"Mississippi" , "Jackson"},
        {"Missouri" , "Jefferson City"},
        {"Montana" , "Helena"},
        {"Nebraska" , "Lincoln"},
        {"Nevada" , "Carson City"},
        {"New Hampshire", "Concord"},
        {"New Jersey" , "Trenton"},
        {"New Mexico" , "Santa Fe"},
        {"New York" , "Albany"},
        {"North Carolina" , "Raleigh"},
        {"North Dakota" , "Bismarck"},
        {"Ohio" , "Columbus"},
        {"Oklahoma" , "Oklahoma City"},
        {"Oregon" , "Salem"},
        {"Pennsylvania" , "Harrisburg"},
        {"Rhode Island" , "Providence"},
        {"South Carolina" , "Columbia"},
        {"South Dakota" , "Pierre"},
        {"Tennessee" , "Nashville"},
        {"Texas" , "Austin"},
        {"Utah" , "Salt Lake City"},
        {"Vermont" , "Montpelier"},
        {"Virginia" , "Richmond"},
        {"Washington" , "Olympia"},
        {"West Virginia" , "Charleston"},
        {"Wisconsin" , "Madison"},
        {"Wyoming" , "Cheyenne"}
};

int correctCount = 0;
string capital;

for (int i = 0; i < 49; i++)
   {
     cout << "What is the capital of " << stateCapital[i][0] << "?" << flush;
     getline(cin, capital);

       if (capital==stateCapital[i][1])
         {
           cout << "Your answer is correct" << endl;
         }
    else 
         {
           cout << "The correct answer should be " << stateCapital[i][1] << endl;
         }
   }

cout << "The correct count is " << correctCount << endl;
return 0;
}