我對C++編程非常陌生,你會明白爲什麼。在char數組中的線性搜索 - C++(Visual Studio 2005)
我想製作一個由我想用線性搜索功能搜索的幾個單詞組成的字符數組。這個數組是否必須是一個二維數組?例如:
char Colors[3][6] = {"red", "green", "blue"};
我想它是這樣的:
char Colors[] = {"red", "green", "blue"};
這給了我一個 「太多的初始化」 的錯誤。
我假設第一種方法是正確的,因爲它說明了數組中元素的數量和元素的最大長度,是否正確?
現在我將如何實現線性搜索功能來查找該數組中的單詞?我可以這樣做如下:
(假設linearSearch功能已申報)
char searchKey;
char element;
char Colors[3][6] = {"red", "green", "blue"};
printf("Enter the color to look for: \n");
scanf("%s", searchKey);
element = linearSearch(Colors, searchKey, ??); //?? is where I don't know what to enter
if (element != -1)
{
printf("Found the word.\n");
}
else
{
printf("Didn't find the word.\n");
}
這可能嗎?如果是這樣,該聲明將查找linearSearch函數?我希望我提供了足夠的信息,以便稍微有用。
編輯:感謝所有的幫助,讓程序按預期工作。
您的代碼看起來不像此窗口中的代碼。爲了解決這個問題,編輯你的問題(上面的鏈接),然後選擇你的代碼,並點擊其上有小1和0的按鈕。 – 2009-05-24 21:24:58