OutFile and InFile of a vector

I created a vector that i sent to an external file at first, but later created two files to write too. Now on a seperate program i am trying to read in the same vector, but the program seems to read the entire data for the five Jersey Numbers the user inputs as one and inputs...Hard to explain with words and i hope my program is easy to understand. is there anyone that can help?

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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
//Andres Robles Programming Assignment 1 pt 1 FEB 1, 2017
//Problem part 1 for asking the user to input jersey number and rating for the players


#include <vector>
#include <fstream>
#include <iostream>
using namespace std;
int main() {


ofstream outFile;
outFile.open("RJN.csv");

if (!outFile.is_open()){
	cout << "Could not open file RJN.csv." << endl;
}
       int totalPlayers, JNUM, Prate, P, i;
       totalPlayers=5;
       P=5;

 vector<int> playNum(P); //vector for the Jersey numbers
 vector<int> rateNum(P); //vector for the Rating of the students
                      

cout << "Please enter " << totalPlayers << " Jersey numbers from 0-99 and a rating for each player from 0-10" << endl; // statement asking the user what to input
    
//for loop to allow the user to input the five jersey numbers
for (i = 0; i < totalPlayers; ++i) {
do{
cout << "\nJersey number for player " << i+1 << ": ";
cin >> playNum.at(i);
cout << "Rating for player " << i + 1 << ": ";
cin >> rateNum.at(i);
                        
if ((rateNum.at(i)>10) || (rateNum.at(i)<0) && (playNum.at(i)>99) || (playNum.at(i)<0))
{cout << "Please enter a valid number for both the Jersey and the Rating" << endl;          
                         
                        }
else
                        {
if ((playNum.at(i)>99) || (playNum.at(i)<0)){
cout << "Please enter a valid number for the Jersey" << endl;
                        }
                        
if ((rateNum.at(i)>10) || (rateNum.at(i)<0))
                        {
cout << "Please enter a valid number for the Rating" << endl;                       
                         
                        }
                        }
}while (( playNum.at(i) > 99) || (playNum.at(i) < 0) || (rateNum.at(i) >10) || (rateNum.at(i) < 0));
                        }
//for loop to allow the users to see what they entered in //(ROSTER)
cout << "\nROSTER:" << endl;
                       
for (i = 0; i < totalPlayers; ++i) { 
cout << "\nJersey number for player " << i+1 << ": " 
<< playNum.at(i)
<< "||Rating for player " << i + 1 << ": "
<< rateNum.at(i) << endl;
                             }

for (i = 0; i < totalPlayers; ++i) {
outFile << playNum.at(i) << ", " ;
                    }
outFile.close();

outFile.open("RRN.csv");
for (i = 0; i < totalPlayers; ++i) {
outFile << playNum.at(i) << ", " ;
                    }
outFile.close();																	
                   
				             
cout << endl << "The Roster of Five Players has now been saved to the File" << endl
<< "called Roster.txt the next program allows you to make changes according to a drop down menu." << endl;  
            
                            
//    system("Pause");
    return 0;
}

this is my second program that is suppose to read from RRN.csv and RJN.csv for the rating number and Jersey number but i cant get it to read the individual integer they enter, it reads in all five integers into the first players line. (a total of five players, five jersey numbers and five ratings)



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 <vector>
#include <fstream>
#include <iostream>
#include <cstdlib>
#include <string>
#include <iomanip>
using namespace std;
int main() {

 int totalPlayers, JNUM, Prate, P, i;
 totalPlayers=5;
 P=5;

ifstream inFile;
inFile.open("RJN.csv");

if (inFile.is_open()){
cout << "The file Roster.txt. has been opened" << endl;
}
      
vector<int> playNum(P); //vector for the Jersey numbers
vector<int> rateNum(P); //vector for the Rating of the students
for (i = 0; i < totalPlayers; ++i) {
inFile >> playNum.at(i); 
        
    }
inFile.close();
inFile.open("RRN.csv");
for (i = 0; i < totalPlayers; ++i) {
inFile >> playNum.at(i); }
inFile.close();
	    

for (i = 0; i < totalPlayers; ++i) {
cout << "\nJersey number for player " << i+1 << ": " 
<< playNum.at(i)
<< "||Rating for player " << i + 1 << ": "
<< rateNum.at(i) << endl;
}

cout << playNum.at(3)<< endl;
//system ("pause");
return 0;




Last edited on
could you rephrase your question? Or provide your original instructions (word for word) ?
(1) Prompt the user to input five pairs of numbers:
• A player's jersey number (0 - 99) and the player's rating (1 - 9).
• Store the jersey numbers in one int vector and the ratings in another int vector.
• Output these vectors (i.e., output the roster).
(2) Implement a menu of options for a user to modify the roster.
• Each option is represented by a single character.
• The program initially outputs the menu, and outputs the menu after a user chooses an option.
• The program ends when the user chooses the option to Quit. For this step, the other options do nothing.

*
Example:
MENU
a - Add player
d - Remove player
u - Update player rating
r - Output players above a rating
o - Output roster
q - Quit

Choose an option:
*

this is what the second part is supposed to look like.
I was able to allow the user to create the Roster of five players with five rankings. However i initially did it with a switch statement. the second case was going to allow the user to modify the initial roster, but I discovered that whatever happens within a "case" stays within that "case" so i couldn't transfer the vector to the second case. that is why i attempted creating the external file but ran into problems as well.
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#include <vector>
#include <iostream>
using namespace std;
int main() {
	int programNum=0;
	int totalPlayers, JNUM, Prate, P, i;


do{
cout << "Please choose the program to the problem: " << endl
     << "1:Prompting User To Input Five Players And Jersey Numbers." <<endl
     << "2:Menu Option For The User To Modify." <<endl
     << "    Which includes problems 2-7." << endl
     << "3:Exit" << endl;
     
cin >> programNum;

if ((programNum > 3) || (programNum < 1)){
   cout << "Please enter either a 1 or a 2, all other numbers are invalid." << endl;
}
if (programNum == 3){
   return 0;  
}
}while((programNum!=1) && (programNum!=2) && (programNum!=3)); 


   
switch (programNum)
{	
   case 1:{
vector<int> playNum(P); //vector for the Jersey numbers
vector<int> rateNum(P); //vector for the Rating of the students
	totalPlayers=5;
	P=5;
       

                      
                      cout << "Please enter " << totalPlayers << " Jersey numbers from 0-99 and a rating for each player from 0-10" << endl; // statement asking the user what to input
    
                      //for loop to allow the user to input the five jersey numbers
                      for (i = 0; i < totalPlayers; ++i) {
                       do{
                       cout << "\nJersey number for player " << i+1 << ": ";
                       cin >> playNum.at(i);
                       cout << "Rating for player " << i + 1 << ": ";
                       cin >> rateNum.at(i);
                        
                        if ((rateNum.at(i)>10) || (rateNum.at(i)<0) && (playNum.at(i)>99) || (playNum.at(i)<0))
                        {
                        cout << "Please enter a valid number for both the Jersey and the Rating" << endl;          
                         
                        }
                        else
                        {
                        if ((playNum.at(i)>99) || (playNum.at(i)<0)){
                        cout << "Please enter a valid number for the Jersey" << endl;
                        }
                        
                        if ((rateNum.at(i)>10) || (rateNum.at(i)<0))
                        {
                        cout << "Please enter a valid number for the Rating" << endl;                       
                         
                        }
                        }
                        
                        
                        }while (( playNum.at(i) > 99) || (playNum.at(i) < 0) || (rateNum.at(i) >10) || (rateNum.at(i) < 0));
                        }
                        //for loop to allow the users to see what they entered in (ROSTER)
                        cout << "\nROSTER:" << endl;
                       
                        for (i = 0; i < totalPlayers; ++i) {
                        cout << "\nJersey number for player " << i+1 << ": " 
                             << playNum.at(i)
                             << "||Rating for player " << i + 1 << ": "
                             << rateNum.at(i) << endl;
                             }
                             break;


}//ending for case 1 brace   
    case 2:{
        char menuLet;
        //do while statment needs work, doesnt recognize the correct variable
        do{
        cout << "Please choose from the following list: "<< endl
             << "a-Add Player" << endl << "d-Remove Player"<< endl 
             << "u-Update Player Rating" << endl << "r-Output Player Above a Rating"<< endl 
             << "o-Output Roster" << endl << "q-quit"<< endl;
             
			 
			 cin >> menuLet;
             if ((menuLet !='a') && (menuLet !='d') && ( menuLet !='u') && (menuLet !='r') && (menuLet !='o') && (menuLet !='q'))
             {
                cout << endl << "Please enter a character from the list above" << endl;
             }
             }while ((menuLet !='a') && (menuLet !='d') && ( menuLet !='u') && (menuLet !='r') && (menuLet !='o') && (menuLet !='q'));
         
         switch (menuLet) //switch within a case
         {
         	case 'a':{
         	
				
			
         		break;
         	}
		 
		 
		 
		 
		 
		 
		 
		 
		 
		 
		 }//brace frr switch within case 2
}//ending for case 2 brace       
} //ending for switch brace   
  //  system("Pause");
return 0;
}

this was my original direction with the switch statements.
Topic archived. No new replies allowed.