when i enter '1' .. the cout is connected,i cant even input the 1st line,which is the 'name = '.. help me pls.

#include<iostream>
#include<string>

using namespace std;
void main()
{
int input;
int choose;

string name;
string icNum;
string parentName;	
string address;
string TelNum, parentTelNum;


cout << "\n\t\t\t STUDENT REGISTRATION SYSTEM" <<endl <<endl;

cout << "1. ADD" <<endl;
cout << "2. EXIT" <<endl <<endl;
cout << "ENTER OPTION : ";
cin >> input;


switch (input){
case 1:
	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;

break;

default:
cout << "Thank you " <<endl;


}

}

Recommended Answers

All 5 Replies

I'd suggest consistency when programming.

Instead of

int input;
cin >> input;

switch(input)
{
case 1:
//...
break;
}

try getline with string and converting the string to an int using atoi(const char*)

string input;

getline(cin, input);
int option = atoi(input.c_str());

switch(option)
{
case 1:
//...
break;
}

This should work.

You could also include the stringstream header <sstream> and convert it through there

#include <iostream>
#include <string>
#include <sstream>

//.....
string input;
//.....
getline(cin, input);

//put input to stringstream
stringstream ToInt(input);

int option;
ToInt>>option; //put the value in an integer

switch(option)
{
case 1:
//...
break;
}

There's an extra '\n' (newline) in the stream (from when you press enter after the number) that's being taken in by the first getline as a signal that input has ended. So it's getting skipped. ShadowScripter's idea is correct and probably preferable (though you could use stringstreams as well) but an alternate would be to put a cin.ignore() before your getline statement to sop up that extra '\n' .

wow...thanks so much guys..
ur great..

There's an extra '\n' (newline) in the stream (from when you press enter after the number) that's being taken in by the first getline as a signal that input has ended. So it's getting skipped. ShadowScripter's idea is correct and probably preferable (though you could use stringstreams as well) but an alternate would be to put a cin.ignore() before your getline statement to sop up that extra '\n' .

Indeed, I was just editing my post adding stringstream to the equation, since its easy and safe.
A good example is in fact cin.ignore(), its simple to use and would get rid of that horrible new line issue.
The '/n' character usually pops up when combinating cin>>var and getline(cin, var) which is the case.

#include<iostream>
#include<string>

using namespace std;
void main()
{
int input;
int choose;

string name;
string icNum;
string parentName;
string address;
string TelNum, parentTelNum;


cout << "\n\t\t\t STUDENT REGISTRATION SYSTEM" <<endl <<endl;

cout << "1. ADD" <<endl;
cout << "2. EXIT" <<endl <<endl;
cout << "ENTER OPTION : ";
cin >> input;

//eliminating terminating character in stream
cin.ignore(); 


switch (input){
case 1:
	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;
        cin.ignore();

break;

default:
cout << "Thank you " <<endl;


}

}

I didn't even notice it before but never use void main() main() always returns an int according to the standard.

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.