2010-03-23 64 views
1

我嘗試刪除各種部件和建築物,但沒有發現lnk2019錯誤消失,甚至沒有產生任何正常錯誤。lnk2019在非常簡單的C++程序中出錯

此刻所有內容都在一個文件中(它不會在以後完成時)。該程序有三個單詞列表,並在其中添加一個行話短語,並且您應該可以添加單詞,刪除單詞,查看列表,還原默認值,保存對文件的更改以及從文件加載更改。

#include "stdafx.h" 
#include <iostream> 
#include <string.h> 

using namespace std; 

const int maxlist = 20; 

string adj1[maxlist], adj2[maxlist], noun[maxlist]; 

void defaultlist(int list) 
{ 
if(list == 1) 
{ 
    adj1[0] = "green"; 
    adj1[1] = "red"; 
    adj1[2] = "yellow"; 
    adj1[3] = "blue"; 
    adj1[4] = "purple"; 

    int i = 5; 
    while(i != maxlist) 
    { 
    adj1[i] = ""; 
    i = i + 1; 
    } 
} 

if(list == 2) 
{ 
    adj2[0] = "shiny"; 
    adj2[1] = "hard"; 
    adj2[2] = "soft"; 
    adj2[3] = "spiky"; 
    adj2[4] = "furry"; 

    int i = 5; 
    while(i != maxlist) 
    { 
    adj2[i] = ""; 
    i = i + 1; 
    } 
} 

if(list == 3) 
{ 
    noun[0] = "cat"; 
    noun[1] = "dog"; 
    noun[2] = "desk"; 
    noun[3] = "chair"; 
    noun[4] = "door"; 

    int i = 5; 
    while(i != maxlist) 
    { 
    noun[i] = ""; 
    i = i + 1; 
    } 
} 
return; 
} 


void printlist(int list) 
{ 
if(list == 1) 
{ 
    int i = 0; 
    while(!(i == maxlist)) 
    { 
    cout << adj1[i] << endl; 
    i = i + 1; 
    } 
} 

if(list == 2) 
{ 
    int i = 0; 
    while(!(i == maxlist)) 
    { 
    cout << adj2[i] << endl; 
    i = i + 1; 
    } 
} 

if(list == 3) 
{ 
    int i = 0; 
    while(!(i == maxlist)) 
    { 
    cout << noun[i] << endl; 
    i = i + 1; 
    } 
} 
return; 
} 

string makephrase() 
{ 
int num1 = rand()%maxlist; 
int num2 = rand()%maxlist; 
int num3 = rand()%maxlist; 
int num4 = rand()%1; 

string word1, word2, word3; 

if(num4 = 0) 
{ 
    word1 = adj1[num1]; 
    word2 = adj2[num2]; 
} 
else 
{ 
    word1 = adj2[num1]; 
    word2 = adj1[num2]; 
} 

word3 = noun[num3]; 

return word1 + " ," + word2 + " " + word3; 
} 

string addword(string word, int list) 
{ 
string result; 

if(list == 1) 
{ 
    int i = 0; 
    while(!(adj1[i] == "" || i == maxlist)) 
    { 
    i = i + 1; 
    } 

    if(i == maxlist) result = "List is full. Please try again."; 
    if(adj1[i] == "") 
    { 
    adj1[i] = word; 
    result = "Word was entered successfully."; 
    } 
} 

if(list == 2) 
{ 
    int i = 0; 
    while(!(adj2[i] == "" || i == maxlist)) 
    { 
    i = i + 1; 
    } 

    if(i == maxlist) result = "List is full. Please try again."; 
    if(adj2[i] == "") 
    { 
    adj2[i] = word; 
    result = "Word was entered successfully."; 
    } 
} 

if(list == 3) 
{ 
    int i = 0; 
    while(!(noun[i] == "" || i == maxlist)) 
    { 
    i = i + 1; 
    } 

    if(i == maxlist) result = "List is full. Please try again."; 
    if(noun[i] == "") 
    { 
    noun[i] = word; 
    result = "Word was entered successfully."; 
    } 
} 
return result; 
} 
string removeword(string word, int list) 
{ 
string result; 

if(list == 1) 
{ 
    int i = 0; 
    while(!(adj1[i] == word || i == maxlist)) 
    { 
    i = i + 1; 
    } 

    if(i == maxlist) result = "Word is not on the list. Please try again."; 
    if(adj1[i] == word) 
    { 
    adj1[i] = ""; 
    result = "Word was removed successfully."; 
    } 
} 

    if(list == 2) 
{ 
    int i = 0; 
    while(!(adj2[i] == word || i == maxlist)) 
    { 
    i = i + 1; 
    } 

    if(i == maxlist) result = "Word is not on the list. Please try again."; 
    if(adj2[i] == word) 
    { 
    adj2[i] = ""; 
    result = "Word was removed successfully."; 
    } 
} 

if(list == 3) 
{ 
    int i = 0; 
    while(!(noun[i] == word || i == maxlist)) 
    { 
    i = i + 1; 
    } 

    if(i == maxlist) result = "Word is not on the list. Please try again."; 
    if(noun[i] == word) 
    { 
    noun[i] = ""; 
    result = "Word was removed successfully."; 
    } 
} 

return result; 
} 



/////////////////////////////main/////////////////////////////////// 


int main() 
{ 
string mainselection; 
string makeselection; 
string phrase; 

defaultlist(1); 
defaultlist(2); 
defaultlist(3); 

cout << "This program generates jargon phrases made of two adjectives and one noun,"; 
cout << " on three lists. Each list may contain a maximum of " << maxlist << "elements."; 
cout << " Please choose from the following menu by typing the appropriate number "; 
cout << "and pressing enter." << endl; 

cout << endl; 

cout << "1. Make a jargon phrase." << endl; 
cout << "2. View a list." << endl; 
cout << "3. Add a word to a list." << endl; 
cout << "4. Remove a word from a list." << endl; 
cout << "5. Restore default lists." << endl; 
cout << "More options coming soon!." << endl; 

cin >> mainselection 

if(mainselection == 1) 
{ 
    phrase = makephrase(); 

    cout << "Your phrase is " << phrase << "." << endl; 
    cout << "To make another phrase, press 1. To go back to the main menu,"; 
    cout << " press 2. To exit the program, press 3." << endl; 

    cin >> makeselection; 

    while(!(makeselection == "1" || makeselection == "2" || makeselection == "3")) 
    { 
    cout << "You have entered an invalid selection. Please try again." << endl; 
    cin >> makeselection; 
    } 

    while(makeselection == "1") 
    { 
    phrase = makephrase(); 
    cout << "To make another phrase, press 1. To go back to the main menu,"; 
    cout << " press 2. To exit the program, press 3." << endl; 
    } 

    if(makeselection == "2") main(); 
    if(makeselection == "3") return 0; 
} 

return 0; 
} 

//Rest of the options coming soon! 
+2

http://msdn.microsoft.com/en-us/library/799kze2z(VS.80).aspx – 2010-03-23 00:48:46

+1

連接器抱怨什麼樣的缺失符號?什麼是確切的錯誤信息? – sth 2010-03-23 00:52:23

回答

1

根據gcc的問題是,你還沒有宣佈正確包括爲rand()功能 - 我加#include <cstdlib>,問題就消失了。

我還要補充一點,在主函數你這樣做:

cin >> mainselection; 
if (mainselection == 1) 

不工作。 Mainselection是一個字符串,只能通過類來比較比較方法,就像這樣:

cin >> mainselection; 
if (mainselection.compare("1")) 

那將返回0,如果他們是平等的。另外,如果你使用std::vector,你可以非常容易地在C++中動態調整大小的數組,你只需要將項目推到vector的後面。意味着你不需要對限制進行硬編碼。

+1

您可以使用==比較C++中的字符串。但是你不能將一個字符串與一個int進行比較。所以'如果(mainselection ==「1」)'可以工作。 – mkj 2010-03-23 01:09:39

1

一些建議,以幫助您:

  • 行107:if(num4 = 0)(賦值運算符)應if(num4 == 0)(等於運算符)。
  • 260行:cin >> mainselection後面缺少分號。
  • 261行:if(mainselection == 1)是非法的,mainselectionstd::string
  • Line 284:if(makeselection == "2") main();您不允許在C++中遞歸調用main()函數。
+1

我稍微軟化了你的答案。 OP也使用了一個編譯器。並非所有的編譯器消息最初都很明顯,有時甚至是有經驗的開發人員。 – 2010-03-23 01:10:41

+0

這很好,但lnk2019是一個鏈接器錯誤,並且此代碼不能編譯。 – 2010-03-23 01:14:22

相關問題