C++ 将字符串逐字符读入函数
我不确定我是否朝着正确的方向前进。该函数起作用。我原以为C++ 将字符串逐字符读入函数,c++,C++,我不确定我是否朝着正确的方向前进。该函数起作用。我原以为for循环也能工作,但事实并非如此 #include <iostream> #include <iomanip> #include <string> #include <cmath> #include <fstream> using namespace std; void bear(string word) { cout << " (c).-.(c) \
for
循环也能工作,但事实并非如此
#include <iostream>
#include <iomanip>
#include <string>
#include <cmath>
#include <fstream>
using namespace std;
void bear(string word) {
cout << " (c).-.(c) \n";
cout << " / ._. \\ \n";
cout << " __\\( Y )/__ \n";
cout << "(_.-/'-'\\-._)\n";
cout << " || " << word << " || \n";
cout << " _.' `-' '._ \n";
cout << "(.-./`-'\\.-.) \n";
cout << " `-' `-' \n";
}
int main()
{
string word;
cout << "Input a word: ";
cin >> word;
for (int i = 0; i < word.length(); i++)
cout << bear(word[i]);
}
#包括
#包括
#包括
#包括
#包括
使用名称空间std;
空熊(字符串字){
您可能有一些不匹配的数据类型
首先,cout#包括
#包括
#包括
#包括
#包括
使用名称空间std;
空熊(字符串字){
coutfor循环不工作,因为函数bear没有返回任何内容
换言之
您正在std::out中调用bear函数,这意味着需要输出一些数据和值。但是您的bear函数不返回任何内容。您的bear函数正在执行输出
要使For循环正常工作,只需删除cout,然后像正常情况一样调用bear函数。std::string
没有只接受单个char
作为输入的构造函数,因此您不能像现在这样将word[i]
传递给bear()
而且,您的for
循环毫无意义
摆脱循环,并按原样将完整的word
传递给bear()
另外,bear()
没有可以传递给cout
的返回值,因此您也需要删除该代码
试试这个:
int main()
{
string word;
cout << "Input a word: ";
cin >> word;
bear(word);
}
intmain()
{
字符串字;
cout>单词;
熊(字);
}
为什么你认为你必须在这个案例中应用循环?调用cout它打印什么?@MichaelChourdakis我怀疑这个代码。您好,欢迎来到StackOverflow!我们很乐意提供帮助,但很难确切地说出你想要实现什么。你想让字母在动画中一个接一个地出现在页面顶部吗同一只熊?你想要很多只熊,每个熊都有自己的字母吗?请你的问题让它更清楚你想做什么。如果它做了你想做的。@Pblogetas
int main()
{
string word;
cout << "Input a word: ";
cin >> word;
bear(word);
}