使用Structure卡在C++代码上
Stuck on C++ code using Structure
目前我被卡住了,不确定从这里到哪里。。。
我应该编写一个程序,声明一个结构来存储玩家的数据。然后声明一个由10个组件组成的数组来存储10名棒球运动员的数据。
该程序从文件中读取并存储十名棒球运动员的数据,包括球员所在的球队、球员姓名、全垒打数、打击率和打击率。
该程序打印出一个菜单(在一个循环中,这样就可以一次又一次地完成),让用户可以选择:
- 打印出所有用户和统计信息
- 打印出特定玩家的统计数据
- 打印出特定团队的所有数据
- 更新特定玩家的数据(更改其中一个统计数据)
在程序终止之前,为用户提供将数据存储在输出文件中的选项。
如果有人有任何提示或建议,我将不胜感激。。。我对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];
}
}
您做得对的一件事是生成printInfoAll
的函数(即使它有缺陷)。请注意,这将不会编译,并且可能会有更多错误。
#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;
}
在参数中添加const的原因是,函数的用户可以看到它承诺不更改值。
相关文章:
- C++我的数学有什么问题,为什么我的代码不能正确循环
- 代码在main()中运行,但在函数中出现错误
- 在VS代码中交叉编译Windows与Linux上的MinGW的SDL程序
- 编译包含字符串的代码时遇到问题
- 我在c++代码中生成了一个运行时#3异常
- 如何在linux终端中同时编译和运行c++代码
- 为cl.exe(Visual Studio代码)指定命令行C++版本
- 在Linux for Windows上编译C++代码时出错
- 我的字符计数代码计算错误.为什么
- 孤立代码块在结构中引发异常
- 在编译C++代码(具有dlib和opencv)到WASM时面临问题
- 为什么我的C#代码在调用回C++COM直到Task时会暂停.等待/线程.加入
- 处理小于cpu数据总线的数据类型.(c++转换为机器代码)
- 此代码是否违反一个定义规则
- 为什么我的代码在输出中增加了93天
- 我的简单if-else语句是如何无法访问的代码
- 使用动态分配的数组会导致代码分析发出虚假的C6386缓冲区溢出警告
- 为什么在这个代码结束循环中没有得到结束
- 在c代码之间共享数据的最佳方式
- 这个指针和内存代码打印是什么?我不知道是打印垃圾还是如何打印我需要的值