I've a text file : Random.txt which comprises of

Jade
12MS234
Male
18
Rocky
12MS324
Male
18
Marx
12MS632
Male
18

Now in my program i've a class

class stud
(
char name[10]
char reg[10]
char gender[10]
int age
)

Now I've to write a code in c++, where i've to read the given data and store this into 3 objects of stud class(array of objects) ...then further i've to manipulate the above stored data...
I think i'm getting error while storing...variables are showing random characters...
Please give me the code.for this program..in C++

Recommended Answers

All 6 Replies

We're not really into giving people complete answers here; we tend to help you fix it yourself.

You say that your code is giving errors. Let's see the code!

I would expect that your file reading code in its simplest possible form would look something like this:

ifstream fileToRead("someFileName");

fileToRead >> objects[0].name;
fileToRead >> objects[0].reg;
fileToRead >> objects[0].gender;
fileToRead >> objects[0].age;

fileToRead >> objects[1].name;
fileToRead >> objects[1].reg;
fileToRead >> objects[1].gender;
fileToRead >> objects[1].age;

fileToRead >> objects[2].name;
fileToRead >> objects[2].reg;
fileToRead >> objects[2].gender;
fileToRead >> objects[2].age;

which you would then have improved with loops and file checking and all that sort of thing.

So what does your code do?

#include<iostream>
#include<fstream>
#include<cstdlib>
#include<cstring>
using namespace std;
class ab
{
public:
string name,reg,gend,age;
void show();

};
void ab::show()
{
    cout<<"\nName:\t"<<name;
    cout<<"\nRegistration #:\t"<<reg;
    cout<<"\nGender:\t"<<gend;
    cout<<"\nAge:\t"<<age;
    cout<<endl;
}
int main()
{
    fstream f;
    f.open("Random.txt",ios::in);
    f.seekg(0);
    ab s1[3];
    string word[30],line;
    int a=0,b,c=0,i;
    if(f.is_open())
    {
        while(!f.eof())
        {
            getline(f,line);
            word[a]=line;
            a++;
        }

    }
    else
    cout<<"\nUnable To Open File\n";
    b=a;
    cout<<"\nNow We Are In Main!\n";
    for(a=0; a<b; a++)
    cout<<word[a]<<endl;
    cout<<"\nNow Merging Data In Class\n";
    for(i=0; i<3; i++)
    {
        s1[i].name=word[c]; c++;
        s1[i].reg=word[c]; c++;
        s1[i].gend=word[c]; c++;
        s1[i].age=word[c]; c++;
    }
    cout<<"\nDisplaying Class\n";
    for(i=0; i<3; i++)
    s1[i].show();
    f.close();
    system("pause");
    return 0;
}

I've done like this in order to make it run. Here i've first stored in string, then manipulated it to save in class data members. Isn't there any way of making it directly initializing with the values given in file.

Isn't there any way of making it directly initializing with the values given in file.

After a fashion, yes. I'd move the parsing aspects into an input operator for the class that handles all of the grunt work. Then you can simply say f >> s1[i];. As a simple example:

#include <fstream>
#include <iostream>
#include <istream>
#include <ostream>
#include <string>
#include <vector>

class Student {
    std::string name;
    std::string id;
    std::string gender;
    int age;
public:
    Student(std::string name = "", std::string id = "", std::string gender = "", int age = 0) :
        name(name), id(id), gender(gender), age(age)
    {}

    friend std::ostream& operator<<(std::ostream& out, const Student& obj);
    friend std::istream& operator>>(std::istream& in, Student& obj);
};

std::ostream& operator<<(std::ostream& out, const Student& obj)
{
    return out << obj.name << "(" << obj.id << "): " << obj.age << " " << obj.gender;
}

std::istream& operator>>(std::istream& in, Student& obj)
{
    in >> obj.name;
    in >> obj.id;
    in >> obj.gender;
    in >> obj.age;

    return in;
}

int main()
{
    std::ifstream in("test.txt");

    if (in) {
        std::vector<Student> students;
        Student temp;

        while (in >> temp) {
            students.push_back(temp);
        }

        for (auto x : students) {
            std::cout << x << '\n';
        }
    }
}

When a class manages its own data internally rather than forcing the calling code to understand it, life is simpler. This is especially important if your class internals might change because then the calling code wouldn't also have to change when that happens.

You code creates an array of 30 strings, then reads from a file into a different string named line, and then copies line into the array. Why? Why are you using the string named line? It's crazy. If you want to read into the array, just do it directly.

You've got a bunch of variables with single letter names. What's a for? What's b for? What's c for? Do yourself a favour and write the code so that when you read it, you can tell what it's doing.

f.seekg(0); What is this for? You've just opened the file. You're at the start of the file.

Isn't there any way of making it directly initializing with the values given in file.

Yes. We call such things "constructors". You can create objects in such a way that they take parameters and do things. That would be the C++ way to do this. However, that's a bit beyond you right now.

The problem here isn't that you can't write some C++. It's that your solution is badly thought out and confusing. You need to stop coding and think about what you're trying to do. This is programming; thinking about a problem in such a way that the solution you come up with lends itself to efficent, elegant, well-written code. All the rest is memorising syntax and learning how to use your tools.

@Moschops ...
The reason for which i've done indirectly is because i don't know why but i wasn't able to get the reading thing directly and as to why i've declared so much variables is because this was just one of the module of my entire program, other variables were part of the whole code. Since, they had nothing to do with the error, i didn't removed them & i know what variable i've defined and what they actually denote.

Since, they had nothing to do with the error, i didn't removed them & i know what variable i've defined and what they actually denote.

We're not psychic. You show us mystery code and ask us what's wrong with it. If we were psychic, we could guess what your variables are for when you choose to give them meaningless names. Sadly, we're not psychic, so we can't.

When you write code, you're not just writing for the compiler. You're writing for humans to read it, including yourself in a month's time when you wonder why you named a variable a.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.