Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I am getting the following compilation error on the program below said error. I

ID: 3857081 • Letter: I

Question

I am getting the following compilation error on the program below said error. I believe the stio( ) function is my issue.

****** BELOW: Compilation Error ********************

$g++ FileProcessor.cpp

FileProcessor.cpp: In function ‘void getNumbers()’:

FileProcessor.cpp:64:29: error: ‘stoi’ was not declared in this scope

   array[i] = stoi(line);

   ^

************* BELOW: FileProcessor.cpp *******************

// File Input
#include <fstream>
#include <iostream>

// To use the getline method ad stoi method
#include <string>
using namespace std;

void getNumbers();
void practiceMet();
void randomText();

/*
Add methods above to main method to run program
- The methods are implemented below
- One method read data from a file
- One method input data into a file
- The other method uses both
*/

int main() {
practiceMet();
}


void practiceMet() {
ofstream output;
output.open("testFile.txt");
  
output << "Hello World!" << endl;
output << "Hello there!" << endl;
output << "Hello again!" << endl;
  
output.close();
  
ifstream input;
input.open("testFile.txt");
  
string line;
  
while (getline(input,line)) {
cout << line << endl;
}
}


/*
Question 2
Create (outside of a .cpp file), a text file with a sequence of 5 numbers.
Create a 5 integer array within a .cpp file, open the text file, and then assign the array to the numbers within the file.
Change the values of those variables, and then override the values originally within the file with the new values.

*/

void getNumbers() {
ifstream input;
input.open("numbers.txt");
  
string line;
int array[5];
int i = 0;
  
while (getline(input,line)) {
array[i] = stoi(line);
i++;
}
  
input.close();
  
ofstream output;
output.open("numbers.txt");
  
  
for (int j = 0; j < 5; j++) {
array[j] *= 2;
output << array[j] << endl;
cout << array[j] << endl;
}
output.close();
  
}

/*
Question 3
Create (outside of a .cpp file), a text file with at least 4 lines of random text.
Modify each odd numbered line at the beginning with “I am odd ” and each even numbered line at the beginning with “I am not ”.
*/


void randomText(){
ifstream input;
input.open("randomText.txt");
  
string ln;
int lineNumber = 0;
  
while (getline(input,ln)) {
lineNumber++;
}
  
input.close();
  
string *line = new string[lineNumber];
string *lineCopy = new string[lineNumber];
  
lineNumber = 0;
  
input.open("randomText.txt");
  
while (getline(input,ln)) {
line[lineNumber] = ln;
lineCopy[lineNumber] = ln;
lineNumber++;
}
  
input.close();
  
// Statistic Professor: Edward Pineda
  
ofstream output;
output.open("randomText1.txt");
  
for (int i = 0; i < lineNumber; i++) {
if(i%2 == 0) {
line[i] = "I am odd " + lineCopy[i];
}else {
line[i] = "I am not " + lineCopy[i];
}
  
output << line[i] << endl;
}
  
output.close();
  
}

Explanation / Answer

Question 1 is working fine

This solution to question2 works, changed a little... Have a look and ask questions if you have any

#include<iostream>

#include<string>

#include<fstream>

#include<cstdlib>

using namespace std;

void getNumbers() {

    ifstream input;

    input.open("numbers.txt");

   

    string line;

    int array[5];

    int i = 0;

   

    while (getline(input,line)) {

       array[i] = atoi(line.c_str());

       i++;

    }

   

    input.close();

   

    ofstream output;

    output.open("numbers.txt");

   

   

    for (int j = 0; j < 5; j++) {

       array[j] *= 2;

       output << array[j] << endl;

       cout << array[j] << endl;

    }

    output.close();

   

}