Phobia
April 26th, 2007, 08:41 AM
I have been work on this all night it is my final program for my introduction to c++ and it is only suppose to be as as hard as you want it to be. It only has to have an vector of objects and one function that uses an iterator. My code compiles but it has an seg faults. I have been up all night finishing three other labs so sorry if i have bad grammar, my syntax error may be real simple but i don't have the full brain power to see what i did wrong with my code.
--Thanks in advance
[
#include <iostream>
#include <string>
#include <vector>
#include <iterator>
using namespace std;
class geterdone
{
public:
geterdone( string g_name,string g_name2,string g_age )
{
name = g_name;
name2 = g_name2;
age = g_age;
}
geterdone()
{
string g_name = "Professor ";
string g_name2 = "Fingers ";
string g_age = "300";
}
string getname(string g_name,string g_name2)
{
cout<<"Enter your first and last name: "<<endl;
istream_iterator<string> input(cin);
g_name = *input;
input++;
g_name2 = *input;
setname(g_name,g_name2);
return g_name;
}
void setname(string g_name,string g_name2)
{
name = g_name;
name2 = g_name2;
return;
}
string getage(string g_age)
{
cout<<"Enter your age: "<<endl;
cin>>g_age;
setage(g_age);
return g_age;
}
void setage(string g_age)
{
age = g_age;
return;
}
void output()
{
ostream_iterator<string> outputs(cout);
cout<<"Name: ";
*outputs = name;
cout<<" ";
*outputs = name2;
cout<<endl;
cout<<"Age: "<<age<<endl;
}
~geterdone()
{
cout<<"GITRDONE!"<<endl;
}
private:
string name,name2,age;
};
int main()
{
string nameit,nameitagin,isage;
char looper;
cout<<"Do you wish to continue\"y or n\" :";
cin>>looper;
vector<geterdone> g_person;
while (looper=='y')
{
vector<geterdone> g_person;
for (int i=0 ;i<g_person.size()+1;i++)
{
g_person[i].getname(nameit,nameitagin);
g_person[i].getage(isage);
g_person[i].output();
}
cout<<"Agin?: y or n: ";
cin>>looper;
}
return 0;
}
[
--Thanks in advance
[
#include <iostream>
#include <string>
#include <vector>
#include <iterator>
using namespace std;
class geterdone
{
public:
geterdone( string g_name,string g_name2,string g_age )
{
name = g_name;
name2 = g_name2;
age = g_age;
}
geterdone()
{
string g_name = "Professor ";
string g_name2 = "Fingers ";
string g_age = "300";
}
string getname(string g_name,string g_name2)
{
cout<<"Enter your first and last name: "<<endl;
istream_iterator<string> input(cin);
g_name = *input;
input++;
g_name2 = *input;
setname(g_name,g_name2);
return g_name;
}
void setname(string g_name,string g_name2)
{
name = g_name;
name2 = g_name2;
return;
}
string getage(string g_age)
{
cout<<"Enter your age: "<<endl;
cin>>g_age;
setage(g_age);
return g_age;
}
void setage(string g_age)
{
age = g_age;
return;
}
void output()
{
ostream_iterator<string> outputs(cout);
cout<<"Name: ";
*outputs = name;
cout<<" ";
*outputs = name2;
cout<<endl;
cout<<"Age: "<<age<<endl;
}
~geterdone()
{
cout<<"GITRDONE!"<<endl;
}
private:
string name,name2,age;
};
int main()
{
string nameit,nameitagin,isage;
char looper;
cout<<"Do you wish to continue\"y or n\" :";
cin>>looper;
vector<geterdone> g_person;
while (looper=='y')
{
vector<geterdone> g_person;
for (int i=0 ;i<g_person.size()+1;i++)
{
g_person[i].getname(nameit,nameitagin);
g_person[i].getage(isage);
g_person[i].output();
}
cout<<"Agin?: y or n: ";
cin>>looper;
}
return 0;
}
[