I have made this program to show the use of Nested Structures. The programming is completely correct, as it doesn't show and warning or error when compiled.
The problem arises with the output. I have done this programming in Visual basic 6.0 and opened the C++ Source file under the New File option.
The problem with the output is that it does not show the "cout" if it is followed by a "gets()" command to get the input of the line from the user including spaces which is not supported by "cin>>" or "cin.getline()" cammands.
Please help me in finding the reason of this wrong Output. The program coding is as shown below :
P.S - THE PROGRAM MIGHT LOOK BIG BUT IS VERY EASY TO UNDERSTAND.
Also i have noticed that when u get the blank space after entering the employee number, if u enter ur name and press enter and then enter your designation, it'll then show the designation and name lines. And then enter house No. etc and then after the ****** part, when the input is shown, it comes properly! Why does that happen???
This is the output that comes when it is run normally
Enter Employee No. - 56
Name :
Designation :
Enter address
House No. : 90002
Area :
City :
Basic pay : 600000
********************************************
Mr./Mrs.
Designation :
Address :
House No. : 90002
Area :
City :
State :
Basic Pay : 600000 Press any Key to Continue_
The problem is that when you use cin it will leave the new line char in the input stream so when you call gets next time you read the rest of the line which likely is empty. One way to solve this is to put cin.ignore (INT_MAX, '\n'); after each time you read from cin.
And this is what i am talking about.... writing between the lines!
Enter Employee No. - 56
Absawis
C.E.O
Name :
Designation :
Enter address
House No. : 9001
Street 95
Los Angeles
U.S.A
Area :
City :
Basic pay : 600000
********************************************
Mr./Mrs. Absawis
Designation : C.E.O
Address :
House No. : 9001
Area : Street 95
City : Los Angeles
State :U.S.A
Basic Pay : 600000 Press any Key to Continue_
@peter87 - the thing is i have never used cin.ignore ever while programming so i really dont know how to use it and what is it! Would please say as to what it is and why & how it is used!
there is no output because when you entering input with 'cin' whith '>>' the new line character "\n" remain in the buffer of cin and, when you use gets() function the string obtained is null, because it check the new line caracter and stop work as defined in http://cplusplus.com/reference/clibrary/cstdio/gets/.
To avoid this, you can puts the cin.ignore() function before any string input, for example:
1 2 3 4 5
int n;
char s[lenght];
cin>>n;
cin.ignore(); //just before the input of a string
gets(s);
However, be careful with gets() function because the lenght of the char array used. Instead of it, i suggest the use of cin.getline() so you can define the max number of the string you have for input
this because you should put the cin.ignore() before any input that is not an atomic type. in this case you must insert ignore():
- before gets(worker.name);
- before gets(worker.address.area);
struct addr //First Structure
{
enum { max_area = 26, max_city = 26, max_state = 26 };
int houseno ;
char area[max_area];
char city[max_city];
char state[max_state];
};
struct emp //Second Structure
{
enum { max_name = 26, max_desig = 16 };
int empno;
char name[max_name];
char desig[max_desig];
addr address; // element of 2nd structure having type of first structure.
float basic;
} worker;
Replace gets() with cin.getline(),
1 2 3 4 5 6 7 8 9
cout<<"Name : ";
gets(worker.name);
cin.getline(worker.name, worker.max_name);
cout<<"\n Designation : ";
gets(worker.desig);
cin.getline(worker.desig, worker.max_desig);
// and so on..
As others have mentioned, any formatted extraction operation (ie. cin >> foo) may leave something in the stream when it is not desirable,
1 2 3 4 5 6 7 8 9 10 11 12
cout<<"\nEnter Employee No.";
cin>>worker.empno; // new line character remains in the stream here.
cin.ignore(std::numeric_limits<streamsize>::max(), '\n'); // ignore everything up to and
// including the new line character.
...
cout<<"\n Enter Address \n";
cout<<"House No. : ";
cin>>worker.address.houseno; // new line character remains in the stream here.
cin.ignore(std::numeric_limits<streamsize>::max(), '\n');
...
Note, you'll need to #include <limits> for the use of std::numeric_limits<streamsize>::max().