1. 创建全局字符数组名称 inputData 和大小 100- 输入字 创建大小为输入字的动态数组;
2。 添加动态数组,存储指向所有单词的指针。输入新单词效果显示所有单词。如果数组太小,则加倍数组大小。
所以我现在如何先创建数组、创建数组并在循环中输入,但我现在不知道如何将单词添加到数组而不是覆盖然后显示所有单词。有什么帮助吗? 也许我只是不明白这个任务。
#include <iostream>;
#include <string.h>;
#include <stdio.h>
char *inputData= new char[100];
using namespace std;
int main(){
// cin>>::inputData;
//char* tablica2= new char (strlen(inputData));
cout<<"how many words"<<endl;
int input;
cin>>input;
int temp=0;
int len=0;
char *tab1= new char[input];
for(int i=0;i<input;i++){
cout<<" enter word "<<i+1<<endl;
char* tem2= new char[20];
cin>>::inputData;
len=len+strlen(::inputData);
cout<<len;
// int temp= temp+ strlen(::inputData);
//cout<<::inputData[temp];
// *(inputData+i)=*tem;
}
最佳答案
我不确定我是否理解你想做什么,但也许你可以使用 ostrstream 类来解决你的部分问题:
#include <iostream.h>
#include <strstream.h>
using namespace std;
main(){
char buf[100];
ostrstream out(buf,sizeof(buf)) //opens output array,like cout but,
//writes the data to the array.
out << "Write text that will be written into buf\n";
out << "This will be attached";
out << ends; //ensures that buf is null-terminated
cout << buf;
return (0);
}
希望对您有所帮助。
关于c++ - cin word 到 char 数组,每个输入的单词显示所有单词。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40401530/