C和C+中的字符串输入+; 我想编写一个C或C++的程序,要求用户在不同时间的运行时间输入字符串,如用户所给出的(空间分离或非空间分离),并将其存储到数组中。请给我C和C++的样例代码。

C和C+中的字符串输入+; 我想编写一个C或C++的程序,要求用户在不同时间的运行时间输入字符串,如用户所给出的(空间分离或非空间分离),并将其存储到数组中。请给我C和C++的样例代码。,c++,C++,e、 g 现在char数组[]=“foo” 现在char数组[]=“池” 我试过: #include<iostream> using namespace std; int main() { int n; cout<<"enter no. of chars in string"; cin>>n; char *p=new char[n+1]; cout<<"enter the string"<<en

e、 g

现在
char数组[]=“foo”

现在
char数组[]=“池”

我试过:

#include<iostream>
using namespace std;

int main()
{
    int n;
    cout<<"enter no. of chars in string";
    cin>>n;
    char *p=new char[n+1];
    cout<<"enter the string"<<endl;
    cin>>p;
    cout<<p<<endl;
    cout<<p;
    return 0;
} 
#包括
使用名称空间std;
int main()
{
int n;
coutn;
char*p=新字符[n+1];
不能使用getline

看看这个例子:

您需要从stdin读取字符,直到遇到某个终止符(例如换行符)并将该字符附加到临时字符数组(char*)的末尾。为此,您应该手动控制溢出和扩展(重新分配+复制)根据需要设置数组。

当您按enter键时,您必须处理同时馈送到cin的额外字符。getline()如果处理好,它将正常工作。这是Windows中cin的行为。Linux中可能也会发生同样的情况。如果您在Windows中运行此代码,它将为您提供所需的内容

#include <iostream>
using namespace std;

int main () {
    int n;

    // Take number of chars
    cout<<"Number of chars in string: ";
    cin>>n;

    // Take the string in dynamic char array
    char *pStr = new char[n+1];
    cout<<"Enter your string: "<<endl;

    // Feed extra char
    char tCh;
    cin.get(tCh);

    // Store until newline
    cin.getline(pStr, n+1);
    cout<<"You entered: "<<pStr<<endl;

    // Free memory like gentle-man
    delete []pStr;

    // Bye bye
    return 0;
}
#包括
使用名称空间std;
int main(){
int n;
//取数
coutn;
//在动态字符数组中获取字符串
char*pStr=新字符[n+1];

CoutPad代码,解释你有什么困难。这也应该是C还是C++?代码需要进入问题,而不是评论。我已经编辑了它,现在请你在下面的问题上再详细说明。@高塔库马尔,顺便说一下C++ @高塔:为什么你是SH?你为什么要在你的问题中编辑未格式化的代码,尽管你可以清楚地看到它没有正确显示,尽管你之前已经演示了如何格式化你的代码?尽管有大量的“格式化常见问题解答”出现在你写文章的地方旁边的窗格?尽管你已经成为Stack Overflow的成员将近一年半了,身后有26个问题?拜托…@Tomalak:鉴于两个句子的第一个字母都是唯一未大写的字母,我认为caps lock是意外打开的。这并不是一个好问题不过,请原谅。但这并不是要求用户给出字符串的长度。它是在编译时静态声明一个字符数组。我希望在运行时初始化数组。@gautam kumar我很确定,您可以不用我为您编写程序就可以这样做。像您在发布的代码中那样分配缓冲区。然后使用getline,如图所示在本例中::我已经尝试过,但它不起作用。请看我在问题中也给出了它。如果您使用的是字符串库版本,那么您可以向它传递一个
std::string
对象,而不必担心分配正确的内存量。
#include<iostream>
using namespace std;

int main()
{
    int n;
    cout<<"enter no. of chars in string";
    cin>>n;
    char *p=new char[n+1];
    cout<<"enter the string"<<endl;
    cin>>p;
    cout<<p<<endl;
    cout<<p;
    return 0;
} 
#include <iostream>
using namespace std;
int main()
{
    int n;
    cout<<"enter no. of chars in string";
    cin>>n;
    char *p=new char[n+1];
    cout<<"enter the string"<<endl;
    cin.getline(p,n);
    cout<<p<<endl;
    cout<<p;
    return 0;
}
#include <iostream>
using namespace std;

int main () {
    int n;

    // Take number of chars
    cout<<"Number of chars in string: ";
    cin>>n;

    // Take the string in dynamic char array
    char *pStr = new char[n+1];
    cout<<"Enter your string: "<<endl;

    // Feed extra char
    char tCh;
    cin.get(tCh);

    // Store until newline
    cin.getline(pStr, n+1);
    cout<<"You entered: "<<pStr<<endl;

    // Free memory like gentle-man
    delete []pStr;

    // Bye bye
    return 0;
}