我正在上C++入门课,我们的一项作业是在网上购物。我们的问题之一是使用线性搜索来创建库存的搜索函数,然后显示该项目的相应价格、库存和可运输性。
出于某种原因,无论我如何尝试和调整它,它总是为搜索返回false
,并且商店不携带该项目,即使我键入了我知道在项目数组中的项目。
例如,如果我在getline
中键入Moon Pie
(它在我的数组中),它仍然会像没有返回一样返回为-1
。这个代码有什么明显的错误吗?
这是我的输入Inventory.txt
Moon Pie 3.50 15 1
Cosmic Brownie 2.00 12 0
Moon Shine 7.00 7 1
Astronaut Icecream 4.00 11 1
Neptune Nuggets 2.50 30 1
Venus Vodka 6.50 10 1
Planet Pop 4.50 20 0
Starry Salad 3.00 15 0
Celeste Cakes 5.00 11 1
Plasma Potion 9.99 4 1
Star Fruit 2.50 10 1
Sun-dae 7.00 20 0
Moon Cheese 5.00 10 1
Milky Way Milkshake 6.50 5 0
Pluto Pie 7.00 9 10
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
using namespace std;
const int MAX = 15;
void searchInventory(string itemNames[], double itemCost[], int itemNoShip[MAX][2]);
int linearSearch(string arr[], int size, string value);
int main() {
int input;
string items[MAX];
double priceItems[MAX];
int noItems[MAX][2];
cout << "n1. Read in Inventoryn";
cout << "2. Display Inventoryn";
cin >> input;
while (input > 2 || input < 1) {
cout << "An error has occured. Please input a value 1 - 2. >> ";
cin >> input;
}
switch (input) {
case 1:
if (readInventory(items, priceItems, noItems) == true) {
cout << "nReading the file...n";
}
break;
case 2:
searchInventory(items, priceItems, noItems);
break;
}
}
bool readInventory(string itemNames[], double itemCost[], int itemNoShip[MAX][2]) {
bool fileRead = false;
ifstream inputFile; // Pointer
inputFile.open("inputInventory.txt");
if (inputFile) // Test if file opened
{
for (int row = 0; row < MAX; row++) {
getline(inputFile, itemNames[row], 't');
inputFile >> itemCost[row];
inputFile >> itemNoShip[row][0];
inputFile >> itemNoShip[row][1];
}
fileRead = true;
inputFile.close();
}
return fileRead;
}
void searchInventory(string itemNames[], double itemCost[], int itemNoShip[MAX][2]) {
string search;
int result;
int position;
cout << "Please type the name of the item you are looking for. > ";
cin.ignore();
getline(cin,search);
result = linearSearch(itemNames, MAX, search);
cout << result;
if (result >= 0) {
cout << "nYour item was found!n";
cout << itemNames[result] << itemCost[result] << itemNoShip[result][0] << "Shippable:" << itemNoShip[result][1];
}
else {
cout << "nThis item was not found in the list.";
}
}
int linearSearch(string arr[], int size, string value) {
int position;
int index;
for (index = 0; index < size; index++) {
if (arr[index] == value) {
position = index;
}
else {
position = -1;
}
}
return position;
}
for (index = 0; index < size; index++)
if (arr[index] == value) {
position = index;
}
else {
position = -1;
}
该循环不断覆盖position
。
除非您的热门元素是数组中的最后一个,否则在找到它之后,下一个元素将导致position
再次设置为-1(除非该元素也匹配)。
一旦找到匹配项,就应该停止循环(或者至少停止更新position
)。
此外,建议用{}
大括号包裹整个循环体,因为这是传统的,也是人们期望看到的,这样代码更容易阅读和理解。
怎么样:
int linearSearch(string arr[], int size, string value)
{
for (int index = 0; index < size; index++)
{
if (arr[index] == value)
return index;
}
return -1;
}
一旦找到该项,就应该中断for循环,否则循环将继续并覆盖position
。编辑:根据PaulMcKenzie的评论,应该用一个值初始化position
,这样它就不会返回垃圾值。
int linearSearch(string arr[], int size, string value) {
int position = -1;
int index;
for (index = 0; index < size; index++) {
if (arr[index] == value) {
position = index;
break;
}
}
return position;
}
问题是cin.ignore()
,因为第一个参数的默认值是1,所以总是会去掉第一个字母。因此,如果有人键入";Moon Pie";,搜索中的值将是"0";oon Pie";。