[C ++篮球队转换声明

问题描述 投票:0回答:1

main.cpp中的switch语句出现问题。无论用户输入什么,它都会返回默认情况。当输入'X'时,它确实退出程序,但是在暂停之前首先返回默认的大小写(“ Invalid option!...”)。问题可能出在我的team.h或team.cpp中,如果需要的话可以包括在内。

这是我的主要:

//-----------------------------------------------------------
//                      CS 215 – Spring 2020
//                          Project 3
//-----------------------------------------------------------
// Author: Julianne Wright
// Section: 002
// Description: Application that allows the user to enter,
//              view, search and sort statistics on a team
//              of basketball players. The program starts
//              by reading data from a file called
//              “bballin.txt”, and ends by writing data
//              to a file called “bballout.txt”. 
//-----------------------------------------------------------
#include <iostream>
#include "team.h"
using namespace std;

//------------------------------------------------------------------
//                           printLogo
//------------------------------------------------------------------
void printLogo() {
    cout << "-----------------------------------------------------\n";
    cout << "                BASKETBALL STATS SUPREME \n";
    cout << "                   by Julianne Wright \n";
    cout << "-----------------------------------------------------\n";
} // printLogo()

//------------------------------------------------------------------
//                           askMenuOption
//------------------------------------------------------------------
// Returns: The menu option entered by the user.
// Prints a menu and allows the user to enter a value. It extracts
// and capitalizes the first letter of the user's input.
//------------------------------------------------------------------
char askMenuOption() {
    string choice;
    cout << endl;
    cout << "P - Print team \n";
    cout << "A - Add game \n";
    //cout << "N - New player \n";
    cout << "D - Display player \n";
    cout << "R - Remove player\n";
    cout << "N - Sort list by name \n";
    cout << "J - Sort list by jersey number\n";
    cout << "X - Exit\n";
    cout << "Enter selection: ";
    cin >> choice;
    return (char)(toupper(choice[0])); // upper case of first character
} // askMenuOption()

//------------------------------------------------------------------
//                           main
//------------------------------------------------------------------
int main() {
    team t;             // create a team object
    char option;        // menu option

    // print logo and read data from input file on startup
    printLogo();
    t.read();

    // repeat menu until eXit is selected
    do {
        option = askMenuOption();
        switch (option) {
        case 'P': t.print();        break;
        case 'A': t.addGame();      break;
        case 'D': t.displayPlayer();break;
        case 'N': t.sortByName();   break;
        case 'J': t.sortByJersey(); break;
        case 'R': t.removePlayer(); break;
        case 'X': t.write();        break;
        default:  cout << "Invalid option! Try again!\n";
        }
    } while (option != 'X');

    system("pause");
    return 0;
} // main()
c++ switch-statement
1个回答
-2
投票

您能提供给我team.h文件,以便我为您测试解决方案吗?

© www.soinside.com 2019 - 2024. All rights reserved.