在收集正确的用户数据并将其存储到结构数组中时遇到问题



这是我的输出,底部的数字应该是用户输入的信息。我试图从用户那里收集信息,并将其实现到一个结构中,该结构中有四个变量,这些变量位于一个结构内,并将它存储到我的类中的数组中。但当我试图打印出数据时,它会给我错误的数字,这意味着它要么没有正确收集数据,要么我的显示功能出了问题。任何帮助都将不胜感激,谢谢!

这是我的主要

int main() {
Workoutarr a;
int pushsize = 0;
cout << "this is a demo of making a progessive overload planner for a push day at the gym" << endl;
pusharr(pushsize);
cout << "This is displaying the push array entered by the user : " << endl;
display(a,pushsize);

这是我的cpp,我的所有功能都位于这里

void pusharr(int& c){
Workoutarr push;
char continu;
string workoutName;
int weight,set,rep;
cout << "Would you like to add workouts to your push day routine : use Y/N" <<endl;
cin >> continu; 
for (int i = 0 ; continu == 'Y' || continu == 'y' ; i++){
cout <<"What is the name of the workout you would like to add? : " <<endl;
cin >> workoutName;
push.pusharr[i].name = workoutName;
cout <<"How much weight you you like to start the increment from :" << endl;
cin >> push.pusharr[i].weight;
push.pusharr[i].reps = 8;
push.pusharr[i].sets = 3;
c++;
cout << "Would you like to contine ? : Y/N"  << endl;
cin >> continu;
}
cout << "We have completed adding to the workout array." << endl;
}
void display(const Workoutarr &a,int c){
for (int i = 0; i < c ; i++){
cout << setw(20) << a.pusharr[i].name << setw(20) << a.pusharr[i].weight <<setw(20) <<  a.pusharr[i].sets << setw(20) << a.pusharr[i].reps << endl;
}
}

这是我的头文件

struct workout {
string name;
int weight;
int sets;
int reps;
};
//type of workout
//what are there goals for working out
class Workoutarr{
public:
int displayCount = 0;
workout pusharr [50];
};
//sets
void repCount();
void pusharr(int& c);
void display(Workoutarr a,int num);
void pusharr(int& c){
Workoutarr push;

这将输入数据推送到局部变量push,该变量在函数退出后被销毁。你想要像你为display做的一样的东西

void pusharr(Workoutarr& push, int& c){
// Workoutarr push;

最新更新