I get answer from your expert but when  run the code the terminal write terminate called after throwing an instance of 'std::invalid_argument' what(): stoi Aborted (core dumped). : What should i change : Expert Answer   Step 1 NOTE: Algorithm specified in the question is implemented in "checkAlgorithm.h". Test program to implement "checkAlgorithm.h" is  specified in "fileCheck.cpp". Code for checkAlgorithm.h: #ifndef H_checkAlgorithm #define H_checkAlgorithm #include  #include  using namespace std; bool check(ifstream &file) {     string s;     int a, b;     getline(file, s);     a = stoi(s);     while (!file.eof())     {         getline(file, s);         b = stoi(s);         if (a > b)         {             return false;         }         a = b;     }     return true; } #endif Step 2 Code for fileCheck.cpp: #include "checkAlgorithm.h" int main() {     int value;     string str;     ifstream inFile;     bool result;     cout << "\nEnter File Name to check: ";     cin >> str;     inFile.open(str);     result = check(inFile);     cout << "\n"           << str;     if (result)     {         cout << " is sorted" << endl;     }     else     {         cout << " is not sorted" << endl;     }     cout << endl;     inFile.close(); }

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

I get answer from your expert but when  run the code the terminal write terminate called after throwing an instance of 'std::invalid_argument' what(): stoi Aborted (core dumped). : What should i change :

Expert Answer

 
Step 1

NOTE:

  • Algorithm specified in the question is implemented in "checkAlgorithm.h".
  • Test program to implement "checkAlgorithm.h" is  specified in "fileCheck.cpp".

Code for checkAlgorithm.h:

#ifndef H_checkAlgorithm
#define H_checkAlgorithm

#include <iostream>
#include <fstream>
using namespace std;

bool check(ifstream &file)
{
    string s;
    int a, b;
    getline(file, s);
    a = stoi(s);
    while (!file.eof())
    {
        getline(file, s);
        b = stoi(s);
        if (a > b)
        {
            return false;
        }
        a = b;
    }
    return true;
}

#endif
Step 2

Code for fileCheck.cpp:

#include "checkAlgorithm.h"
int main()
{
    int value;
    string str;
    ifstream inFile;
    bool result;
    cout << "\nEnter File Name to check: ";
    cin >> str;
    inFile.open(str);
    result = check(inFile);
    cout << "\n"
          << str;
    if (result)
    {
        cout << " is sorted" << endl;
    }
    else
    {
        cout << " is not sorted" << endl;
    }
    cout << endl;
    inFile.close();
}
C- FileCheck.cpp x
C checkAlgorithm.h ×
E file.txt
E file1.txt
G- FileCheck.cpp > O main()
C checkAlgorithm.h > check(ifstream &)
E file.txt
E file1.txt
#include "checkAlgorithm.h"
int main()
{
int value;
#ifndef H_checkAlgorithm
#define H_checKAlgorithm
1.
1.
1.
2
1.
FR
6.
2
3
9
4
4.
4.
#include <iostream>
4.
5
4.
5
5n
string str;
5n
#include <fstream>
5n
1
5n
6.
6
ifstream inFile;
bool result;
6.
using namespace std;
6.
4
6.
7
7
7\
7
8.
bool check(ifstream &file)
{
0.
cout <« "\nEnter File Name to cher
0.
9.
cin >> str;
6.
10
infile.open(str);
result - check(inFile);
10
string s;
int a, b;
getline(file, s);
a = stoi(s);
while (!file.eof())
{
getline(file, s);
b = stoi(s);
11
11
12
cout << "\n"
12
« str;
if (result)
{
cout <« " is sorted" << endl;
13
13
14
14
15
15
16
16
17
}
17
PROBLEMS
OUTPUT
DEBUG CONSOLE
TERMINAL
1: powershell
A X
PS D:\CPP> g+H FileCheck.cpp -o FileCheck
PS D:\CPP> .//Filecheck.exe
Enter File Name to check: file.txt
file.txt is not sorted
PS D:\CPP> ./FileCheck.exe
Enter File Name to check: file1.txt
file1.txt is sorted
PS D:\CPP>
Transcribed Image Text:C- FileCheck.cpp x C checkAlgorithm.h × E file.txt E file1.txt G- FileCheck.cpp > O main() C checkAlgorithm.h > check(ifstream &) E file.txt E file1.txt #include "checkAlgorithm.h" int main() { int value; #ifndef H_checkAlgorithm #define H_checKAlgorithm 1. 1. 1. 2 1. FR 6. 2 3 9 4 4. 4. #include <iostream> 4. 5 4. 5 5n string str; 5n #include <fstream> 5n 1 5n 6. 6 ifstream inFile; bool result; 6. using namespace std; 6. 4 6. 7 7 7\ 7 8. bool check(ifstream &file) { 0. cout <« "\nEnter File Name to cher 0. 9. cin >> str; 6. 10 infile.open(str); result - check(inFile); 10 string s; int a, b; getline(file, s); a = stoi(s); while (!file.eof()) { getline(file, s); b = stoi(s); 11 11 12 cout << "\n" 12 « str; if (result) { cout <« " is sorted" << endl; 13 13 14 14 15 15 16 16 17 } 17 PROBLEMS OUTPUT DEBUG CONSOLE TERMINAL 1: powershell A X PS D:\CPP> g+H FileCheck.cpp -o FileCheck PS D:\CPP> .//Filecheck.exe Enter File Name to check: file.txt file.txt is not sorted PS D:\CPP> ./FileCheck.exe Enter File Name to check: file1.txt file1.txt is sorted PS D:\CPP>
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 2 steps with 2 images

Blurred answer
Knowledge Booster
File Input and Output Operations
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education