C++ C++;字符串作为函数中的数组,不断获取错误消息
我的任务是创建一个简单的字符串读取器,要求用户输入一个句子,并修复以下问题:C++ C++;字符串作为函数中的数组,不断获取错误消息,c++,arrays,string,function,C++,Arrays,String,Function,我的任务是创建一个简单的字符串读取器,要求用户输入一个句子,并修复以下问题: 首先,使用intsplitsent的函数原型(字符串句子、字符串单词[]、intmaxwords)其中函数返回句子中的字数和“maxWords” 如果用户的输入以小写字母开头,请将其大写 如果用户输入包含字符串“计算机科学”,请将字符串切换为“CS” 对于第一个任务,我被迫不使用动态数组、向量和任何“char”变量。此外,我必须保留给定的原型 我当然可以在主函数中使用单个for循环来计算有多少个单词,但是给定的任务
- 首先,使用
intsplitsent的函数原型(字符串句子、字符串单词[]、intmaxwords)代码>其中函数返回句子中的字数和“maxWords”
- 如果用户的输入以小写字母开头,请将其大写
- 如果用户输入包含字符串“计算机科学”,请将字符串切换为“CS”
In function 'int main()':
[Error] expected primary-expression before ']' token
At global scope:
[Error] declaration of 'words' as array of references
[Error] expected ')' before ',' token
[Error] expected unqualified-id before 'int'
我被困在这里好几个小时了,我再也不能从谷歌上得到更多了。请告知
#include <iostream>
#include <string>
using namespace std;
int splitSent(string sentence, string words[], int maxWords);
int main()
{
string sentence, words;
int maxWords;
cout << "Enter a sentence. (Maximum words allowed - 100)" << endl;
getline(cin, sentence);
splitSent(sentence, words[], maxWords);
return 0;
}
int splitSent(string sentence, string& words[], int& maxWords)
{
int temp = 0, count = 1;
for (int j = 0; j < sentence.length(); j++)
if (sentence[i] == ' ')
count++;
words[count];
maxWords == count;
for (int i = 0; i < sentence.length(); i++) {
if (sentence[i] == ' ')
temp++;
else if (sentence[i] != ' ')
words[temp] += sentence[i];
}
return (count);
}
#包括
#包括
使用名称空间std;
int splitSent(字符串句子、字符串单词[]、int maxWords);
int main()
{
串句、词;
int-maxWords;
结果表明,存在语法错误和逻辑问题
我注意到您试图将单词[]
传递到函数中。
何时使用单词
以及何时使用单词[]
可能会造成混淆。
声明时,请使用字符串[]
,例如在函数定义的参数列表中。
这是为了告诉函数期望得到什么。
当你传递信息时,请使用单词
,因为你传递的是整个信息。
在函数内部,使用words[index]
,因为您引用的是数组的特定元素
您必须将单词
声明为字符串的数组
,而不是字符串
您只需直接传入字符串数组,而不是通过引用传入。
当您直接传递它时,您没有传递数组的副本,编译器确保函数具有数组中第一个元素的地址,因此没有性能损失,您正在处理原始数组-这正是您想要的
顶部的函数声明与下面的函数定义不同。
它们应该是相同的-声明是您应该使用的版本
int splitSent(string sentence, string words[],int maxWords);
您希望通过引用maxWords
,但不应在函数中修改,因为它保留数组的大小。
它会阻止您跑过数组的末尾
函数splitSent
忽略多个空格。它读取maxWords
,但不更新它。
它返回找到的实际字数。
这可能大于maxWords
,但words
数组的写入长度仅为maxWords
// returns number of words in sentence
// updates words[], using `maxWords` to prevent writing past end of array
int splitSent(string sentence, string words[], int maxWords)
{
int word_index = 0;
int letter_count = 0;
//words[count]; // not sure what was intended by this statement
for (int i = 0; i<sentence.length(); i++) {
if (sentence[i] == ' ') {
// handles multiple spaces in a row
if (letter_count > 0) {
word_index++;
letter_count = 0;
}
}
else if (word_index < maxWords) { // check the word_index is within bounds of array
words[word_index] += sentence[i];
letter_count++;
}
}
if (letter_count > 0)
return (word_index + 1);
return (word_index);
}
int main( ) {
string sentence;
int const maxWords = 100; // constant integer, so compiler lets us use it to declare the size of the array
string words[maxWords]; // function expects an array of strings, so have to declare one
cout << "Enter a sentence. (Maximum words allowed - " << maxWords << ")" << endl;
getline(cin, sentence);
int wordCount = splitSent(sentence, words, maxWords);
return 0;
}
//返回句子中的字数
//使用“maxWords”更新单词[],以防止写入超过数组末尾
int splitSent(字符串句子、字符串单词[]、int maxWords)
{
int-word_索引=0;
整数字母计数=0;
//words[count];//不确定此语句的意图
对于(int i=0;i 0){
word_索引++;
字母计数=0;
}
}
否则,如果(word_index0)
返回(单词索引+1);
返回(单词索引);
}
int main(){
串句;
int const maxWords=100;//常量整数,所以编译器允许我们使用它来声明数组的大小
string words[maxWords];//函数需要一个字符串数组,因此必须声明一个字符串数组
cout Google不能代替一本指导书。由于代码错误,你急需几本书。有什么实际的帮助吗…?编译器错误消息没有帮助吗?我一直在函数调用中混淆数组的语法。非常感谢!我忍不住要了单词[]在函数原型中。您能解释一下为什么将maxWords声明为常量变量吗?即使在函数调用后它也不会更改,并且没有常量也可以正常工作。有两个原因。首先,您只能声明一个大小恒定的数组,所以字符串字[maxWords]只有当maxWords
声明为const
时,才允许使用
。其次,当您假定某个值为常量时,最好让编译器检查您是否在任何地方错误地更改了它。通过声明它const
,如果发现您在任何地方写入它,编译器将向您显示一个错误。情况并非如此d在这里检查,但在更大的程序中,手动查找由于将依赖的变量修改为常数而导致的错误可能非常困难。非常感谢。我非常感谢您的帮助。