Hello,

My problem is that the program always gives the same output:

There were no matching numbers !!!

The source of my program looks as follows:

#include <iostream>

using namespace std;

int main(void)
{
        const int MAX_CHARS = 500;
        const int MAX_NUMS = 5;
        const int MAX_NUM_CHARS = 25;

        char values_input[MAX_CHARS];
        char number_to_find[MAX_NUM_CHARS];
        char num[MAX_NUM_CHARS];

        char chr_digits[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '\0'};
        int value_array[MAX_NUMS] = {0}; // initialize to zero
        int value_pos_array[MAX_NUMS] = {0}; // initialize to zero

        cout << "Enter " << MAX_NUMS << " values: ";
        cin.getline(values_input, MAX_CHARS);
        cout << endl;

        cout << "Enter a number to search: ";
        cin.getline (number_to_find, MAX_NUM_CHARS);

        /* Save the user input in an array */ // I think the error is in this part
        for(int i = 0; i < MAX_CHARS; i++)
        {
            for(int j = 0; j < 10; j++)
            {
                if (values_input[i] == chr_digits[j])
                {
                    // we read a normal number
                    // add the character to the num c-string
                    num[j] = values_input[i];
                } else if (values_input[i] == ' '){
                    // we read a space
                    // store the final number and reset num
                    value_array[j] = atoi(num);
                    //num = '';
                } else {
                    // we read an 'unknown' character
                }
            }
        }

        /* Count the position(s) of number_to_find */
        int count_match = 0;
        for(int i = 0; i < MAX_NUMS; i++)
        {
            if (value_array[i] == atoi(number_to_find))
            {
                count_match++; // increment by one
                value_pos_array[i] = i; // store the position of the number
            }
        }

        /* Display the result on the screen */
        if(count_match == 0)
        {
            // no numbers found
            cout << "There were no matching numbers !!!" << endl;
        } else {
            // matching numbers were found
            // display the positions of those numbers on the screen

            cout << "Found " << count_match << " match(es)" << endl;
            cout << "On the following positions: ";

            for(int i = 0; i < MAX_NUMS; i++)
            {
                if(value_pos_array[i] == 0)
                {
                    // all the positions of the searched number have been displayed
                    cout << endl;
                } else {
                    // display the following position on the screen
                    cout << value_pos_array[i];
                    if(value_pos_array[i] != 0)
                    {
                        cout << ", ";
                    }
                }
            }
        }

        return 0;
}

I wrote this program because I tried to address the problem described in the following thread: http://www.daniweb.com/forums/thread178893.html

Thanks in advance!

Recommended Answers

All 4 Replies

Alas, you got char codes instead of integers. Try to run your code with debugger...

const int NUMBERS = 5;
int tux4life()
{
    int num[NUMBERS];
    const int n = sizeof num/sizeof *num;
    cout << "Enter " << n << " integers: ";
    int i;
    for (i = 0; i < n && (cin >> num[i]); ++i)
        ;
    if (i < n) {
        cout << "Not enough numbers, bye..." << endl;
        return 1;
    }
    cin.ignore(1000,'\n');
    int number;
    cout << "Enter number to search: ";
    if (cin >> number) {
        for (i = 0; i < n && number != num[i]; ++i)
            ;
        if (i < n)
            cout << "Position " << i+1 << '\n';
        else
            cout << "Not found\n";
    } else {
        cout << "Not a number...\n";
        return 2;
    }
    cout.flush();
    return 0;
}

This is called, to kill a Mosquito with a Bazooka.
Why did you went into the char thing. Look at ArkM's code, its neat, decent and much faster. And does almost the same work which you were trying to accomplish.
BTW, char chr_digits[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '\0'}; is same as char chr_digits[] ="0123456789";

BTW, char chr_digits[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '\0'}; is same as char chr_digits[] ="0123456789";

True. But based on the code you don't seem to need a string, just an array of characters. So char chr_digits[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'}; is the 'correct' definition.

Thank you for replying!

I'll rewrite my code, I forgot about 'cin' (that it also can read normal numbers, that's why I was using chars[] etc ...)

I think with the knowledge you provided me, my problem is as good as solved...

tux4life

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.