非常简单的 c++ 中的 lnk2019 错误程序

发布于 2024-08-26 00:33:34 字数 5289 浏览 9 评论 0原文

我尝试过删除各种部件并进行构建,但没有任何方法可以使 lnk2019 错误消失,甚至不会产生任何正常错误。

目前所有内容都在一个文件中(完成后不会更晚)。该程序具有三个单词列表,并从中创建一个行话短语,您应该能够添加单词、删除单词、查看列表、恢复默认值、保存对文件的更改以及从文件加载更改。

#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!

I have tried removing various parts and building, but nothing makes the lnk2019 error go away, or even produces any normal errors.

Everything is in the one file at the moment (it won't be later when it is finished). The program has three lists of words and makes a jargon phrase out of them, and you are supposed to be able to add words, remove words, view the lists, restore defaults, save changes to file, and load changes from file.

#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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

¢蛋碎的人ぎ生 2024-09-02 00:33:34

根据 gcc 的问题是,您尚未声明 rand() 函数的正确包含 - 我添加了 #include 然后问题就消失了。

我还应该补充一点,在您的主要功能中,您已经完成了以下操作:

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

这不起作用。 Mainselection 是一个字符串,只能使用类比较方法进行比较,如下所示:

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

如果相等,则返回 0。

另外,如果您使用 std::vector ,您可以在 C++ 中非常轻松地动态调整大小的数组,只需不断地将项目推到向量的后面即可。这意味着您不需要对限制进行硬编码。

The problem according to gcc is that you haven't declared the correct include for the rand() function - I added #include <cstdlib> and the problem disappeared.

I should also add that in your main function you've done this:

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

which doesn't work. Mainselection is a string and can only be compared using the class compare method, like so:

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

that'll return 0 if they're equal.

Also, if you use std::vector you can have dynamically sized arrays very easily in C++, you just keep pushing items onto the back of the vector. Will mean you don't need to hard-code the limits.

落墨 2024-09-02 00:33:34

一些可以帮助您的建议:

  • 第 107 行:if(num4 = 0)(赋值运算符)应为 if(num4 == 0)(等于运算符)。
  • 第 260 行:cin >> 后面缺少一个分号主要选择
  • 第 261 行:if(mainselection == 1) 是非法的,mainselectionstd::string
  • 第 284 行: if(makeselection == "2") main(); 不允许在 C++ 中递归调用 main() 函数。

Some suggestions to help you:

  • Line 107: if(num4 = 0) (assignment operator) should be if(num4 == 0) (equal to operator).
  • Line 260: You're missing a semicolon after cin >> mainselection.
  • Line 261: if(mainselection == 1) is illegal, mainselection being a std::string.
  • Line 284: if(makeselection == "2") main(); You're not allowed to call the main() function recursively in C++.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文