将单词读入指向结构的动态分配数组
Reading in words to a dynamically allocated array pointing to a struct
我正在尝试获取用户输入的字符串,然后将其清理一下并将逐字存储在动态分配的数组中。该数组指向一个结构。我试图将数组存储在 "English" 结构变量中。
这是我想出的,但是当我 运行 代码时,我的数组测试 cout 没有输出,所以我觉得这些词没有被复制到数组中。
//This program will take in a English sentence from the user where it will then store the string in an dynamically allocated array
//where it will be run through a functions which will convert the English sentence to pig Latin. The
//final sentence will be displayed to the screen for the user to see.
#include <iostream>
#include <cstring>
#include <string>
using namespace std;
struct Word{
string english;
string piglatin;
};
Word * sentenceSetup(string, int &);
int main()
{
int Size=1;//default how many words in the sentence.
string userSent;
//ask the user for a sentence they want to convert
cout<<"Hello, please enter a string to convert to PigLatin: "<<endl;
getline(cin, userSent);
Word * arr = sentenceSetup(userSent,Size);
for (int i=0; i<Size;i++)
{
cout<< arr[i].english<<endl;
}
return 0;
}
//**************************************************************
//sentenceSetup Definition: In this function we will be asking *
//the user to enter in a string which then will be counted *
//for how many words it has and creating an array of structs *
//big enough to hold that string. The array will then be *
//returned to the calling function. NOTE: This function should *
//remove all capitalization and special characters except for *
//the end period, exclamation mark, or question mark. *
//**************************************************************
Word * sentenceSetup(string userSent, int &size)
{
char nextCharacter;
//check the input for any unwanted special characters not listed in the function def.
for( int i=0; i<int(userSent.size()); i++)
{
nextCharacter = userSent.at(i);
if(ispunct(userSent[i]))
{
userSent.erase(i--, 1);
}
}
//change the whole sentence to lower case
for (int i=0; i<int(userSent.size()); i++)
{
userSent[i]=tolower(userSent[i]);
}
//Check each character in the string to see if it is a space. If the loop
//notices a space then a space equals a word in the string.
for (int i =0; i<int(userSent.size());i++)
{
nextCharacter = userSent.at(i); //Reads the character
if(isspace(userSent[i]))
{
size++;
}
}
//test to see if num count works
cout<<"There is "<<size << " words in the sentence."<<endl;
//test to see if special characters removed
//cout<<userSent<<endl;
//allocate an array to store the words in for the struct Word
Word *temp= new Word[size];
int count =0;
string word;
for(count=0;count<size;count++)
{
if(isspace(userSent[count]))
{
word =userSent.substr(0,count);
temp[count].english=word;
userSent.erase(0,count);
}
}
//test
for(count =0; count<size;count++)
{
cout<<temp[count].english;
}
return temp;
}
问题在于您如何尝试将数据写入动态分配的 Word* 临时变量以及您为此使用的 for 循环。以上代码需要修改如下
我正在创建一个 main 字符串数组,该数组将由函数
返回
//allocate an array to store the words in for the struct Word
Word *main= new Word[size];
int count =0;
string word;
//Variable to keep track of start of each word
int start_count =0;
//Create a temp pointer to iterate through the memory allocated for each word
Word *temp = main;
//Variable to keep track of length of each word
int bytes_to_cpy =0;
for循环应该循环到句子的长度,而不仅仅是单词的数量
for(count=0;count<=userSent.length();count++){
if(isspace(userSent[count]))
{
//Copy the required bytes as a substring
word =userSent.substr(start_count,bytes_to_cpy);
//Copy the word to the main array
temp->english=word;
//Increment the pointer;
temp++;
//Ignore the white space for the next word
start_count=count+1;
//Reset the length of the word
bytes_to_cpy=0;
}
//Count the length of the word
bytes_to_cpy++;
}
//Add the last word to the array
word =userSent.substr(start_count,userSent.length()-start_count);
temp->english = word;
temp = main;
//test
for(count =0; count<size;count++)
{
cout<<temp->english<<endl;
temp++;
}
return main;
除此之外,您还需要处理多个空格,在我们计算字数的函数中用户输入字符串开头和结尾的空格。您当前的实施不考虑这些。
我正在尝试获取用户输入的字符串,然后将其清理一下并将逐字存储在动态分配的数组中。该数组指向一个结构。我试图将数组存储在 "English" 结构变量中。
这是我想出的,但是当我 运行 代码时,我的数组测试 cout 没有输出,所以我觉得这些词没有被复制到数组中。
//This program will take in a English sentence from the user where it will then store the string in an dynamically allocated array
//where it will be run through a functions which will convert the English sentence to pig Latin. The
//final sentence will be displayed to the screen for the user to see.
#include <iostream>
#include <cstring>
#include <string>
using namespace std;
struct Word{
string english;
string piglatin;
};
Word * sentenceSetup(string, int &);
int main()
{
int Size=1;//default how many words in the sentence.
string userSent;
//ask the user for a sentence they want to convert
cout<<"Hello, please enter a string to convert to PigLatin: "<<endl;
getline(cin, userSent);
Word * arr = sentenceSetup(userSent,Size);
for (int i=0; i<Size;i++)
{
cout<< arr[i].english<<endl;
}
return 0;
}
//**************************************************************
//sentenceSetup Definition: In this function we will be asking *
//the user to enter in a string which then will be counted *
//for how many words it has and creating an array of structs *
//big enough to hold that string. The array will then be *
//returned to the calling function. NOTE: This function should *
//remove all capitalization and special characters except for *
//the end period, exclamation mark, or question mark. *
//**************************************************************
Word * sentenceSetup(string userSent, int &size)
{
char nextCharacter;
//check the input for any unwanted special characters not listed in the function def.
for( int i=0; i<int(userSent.size()); i++)
{
nextCharacter = userSent.at(i);
if(ispunct(userSent[i]))
{
userSent.erase(i--, 1);
}
}
//change the whole sentence to lower case
for (int i=0; i<int(userSent.size()); i++)
{
userSent[i]=tolower(userSent[i]);
}
//Check each character in the string to see if it is a space. If the loop
//notices a space then a space equals a word in the string.
for (int i =0; i<int(userSent.size());i++)
{
nextCharacter = userSent.at(i); //Reads the character
if(isspace(userSent[i]))
{
size++;
}
}
//test to see if num count works
cout<<"There is "<<size << " words in the sentence."<<endl;
//test to see if special characters removed
//cout<<userSent<<endl;
//allocate an array to store the words in for the struct Word
Word *temp= new Word[size];
int count =0;
string word;
for(count=0;count<size;count++)
{
if(isspace(userSent[count]))
{
word =userSent.substr(0,count);
temp[count].english=word;
userSent.erase(0,count);
}
}
//test
for(count =0; count<size;count++)
{
cout<<temp[count].english;
}
return temp;
}
问题在于您如何尝试将数据写入动态分配的 Word* 临时变量以及您为此使用的 for 循环。以上代码需要修改如下
我正在创建一个 main 字符串数组,该数组将由函数
返回//allocate an array to store the words in for the struct Word
Word *main= new Word[size];
int count =0;
string word;
//Variable to keep track of start of each word
int start_count =0;
//Create a temp pointer to iterate through the memory allocated for each word
Word *temp = main;
//Variable to keep track of length of each word
int bytes_to_cpy =0;
for循环应该循环到句子的长度,而不仅仅是单词的数量
for(count=0;count<=userSent.length();count++){
if(isspace(userSent[count]))
{
//Copy the required bytes as a substring
word =userSent.substr(start_count,bytes_to_cpy);
//Copy the word to the main array
temp->english=word;
//Increment the pointer;
temp++;
//Ignore the white space for the next word
start_count=count+1;
//Reset the length of the word
bytes_to_cpy=0;
}
//Count the length of the word
bytes_to_cpy++;
}
//Add the last word to the array
word =userSent.substr(start_count,userSent.length()-start_count);
temp->english = word;
temp = main;
//test
for(count =0; count<size;count++)
{
cout<<temp->english<<endl;
temp++;
}
return main;
除此之外,您还需要处理多个空格,在我们计算字数的函数中用户输入字符串开头和结尾的空格。您当前的实施不考虑这些。