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!)
Related
hi guys so we have a school activity, and we are instructed to make an inventory system. I managed to do the input part of the code however i cannot display the same way the picture below shows:
This is the code of my work:
#include <iostream>
#include <iomanip>
#include <cstring>
#include <windows.h>
#include <cstdlib>
#include <fstream>
using namespace std;
//global declaration
int i;
int j;
const int passctr=3;
struct inventory{
double price;
int stock;
int sold;
int left;
};
struct itemType {
inventory items;
string itemname;
string brand;
};
struct userType{
string name;
string password;
};
int main()
{
//declaration
int prodqty,brandqty;
int prodarr;
int qty;
itemType item[prodqty],display;
userType user;
for(i=0;i<passctr;i++)
{
cout<<"Try no."<<i+1<<endl;
string xpassword="pogisibry";
cout<<"PRODUCT INVENTORY SYSTEM"<<endl;
cout<<"Enter Username: ";
getline(cin,user.name);
cout<<"Enter Password: ";
getline(cin,user.password);
try
{
if(user.password!=xpassword)
{
throw user.password;
}
else
{
cout<<"Welcome user "<<user.name<<"!!!"<<endl;
break;
}
}
catch (string reenter)
{
cout<<"Wrong password!"<<endl
<<"Please re enter!"<<endl;
}
system("pause");
system("cls");
}
if (i==passctr)
{
cout<<"locking inventory system!"<<endl
<<"exiting the program...";
exit(1);
}
cout<<"Enter desired number of products: ";
cin>>prodqty;
for(i=0;i<prodqty;i++)
{
cin.ignore();
cout<<"Product ["<<i+1<<"]: ";
getline(cin,item[i].itemname);
cout<<"How many "<<item[i].itemname<<"? : ";
cin>>brandqty;
cout<<endl;
for(j=0;j<brandqty;j++)
{
cin.ignore();
cout<<item[i].itemname<<"["<<j+1<<"]: ";
getline(cin,item[j].brand);
cout<<endl;
cout<<"Price: Php ";
cin>>item[j].items.price;
cout<<"Stock: ";
cin>>item[j].items.stock;
cout<<"Sold: ";
cin>>item[j].items.sold;
}
}
cout<<"PROD NO. PRODUCT NAME PRICE STOCK SOLD LEFT"<<endl;
for(i=0;i<prodqty;i++)
{
cout<<left<<setw(13)<<"["<<i+1<<"]"
<<setw(12)<<item[i].itemname
<<setw(14)<<item[i].brand
<<endl;
for(j=0;j<brandqty;j++)
{
item[j].items.left=item[j].items.stock-item[j].items.sold;
cout<<left<<setw(10)<<item[j].items.price
<<setw(10)<<item[j].items.stock
<<setw(10)<<item[j].items.sold
<<setw(10)<<item[j].items.left
<<endl;
}
}
}
I want to fix the display just like on the image above.
I did some array but it did not display other items I input.
Unable to build a single C++ file in Geany.
A program to show the employee details. When I clicked compile, it shows compilation successful. But when I build it, it shows this. Can't execute too.
#include<iostream>
#include<conio.h>
using namespace std;
class employee
{
public:
int id,age;
char name[25];
float salary;
void getdata()
{
cout<<"\n enter employee id";
cin>>id;
cout<<"enter emplyee name";
cin>>name;
cout<<"\n enter employee age";
cin>>age;
cout<<"\n enter emplyee salary";
cin>>salary;
}
void putdata()
{
cout<<"\n id="<<id<<"\n name="<<name<<"\n age="<<age<<"n salary="<<salary;
}
void main()
{
int i;
employee e[2];
for(i=0;i<2;i++)
{
cout<<"\n enter details of"<<i+1<<"employee";
e[i].getdata();
}
cout<<"\n details of employees";
for(i=0;i<2;i++)
{
e[i].putdata();
}
getch();
}
};
Error Code:
#include<iostream>
#include<string>
using namespace std;
class data{
string name;
string code;
public :
void getname()
{
cout<<"Enter name : ";
getline(cin,name);
}
void getcode()
{
cout<<"Enter code : ";
getline(cin,code);
}
void display()
{
cout<<"Name : "<<name;
cout<<"Code : "<<code;
}
};
int main()
{
int n,i=0; cin>>n;
data stud[n];
while(i<n)
{
stud[i].getname();
stud[i].getcode();
i++;
}
for(int i=0;i<n;i++)
stud[i].display();
return 0;
}
In Line 13: The getline() is not executed. Can anyone explain its cause and an alternative?
Initially, I thought that I may have missed a header file or the syntax is wrong, but that is not the case as the syntax for input of code works just fine.
When using cin and getline function alternatively, you should clear the cin input stream. by using cin.ignore() funtion.
Here I am using cin.ignore() after your cin statements because you want to ignore the "\n" left in the buffer after taking your int variable with cin.
#include<iostream>
#include<string>
using namespace std;
class data{
string name;
string code;
public :
void getname()
{
cout<<"Enter name : ";
getline(cin,name);
}
void getcode()
{
cout<<"Enter code : ";
getline(cin,code);
}
void display()
{
cout<<"Name : "<<name;
cout<<"Code : "<<code;
}
};
int main()
{
int n,i=0;
cin>>n;
cin.ignore();
data stud[n];
while(i<n)
{
stud[i].getname();
stud[i].getcode();
i++;
}
for(int i=0;i<n;i++)
stud[i].display();
return 0;
}
Hope This Might Helps: )
For starters there is a typo in this function
void getname()
{
cout<<"Enter name : ";
getline(cin,code);
^^^^
}
And this declaration of the array
int n,i=0; cin>>n;
data stud[n];
is invalid. The variable n is not initialized and variable length arrays is not a standard C++ geature.
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 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++?