本文介绍了我需要结构的帮助的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿所以我正在编写一个关于结构的程序,它使用用户输入的字段,如年龄,性别等,我想知道我将如何能够输出字符串中的所有名称



我尝试了什么:



  #include   <   iostream  >  
#include < string >
#include < fstream >
#include < ; 例外 >
#include < stdexcept >

使用 namespace std;

// 学习结构
struct 学生{
int 年龄;
string sex,name,f_name,l_name,matricno,religion,bloodgroup;

};
// 查看文件中记录的函数。
Student readfunc( ){
fstream things( studentData.txt,ios :: | ios :: out | ios :: app);

学生假人;

cout<< 输入matric number<< endl ;
cin>> dummystud.matricno;

cout<< 输入年龄<< endl;
cin>> dummystud.age;

cout<< 输入名字<< endl ;
cin>> dummystud.f_name;

cout<< 输入姓氏<< endl ;
cin>> dummystud.l_name;

cout<< 输入性别<< endl ;
cin>> dummystud.sex;
if (dummystud.sex!= M || dummystud.sex!= F){
throw runtime_error( 出了问题 );
}


cout<< 什么是宗教<< ENDL;
cin>> dummystud.religion;
cout<< 输入血型<< endl;
cin>> dummystud.religion;

return dummystud;

}

int main()
{
int n;
cout<< 输入学生人数<< endl;
cin>> n;

学生[n];
int i;

for (i = 0 ; i< n; i ++){
cout<< 输入学生的<<(i + 1)< ;< 以下详细信息\ n \ n<< endl;
student [i] = readfunc();
}

return 0 ;
}
解决方案



Hey so I am writing a program on structs and it uses user input for fields like age, sex etc and i was wondering how i would be able to output all the names in the string

What I have tried:

#include <iostream>
#include <string>
#include <fstream>
#include <exception>
#include <stdexcept>

using namespace std;

// learning structs
struct Student{
    int age;
    string sex,name,f_name,l_name,matricno,religion,bloodgroup;

    };
    // function to view a record in a file.
Student readfunc(){
   fstream things("studentData.txt", ios::in | ios::out | ios::app);

    Student dummystud;

    cout<<"Enter matric number"<<endl;
    cin>> dummystud.matricno;

    cout<<"Enter age"<<endl;
    cin>> dummystud.age;

    cout<<"Enter first name"<<endl;
    cin>>dummystud.f_name;

    cout<<"Enter last name"<<endl;
    cin>>dummystud.l_name;

    cout<<"Enter the sex"<<endl;
    cin>>dummystud.sex;
        if (dummystud.sex!="M" || dummystud.sex!="F"){
            throw runtime_error("Something went wrong");
        }


    cout<<"What is the religion"<<endl;
    cin>>dummystud.religion;
    cout<<"Enter the blood group"<<endl;
    cin>>dummystud.religion;

    return dummystud;

}

int main()
{
   int n;
   cout<<"ENTER THE NUMBER OF STUDENTS"<<endl;
   cin>>n;

   Student student[n];
   int i;

   for(i=0; i <n; i++){
    cout<<"Enter Student's "<<(i+1)<<"details below\n\n"<<endl;
    student[i]=readfunc();
   }

    return 0;
}
解决方案



这篇关于我需要结构的帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-22 22:05
查看更多