Таким образом, в настоящее время я получаю эту ошибку, говорящую: «прекращается вызов после бросания экземпляра 'std :: logic_error' what (): basic_string :: _ M_construct null not valid Aborted (core dumped)" Я не уверен, почему я получаю это сообщение. Я предполагаю, что это как-то связано с некоторыми возвращаемыми значениями или что некоторые из моих строк установлены равными argv. Любая помощь будет оценена.
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int* insert(int arr[], int& size, int value, int position)
{
int *arrb = new int[size+1];
int i;
for(i = 0; i < position; i++)
arrb[i] = arr[i];
arrb[i] = value;
size = size+1;
for(int j = i+1; j < size; j++)
{
arrb[j] = arr[j-1];
}
delete arr;
arr = arrb;
return arr;
}
int* remove(int arr[], int& size, int position)
{
int *arrb = new int[size-1];
int i;
for(i = 0; i < position; i++)
{
arrb[i] = arr[i];
size = size-1;
}
for(int j = i; j < size; j++)
{
arrb[j] = arr[j+1];
}
delete arr;
arr = arrb;
return arr;
}
int count(int arr[], int size, int target)
{
int total = 0 ;
for(int i=0;i<size;i++)
{
if(arr[i] == target)
{
total += 1 ;
}
}
return total ;
}
void print(int arr[], int size)
{
cout<< "[";
for(int i = 0; i < size; i++)
{
cout<< arr[i];
if(i < size -1)
{
cout<< ", ";
}
}
cout<< "]";
}
int main(int argc, char** argv)
{
int value = 0;
int position = 0;
int choice = 0;
int numEntries = 0;
int* nums = nullptr;
string fileName = argv[1];
ifstream inFile;
inFile.open(fileName);
if( inFile.is_open() )
{
inFile >> numEntries;
string sizeS = argv[2];
int size = 0;
size = stoi(sizeS);
nums = new int[numEntries];
for(int i = 0; i < size; i++)
{
inFile >> nums[i];
}
cout<< "Make a selection: \n";
cout<< "1) Insert\n";
cout<< "2) Remove\n";
cout<< "3) Count\n";
cout<< "4) print\n";
cout<< "5) Exit\n";
cout<< "Choice: ";
cin >> choice;
do{
if(choice == 1)
{
cout<< "Enter number to insert: ";
cin>> value;
do{
cout<< "Enter position: ";
cin>> position;
insert(nums,size,value,position);
}while(position < 0 || position > size);
nums = insert(nums, size, value, position);
}
if(choice == 2)
{
do{
cout<< "Insert a position: ";
cin >> position;
if (position < 0 || position >= size)
{
cout << "Please enter a correct position.\n";
}
}while(position < 0 || position >= size);
nums = remove(nums, size, position);
}
if(choice == 3)
{
cout<< "Pick a number to count: ";
int target = 0;
cin >> target;
count(nums, size, target);
}
if(choice == 4)
{
print(nums, size);
}
}while(choice != 5);
}else{
cout<< "\nError";
}
delete nums;
return (0);
}