i am trying to write a program that that accept list of names in vector, write them out, remove a name, sort the list save it to file. wondering if i could get help.
here is my code.
/*
void addName()
{
/* en vektor that can hold full namn*
vector<string> names;
/* en sentinel used to end input*
string sentinel= "end";
int counter=0;
cout<<" ******************Namelist*****************\n";
/* ask after first name*/
cout << " input first name and stop input with'end': ";
cin>>for_namn;
/* controll first name before last name input*/
while (for_namn != sentinel)
{
/* input last name*/
cout << " input lastname: ";
cin>>last_name;
/* make two name inputs to full name*/
lägger kopia av efternamn sist i fullnamn *
strcat(full_name,last_name);
strcat(full_name," ");
strcat(full_name,f_namn);
names.push_back(full_name);
/* empty full_name to be able to take new full namn*
*full_name = NULL;
/* Push fullname onto vector*/
cout << " input first name or end input with 'end': ";
cin>>f_namn;
}
/* skapa en file att lagrar vektor*
fstream inout;
inout.open("list.txt",ios::out | ios::app);
/* använder en for loop att lagrar vektor i filen*
for ( int i=counter; i < names.size(); i++ )
{
inout<< names[i] << endl;
}
inout.close();
cout<<" *****NAMNLISTA*****"<<endl;
/*exit program if ifstream could not open file*
if(!innamnlist)
{
cerr << " filen inte kan öppna"<<endl;
exit(1);
}/*end if*
/* ifstream construktor opend the file*
ifstream innamnlist;
innamnlist.open("list.txt", ios::in);
/*exit program if ifstream could not open file*
if(innamnlist.is_open())
{
int row= 0;
while (getline(innamnlist, namevektor2[row]))
{
listnamn.push_back(namevektor2[row]);
row++;
}
}
cout<<" vilket namn Söka du efter: ";
cin.get();
getline(cin,searchnamn);
cout<<endl;
// sök_i_listan(namevektor2,searchnamn,size);
for(int i=0;i<listnamn.size(); i++)
if(searchnamn == listnamn[i])
{ found = true;
}
if(found== true)
cout<<searchnamn<<" finns i namnlista\n"<<endl;
else
cout<<searchnamn<<" saknas i namnlista"<<endl;
use [code] tags and proper indentation.
state your problems exactly.
try to omit the irrelevant parts of your code, or provide a simplified version of it which has the same problem, if you can.
we're busy(/lazy) people..
thanks hamsterman
here is my forst code. we can start from there.
i am suppose to write a code that ask user to input names which is push onto a vector.
this vector vill be read by other function as
void writeoutnames();
void remove name();
and son on.
but i think its better we start with the first option which is to accept input from user and put it in vector.
thanks for your patience.
void addName()
{
// variable for name input
const int SIZE = 30, SIZE1 = 60;
char f_namn[SIZE] = "";
char last_namn[SIZE]="";
char full_name[SIZE1]="";
/* en vektor that can hold full namn*
vector<string> names;
/* en sentinel used to end input*
string sentinel= "end";
int counter=0;