Click here to Skip to main content
15,897,371 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello Everyone!

I Am Making Littler program in C++ Using Class, But i STILL got one Error,When i take Student Number From the User then it skip the One Name And Start From the 2nd Name ,Please Tell Me About the Problem.
C++
// 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;
public:
	void get_name(int);
};
//Get Name From The User
void Menu_Account::get_name(int student)
	{
		cout<<"\n\t\t\tEnter Student Name."<<endl;
		i=0;
		do
		{//Problem Start From There
			cout<<"Enter Student "<<i+1<<" Name:";
			cin.getline(name[i],20);
		}while(i<student);>
		cout<<"Enter Name is."<<endl;
		for ( i = 0; i < student; i++)
		{
			cout<<name[i]<<endl;
		}
	}
int _tmain(int argc, _TCHAR* argv[])
{
	int student=0;
	Menu_Account obj;
	cout<<"How Many Student You Have:";
	cin>>student;
	obj.get_name(student);
	system("pause");
	return 0;
}
Posted
Updated 17-Aug-14 3:06am
v2
Comments
[no name] 17-Aug-14 9:15am    
You would have to fix the compilation errors in your code first.

The problem is that you never increment the value of i. i will always be the same in your do ... while loop, so the loop will be infinite. At the end of the code in your do ... while loop, add i++;.

Also, the size of your array with student names is 3. And if the given number of students is more than 3, your program won't be able to store more than 3 students in the array. So you'll need to check whether the given number of students is greater than 3. And instead of hard-coding that number, you can write a #define statement for it to make it a constant (you can do the same for the length). Why? Because if you use the same number at multiple places, you just have to change the value of your constant, instead of all occurrences of the number.

And as Wes Aday said, you'll need to use cin.ignore() to ignore the terminating newline character, which is still on the stream.

So, you can change your code into this:

Add this line of code before your Menu_Account class:
C++
#define MAX_STUDENTS 3
#define MAX_LENGTH 20

Then, in your Menu_Account class, replace char name[3][20]; with:
C++
char name[MAX_STUDENTS][MAX_LENGTH];

And then, update your get_name method:
C++
void Menu_Account::get_name(int student)
{
    if (student > MAX_STUDENTS)
    {
        cout<<"Number too high, there are max. "<<MAX_STUDENTS<<" accepted."<<endl;
        return;
    }
    cout<<"\n\t\t\tEnter Student Name."<<endl;
    i=0;
    do
    {//Problem Start From There
        cout<<"Enter Student "<<i+1<<" Name:";
        cin.getline(name[i],20);
        cin.ignore();
        i++;
    }while(i<student);
    cout<<"Enter Name is."<<endl;
    for ( i = 0; i < student; i++)
    {
        cout<<name[i]<<endl;
    }
}
 
Share this answer
 
v4
Comments
Member 11004573 17-Aug-14 9:16am    
No Thsi is No Error , my Error Is Logically when i take Student Number From the User then it skip the Student Name And Start Name Taking From the Student 2,this is my problem.
[no name] 17-Aug-14 9:39am    
Yes! Well that is the next problem you will have. If you had said that the getline is being skipped over because there is still a '\n' left in the buffer, then someone would have told you to call cin.ignore().
Thomas Daniels 17-Aug-14 10:12am    
I edited my answer.
Hi,

In your code as people told, after entering students number , you are pressing enter button. So this will be still in buffer and which will be taken as name for first student. To avoid this behavior, after cin.getline(name[i],20); line place cin.Ignore() or getchar(). Then you will get your desired result.
 
Share this answer
 
Comments
Member 11004573 18-Aug-14 1:39am    
Thank you For Telling Me.
You are always welcome, finally you got the solution. Its pleasure to hear you.
 
Share this answer
 

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