rynoclank
rynoclank

Reputation: 13

Having trouble with a string. Debug is claiming "bad pointer"

I'm writing a program that acts as a number system converter and haven't even made it to the fun math and number values part before screwing up.

At the very end I declared a string "value_array" and it is has a "bad ptr" note on it when I step through the program. This is impeding me from moving on.

#include <iostream>
#include <ctype.h>
#include <stdlib.h>
#include <string>

using namespace std;

int main()
{
//Initialization:
int base = 0;
int target = 0;
int i = 0;

//Won't exit until the user inputs a viable number for a base (between 1 and 16 inclusively).
for (i = 0; i < 1; i += 0)
{
    cout << "Enter the base number system: ";
    cin >> base;
    cout << endl;

    if (base>=2 && base<=16)
    {
        i++;
    }

    else
    {
        cout << "Invalid value. Please input a value between 1 and 16 inclusively." << endl;
    }
}


//Same as before but makes sure the target is a valid number.

for (i = 0; i < 1; i += 0)
{
    cout << "Enter the target number system: ";
    cin >> target;
    cout << endl;

    if (target>=2 && target<=16)
    {
        i++;
    }

    else
    {
        cout << "Invalid value. Please input a value between 1 and 16 inclusively." << endl;
    }
}

string value_array = ""; //editted

cout << "Enter value in base (with no spaces): ";
cin >> value_array; //editted 

//int k = basevalue(value_array,base);//Please disregard. Can't use this function until the strings are usable.

return 0;
}

Upvotes: 1

Views: 126

Answers (2)

Alvarey
Alvarey

Reputation: 91

You can use std::cin on value_array, but first remove constness. You can't modify const string.

Edit

You have to #include <string>.

Upvotes: 1

psliwa
psliwa

Reputation: 1092

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

Upvotes: 0

Related Questions