gpt4 book ai didi

c++ - 有符号/无符号不匹配和函数在转换为函数时不带 2 个参数

转载 作者:太空宇宙 更新时间:2023-11-04 13:38:26 25 4
gpt4 key购买 nike

所以我能够编写一个将字符串转换为摩尔斯电码的程序,但是老师想要一个函数中的转换部分,我做到了,但是它给出了错误警告 C4018:“<”:第 29 行有符号/无符号不匹配和错误 C2660:“toMorse”:函数在第 18 行不接受 2 个参数你会认为在能够让正常程序运行之后这将是小菜一碟,但此时我的大脑已经死了。任何帮助找出它为什么这样做的帮助将不胜感激,尽管我想我可以在 main{} 中将它全部交上任务不会太糟糕。

#include <iostream>
#include <string>
using namespace std;

// Function Prototypes
string toMorse(char);

int main()
{

string userInput;

cout << "Please input a string to convert to morse code." << endl;
getline(cin, userInput, '\n');

int stringSize = userInput.length();

toMorse(userInput, stringSize);

}

//*****************************************************************
//Function to convert into morse code
//*****************************************************************
string toMorse(string morseArray, int stringSize)
{
string *toConvert = new string[stringSize];

for (size_t i = 0; i < stringSize; i += 1)
{
if (toupper(userInput.at(i)) == 'A')
toConvert[i] = ".-";
// will fill in the rest once problems are solved
else toConvert[i] = " ";
}

for (size_t x = 0; x < stringSize; x += 1)
cout << toConvert[x] << " ";

cout << endl;

delete[] toConvert;

return 0;
}

所以我能够根据给出的提示让它工作,但现在每次我运行以下代码时(它翻译正确,就在我按下回车键时它显示正确的输入但也说“调试断言失败!表达式: MSVCP120D.dll 第 1168 行上的空指针无效

#include <iostream>
#include <string>
using namespace std;

// Function Prototypes
string toMorse(string, size_t);

int main()
{

string userInput;

cout << "Please input a string to convert to morse code." << endl;
getline(cin, userInput, '\n');

int stringSize = userInput.length();

toMorse(userInput, stringSize);

}

//*****************************************************************
//Function to convert into morse code
//*****************************************************************
string toMorse(string morseArray, size_t stringSize)
{
string *toConvert = new string[stringSize];

for (size_t i = 0; i < stringSize; i += 1)
{
if (toupper(morseArray.at(i)) == ' ')
toConvert[i] = " ";
else if (toupper(morseArray.at(i)) == ',')
toConvert[i] = "--..--";
else if (toupper(morseArray.at(i)) == '.')
toConvert[i] = ".-.-.-";
else if (toupper(morseArray.at(i)) == '?')
toConvert[i] = "..--..";
else if (morseArray.at(i) == '0')
toConvert[i] = "-----";
else if (morseArray.at(i) == '1')
toConvert[i] = ".----";
else if (morseArray.at(i) == '2')
toConvert[i] = "..---";
else if (morseArray.at(i) == '3')
toConvert[i] = "...--";
else if (morseArray.at(i) == '4')
toConvert[i] = "....-";
else if (morseArray.at(i) == '5')
toConvert[i] = ".....";
else if (morseArray.at(i) == '6')
toConvert[i] = "-....";
else if (morseArray.at(i) == '7')
toConvert[i] = "--...";
else if (morseArray.at(i) == '8')
toConvert[i] = "---..";
else if (morseArray.at(i) == '9')
toConvert[i] = "----.";
else if (toupper(morseArray.at(i)) == 'A')
toConvert[i] = ".-";
else if (toupper(morseArray.at(i)) == 'B')
toConvert[i] = "-...";
else if (toupper(morseArray.at(i)) == 'C')
toConvert[i] = "-.-.";
else if (toupper(morseArray.at(i)) == 'D')
toConvert[i] = "-..";
else if (toupper(morseArray.at(i)) == 'E')
toConvert[i] = ".";
else if (toupper(morseArray.at(i)) == 'F')
toConvert[i] = "..-.";
else if (toupper(morseArray.at(i)) == 'G')
toConvert[i] = "--.";
else if (toupper(morseArray.at(i)) == 'H')
toConvert[i] = "....";
else if (toupper(morseArray.at(i)) == 'I')
toConvert[i] = "..";
else if (toupper(morseArray.at(i)) == 'J')
toConvert[i] = ".---";
else if (toupper(morseArray.at(i)) == 'K')
toConvert[i] = "-.-";
else if (toupper(morseArray.at(i)) == 'L')
toConvert[i] = ".-..";
else if (toupper(morseArray.at(i)) == 'M')
toConvert[i] = "--";
else if (toupper(morseArray.at(i)) == 'N')
toConvert[i] = "-.";
else if (toupper(morseArray.at(i)) == 'O')
toConvert[i] = "---";
else if (toupper(morseArray.at(i)) == 'P')
toConvert[i] = ".--.";
else if (toupper(morseArray.at(i)) == 'Q')
toConvert[i] = "--.-";
else if (toupper(morseArray.at(i)) == 'R')
toConvert[i] = ".-.";
else if (toupper(morseArray.at(i)) == 'S')
toConvert[i] = "...";
else if (toupper(morseArray.at(i)) == 'T')
toConvert[i] = "-";
else if (toupper(morseArray.at(i)) == 'U')
toConvert[i] = "..-";
else if (toupper(morseArray.at(i)) == 'V')
toConvert[i] = "...-";
else if (toupper(morseArray.at(i)) == 'W')
toConvert[i] = ".--";
else if (toupper(morseArray.at(i)) == 'X')
toConvert[i] = "-..-";
else if (toupper(morseArray.at(i)) == 'Y')
toConvert[i] = "-.--";
else if (toupper(morseArray.at(i)) == 'Z')
toConvert[i] = "--..";
else toConvert[i] = " ";
}

for (size_t x = 0; x < stringSize; x += 1)
cout << toConvert[x] << " ";

cout << endl;

delete[] toConvert;

return 0;
}

非常感谢。通过将其更改为正确的返回类型,我能够使其正常运行。再次感谢。感谢大家的帮助!

最佳答案

C4018 警告是关于比较的。您可以像这样使用函数原型(prototype):

string toMorse(string morseArray, size_t stringSize);

此外,您还在文件的开头将 toMorse 函数定义为 string toMorse(char);,这与下面的内容不同。

关于c++ - 有符号/无符号不匹配和函数在转换为函数时不带 2 个参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28727922/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com