[英]Stuck on C++ code using Structure
Currently I am stuck and not sure where to go from here... 目前,我被困住了,不知道从这里去哪里...
I'm supposed to write a program that declares a struct to store the data for a player. 我应该编写一个程序,该程序声明一个用于存储播放器数据的结构。 Then declare an array of 10 components to store the data for 10 baseball players.
然后声明一个由10个组件组成的数组,以存储10个棒球运动员的数据。
The program reads from a file and stores the data for ten baseball players, including player's team, name of player, number of homeruns, batting average, and runs batted in. 该程序从一个文件中读取并存储十名棒球运动员的数据,包括运动员的球队,运动员的姓名,全垒打的数量,击球的平均值以及击球的次数。
The program prints out a menu (in a loop, so this can be done again and again) giving the user a choice to: 该程序打印出一个菜单(循环显示,因此可以一次又一次地完成),使用户可以选择:
Before the program terminates, give the user the option to store the data in an output file. 在程序终止之前,请为用户提供将数据存储在输出文件中的选项。
If anyone has ANY TIPS OR ADVICE I will be very grateful... I'm fairly new to coding in C++ and just stuck here... thank you in advance... 如果有人有任何建议或建议,我将不胜感激...我对使用C ++进行编码还很陌生,只是停留在这里...提前谢谢...
#include <iostream>
#include <fstream>
using namespace std;
struct BaseballID
{
string teamName, playerFirstName, playerLastName;
int homeRuns, rbi;
double batting_average;
};
int main()
{
BaseballID listofplayers[10];
ifstream infile;
infile.open("/users/AlecKleyer/Desktop/computer science term 2/BaseballStats.txt");
if (!infile)
{
cout << "Error opening file!";
return 0;
}
for (int j = 0; j < 10; j++) {
infile >> listofplayers[j].teamName >> listofplayers[j].playerFirstName >> listofplayers[j].playerLastName >>listofplayers[j].homeRuns >> listofplayers[j].rbi >> listofplayers[j].batting_average;
}
cout << "Please Type The Following Letter: ";
cout << "\n(A) For All Users and Stats";
cout << "\n(B) For A Specific Player";
cout << "\n(C) Print out for specific team";
cout << "\n(D) Update stats for a player";
char input = 0;
cin >> input;
if (input == 'A' || input == 'a') {
printInfoAll(*listofplayers[]);
}
if (input == 'B' || input == 'b') {
}
if (input == 'C' || input == 'c') {
}
if (input == 'D' || input == 'd') {
}
}
void printInfoAll(listofplayers1[])
{
for (int i = 0; i < 10; i++) {
cout << &listofplayers[i];
}
}
One thing you did right was making a function of printInfoAll
(even if its buggy). 您做对的一件事是使
printInfoAll
的功能(即使它有printInfoAll
)。 Note this will not compile and there might be more errors. 请注意,这将无法编译,并且可能会有更多错误。
#include <iostream>
#include <fstream>
using namespace std;
struct BaseballID
{
string teamName, playerFirstName, playerLastName;
int homeRuns, rbi;
double batting_average;
};
void printInfo(const BaseballID& id) {
cout << id.teamName << " " << id.playerFirstName // and so on!!!!
<< "\n"; // and a newline.
}
void printInfoAll(const BaseballID listofplayers1[]) // we need a type and a paramter
{
for (int i = 0; i < 10; i++) {
cout << printInfo(listofplayers[i]); // you
}
}
void printMenu() { // factor out the components in easy reusable parts.
cout << "Please Type The Following Letter: ";
cout << "\n(A) For All Users and Stats";
cout << "\n(B) For A Specific Player";
cout << "\n(C) Print out for specific team";
cout << "\n(D) Update stats for a player";
}
int main()
{
BaseballID listofplayers[10]; // consider using std::vector instead
// from here
ifstream infile;
infile.open("/users/AlecKleyer/Desktop/computer science term 2/BaseballStats.txt");
if (!infile.isOpen()) // I think the isOpen is needed.
{
cout << "Error opening file!";
return 0;
}
for (int j = 0; j < 10; j++) {
infile >> listofplayers[j].teamName >> listofplayers[j].playerFirstName >> listofplayers[j].playerLastName >>listofplayers[j].homeRuns >> listofplayers[j].rbi >> listofplayers[j].batting_average;
// hmm you trust your indata I don't check for errors here.
}
// to here should be a function, but then you need to restructure a bit more
printMenu();
char input = 0;
cin >> input;
switch(input) {
case 'A': case 'a':
printInfoAll(*listofplayers[]);
break;
case 'B': // and so on
// code for 'B' and 'b'
break;
....
default:
printMenu();
break;
}
// at this point you will find out you should have put it all in a loop.
return EXIT_SUCCESS;
}
The reason for adding const to the parameters is so that the user of the functions can see it promises not to change the values. 向参数添加const的原因是使函数的用户可以看到它保证不会更改值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.