The problem you experience is caused by the constness by value_array
. You declared it const
which means that it cannot be modified by anything (it is read-only) during it's lifetime. Thus when you try to assign a string obtained from user input to it, compiler informs you that it is impossible. Everything works fine after removal of the const
keyword.
It's hard to say what is the exact reason of the bad pointer error you experience. According to this answer it can be caused by inappropriate usage of raw pointers. If you use them somewhere after the above code, make sure any of them is NULL
, nullptr
nor 0
when used.
I modified your code a little bit and added a few comments, I hope they will be useful during development of your program:
#include <iostream>
#include <ctype.h>
#include <stdlib.h>
using namespace std;
int main()
{
//Initialization:
int base = 0;
int target = 0;
//PiotrSliwa: The loop can be removed with if-statement modified like below
cout << "Enter the base number system: ";
cin >> base;
cin.ignore(1024, '\n'); // PiotrSliwa: ignore up to 1024 characters or until newline is found in order to avoid bugs caused by more-than-required user input characters
cout << endl;
if (base<2 || base>16)
{
cout << "Invalid value. Please input a value between 1 and 16 inclusively." << endl;
return 0;
}
//PiotrSliwa: The loop can be removed with if-statement modified like below
cout << "Enter the target number system: ";
cin >> target;
cin.ignore(1024, '\n');
cout << endl;
if (target<2 && target>16)
{
cout << "Invalid value. Please input a value between 1 and 16 inclusively." << endl;
return 0;
}
string value_array = ""; //PiotrSliwa: removed 'const' from string which caused an error
cout << "Enter value in base (with no spaces): ";
cin >> value_array; //PiotrSliwa: The simplest method of obtaining user input and redirecting it to 'string' variable
cin.ignore(1024, '\n');
//int k = basevalue(value_array,base);//Please disregard. Can't use this function until the strings are usable.
return 0;
}
Live demo
Also, consider enclosing functionalities into functions (or objects) to avoid code duplications as well as to provide reusable code and improve readability. For example:
#include <iostream>
#include <ctype.h>
#include <stdlib.h>
using namespace std;
template<typename T>
T getUserInput(string message)
{
cout << message;
T input;
cin >> input;
cin.ignore(1024, '\n');
cout << endl;
return input;
}
bool isValidNumberSystem(int numberSystem)
{
return numberSystem>=2 && numberSystem<=16;
}
int main()
{
int base = getUserInput<int>("Enter the base number system: ");
if (!isValidNumberSystem(base))
{
cout << "Invalid value. Please input a value between 1 and 16 inclusively." << endl;
return 0;
}
int target = getUserInput<int>("Enter the target number system: ");
if (!isValidNumberSystem(target))
{
cout << "Invalid value. Please input a value between 1 and 16 inclusively." << endl;
return 0;
}
string value_array = getUserInput<string>("Enter value in base (with no spaces): ");
//int k = basevalue(value_array,base);//Please disregard. Can't use this function until the strings are usable.
return 0;
}
Live demo