C ++字符串中的问题 [英] Problem In C++ String

查看:79
本文介绍了C ++字符串中的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

1:请告诉我如何在C ++中限制用户的字符数量。



2:还告诉我这个程序中的错误因为什么时候我从用户输入输入(学生姓名)并且它ID不显示我任何想法。如果它偶然显示输出然后它只显示用户的名字而不是全名由用户输入。请参阅代码如下。



例如:

用户输入:QASIM ASHAN

Usman Ahsan,Zaid Ahsan。



代码:

  #include   <   iostream  >  
使用 命名空间标准;

main()
{
char name [ 20 ];
int x;
for (x = 0 ; x< 3; x ++)
{
cout<< 输入学生<< x + 1<< < span class =code-string>
名称:;
cin.getline(name, 20 );
}
cout<< 输入名称详细信息。<< ; ENDL;
for (x = 0 ; x< 3; x ++)
{
cout<< name [x];
}
system( pause);
}

解决方案

这是因为您将整个字符串读取到同一个缓冲区并在呈现时结果,您只显示它的前3个字符。也许你打算使用一系列缓冲区。类似于:

  char  name  [ 3 ]  [ 20 ]; 
int x;

for (x = 0 ; x< 3; x ++)
{
cout<< 输入学生<< x + 1< ;< 名称:;
cin.getline(name [x] 20 );
}

cout<< 输入名称详细信息。<< ENDL;

for (x = 0 ; x< 3; x ++)
{
cout<< name [x];
}

system( pause);


1:Please Tell me How can i Take Limit Number Of Character From The User In C++.

2:And also tell me Error in this program Because when i take Input From The User ("Student Name") and it id did Not Show Me Any think.if by chance it show OUTPUT Then it only show the First Name Of The User not full name Which Enter By the User.Please See The Code Below.

For Example:
User Input:QASIM ASHAN
Usman Ahsan, Zaid Ahsan.

Code:

#include<iostream>
using namespace std;

main()
{
    char name[20];
    int x;
    for(x=0; x<3; x++)
    {
        cout<<"Enter Student "<<x+1<<" Name:";
        cin.getline(name,20);
    }
    cout<<"Enter Name Detail."<<endl;
    for(x=0;x<3;x++)
    {
        cout<<name[x];
    }
    system("pause");
}

解决方案

That happens because you read the whole of the strings to the same buffer and, when presenting the results, you show only the first 3 characters of it. Maybe you intended to use an array of buffers. Something like:

char name[3][20];
int x;

for(x=0;x<3;x++)
{
	cout<<"Enter Student "<<x+1<<" Name:";
	cin.getline(name[x],20);
}

cout<<"Enter Name Detail."<<endl;

for(x=0;x<3;x++)
{
	cout<<name[x];
}

system("pause");


这篇关于C ++字符串中的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆