Click here to Skip to main content
15,891,431 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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:

C++
#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;
}
Posted
Updated 21-Oct-18 21:10pm
v2

C++
for(i=0; i <n; i++)
{
    cout << student[i].name << endl;
    cout << student[i].f_name << endl;
    cout << student[i].l_name << endl;
    cout << student[i].matricno << endl;
    cout << student[i].religion << endl;
    cout << student[i].bloodgroup << endl;
    cout << student[i].age << endl;
    cout << student[i].sex << endl;
}
 
Share this answer
 
Comments
CPallini 22-Oct-18 3:11am    
5.
Richard already showed you the solution. In addition you could overload the ostream insertion operator (<<), e.g.
C++
#include <iostream>
#include <string>

using namespace std;

struct Student
{
  int age;
  string sex,name,f_name,l_name,matricno,religion,bloodgroup;
};

// ostream operator  << overloading
ostream & operator << ( ostream & os, const Student & st)
{
  os << st.name << "\n";
  os << st.f_name << "\n";
  os << st.l_name << "\n";
  os << st.age << "\n";
  os << st.matricno << "\n";
  os << st.religion << "\n";
  os << st.bloodgroup << "\n";
  return os;
}


int main()
{
  Student foo{ 32, "M", "Foo-Bar", "Foo", "Bar", "A12", "Foobist","A"};
  cout << foo << endl;// now you can feed cout with a Student struct instance
}
 
Share this answer
 
Comments
Rick York 22-Oct-18 10:49am    
I would be more inclined to add an output method to the structure. Along with an input method that prompts for data entry.
CPallini 22-Oct-18 12:21pm    
Well, you are very free to do that. :-)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900