簡體   English   中英

C ++程序需要幫助進行調試

[英]C++ Program need help to debug

#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,對我來說很好用。

cin >> 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;
}

std :: cin默認情況下忽略空格。

要在輸入中包含空格,請嘗試:

getline(cin, team_name);

這將拾取一行中的所有字符,直到按Enter。 這在

#include<string>

閱讀choice后,需要刷新std::cin緩沖區

#include <limits>
//...

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

請參閱此問題以獲取詳細說明。

另外,如果要從標准輸入中讀取帶空格的字符串,請替換為:

cin >> team_name;

有了這個:

getline(cin, team_name);

正如其他答案中已經提到的那樣。 這次無需刷新std::cin ,因為您已經閱讀了整行。

最后,從您的games.txt刪除多余的換行符:

5
SD Mines
21 17
Northern State
... 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM