Landan Allen
Landan Allen

Reputation: 1

Error - Returning only 1 value, not searching the array

I am having trouble getting my code to search an array. No matter what I search, it keeps only returning the cout statement of " Console Not Found". Any suggestions on how to fix this? My program must be in parallel arrays.

void lookUpPrice(string name[], float prices[], int size)
{
    string search;
    cout << " Enter Console Name:   ";
    cin >> search; 
    getline(cin, search);

    int index = -1;
    for (int x = 0; x < x; ++x)
    {
        if (name[x] == search)
        {
            index = x; break;
        }
    }
    if (index == -1)
        cout << "Console not found " << endl;
    else
        cout << "The current price for " << search << " $" << setprecision(2) << name[index] << endl;
}

Upvotes: 0

Views: 43

Answers (1)

Vlad Feinstein
Vlad Feinstein

Reputation: 11311

Your

x < x;

is NEVER true! Check your condition.

You may want to use

for (int x = 0; x < size; ++x)

Upvotes: 4

Related Questions