【发布时间】:2013-06-18 04:31:51
【问题描述】:
大家好,我尝试寻找答案,但找不到。 (我找到了如何使用它)但问题是我只是不知道为什么我的代码不起作用。这是我的代码:
#include <iostream>
#include <string>
using namespace std;
string acorta(string palabra, int carac)
{
string acortado;
acortado=palabra;
if(palabra.length() > carac)
{
return acortado;
}
else
{
acortado.resize(carac);
return acortado;
}
}
int main(int argc, char** argv) {
cout<< acorta("Univesidad",5)<<endl; // here it should show "Unive"
cout<< acorta("Secretariado",10)<<endl; //here it should show "Secretaria"
cout<< acorta("Estudio",11)<<endl; //here it should show "Estudio" since th number is long than the word
return 0;
}
据说程序应该接收一个字符串和一个整数,因为它应该返回字符串,只要 int 说。例如 ("Laptop",4) 它应该返回 "Lapt"。如果 int 比单词大,那么它应该返回整个单词。
问题是程序在不应该这样做的时候返回了整个单词。所以我认为问题在于它没有进入我的功能。如果我错了,请纠正我。
【问题讨论】:
-
嗨。你自己尝试过什么来修复它,它是如何破坏的?
-
@chris 谢谢。伟大的贡献,而且我不敢相信我没有看到那个粗心和过度看到的错误。我正在努力解决这个问题(我是编程新手)。再次感谢。
-
@TarynEast 我尝试移动除了 if(palabra.length > carac) 部分之外的所有内容。我不知道为什么我没有想到那部分,我只是认为这是理所当然的。
标签: c++ string function resize user-defined-functions