variable equals vector object

how I can make variable equals vector object

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
  #include <iostream>
#include <ostream>
#include <string>
#include <fstream>
#include <vector>
#include <iterator>


using namespace 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, I fixed it
my code now is
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
#include <iostream>
#include <ostream>
#include <string>
#include <fstream>
#include <vector>
#include <iterator>


using namespace std;




int main()
{ string user;
int ps,k;
      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();
      ifstream op("playerlist.txt");
      string lin;
      while(getline(op, lin))
      vp.push_back(lin);
      in.close();
      cin>> ps;                    
       user= vp[ps];
       cout << user << endl;
       cout << "the player has been selected" << endl;
       cout << "You have been redirected to the main menu" << endl;
      cin >> k;
    return 0;
}


but my file looks like this

1-John
2-Adm
3-Dave


and user will be 1-John not just John.
I need the user to be just the name"John" with out the number so I can use it later.
closed account (j3Rz8vqX)
Reassign the value, after a sub string.

Example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
#include <vector>
using namespace std;
int main()
{
    vector<string> vp;
    vp.push_back("1-John");
    vp.push_back("2-Adm");
    vp.push_back("3-Dave");
    for(int i = 0;i<vp.size();++i)
    {
        vp[i]=vp[i].substr(2);//Take a substring of the string!
        cout<<vp[i]<<endl;
    }
    return 0;
}
John
Adm
Dave

Process returned 0 (0x0)   execution time : 0.008 s
Press any key to continue.
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
to be more clear I need it to work with
1
2
3
4
5
6
7
ifstream re((user+".txt").c_str());
      string go;
      while(getline(re, go))
      cout << go << endl;
      re.close();                   
     cout << user << endl;
     }
closed account (j3Rz8vqX)
cout << go.substr(2) <<endl
Will print out
John
from a file read of
1-John
skipping the first two characters.

I try to provide examples, of methods, needed to be taken to achieve solutions.
Thanks man I got it. here is my code now it works fine.
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
#include <iostream>
#include <ostream>
#include <string>
#include <fstream>
#include <vector>
#include <iterator>


using namespace std;




int main()
{ string user;
int ps,k;
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();
      ifstream op("playerlist.txt");
      string lin;
      while(getline(op, lin))
      vp.push_back(lin);
      in.close();
      cin>> ps;
      vp[i]=vp[i].substr(3);
      user= vp[ps];
       cout << user << endl;
       cout << "the player has been selected" << endl;
       cout << "You have been redirected to the main menu" << endl;
      ifstream re((user+".txt").c_str());
      string go;
      while(getline(re, go))
      cout << go << endl;
      re.close();                   
     cout << user << endl;
cin >> k;
    return 0;
}


now my problem are
1- it just works with the first name not with the others
2- how to delete a name from the file like this

1-John
2-Adm
3-Dave


after delete Adm will be

1-John
2-Dave
Last edited on
I fixed the first problem

1
2
vp[i]=vp[i].substr(3);
      user= vp[ps];


changed to
1
2
vp[ps]=vp[ps].substr(3);
      user= vp[ps];


but still have problem with number 2
closed account (j3Rz8vqX)
Delete a name from file:

Copy all of the data from the file over to your vector or array.

Remove the people you no longer want with vector erase method.

Then when you write back, you would truncate the file being written to, not append.

Example:
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
#include <iostream>
#include <vector>
#include <fstream>
using namespace std;
int main()
{
    vector<string> vp;
    vp.push_back("1-John");
    vp.push_back("2-Adm");
    vp.push_back("3-Dave");
    vp.erase(vp.begin()+1);//The first person + 1 = the second person, 2-Adam!

    ofstream file("data.txt",ios::out|ios::trunc);
    if(file.is_open())
    {
        for(int i = 0;i<vp.size();++i)
        {
            vp[i]=vp[i].substr(2);//Take a substring of the string!
            file<<i+1<<'-'<<vp[i]<<'\n';
            cout<<vp[i]<<endl;
        }
        file.close();
    }

    return 0;
}
what is wrong the program crash every time I run it
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
{
#include <iostream>
#include <ostream>
#include <string>
#include <fstream>
#include <vector>
#include <iterator>


using namespace 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;
      } 
closed account (j3Rz8vqX)
Edit:
Your curly bracket on line #1 needs to be below main; main is missing its '{'.

'A' possible solution (You've already done all the work, I am simply providing assistance):
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
#include <iostream>
#include <ostream>
#include <string>
#include <fstream>
#include <vector>
#include <iterator>
using namespace 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...
Last edited on
thanks so much , now everything is ok
Topic archived. No new replies allowed.