nothing
Jul 29, 2013 at 4:26pm UTC
nothing
Last edited on Jul 29, 2013 at 5:53pm UTC
Jul 29, 2013 at 4:48pm UTC
Not only this is invalid C++ (array size must be const), the value of
a
is undefined when the array is constructed.
Why aren't you using
std::vector<bio>
?
Jul 29, 2013 at 5:08pm UTC
OK got it
Jul 29, 2013 at 5:09pm UTC
You created an empty vector
vector<bio>arrays;
So it has no elements and as the consequence this code
getline (cin,arrays[i] .name);
is invalid.
Jul 29, 2013 at 5:16pm UTC
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
using namespace std;
struct bio{
string name;
string job;
int age;
string address;
char gender;
string hobby;
};
int main()
{
int a;
bio erin;
vector<bio>arrays;
char e, f, g;
ofstream fout;
fout.open ("1.txt" , ofstream::out | std::ofstream::app);
cout << "Sa veta do te regjistrosh? \n" ;
cin >> a;
for (int i=0;i<a;i++)
{
cout << "Fut emrin dhe mbiemrin\n" ;
cin >> e;
getline (cin,erin.name);
cout << "\nFut pozicionin e punes\n" ;
getline (cin,erin.job);
cout << "\nFut moshen\n" ;
cin >> erin.age;
cout << "\nFut adresen\n" ;
cin >>f;
getline (cin,erin.address);
cout << "\nFut gjinine\n" ;
cin >> erin.gender;
cout << "\nFut preferencat\n" ;
cin >> g;
getline (cin,erin.hobby);
erin.name=e+erin.name;
erin.address=f+erin.address;
erin.hobby=g+erin.hobby;
arrays.push_back(erin.name);
arrays.push_back(erin.job);
arrays.push_back(erin.age);
arrays.push_back(erin.address);
arrays.push_back(erin.hobby);
fout << arrays;
}
fout.close();
return 0;
}
Jul 29, 2013 at 5:16pm UTC
how to write... This is wrong too
Jul 29, 2013 at 5:23pm UTC
This part of the updated code is also invalid.
1 2 3 4 5 6
arrays.push_back(erin.name);
arrays.push_back(erin.job);
arrays.push_back(erin.age);
arrays.push_back(erin.address);
arrays.push_back(erin.hobby);
fout << arrays;
Jul 29, 2013 at 5:27pm UTC
Vectors are lame... I fixed the problem
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
struct bio{
string name;
string job;
int age;
string address;
char gender;
string hobby;
};
int main()
{
ofstream fout;
fout.open ("1.txt" , ofstream::out | std::ofstream::app);
cout << "Sa veta do te regjistrosh? \n" ;
int a;
char e, f, g;
cin >> a;
bio arrays[a];
for (int i=0;i<a;i++)
{
cout << "Fut emrin dhe mbiemrin\n" ;
cin >> e;
getline (cin,arrays[i].name);
cout << "\nFut pozicionin e punes\n" ;
getline (cin,arrays[i].job);
cout << "\nFut moshen\n" ;
cin >> arrays[i].age;
cout << "\nFut adresen\n" ;
cin >>f;
getline (cin,arrays[i].address);
cout << "\nFut gjinine\n" ;
cin >> arrays[i].gender;
cout << "\nFut preferencat\n" ;
cin >> g;
getline (cin,arrays[i].hobby);
arrays[i].name=e+arrays[i].name;
arrays[i].address=f+arrays[i].address;
arrays[i].hobby=g+arrays[i].hobby;
fout << arrays[i].name << "\n" ;
fout << arrays[i].job << "\n" ;
fout << arrays[i].age << "\n" ;
fout << arrays[i].address<< "\n" ;
fout << arrays[i].gender << "\n" ;
fout << arrays[i].hobby << "\n\n" ;
}
fout.close();
return 0;
}
Jul 29, 2013 at 5:32pm UTC
I do not see the necessity to use the vector in your code. It would be enough to define one object of the struct type that in loop to write its fields in the file.
Last edited on Jul 29, 2013 at 5:33pm UTC
Topic archived. No new replies allowed.