Updating objects with getline doesn't seem to work - c++

I'm having odd behavior when creating and using a class.
I create multiple instances of that class, sequentially - and then I create a few temp variables to prompt the user to enter some data, then i use basic mututaor functions and pass the data to the parts of the object. I do this for the first item, then the second, etc.
What happens when the declarations of those personalData elements are sequential then for some reason the cin/getline statements skip over some and i can't enter all the data correctly. When i spread them out between statements have one object created then the input from the user then updates, then the next object it works better but still not in 100% of the cases. Lastly i found that creating different variables for inputting data - my temp variables also resolves the problem but that seems strange to have to create a dozen variables when i should be able to input data/update existing ones?
class personalData
{
private:
string name;
string address;
int phone;
int age;
public:
void setname(string);
void setadress(string);
void setphone(int);
void setage(int);
string getname()const;
string getadress()const;
int getage()const;
int getphone()const;
};
void personalData::setname(string n)
{ name=n; }
void personalData::setadress(string a)
{ address=a; }
void personalData::setphone(int p)
{ phone=p;}
void personalData::setage(int ag)
{age=ag;}
string personalData::getname()const
{return name;}
string personalData::getadress()const
{return address;}
int personalData::getage()const
{return age;}
int personalData ::getphone()const
{return phone;}
int main()
{
personalData my;
personalData friendd;
personalData family;
string n;
string a;
int p;
int g;
cout<<"enter your name";
getline(cin,n);
cout<<"enter your address:";
getline(cin,a);
cout<<"enter your phone:";
cin>>p;
cout<<"enter your age";
cin>>g;
my.setname(n);
my.setadress(a);
my.setphone(p);
my.setage(g);
cout<<"enter your friend's name";
getline(cin,n);
cout<<"enter your friend's address:";
getline(cin,a);
cout<<"enter your friend's phone:";
cin>>p;
cout<<"enter your friend's age";
cin>>g;
friendd.setname(n);
friendd.setadress(a);
friendd.setphone(p);
friendd.setage(g);
cout<<"enter your family member's name";
getline(cin,n);
cout<<"enter your family member's address:";
getline(cin,a);
cout<<"enter your family member's phone:";
cin>>p;
cout<<"enter your family member's age";
cin>>g;
family.setname(n);
family.setadress(a);
family.setphone(p);
family.setage(g);
cout<<"my name"<<my.getname()<<endl;
cout<<"my address"<<my.getadress()<<endl;
cout<<"my phone"<<my.getphone()<<endl;
cout<<"my age"<<my.getage()<<endl;
// more cout statements with objects.getData ...
return 0;
}
Sorry for the long piece of code, I'm stuck and can't figure out what the issues are.
Thanks.

Related

How to fix interrupted console when using inheritance

I'm a student and I'm just learning inheritance, so i do a simple example. But when i excuted my code, it just did the first statements and stopped. What's my fault? I have so experience to ask for debugging.
class people
{
private:
string name;
protected:
string phone;
public:
string age;
string setName(string name)
{
this-> name = name;
}
string getName()
{
return name;
}
};
class student: public people
{
public:
string setPhone(string phone)
{
this-> phone = phone;
}
string getPhone()
{
return phone;
}
};
int main()
{
student ict;
string name, phone;
cout<<"\nEnter name: "; getline(cin,name);
ict.setName(name);
cout<<"\nEnter phone: "; getline(cin,phone);
ict.setPhone(phone);
cout<<"\nEnter age: "; cin>>ict.age;
cout<<"\n==============================\nYour info: ";
cout<<"\nName:"<<ict.getName();
cout<<"\nPhone: "<<ict.getPhone();
cout<<"\nAge: "<<ict.age;
return 0;
}
As far as I can see your issue is that your setName() and setPhone() functions have a string return type, however they don't change anything. In general you should have these as a void return type.
When I made this minor change the code compiled and ran perfectly fine for me.
On that sidenote, your method/reasoning to have the setPhone() function in the student class is a bit odd, especially since the variable of phone is itself in your people class instead.

classes with two objects

this code is working fine for first instance but for second instance it is not working
input :
poorva
17
26
raju
18
28
for second object i.e raju it is not taking name and roll no and shows some garbage value here is the ideone link https://ideone.com/uxwMAc
#include<bits/stdc++.h>
using namespace std;
class student
{
int roll;
int age;
char name[20];
public:
void getData();
void showData();
};
void student::getData()
{
char n[20];
int a,r;
cout<<"enter name of student \n";
gets(name);
cout<<"enter age of student \n";
cin>>age;
cout<<"enter roll no of student \n";
cin>>roll;
}
void student::showData()
{
cout<<"details are \n\n";
cout<<"name is :";
puts(name);
cout<<"age is "<<age<<endl;
cout<<"roll number is "<<roll<<endl;
}
int main()
{
student poorva ;
student raju ;
//for poorva
poorva.getData();
poorva.showData();
//for raju
raju.getData();
raju.showData();
return 0;
}
puts and gets are C-derived functions from <cstdio>. cin and cout are C++ streams from <iostream>. Don't mix them, they interact badly when they both read from (or write to) the same underlying stream without co-ordinating.
Try using cin.getline instead, and remove puts entirely. Ideally replace your fixed character array with std::string too.
Oh, and don't #include<bits/stdc++.h>. That's an implementation detail. Wherever you got the idea that it's reasonable to use this directly, is a bad place to learn C++ and you should stop using it (and maybe name & shame it here).
Your code should probably begin with
#include <iostream>
instead.
Tey the following
Student raju =new Student () ;
Though that should have worked.
Same with pooja

How do i input strings with spaces?

I want to input a string (with spaces in between) and i am using cin.getline(). But i am getting a run time error (loop executes infinitely).
class account
{
int acno;
int password;
char name[50];
char address[100];
char sex;
string phonenumber;
public:
void create_account(); //function to get data from user
void show_account() const; //function to show data on screen
void modify(); //function to add new data
void withdraw(int,int); //function to accept amount and subtract from balance amount
void donate(int,int); //function to accept amount and add to balance amount
void report() const; //function to show data in tabular format
int retacno() const; //function to return account number
int retpassword() const; //function to return password
}; // class definition
this is my function for inputting the data for a class record.
void account::create_account()
{
cout<<"\nEnter The account No. :";
cin>>acno;
cout<<"\n\nEnter The Name of The account Holder : ";
cin.getline(name,49);
cin.ignore();
cout<<"\n\nEnter your Password (Max 8 characters) : ";
cin>>password;
cin.ignore();
cout<<"\n\nEnter your Address : ";
cin.getline(address,99);
cin.ignore();
cout<<"\nEnter your Contact Number: ";
cin>>phonenumber;
cin.ignore();
cout<<"\nSex (Enter M for male and F for female): ";
cin>>sex;
cout<<"\n\n\nAccount Created..\n\n";
}
When I execute this I get a run time error if I introduce spaces in between string entries
and it skips the string input if I don't use cin.ignore() function.
Use the free-standing std::getline function, not the member function. The former operates on a std::string, so you don't have to mess around with a raw pointer.

Problem in accepting a blank space to string in c++

I wrote this code to obtain name. telephone and address of a person and then i input these into class object variables:
#include<iostream>
#include<cstdlib>
#include<fstream>
#include<string>
using namespace std;
class contact{
public:
string name;//ALL CLASS VARIABLES ARE PUBLIC
unsigned int phonenumber;
string address;
contact(){//Constructor
name= "Noname";
phonenumber= 0;
address= "Noaddress";
}
/*void input_contact_name(string s){//function to take contact name
name=s;
}
void input_contact_number(int x){//function to take contact number
phonenumber=x;
}
void input_contact_address(string add){//function to take contact address
address=add;
}*/
};
int main(){
contact *d;
d= new contact[200];
string name,add;
int choice;//Variable for switch statement
unsigned int phno;
static int i=0;//i is declared as a static int variable
bool flag=false;
cout<<"\tWelcome to the phone Directory\n";//Welcome Message
cout<<"Select :\n1.Add New Contact\n2.Update Existing Contact\n3.Delete an Existing Entry\n4.Display All Contacts\n5.Search for a contact\n6.Exit PhoneBook\n\n\n";//Display all options
cin>>choice;//Input Choice from user
while(!flag){//While Loop Starts
switch(choice){//Switch Loop Starts
case 1:
cout<<"\nEnter The Name\n";
cin>>name;
d[i].name=name;
cout<<"\nEnter the Phone Number\n";
cin>>phno;
d[i].phonenumber=phno;
cout<<"\nEnter the address\n";
cin>>add;
d[i].address=add;
i++;
flag=true;
}
}
return 0;
}
However If I enter the name separated with its surname, the code bypasses the next cins and exits. Can some one help me out why this happens?
Same happens when I enter 10 digit cell no.
Thanks in advance
Use std::getline() instead of operator>> to extract a std::string containing whitespace.

Classes and Objects in C++

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!)