| | |
small problem in the string
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Nov 2007
Posts: 9
Reputation:
Solved Threads: 0
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
---------------------------------
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...
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
C++ Syntax (Toggle Plain Text)
#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...
Last edited by sunrise2007; Nov 14th, 2007 at 8:20 pm.
You have two problems:
1. Add a break statement after you set i to zero:
2. You need to get that last newline you left in the buffer after getting the year.
The trouble happens because you are mixing >> with getline().
Hope this helps.
1. Add a break statement after you set i to zero:
C++ Syntax (Toggle Plain Text)
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.
C++ Syntax (Toggle Plain Text)
... cin>>d>>m>>y; cin.ignore( 10000, '\n' ); ...
Hope this helps.
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.
•
•
•
•
Enter name,please:
0123
Enter the date
1 1 2007
Enter correct name of student, please
9jake
•
•
•
•
Enter name,please:
no2
Enter the date
1 1 2007
Enter correct name of student, please
m
•
•
Join Date: Nov 2007
Posts: 9
Reputation:
Solved Threads: 0
•
•
•
•
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.
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 ??????????
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.
•
•
Join Date: Nov 2007
Posts: 9
Reputation:
Solved Threads: 0
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
-------------
Enter name ,please
123
Enter the date
1 1 2007
Enter correct Name of student ,please:
9jack
9jack
1 1 2007
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
c++ Syntax (Toggle Plain Text)
#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
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
![]() |
Similar Threads
- File Attachment Small Problem (PHP)
- A small problem in the output (C++)
- splitting a ftp connection string to it's parts (Shell Scripting)
- Splitting a string? (C)
- Problem in String Search:Please Help (C)
- Problem with string variable in void prnt function (C++)
- Problem with String replaceAll method (Java)
- Problem with string search of an array (C++)
- ** Need Help ** in a small C++ problem (C++)
Other Threads in the C++ Forum
- Previous Thread: call and sms blocker
- Next Thread: How to cross reference objects?
| Thread Tools | Search this Thread |
api application array arrays based beginner binary bmp c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count database delete deploy developer dll download dynamiccharacterarray email encryption error file format forms fstream function functions game generator givemetehcodez graph gui homeworkhelp iamthwee ifstream image input int java lib library linker list loop looping loops map math matrix memory multiple newbie news number numbertoword output pointer problem program programming project python random read recursion recursive reference rpg simple sorting string strings temperature template text text-file tree url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets






