I am wondering why I am getting an expected primary-expression before 'remainingNumers' and expected primary-expression before 'lastNums' in line 23. Can you guys tell me what's causing this? Thanks.
#include <iostream>
#include <fstream>
#include <vector>
usingnamespace std;
void seekLine();/* helps the user find a certain position in the file*/
void addNumber(vector<int>remainingNumbers,vector<string> lastNums); /* Adds missing number to txt file */
void getEnd();/* Gets the int from 70-99 from the file and saves it to lastNums*/
int main()
{
int answer;//stores answer of which program the user wishes to access
int temp;
vector<int> remainingNumbers; //stores array of missing numbers
for(int i=25;i<71;i++){
temp=i;
remainingNumbers.push_back(temp);
}
cout<<"Which program would you like to access? \n 1)Add missing numbers to text file \n 2)Seek a position \n";
cin>>answer;
if(answer==1){
addNumber(vector<int>remainingNumbers,vector<string> lastNums);
}
elseif(answer==2){
seekLine();
}
elseif(answer==3){
getEnd();
}
else{
cout<<"invalid response";
}
}
/* helps the user find a certain position in the file*/
void seekLine()
{
int input; //stores userinput to seek position
int answer2; //stores option to seek again
int answer3; //stores option to return to main or end program
cout<<"Enter which position you would like to seek: ";
cin>>input;
ifstream file("file1.txt",ios::in);
ifstream fileOne;
if(!file.is_open())
{
cout<<"Error, file cannot open.";
}else
{
string line;
file.seekg(input);
getline(file,line);
cout << line <<endl;
cout<<"Would you like to seek another line? \n 1)Yes \n 2)No \n";
cin >>answer2;
if(answer2==1){
seekLine();
}
elseif(answer2==2){
cout<<"return to main? \n 1)Yes \n 2)No \n";
cin >>answer3;
if(answer3==1){
main();
}
elseif(answer3==2){
cout << "Good bye.";
}
else{
cout<<"invalid answer";
}
}
else{
cout<<"invalid answer";
}
}
}
/* Adds missing number to txt file */
void addNumber(vector<int> remainingNumbers, vector<string>lastNums)
{
getEnd();
fstream file;
file.open("file1.txt");
if(!file.is_open())
{
cout<<"Error, file cannot open.";
}else
{
file.seekg(120);
for(int i=0;i<46;i++)
{
file.fill(' ');
file.width(4);
file<<remainingNumbers[i]<<'\n';
}
for(int i=0;i<lastNums.size();i++){
file.fill(' ');
file.width(4);
file<<lastNums[i]<<'\n';
}
cout <<file.tellp()<<endl;
}
}
/* Gets the int from 70-99 from the file and saves it to lastNums*/
void getEnd()
{
string temp2;
vector<string>lastNums;
fstream file("file1.txt");
file.seekg(120,ios::beg);
while(file >>temp2){
lastNums.push_back(temp2);
}
}
Take out the vector<int> part of line 23- you already declared it once, no need to declare it again. However, more importantly- lastNums doesn't exist. In no instance of main is it declared, yet you pass it as a parameter.
You can't pass it if it doesn't exist. Either way, you don't need the variable type when passing things as a parameter in a function call- just calling addNumber(remaniningNumbers, lastNums); would work fine.
Either way, you need to create lastNums beforehand and actually fill it with what it needs to be filled with anyway. Then you can pass it.
My lastNum is in another function but I guess it doesn't exist yet since it reads the main function first.
Should I just add my lastNum function to my main function?
If it is created in another function, that means that it is local to that function- it only exists inside of it. Even though lastNums is in getEnd(), it does not exist in main, or anywhere else.