C ++程序需要帮助进行调试

时间:2016-10-20 05:36:55

标签: c++ string cin spacing

#include <iostream>
#include <fstream>
#include <iomanip>
#include <stdlib.h>
using namespace std;

struct football_game

{
    string visit_team;
    int home_score;
    int visit_score;
};

void printMenu();

int main()
{
    int i, totalValues = 0;
    ifstream inputFile;
    string temp = "";

    inputFile.open("games.txt");

    if (!inputFile)
    {
        cout << "Error opening Input file!" << endl;
        exit(101);
    }

    inputFile >> totalValues;
    getline(inputFile, temp);

    cout << "                            *** Football Game Scores *** " << endl << endl;
    cout << " * Total Number of teams : " << totalValues << endl << endl;

    football_game* records = new football_game[totalValues];

    // while (!inputFile.eof())
    // {// == NULL) {

    for (i = 0; i < totalValues; i++)
    {
        getline(inputFile, records[i].visit_team);
        cout << records[i].visit_team << endl;
        inputFile >> records[i].home_score >> records[i].visit_score;
        cout << records[i].home_score << "  " << records[i].visit_score << endl;
        getline(inputFile, temp);
    }
    //}
    cout << endl;


    int choice = 0;
    int avg_home_Score = 0;
    int avg_visit_Score = 0;

    printMenu(); // prints menu


    cout << "Please Enter a choice from the Menu : ";
    cin >> choice;
    cout << endl << endl;

    while (true)
    {
        switch (choice)
        {
            case 1:
                cout << "               Score Table " << endl;
                cout << "         ***********************" << endl << endl;

                cout << "        VISIT_TEAM"
                     << "      "
                     << " HIGH_SCORE"
                     << "   "
                     << "VISIT_SCORE " << endl;
                cout << "       -----------"
                     << "      "
                     << "-----------"
                     << "  "
                     << "------------" << endl;


                for (int i = 0; i < totalValues; i++)
                {
                    cout << '|' << setw(18) << left << records[i].visit_team << "    " << '|'
                         << setw(7) << right << records[i].home_score << "     " << '|' << setw(7)
                         << right << records[i].visit_score << "     " << '|' << endl;
                }

                cout << endl << endl << endl;

                break;
            case 2:
            {
                string team_name;
                cout << "Enter the Team Name  :  ";
                cin >> team_name;
                for (int i = 0; i < totalValues; i++)
                {
                    if (records[i].visit_team == team_name)
                    {

                        cout << "        VISIT_TEAM"
                             << "      "
                             << " HIGH_SCORE"
                             << "   "
                             << "VISIT_SCORE " << endl;
                        cout << "       -----------"
                             << "      "
                             << "-----------"
                             << "  "
                             << "------------" << endl;
                        cout << '|' << setw(18) << left << records[i].visit_team << "    " << '|'
                             << setw(7) << right << records[i].home_score << "     " << '|'
                             << setw(7) << right << records[i].visit_score << "     " << '|'
                             << endl;
                    }
                }
                cout << endl;
                break;
            }
            case 3:
            {
                for (int i = 0; i < totalValues; i++)
                    avg_home_Score += records[i].home_score;
                cout << "Average home_score: " << (avg_home_Score / totalValues) << endl << endl;
                break;
            }
            case 4:
            {
                for (int i = 0; i < totalValues; i++)
                    avg_visit_Score += records[i].visit_score;
                cout << "Average visit_score: " << (avg_visit_Score / totalValues) << endl << endl;
                break;
            }
            default:
            {
                cout << "Please enter valid input !!" << endl;
                break;
            }
        }
        printMenu();
        cin >> choice;
    }
    return 0;
}

void printMenu()
{
    cout << "                      Menu Options                 " << endl;
    cout << "                    ================               " << endl;
    cout << "     1. Print Information of all Games[Table Form] " << endl;
    cout << "     2. Print Information of a Specific Game       " << endl;
    cout << "     3. Print Average points scored by the Home Team during season" << endl;
    cout << "     4. Print Average points scored against the Home Team" << endl << endl << endl;
}

这是我正在使用的输入文件

games.txt

5

SD Mines

21 17

Northern State

10 3

BYU

10 21

Creighton

14 7

Sam Houston State

14 24

当我从输出屏幕使用第二个选项(特定游戏的打印信息)时, 它要求我输入团队名称,当我输入团队名称时。 例如:SD Mines它给了我一个错误,但当我输入没有空格的团队名称时:BYU它对我来说很好。

3 个答案:

答案 0 :(得分:0)

  

cin&gt;&gt; TEAM_NAME;

仅输入空间。

您可能希望使用cin.getline()将空格分隔的字符串作为输入。

一个展示同样的小程序:

#include <iostream>
#include <string>

int main ()
{
    std::string name;

    std::cout << "Please, enter your full name: ";
    std::getline (std::cin,name);
    std::cout << "Name is : , " << name << "!\n";

    return 0;
}

答案 1 :(得分:0)

std :: cin默认忽略空格。

要在输入中包含空格,请尝试:

getline(cin, team_name);

这将获取一行中的所有字符,直到您按Enter键。这可以在

中找到
#include<string>

答案 2 :(得分:0)

阅读choice后,您需要to flush the std::cin buffer

#include <limits>
//...

cin >> choice;
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');

有关详细说明,请参阅this question

此外,如果您想从标准输入中读取带空格的字符串,请将其替换为:

cin >> team_name;

用这个:

getline(cin, team_name);

正如其他答案中已提到的那样。这次不需要刷新std::cin,因为您已经阅读了整行。

最后,从games.txt

中删除额外的换行符
5
SD Mines
21 17
Northern State
...