small problem in the string

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Nov 2007
Posts: 9
Reputation: sunrise2007 is an unknown quantity at this point 
Solved Threads: 0
sunrise2007 sunrise2007 is offline Offline
Newbie Poster

small problem in the string

 
0
  #1
Nov 14th, 2007
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
  1. #include<iostream>
  2. #include<string>
  3. #include<iomanip>
  4. using namespace std;
  5. #include<ctype.h>
  6.  
  7. void validate_name(string &name)//validate_name for each student
  8. {
  9. int i;
  10. for(i=0;i<(name.length());i++)
  11. {
  12. while( !(isalpha(name[i])) && !(isspace(name[i])) )
  13. {
  14. cout<<"Enter correct Name of student ,please:\n";
  15. getline(cin,name);
  16. i=0;
  17. }
  18. }
  19.  
  20. }
  21. class student
  22. {
  23. private:
  24. string name;
  25. public:
  26. student(string N)
  27. {
  28. set_name(N);
  29. }
  30. void set_name(string n)
  31. {
  32. validate_name(n);
  33. name=n;
  34. }
  35. void print()
  36. {
  37. cout<<name;
  38. }
  39. };
  40. int main()
  41. {
  42. string name;
  43. int d,m,y;
  44. cout<<"Enter name ,please\n";
  45. getline(cin,name);
  46. cout<<"Enter the date\n";
  47. cin>>d>>m>>y;
  48. student s1(name);
  49. s1.print();
  50. cout<<d<<" "<<m<<" "<<y;
  51. return 0;
  52. }
---------------------------------
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...
Last edited by sunrise2007; Nov 14th, 2007 at 8:20 pm.
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 1,953
Reputation: Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of 
Solved Threads: 214
Featured Poster
Duoas's Avatar
Duoas Duoas is offline Offline
Posting Virtuoso

Re: small problem in the string

 
0
  #2
Nov 14th, 2007
You have two problems:

1. Add a break statement after you set i to zero:
  1. void validate_name(string &name)//validate_name for each student
  2. {
  3. int i;
  4. for(i=0;i<(name.length());i++)
  5. {
  6. while( !(isalpha(name[i])) && !(isspace(name[i])) )
  7. {
  8. cout<<"Enter correct Name of student ,please:\n";
  9. getline(cin,name);
  10. i=0;
  11. break;
  12. }
  13. }
  14.  
  15. }

2. You need to get that last newline you left in the buffer after getting the year.
  1. ...
  2. cin>>d>>m>>y;
  3. cin.ignore( 10000, '\n' );
  4. ...
The trouble happens because you are mixing >> with getline().

Hope this helps.
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 9
Reputation: sunrise2007 is an unknown quantity at this point 
Solved Threads: 0
sunrise2007 sunrise2007 is offline Offline
Newbie Poster

Re: small problem in the string

 
0
  #3
Nov 15th, 2007
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
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 1,953
Reputation: Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of 
Solved Threads: 214
Featured Poster
Duoas's Avatar
Duoas Duoas is offline Offline
Posting Virtuoso

Re: small problem in the string

 
0
  #4
Nov 15th, 2007
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.
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 9
Reputation: sunrise2007 is an unknown quantity at this point 
Solved Threads: 0
sunrise2007 sunrise2007 is offline Offline
Newbie Poster

Re: small problem in the string

 
0
  #5
Nov 15th, 2007
Originally Posted by Duoas View Post
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 ...
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 1,953
Reputation: Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of 
Solved Threads: 214
Featured Poster
Duoas's Avatar
Duoas Duoas is offline Offline
Posting Virtuoso

Re: small problem in the string

 
0
  #6
Nov 16th, 2007
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.
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 9
Reputation: sunrise2007 is an unknown quantity at this point 
Solved Threads: 0
sunrise2007 sunrise2007 is offline Offline
Newbie Poster

Re: small problem in the string

 
0
  #7
Nov 16th, 2007
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
  1. #include<iostream>
  2. #include<string>
  3. #include<iomanip>
  4. using namespace std;
  5. #include<ctype.h>
  6.  
  7. void validate_name(string &name)//validate_name for each student
  8. {
  9. int i;
  10. for(i=0;i<(name.length());i++)
  11. {
  12. while( !(isalpha(name[i])) && !(isspace(name[i])) )
  13. {
  14. cout<<"Enter correct Name of student ,please:\n";
  15. getline(cin,name);
  16. i=0;
  17. break;
  18. }
  19. }
  20.  
  21. }
  22. class student
  23. {
  24. private:
  25. string name;
  26. public:
  27. student(string N)
  28. {
  29. set_name(N);
  30. }
  31. void set_name(string n)
  32. {
  33. validate_name(n);
  34. name=n;
  35. }
  36. void print()
  37. {
  38. cout<<name;
  39. }
  40. };
  41. int main()
  42. {
  43. string name;
  44. int d,m,y;
  45. cout<<"Enter name ,please\n";
  46. getline(cin,name);
  47. cout<<"Enter the date\n";
  48. cin>>d>>m>>y;
  49. cin.ignore( 10000, '\n' );
  50. student s1(name);
  51. s1.print();
  52. cout<<d<<" "<<m<<" "<<y;
  53. return 0;
  54. }
-------------
Enter name ,please
123
Enter the date
1 1 2007
Enter correct Name of student ,please:
9jack
9jack
1 1 2007
Last edited by Ancient Dragon; Nov 16th, 2007 at 6:20 pm. Reason: replaced quote tags with code tags. Please do not use quote tags to post code
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 1,953
Reputation: Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of 
Solved Threads: 214
Featured Poster
Duoas's Avatar
Duoas Duoas is offline Offline
Posting Virtuoso

Re: small problem in the string

 
0
  #8
Nov 16th, 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.
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 9
Reputation: sunrise2007 is an unknown quantity at this point 
Solved Threads: 0
sunrise2007 sunrise2007 is offline Offline
Newbie Poster

Re: small problem in the string

 
0
  #9
Nov 17th, 2007
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:-)
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC