Click here to Skip to main content
15,885,839 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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:
C++
#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");
}
Posted
Updated 11-Aug-14 5:26am
v3

1 solution

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:


C++
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");
 
Share this answer
 
v2
Comments
Member 11004573 11-Aug-14 11:07am    
Yes you Give me the Correct Solution Thank you For Answer me , But there is still little Problem. When i take Number OF STUDENT From the user then loop skip one name and start from the 2nd Please Tell me About the Problem .

// student_Record_Management_C++_1.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include<iostream>
#include<string>
using namespace std;
class Menu_Account
{
private:
char name[3][20];
int i,student;
public:
void get_name();
};
//Get Name From The User
void Menu_Account::get_name()
{
cout<<"How Many Name Student You Have:";
cin>>student;
for ( i = 0; i < student; i++)
{
cout<<"Enter Student "<<i+1<<" Name:";
cin.getline(name[i],20);
}
cout<<"Enter Name is."<<endl;
for ( i = 0; i < student; i++)
{
cout<<name[i];
}
}
int _tmain(int argc, _TCHAR* argv[])
{
Menu_Account obj;
obj.get_name();
system("pause");
return 0;
}
Shmuel Zang 11-Aug-14 14:27pm    
The code above doesn't look like it skips a name. But, what happens if the user enters a student number greater than 3...
Maybe you want to use an std::vector.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900