程序不工作C ++ [英] Program not working C++

查看:85
本文介绍了程序不工作C ++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

无法找到错误,只接受1名学生的输入,当你输入第二名的名字时,它只是跳过所有内容并显示每个除了第一个之外的奇怪数字。这里是代码(ps我是c ++的初学者)

not being able to find the error, only takes the input of 1 student, and when you enter the name of the second it just skips everything and shows weird numbers as a result of each except for the first one.here's the code(ps I'm a beginner to c++)

#include<iostream>
#include<stdio.h>
using namespace std;
struct student{
    int age, marks[5], clas, avg;
    char name[10], res;
};
int main(){
    student p[5];
    cout<<"enter name, class, age, and marks in 5 subjects :\n";
    for(int i=0; i<5; i++){                   //input data for each student
        cout<<"student "<<i+1<<"\n";
        gets(p[i].name);
        cin>>p[i].clas;
        cin>>p[i].age;
        for(int j=0; j<5; j++){
            cin>>p[i].marks[j];
        }
    }
    for(int k=0; k<5; k++){     //calculate average                                           
        p[k].avg=0;
        for(int f=0; f<5; f++){
            p[k].avg+=p[k].marks[f];
        }
        p[k].avg/=5;
    }
    for(int x=0; x<5; x++){           //calculate result on basis of average                              
        if(p[x].avg>=40){
            p[x].res='P';
        }
        else{
            p[x].res='F';
        }
    }
    cout<<"RESULT :\n";     //output data for each student
    for(int m=0; m<5; m++){                                     
        cout<<"STUDENT "<<m+1<<"\n";
        puts(p[m].name);
        cout<<"age "<<p[m].age;
        cout<<"class "<<p[m].clas;
        for(int n=0; n<5; n++){
            cout<<p[m].marks[n]<<endl;
        }
        cout<<"average "<<p[m].avg;
        cout<<"result "<<p[m].res;
    }
}





我的尝试:



起初,我将cin.getline更改为gets但仍然无法正常工作,它没有正确输入,结果是随机数,除了显示结果的第一个学生是正确的



What I have tried:

at first, i changed cin.getline to gets but still does not work properly, it does not take the input properly and the result is random numbers, except for the first student whose result displayed is correct

推荐答案

嘿伙计,你有 C ++ ,不是吗?

Hey man, you have C++, don't you?
#include <iostream>
#include <vector>
#include <array>
#include <sstream>
using namespace std;


class Student
{
  string name;
  int clas;
  int age;
  array<int, 5> mark;

public:
  double average();
  char result(double average);

  bool fromUser(const string & prompt);
  void toUser(const string & header);
};

bool Student::fromUser(const string & prompt)
{
  cout << prompt << endl;
  cout << "name: ";
  cin >> name;

  cout << "class: ";
  cin >> clas;
  cout << "age: ";
  cin >> age;
  cout << "now enter 5 marks\n";

  for ( auto & m : mark)
    cin >> m;

  return cin.good();
}

double Student::average()
{
  double avg = 0.0;
  for (const auto &  m : mark)
    avg += m;

  avg /= mark.size();
  return avg;
}

char Student::result(double average)
{
  return (average >= 40.0 ? 'P': 'F');
}

void Student::toUser(const string & header)
{
  cout << header << endl;
  cout << "name: " << name << endl;
  cout << "class: " << clas << endl;
  cout << "age: " << age << endl;
  cout << "marks: " << endl;
  for (const auto & m : mark)
    cout << "  " << m << endl;

  double avg = average();
  cout << "average: " << avg << endl;
  cout << "result: " << result(avg) << endl;
}

int main()
{
  vector <Student> student;
  constexpr int STUDENTS=2;

  for (int i=0; i<STUDENTS; ++i)
  {
    ostringstream oss;
    oss << "please enter data for student " << (i+1) ;
    Student s;
    s.fromUser(oss.str());
    student.push_back(s);
  }

  cout << "******************************" << endl;

  for (int i=0; i<student.size(); ++i)
  {
    ostringstream oss;
    oss << endl << "data of student " << (i+1);
    student[i].toUser(oss.str());
  }
}


您需要使用调试器。搜索一些在线教程,了解开发IDE中的用法。



我猜您的代码中的类型不匹配。
You need to work with the debugger. Search some online tutorials for the usage in your development IDE.

I guess that you mismatched the types in your code.


这篇关于程序不工作C ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆