#include <iostream>
#include <ostream>
#include <string>
#include <fstream>
#include <vector>
#include <iterator>
usingnamespace std;
int main()
{ string ps;
vector<string>vp;
cout << "Select one from the following players by choosing his number" << endl;
ifstream in("playerlist.txt");
string temp;
while(getline(in, temp))
cout << temp << endl;
vp.push_back(temp);
in.close();
cin>> ps;
if (ps== "1")
{
user= vp[ps];
cout << user << endl;
cout << "the player has been selected" << endl;
cout << "You have been redirected to the main menu" << endl;
}
return 0;
}
The errors are (probably) 'user' hasn't been defined and that a string 'ps' is not a valid subscript into the vector 'vp'; you'll need to use to number. You probably mean to convert the string 'ps' into a number to use as a subscript.
Thanks for fast replay, but my file is already exist with the form
1-John
2-Adm
3-Dave
what I want is that when I read from the file and make it equals to the variable user I need "user " to take just the name without the number that already exist in the file
{
#include <iostream>
#include <ostream>
#include <string>
#include <fstream>
#include <vector>
#include <iterator>
usingnamespace std;
int main()
int rmp;
cout << "Remove one from the following players by choosing his number" << endl;
vector<string>vpr;
ifstream inn("playerlist.txt");
string tempn;
while(getline(inn, tempn))
cout << tempn << endl;
vpr.push_back(tempn);
inn.close();
ifstream opn("playerlist.txt");
string linn;
while(getline(opn, linn))
vpr.push_back(linn);
inn.close();
cin>> rmp;
vpr.erase(vpr.begin()+rmp);
ofstream file("data.txt",ios::out|ios::trunc);
if(file.is_open())
{
for(int i = 0;i<vpr.size();++i)
{
vpr[i]=vpr[i].substr(2);//Take a substring of the string!
file<<i+1<<'-'<<vpr[i]<<'\n';
cout<<vpr[i]<<endl;
}
file.close();
}
/*remove("playerlist.txt");*/
rename("data.txt","playerlist1.txt");
cout << "the player has been removed" << endl;
cout << "You have been redirected to the main menu" << endl;
return 0;
}
#include <iostream>
#include <ostream>
#include <string>
#include <fstream>
#include <vector>
#include <iterator>
usingnamespace std;
int main()
{
int rmp;
cout << "Remove one from the following players by choosing his number" << endl;
vector<string>vpr;
//Reading from file:
ifstream inn("playerlist.txt");
string tempn;
if(inn.is_open())
{
while(getline(inn, tempn))
{
cout << tempn << endl;
tempn=tempn.substr(2);//Moved it here to better reflect modification during read.
vpr.push_back(tempn);
}
inn.close();
}
//Removing 'a data' from array: (data illustrated during reading process)
cin>> rmp;
if(rmp>0)//Don't waste time if not a valid removal:
{
string removed = vpr[rmp];
vpr.erase(vpr.begin()+rmp-1);
//Writing data back to file, truncating data.
ofstream file("playerlist.txt",ios::out|ios::trunc);
if(file.is_open())
{
for(int i = 0;i<vpr.size();++i)
{
file<<i+1<<'-'<<vpr[i]<<'\n';
cout<<vpr[i]<<endl;
}
file.close();
}
cout << "the player '"<<removed<<"' has been removed" << endl;
}
else//No writing was needed, no one was removed.
cout<<"Invalid, no one was removed."<<endl;
cout << "You have been redirected to the main menu" << endl;
return 0;
}
Make use of curly brackets. That was the reason why you weren't pushing multiple data into the array during each call of getline(), using your while loop.
A while loop without "{}" will only cause the very next line to repeat.
Also, you'd want to supply if conditions, for file operation (file.is_open()), to ensure you are able to access your data. I understand that it would most likely be there, but you'd never know...