I am inputting strings. But my program jumps to the next line while inputting name and address. When the name lines runs, it does not input string and jumps to the father name and same is done with the next lines at address.
What is the issue please tell me . thanks.
#include<iostream>
#include<string.h>
#include<sstream>
using namespace std;
class StaffRegistration{
private:
///data members
string cnic, name, fatherName, dob, qualification, designation, joiningDate, address;
int day, month, year;
int salary, contact;
///set methods
void setCNIC(string sCNIC);
void setName(char sname[]);
void setFatherName(string sfName);
void setDOB(char sdob[]); //array because its fixed
void setQualifaction(string sq);
void setDesignation(string sdesignation);
void setJoiningDate(char sjDate[]);
void setAddress(string sAddress);
void setSalary(int);
void setContact(int scontact);
//get methods
void getCNIC();
void getName();
void getFatherName();
void getDOB();
void getQualification();
void getDesignation();
void getJoiningDate();
void getAddress();
void getSalary();
void getContact();
public:
void inputStaffData();
void displayStaffData();
};
void StaffRegistration::inputStaffData()
{
cout<<"Please enter National Identity Card Number (CNIC)\n";
getCNIC();
cout<<"\nPlease enter name \n";
getName();
cout<<"\nPlease enter father name \n";
getFatherName();
cout<<"\nPlease enter date of birth (dd,mm,yyyy)\n";
getDOB();
cout<<"\nPlease enter qualification\n";
getQualification();
cout<<"\nPlease enter designation\n";
getDesignation();
cout<<"\nPlease enter salary\n";
getSalary();
cout<<"\nPlease enter joining date (dd,mm,yyyy)\n";
getJoiningDate();
cout<<"\nPlease enter contact number\n";
getContact();
cout<<"\nPlease enter address information\n";
getAddress();
cout<<endl<<endl<<"Record added successfully!\n";
}
// definitions of getter functions
void StaffRegistration::getName()
{
//char name[40];
string name;
getline(cin, name);
cout<<name;
}
void StaffRegistration::getCNIC()
{
string gCNIC;
cin>>gCNIC;
setCNIC(gCNIC);
}
void StaffRegistration::getFatherName()
{
string fname;
getline(cin, fname);
setFatherName(fname);
}
void StaffRegistration::getDOB()
{
char dob[11];
cin>>dob;
setDOB(dob);
}
void StaffRegistration::getQualification()
{
string q;
getline(cin, q);
setQualifaction(q);
}
void StaffRegistration::getDesignation()
{
string designation;
getline(cin, designation);
setDesignation(designation);
}
void StaffRegistration::getSalary()
{
int salary;
cin>>salary;
setSalary(salary);
}
void StaffRegistration::getJoiningDate()
{
char jdate[11];
cin>>jdate;
setJoiningDate(jdate);
}
void StaffRegistration::getContact()
{
int con;
cin>>con;
setContact(con);
}
void StaffRegistration::getAddress()
{
string add;
getline(cin, add);
setAddress(add);
}
////definitions of setter functions
void StaffRegistration::setName(char sName[])
{
name=sName;
}
void StaffRegistration::setCNIC(string sCNIC)
{
cnic=sCNIC;
}
void StaffRegistration::setFatherName(string sfName)
{
fatherName=sfName;
}
void StaffRegistration::setDOB(char sdob[])
{
dob=sdob;
}
void StaffRegistration::setQualifaction(string sq)
{
qualification=sq;
}
void StaffRegistration::setDesignation(string sdesignation)
{
designation=sdesignation;
}
void StaffRegistration::setSalary(int s)
{
salary=s;
}
void StaffRegistration::setJoiningDate(char sjDate[])
{
joiningDate=sjDate;
}
void StaffRegistration::setContact(int scontact)
{
contact=scontact;
}
void StaffRegistration::setAddress(string sAddress)
{
address=sAddress;
}
int main()
{
StaffRegistration a;
a.inputStaffData();
}
use cin.ignore(); before using getline() to empty the buffer.Read more here: When and why do I need to use cin.ignore() in C++?
Related
I'm working on this code and it works with cin>>partname; instead of using getline(cin, partname); in the showpart function but for only names without blank space. But with using getline(cin, partname); it produces an error
error: no matching function for call to ‘getline(std::istream&, const string&)’
#include<iostream>
#include<cstring>
using namespace std;
class Inventory
{
private:
int partno;
string partname;
float cost;
void getpart()
{
cout<<"Enter the part number"<<endl;
cin>>partno;
cout<<"Enter the part name"<<endl;
cin>>partname;
cout<<"Enter the cost"<<endl;
cin>>cost;
}
public:
Inventory()
{
partno = 0;
partname = " ";
cost = 0.0;
}
Inventory(int pn,string pname,float c)
{
partno = pn;
partname = pname;
cost = c;
}
void setpart()
{
getpart();
}
void showpart() const
{
cout<<"Inventory details"<<endl;
cout<<"Part Number: "<<partno<<endl;
cout<<"Part Name: ";
getline(cin, partname);
cout<<"\nCost: "<<cost<<endl;
}
};
int main()
{
Inventory I1(1,"Resistor", 25.0), I2;
I2.setpart();
I1.showpart();
I2.showpart();
}
I've looked through similar errors but they seem not to be helpful.Thank you for looking through this.
#include <string> instead of <cstring>.
Also:
void showpart() const can't be const since you update partname (which is an odd activity for a function called show-something).
I suspect that you want to show the partname, not to update it:
void showpart() const
{
cout<<"Inventory details"<<endl;
cout<<"Part Number: "<<partno<<endl;
cout<<"Part Name: "<<partname<<endl;
cout<<"Cost: "<<cost<<endl;
}
This question already has answers here:
Why does std::getline() skip input after a formatted extraction?
(5 answers)
Closed 6 years ago.
#include<conio.h>
#include<iostream>
using namespace std;
class Student
{
private:
char name[20];
char rollno[20];
public:
void input();
void display();
};
class Acadamic_details:public Student
{
private:
float marks,percentage;
public:
void input();
void display();
};
class Guardian_details:public Acadamic_details
{
private:
char F_name[20],M_name[20];
public:
void input();
void display();
};
void Student::input()
{
cout<<endl<<" Enter the Name : ";
cin.getline(name,30,'\n');
cout<<endl<<" Enter the Roll No :";
cin.getline(rollno,30,'\n');
}
void Acadamic_details::input()
{
Student::input();
cout<<endl<<" Enter the Marks out of 500 :";
cin>>marks;
}
void Guardian_details::input()
{
Acadamic_details::input();
cout<<endl<<" Enter the Father's Name :";
cin.getline(F_name,30,'\n');
cout<<endl<<" Enter the Mother's Name :";
cin.getline(M_name,30,'\n');
}
void Student::display()
{
cout<<endl<<" Name : "<<name<<endl;
cout<<" Roll No : "<<rollno<<endl;
}
void Acadamic_details::display()
{
Student::display();
percentage=(marks/500)*100;
cout<<endl<<" Marks : "<<marks<<"/500"<<endl;
cout<<" Percentage : "<<percentage<<"%"<<endl;
}
void Guardian_details::display()
{
Acadamic_details::display();
cout<<endl<<" Father's Name : "<<F_name<<endl;
cout<<" Mother's Name : "<<M_name<<endl;
}
int main()
{
Guardian_details g1;
cout<<" Enter the Student's Details "<<endl;
g1.input();
cout<<" Student's Details Are : "<<endl;
g1.display();
getch();
return 0;
}
This is the Implementation of Multilevel Inheritance.
There is no compilation Error encountered during Compilation.
But When I executed the Program I am facing a run time Problem
void Guardian_details::input()
{
Acadamic_details::input();
cout<<endl<<" Enter the Father's Name :";
cin.getline(F_name,30,'\n');
cout<<endl<<" Enter the Mother's Name :";
cin.getline(M_name,30,'\n');
}
from the above Code Snippet , cin.getline(F_name,30,'\n'); is not working.
Everytime Run the Program It directly Execute the cout<<endl<<" Enter the Mother's Name :";
cin.getline(M_name,30,'\n');
skipping the cin.getline(F_name,30,'\n');.
Here is the Output Screen :
Can anyone Explain Why is Happening so and What is the Alternative to that Code.
Thanks in Advance.
In Acadamic_details::input when you do
cin>>marks;
the newline is left in the buffer for the next call to getline, which interprets it as an empty line.
I encountered a problem where gets() is not working sometimes without any error compiling. In other words, gets() will not return any value but no warning or error explanation.
Here is the code where it's not returning value
#include<iostream>
#include<conio.h>
#include<stdio.h>
using namespace std;
class student
{
private:
int admno;
char sname[20];
public:
void Takedata()
{
cout<<"Enter admission number ";
cin>> admno;
cout<<"Enter student name " ;
gets(sname);
}
void Showdata()
{
cout<<"Admission number "<<admno<<"\nStudent name "<<sname;
}
};
int main ()
{
student obj ;
obj.Takedata();
obj.Showdata();
getch();
return 0;
}
And in contrast here is the code where it's returning value to "sname"
#include<iostream>
#include<conio.h>
#include<stdio.h>
using namespace std;
class student
{
private:
int admno;
char sname[20];
public:
void Takedata()
{
cout<<"Enter student name " ;
gets(sname);
}
void Showdata()
{
cout<<"\nStudent name "<<sname;
}
};
int main ()
{
student obj ;
obj.Takedata();
obj.Showdata();
getch();
return 0;
}
If anything is unclear don't hesitate to ask me! I'm glad to accept ant answer/solution/advise!
Use cin.ignore() before taking string input.I try this and it works fine.
#include<iostream>
#include<conio.h>
#include<stdio.h>
using namespace std;
class student
{
private:
int admno;
char sname[20];
public:
void Takedata()
{
cout<<"Enter admission number ";
cin>> admno;
cin.ignore();
cout<<"Enter student name " ;
gets(sname);
}
void Showdata()
{
cout<<"\nAdmission number "<<admno<<"\nStudent name "<<sname;
}
};
int main ()
{
student obj ;
obj.Takedata();
obj.Showdata();
getch();
return 0;
}
I have a vector of class "Account". It's private to a class BankingSystem. Here's how I have them defined.
Account Class:
struct newAccount
{
string firstName;
string lastName;
string accountPass;
int accountID;
float accountBalance;
}; //end of structure newAccount
class Account
{
string firstName;
string lastName;
string accountPass;
int accountID;
float accountBalance;
private:
int depositAmount;
int withdrawAmount;
public:
static newAccount createAccount( int, float, string, string, string ); //creates new account
void deposit( int ); //deposits money into account
void withdraw(int); //withdrawals money from account
int retdeposit() const; //function to return balance amount
friend class BankingSystem;
}; //end of class Account
BankingSystem Class:
class BankingSystem
{
int accountID;
char fileName;
private:
std::vector<Account> accounts_;
public:
static void addAccount();
static void storeAccount( newAccount );
void deleteAccount();
void accountInquiry();
void saveAccounts();
void loadAccountsFromFile();
friend class Account;
}; // end of class BankingSystem
I'm trying to store new accounts in the vector in this manner.
1) addAccount function in BankingSystem.h
void BankingSystem::addAccount()
{
int ID;
float balance;
std::string pass, first, last;
cout << "\n\t Enter the Account ID: ";
cin >> ID;
cout << "\n\t Enter the passcode: ";
cin >> pass;
cout << "\n\t Enter Client's first name: ";
cin >> first;
cout << "\n\t Enter Client's last name: ";
cin >> last;
cout << "\n\t Enter starting balance: ";
cin >> setw(6) >> balance;
storeAccount( Account::createAccount( ID, balance, pass, first, last ) );
return;
}
2) createAccount in Account.h
newAccount Account::createAccount( int ID, float balance, string first, string last, string pass )
{
newAccount a;
a.accountID = ID;
a.accountBalance = balance;
a.firstName = first;
a.lastName = last;
a.accountPass = pass;
return a;
}
3) storeAccount in BankingSystem.h
void BankingSystem::storeAccount( newAccount a )
{
accounts_.push_back(a);
}
Everything is working fine except storing data in the vector. The line accounts_.push_back(a); has this error; "invalid use of member 'accounts_' in static member function."
A static method does not have access to a class instance (no this) so inside of storeAccount and addAccount the member accounts_ does not exist.
FYI: nothing after a return statement will be executed so the line cout << "\n\t Account ID: " << a.accountID << " added successfully."; is rather useless in your current code.
Consider the following implementation for reference:
using namespace std;
class Account
{
private: // data members
string firstName;
string lastName;
string accountPass;
int accountID;
float accountBalance;
public:
// constructor that initializes members
Account(int id, float bal, const string& fname, const string& lname, const string& pass)
: accountID(id), accountBalance(bal), firstName(fname), lastName(lname), accountPass(pass) {}
}; //end of class Account
class BankingSystem
{
private: // data members
int accountID;
char fileName;
vector<Account> accounts_;
public:
void addAccount()
{
int ID;
float balance;
string pass, first, last;
// prompt input, initialize values, etc
// construct a new Account from values and add it to vector
accounts_.push_back(Account(ID, balance, first, last, pass));
}
void storeAccount( const Account& newAccount )
{
// add an already initialized account
accounts_.push_back(newAccount);
}
}; // end of class BankingSystem
A static member function doesn't have any special access to member variables like accounts_.
addAccount and storeAccount are static member functions. You must have made them so for a reason but it was a mistake. Remove that static and you will remove this error. I'm guessing you'll then have a different problem. If so ask another question and find out the right way to solve that.
The method is static, so it has no "this" pointer, so it has no idea what object you want to access the accounts_ variable of. Also, you will never see that printout on createAccount() because it's after the return call.
class anurag
{
private:
int rollno;
char name[50];
int marks;
float percen;
void percentage(int num)
{
percen=(num/500)*100;
}
public:
void getdata(void)
{
cout<<"\n\nEnter the name of the student:";
gets(name);
cout<<"\n\nEnter the roll no: and the marks:";
cin>>rollno>>marks;
percentage(marks);
}
void display(void)
{
cout<<"\n\nThe name of the student is:";
cout.write(name,50);
cout<<"\n\nThe roll no: of the student is:";
cout<<rollno;
cout<<"\n\n The marks obtained is:"<<marks;
cout<<"\n\nThe percentage is:"<<percen;
}};
void main()
{
clrscr();
anurag F;
F.getdata();
F.display();
getch();
}
why the following code is not giving the desired output?
Because you have a bug.
#include<iostream>
#include<conio.h>
using namespace std;
class anurag
{
private:
int rollno;
string name;
int marks;
float percen;
public:
void percentage(float num)
{
percen=(num/500)*100;
}
public:
void getdata(void)
{
cout<<"\n\nEnter the name of the student:";
cin>>name;
cout<<"\n\nEnter the roll no: and the marks:";
cin>>rollno>>marks;
percentage(marks);
}
void display(void)
{
cout<<"\n\nThe name of the student is:";
cout<<name;
cout<<"\n\nThe roll no: of the student is:";
cout<<rollno;
cout<<"\n\nThe marks obtained is:"<<marks;
cout<<"\n\nThe percentage is:"<<percen<<"%";
}};
int main()
{
//clrscr();
anurag F;
F.getdata();
F.display();
getch();
return 0;
}
I made some changes. The int num should be float. The program works fine now.
(Pardon me if the changes I've made are wrong. I'm not experienced with coding. I just tried to get rid of that error!)