im a fresh beginner...

Jun 17, 2010 at 3:55am
#include <iostream>
#include <string>
using namespace std;
void main ()
{

string name;
string icNum;
string parentName;
string address;
string TelNum, parentTelNum;
int choose =1;

do{
cout << "Please Enter Student Name : ";
getline(cin, name);
cout << "Student's IC number/passport number: " ;
getline(cin,icNum);
cout << "Student's address: " ;
getline(cin, address);
cout << "Parent's name: " ;
getline(cin,parentName);
cout << "Enter parent's telephone number: " ;
getline(cin,parentTelNum);

cout << "\nYour input was : " <<endl <<endl;
cout << "Student's name: " << name <<endl;
cout << "Student's IC number or passport number : " << icNum << endl;
cout << "Student's address : " << address <<endl;
cout << "Parent's name : " << parentName <<endl;
cout << "Parent's telephone number : " << parentTelNum << endl <<endl;

cout << "Press 1 - Re-enter particular" <<endl;
cout << "Press 2 - Confirm that the student's particular is correct" << endl;
cout << "Options : ";
cin >> choose;

}while(choose!=2);
cout << "Thank you. Info has been saved" << endl;

}




everything was right, until the end, where user have to choose 1 or 2...
when the user enters 1, the "Please Enter Student Name : "; and cout << "Student's IC number/passport number: " ; will be displayed together in one line, disabling user to enter the Student name...
how to solve?
Jun 17, 2010 at 4:00am
What you are seeing is a result of mixing operator>> and getline(). Choose one or the other.
Jun 17, 2010 at 8:01am
It's because when you entered the number 1 into choose, you also entered the new-line character '\n' when you pressed return.

Reading choose only read up to the new-line character and left it in the buffer. That means the next call to getline() will read that character thinking you just typed it.

The thing to do is issue a getline() immediately after you read in your choose variable to absorm the new-line that you entered:

1
2
3
4
		cout << "Options : ";
		cin >> choose;
		string dummy;
		getline(cin, dummy); // absorb rest of input 
Jun 17, 2010 at 10:20am
yup ur right Galik.......I would like to suggest him to use system("cls") to keep the screen clean up! by using a lib file stdlib.h , thanks .
Jun 17, 2010 at 2:24pm
closed account (1yR4jE8b)
NO NO NO NO NO NO NO NO NO

Don't EVER EVER EVER use system("anything"); EVER

It's not portable, and opens up massive security holes in your program.

DON'T DO IT EVER EVER EVER EVER EVER EVER EVER
Jun 17, 2010 at 3:03pm
What are you trying to say darkestfright??? 8^)

I always wonder why a programmer would want to clear away all previous output anyway. It's a console program and you're trying to learn. Cosmetics shouldn't be an issue at this stage.

@markjsoria: void main() is technically incorrect even though most compilers accept it. You should be using int main() {... code ... } return 0. That way the OS gets a proper return value from your code rather than a random garbage value.
Jun 17, 2010 at 3:31pm
cnoeval wrote:
What are you trying to say darkestfright?

I assume darkestfright was referring to the problem described here:
http://stackoverflow.com/questions/1107705/systempause-why-is-it-wrong

The second answer addresses the security issue.
Last edited on Jun 17, 2010 at 4:04pm
Jun 17, 2010 at 4:17pm
I always wonder why a programmer would want to clear away all previous output anyway. It's a console program and you're trying to learn. Cosmetics shouldn't be an issue at this stage.
Also, it's against the spirit of the console. Imagine if there was a command that blanked all the paper the line printer outputted and rewound the paper feed.
Jun 17, 2010 at 9:39pm
In this program using getline for all inputs is very reasonable. However, sometimes you don't want to use getline for every input. if you understand the issue there are other ways of solving the problem.
http://www.parashift.com/c++-faq-lite/input-output.html#faq-15.6

Jun 18, 2010 at 1:06am
thank you guys...
that was helpful..
Topic archived. No new replies allowed.