hi::
i back with small problem and i wish to help me.
i have problem with getline()
i search about the solution and i find it but the problem is still
see the example

#include<iostream>
#include<string>
#include<iomanip>
using namespace std;
#include<ctype.h>

void validate_name(string &name)//validate_name for each student
{
	int i;
	for(i=0;i<(name.length());i++)
	{
		while( !(isalpha(name[i])) && !(isspace(name[i])) )
		{
			cout<<"Enter correct Name of student ,please:\n";
			getline(cin,name);
			i=0;
		}
	}
	
}
class student
{
	private:
		string name;
	public:
		student(string N)
		{
			set_name(N);
		}
		void set_name(string n)
		{
			validate_name(n);
			name=n;
		}
		void print()
		{
			cout<<name;
		}
};
int main()
{
	string name;
	int d,m,y;
	cout<<"Enter name ,please\n";
	getline(cin,name);
	cout<<"Enter the date\n";
	cin>>d>>m>>y;
    student s1(name);
    s1.print();
    cout<<d<<" "<<m<<" "<<y;
	return 0;
}

---------------------------------
The out put
Enter name ,please
123//incorrcet name
Enter the date
1
2
2000
Enter correct Name of student ,please: // i can not write here the name to correct it ,it go out loop without checked and the function print is invoked

1 2 2000
-----------------------------
see the cooments in the output that describe the problem
i hope see the solution and explaination to complet my projct
Thank you and good luck...

Recommended Answers

All 8 Replies

You have two problems:

1. Add a break statement after you set i to zero:

void validate_name(string &name)//validate_name for each student
{
	int i;
	for(i=0;i<(name.length());i++)
	{
		while( !(isalpha(name[i])) && !(isspace(name[i])) )
		{
			cout<<"Enter correct Name of student ,please:\n";
			getline(cin,name);
			i=0;
			break;
		}
	}
	
}

2. You need to get that last newline you left in the buffer after getting the year.

...
cin>>d>>m>>y;
cin.ignore( 10000, '\n' );
...

The trouble happens because you are mixing >> with getline().

Hope this helps.

the problem is solved thank you Mr.Duoas
but i have qustion...
excuse me .....
1-why it must Add a break statement after you set i to zero:???

2- why you put (10000)??in line 3
what does it mean??
Thank you Mr.Duoas so much

If you want to know, take out the break and try the following input:

Enter name,please: 0123 Enter the date 1 1 2007 Enter correct name of student, please 9jake

Also:

Enter name,please: no2 Enter the date 1 1 2007 Enter correct name of student, please m

The 10000 means to read a maximum of 10,000 characters. It is just some really big number that is sure to be larger than any line of text the user input after "2007" but before hitting ENTER.

If you want to know, take out the break and try the following input:

Also:


The 10000 means to read a maximum of 10,000 characters. It is just some really big number that is sure to be larger than any line of text the user input after "2007" but before hitting ENTER.

i take out break and i try your input the code is work and
when i leave it ,the code did not work ??????????

i did not now what the problem???

see the out put:::
Enter name ,please
0123
Enter the date
1 1 2007
Enter correct Name of student ,please:
9jack
9jack//it must show the error massege,to enter correct name ???
1 1 2007

thank you ...

i take out break and i try your input the code is work and
when i leave it ,the code did not work ??????????

Yes, that's the whole point.

Get a piece of paper and a pencil and draw yourself what is happening when you use the code without the break statement, and you will see why it doesn't work.

Good luck.

sorry Duoas i did not understand
the code is not working with break statement
and it is working without it...
see the code i am using eclipse
and the out put

#include<iostream>
#include<string>
#include<iomanip>
using namespace std;
#include<ctype.h>

void validate_name(string &name)//validate_name for each student
{
	int i;
	for(i=0;i<(name.length());i++)
	{
		while( !(isalpha(name[i])) && !(isspace(name[i])) )
		{
			cout<<"Enter correct Name of student ,please:\n";
			getline(cin,name);
			i=0;
			break;
		}
	}
	
}
class student
{
	private:
		string name;
	public:
		student(string N)
		{
			set_name(N);
		}
		void set_name(string n)
		{
			validate_name(n);
			name=n;
		}
		void print()
		{
			cout<<name;
		}
};
int main()
{
	string name;
	int d,m,y;
	cout<<"Enter name ,please\n";
	getline(cin,name);
	cout<<"Enter the date\n";
	cin>>d>>m>>y;
	cin.ignore( 10000, '\n' );
    student s1(name);
    s1.print();
    cout<<d<<" "<<m<<" "<<y;
	return 0;
}

-------------
Enter name ,please
123
Enter the date
1 1 2007
Enter correct Name of student ,please:
9jack
9jack
1 1 2007

Hmm, you know, you are absolutely right. I must have been really tired when I did that. Yes, leave the break statement out. (Or, leave it in and set i=-1 .)

Good job.

Thank you Mr. Duoas, I thought that was wrong understanding or given wrong solution
Thank you your cooperation and patience
Maybe that come and ask again because I benefited greatly from your website:-)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.