| | |
please help me with structures
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
hello every one.. i m a newbie and was going through structures.the code is as followes:
the problem is when i run the program.. for the first structure it asks for the name.. from the next it skips of name input but asks for other field.Thus when the database is complete i gate just the first name but rest structures it remains empty... please run it if i am not clear...
regards
cd-4+
C++ Syntax (Toggle Plain Text)
#include<string> using namespace std; #define n_emp 3 struct workerdb { string name; short age; int salary; }employee[n_emp]; void printdb (workerdb); int main() { int n; for (n=0;n<n_emp;n++) { cout<<"name: "<<endl; getline(cin,employee[n].name); cout<<endl; cout<<"age: "<<endl; cin>>employee[n].age; cout<<endl; cout<<"salary: "<<endl; cin>>employee[n].salary; cout<<endl; } for (n=0;n<n_emp;n++) { printdb (employee[n]); } return 0; } void printdb(workerdb employee) { cout<<"name: "<<employee.name<<"\t"; cout<<"age: "<<employee.age<<"\t"; cout<<"salary: "<<employee.salary<<endl; }
regards
cd-4+
Last edited by CD-4+; Sep 18th, 2009 at 1:24 am.
Just add
at the bottom of your for() loop for input and this will ignore the '\n' character from the last cin.
Also it looks like you do not have
at the top of your file for cin and cout usage.
C++ Syntax (Toggle Plain Text)
cin.ignore();
Also it looks like you do not have
C++ Syntax (Toggle Plain Text)
#include <iostream>
hi .. i m sorry actually while copying i may have missed out.. but that wasn't the problem.. well i don't know if this way is good i found a new way...
thanks for your time on this .
regards
C++ Syntax (Toggle Plain Text)
#include<iostream> #include<string> using namespace std; #define n_emp 3 struct workerdb { string name; short age; int salary; }employee[n_emp]; void printdb (workerdb); int main() { int n; for (n=0;n<n_emp;n++) { cout<<"name: "<<endl; cin>>(employee[n].name); cout<<endl; cout<<"age: "<<endl; cin>>employee[n].age; cout<<endl; cout<<"salary: "<<endl; cin>>employee[n].salary; cout<<endl; } for (n=0;n<n_emp;n++) { printdb (employee[n]); } return 0; } void printdb(workerdb employee) { cout<<"name: "<<employee.name<<"\t"; cout<<"age: "<<employee.age<<"\t"; cout<<"salary: "<<employee.salary<<endl; }
regards
The whole reason why you would use the getline() function is to pick up spaces. If I were to input the name "Jim" with just cin, this would work but if I were to give him a last name "Jim McTavish" then it would only take in "Jim" and ignore the space and everything after. With getline() you would get the whole name.
By using cin.ignore() I was able to get it working fine with your code and this adjustment.
By using cin.ignore() I was able to get it working fine with your code and this adjustment.
![]() |
Similar Threads
- Creating dynamic array structures (C++)
- Why Data Structures???...QUESTIONS INSIDE (C++)
- dynamic array of structures problem (C++)
- C++ structures (C++)
- Read and writing strings from structures (C++)
- Java Collections: ADTs, Data Structures & Algorithms (Java)
Other Threads in the C++ Forum
- Previous Thread: Fuzzy string search or searching for misspelled words
- Next Thread: cpp compiler
| Thread Tools | Search this Thread |
api array based binary c++ c/c++ calculator char char* class classes code coding compile console conversion count database delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory multiple news number numbertoword output parameter pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings struct temperature template templates test text text-file tree unix url variable vector video visualstudio win32 windows winsock wordfrequency wxwidgets






