'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;
}
Solution 1:[1]
Your
x < x;
is NEVER true! Check your condition.
You may want to use
for (int x = 0; x < size; ++x)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
