I'm having a problem with a program of mine at the moment. Basically, the thought process was to see if the input was whether an int or a string. But as soon as I tried to test it out, it produced an error along the lines of "could not convert 'string' from std::string to bool"

Much help would be appreciated.

Here's the code:

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

int main()
{

    vector <string> words(9);
        words[0] = "One";
        words[1] = "Two";
        words[2] = "Three";
        words[3] = "Four";
        words[4] = "Five";
        words[5] = "Six";
        words[6] = "Seven";
        words[7] = "Eight";
        words[8] = "Nine";
    vector <int> numbers(9);
        numbers[0] = 1;
        numbers[1] = 2;
        numbers[2] = 3;
        numbers[3] = 4;
        numbers[4] = 5;
        numbers[5] = 6;
        numbers[6] = 7;
        numbers[7] = 8;
        numbers[8] = 9;

    string word;
    int number;

    if (number) {               // Check if the input is a number.
        while (cin >> number) {
            for (int x = 0; x < words.size(); x++)
                if (number == x)
                    cout << "The number when stated in words is: " << words[x-1] << endl;
            }
    }
    else if (word) {           // This is the problem. Purpose is to check if the input is a string.
        while (cin >> word) {
            for (int y = 0; y < numbers.size(); y++)
                if (word = words[y-1])
                    cout << "The word when stated in a number is: " << numbers[y-1] << endl;
        }
    }
}

if (number) { // Check if the input is a number.

This does not check that the input is a number. Not only because at this point there has been no input, but also because it will attempt to turn number into a bool and test it for being "true". The rule is that any int value other than zero is considered true, so if you do put an input into number, you'll simply be testing that it's not zero.

else if (word) { // This is the problem. Purpose is to check if the input is a string.

Again ignoring that there has been no input, this is an attempt to turn a string into a bool and check if it's "true". There is no rule for turning a string into a bool. The compiler has no idea how to do that, and is telling you that it has no idea.

If you want the user to be able to enter either a string or an int, you're going to have to take in whatever they type as a string, and then attempt to convert it to an int; C++11 introduced some numeric conversions for string - http://en.cppreference.com/w/cpp/string/basic_string/stol

You could always use typeof ()

typeof is not standard C++. Even if it were, it tells you what type something is. We already know that the variable created like this:

int number;

is an int, because it says it right there, and we know that the variable created like this:

string word;

is a string, because it says it right there.

Perhaps this article explains it better than I can say it.

#include <iostream>
#include <string>
#include <map>
#include <algorithm>

using namespace std;

int main()
{
    /* Looks like you want something like a bimap, which isn't in the STL */
    /* so using a single map here, and swapping the key / value pairs     */
    /* later to avoid redundancyto some extend.                           */
    map < string, int > map_word_to_num
    { { "One"  , 1 }, { "Two"  , 2 }, { "Three", 3 }
    , { "Four" , 4 }, { "Five ", 5 }, { "Six"  , 6 }
    , { "Seven", 7 }, { "Eight", 8 }, { "Nine" , 9 } };

    map < int, string > map_num_to_word;

    /* fill map_num_to_word with the values from map_word_to_num, but the */
    /* keys become values and vice versa.                                 */
    for (auto& e : map_word_to_num)
    {
        /* Use emplace() if your compiler supports it.. */
        map_num_to_word.emplace(e.second, e.first);
    }

    /* The string where we will store the user's input in */
    string line;

    /* Read a line of input from stdin */
    cout << "Input: ";

    if (!getline(cin, line))
    {
        cerr << "I/O error on getline()\n";
        return 0;
    }

    /* Remove all whitespaces from the input */
    line.erase(remove_if(line.begin(), line.end(), isspace), line.end());

    /* The variable in which we will store potential following characters */
    size_t pos = 0;

    try
    {
        /* The integer representation of the read line                      */
        /* Throws an invalid_argument exception if it cannot convert to int */
        int int_val = stoi(line, &pos);

        /* There was more content on the line that could not be converted to an integer */
        if (pos < line.size())
        {
            throw invalid_argument(line);
        }

        /* Try to map the number to a word, throws an out_of_range exception if it can't */
        cout << "The number when stated in words is: \"" << map_num_to_word.at(int_val) << "\".\n";
    }
    catch (const invalid_argument&) 
    {
        try
        {
            /* Try to map the word to a number, throws an out_of_range exception if it can't */
            /* Note that this is case-sensitive. Easy to modify that if needed..             */
            cout << "The word when stated as a number is: \"" << map_word_to_num.at(line) << "\".\n";
        }
        catch (const out_of_range&)
        {
            cerr << "The entered word is not a (supported) number.\n";
        }   
    }
    catch (const out_of_range&)
    {
        cerr << "The entered number is out of range.\n";
    }

    return 0;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.