c++ - cin word 到 char 数组,每个输入的单词显示所有单词。

标签 c++ arrays char

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/

相关文章:

c# - 选择随机字符

c++ - 用于在嵌套容器中删除的删除删除惯用语? (删除外部的;C++ STL)

c++ - Eclipse c++ makefile 项目输出

javascript - 我必须创建一个函数,它返回传入的数组的最后一个元素

python - 如何在 Python 中创建数组

c++ - 如果它是数字 c++,则测试将 int 转换为 char

C++ 类内线程并发

c++ - glClientWaitSync 不适用于 MacOS

python - 有效地将 3D Numpy 数组 reshape 为 1D 列表加坐标向量

c - 带有用户输入的结构数组内的 strstr char 数组