-6

I'm completely new to C++ and currently I'm trying to read very basic text file which look like this:

Dr John Doe
British
2
Soccer
Swimming

and my expected output should look like:

My information
Name: John Doe
Nationality: British
I have 2 hobbies:
1. Soccer
2. Swimming

My header file:

#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <cstdlib>
#include <ctime>
#include <vector>
using namespace std;

const int MAX = 80;
const int MAXNO = 5;
enum Title {Miss, Mrs, Mr, Dr, Unknown};
struct Date
{
 int day; 
 int month;
 int year;
};

struct MyInfo
{
    char name [MAX];
    char national [MAX];
    int noOfHobbies;
    char hobby [MAXNO][MAX];
};

void getMyInfo (fstream& , char[] , MyInfo&);
void displayMyInfo (MyInfo);

My functions:

#include "Lab_1.h"
void getMyInfo (fstream& afile,char fileName[], MyInfo& x) {
    afile.open (fileName);

    if (!afile)
    {
        cout << "Binary file " << fileName << " opened for creation failed" << endl;
        exit (-1);
    }

    cout << "\n" << "Begin reading of " << fileName << endl;

    string line;
    while(getline(afile, line))
    {
        afile >> x.national;
        afile >> x.noOfHobbies;*/
        if (afile >> x.name >> x.national >> x.noOfHobbies) {
            cout << "Name: " << x.name << ", " 
                << "National: " << x.national << ", " 
                << "noOfHobbies: " << x.noOfHobbies << ", " 
                << endl;
        }
    }
}    

void displayMyInfo (MyInfo x) {

}

My main function:

#include "Lab_1.h"

int main () {
    fstream afile;
    MyInfo x;
    string fileName;

    getMyInfo(afile,"textfile.txt",x);

    //displayMyInfo(x);

    afile.close ();

}

The above code output nothing because I just put everything I understand over the forum with similar question. Since I'm already stuck for 1 day even though I've already done a lot of research but most of them suggest to use vector which I'm not familiar with at this moment, so can someone give me a solution to this problem? Thank you very much for your help in advance.

Resolution
  • 759
  • 1
  • 7
  • 18

1 Answers1

3

Random act of madness kindness:

Live On Coliru

#include <fstream>
#include <set>

struct Person {
    std::string name;
    std::string nationality;
    std::set<std::string> hobbies;

    friend std::istream& operator>>(std::istream& is, Person& into) {
        size_t n = 0;
        if (getline(is, into.name) &&
            getline(is, into.nationality) &&
            is >> n && is.ignore(1024, '\n')) 
        {
            while (n--) {
                std::string hobby;
                if (getline(is, hobby))
                    into.hobbies.insert(hobby);
                else
                    is.setstate(std::ios::failbit);
            }
        }
        return is;
    }
};


#include <iostream>

int main() {
    std::ifstream ifs("input.txt");

    Person p;
    if (ifs >> p) {
        std::cout << "My information\n";
        std::cout << p.name << "\n";
        std::cout << p.nationality << "\n";
        std::cout << "I have " << p.hobbies.size() << " hobbies:\n";
        size_t counter = 0;
        for(auto const& hobby : p.hobbies) {
            std::cout  << ++counter << ". " << hobby << "\n";
        }
    } else { 
        std::cerr << "Parse failure\n";
    }
}
sehe
  • 374,641
  • 47
  • 450
  • 633
  • Thank you for your help. I'll try to understand your code and if have any question I'll ask for your help – Resolution Jul 07 '15 at 13:03
  • Or maybe, you'll search for information first :) http://en.cppreference.com/w/cpp/ or http://stackoverflow.com/search?q=something – sehe Jul 07 '15 at 13:04
  • Thanks for your tips. This is urgent but after this exercise I'll digging into C++ more to understand better before asking. – Resolution Jul 07 '15 at 13:06
  • @Resolution oh god. What have I done. You know you can't turn this is as your assignment, right. Learning is the urgent bit. – sehe Jul 07 '15 at 13:08
  • No worry, I'm not a copycat and I'll write my own application based on your answer, I need to understand as well since I cannot ask stackoverflow during my test, right :) – Resolution Jul 07 '15 at 13:14
  • Actually, your answer is somewhere far away from what I've learned :) but thanks for your help though. – Resolution Jul 07 '15 at 13:19
  • 3
    Maybe you should let your prof see this code. So they can know what C++ actually looks like (Hint: It's not C with `std::cout`). It's not your fault. You can always read a good book on C++ yourself, like we all did – sehe Jul 07 '15 at 13:21
  • Hello @sehe, would you mind suggest me a good book that I should read as you've already mentioned above? – Resolution Jul 10 '15 at 11:32
  • @Resolution We have a good list here: http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list - it has some reviews and backgrounds and categorization – sehe Jul 10 '15 at 11:33
  • Thank you, I'll take a look through that list :) – Resolution Jul 10 '15 at 11:34