Build giving 1d returned 1 exit status - c++

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:

Related

displaying an array in an array on screen using struct

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.

How to fix this string validation (Telephone directory system)

Im talking about the string validation in void enter() , what im trying to do is create a system , where if someone accidentally puts a number in Enter name field , they need to write it again
im just not being able to solve this string validation at all.
I tried something like
after cout<<"Enter name";
for(int i=0;i<strlen(name);i++)
{
gets(name);
if(isalpha(name[i]))
cout<<"There is a number in the input , try again";
}
include<fstream.h>
include<stdio.h>
include<conio.h>
include<string.h>
include<stdlib.h>
class Directory
{
char name[20];
char num[30];
char address[50];
public:
void enter()
{
cout<<"Enter name: "<<endl;
gets(name);
cout<<"Enter number: "<<endl;
gets(num);
cout<<"Enter address: "<<endl;
gets(address);
}
it did work , but if I enter a name "mar3", now i have to enter Mark four times for it to take the record.
I think this is what you want to do. You want to reenter the name untill it meets your requrement.
bool validate(char name[])
{
for(int i=0;i<strlen(name);i++)
if(isalpha((unsigned char)name[i]))
{
cout<<"There is a number in the input , try again";
return 0;
}
return 1;
}
and while reading name do this.
gets(name);
while(!validate(name))
{
gets(name);
}
UPD :
use string and getline to read.
#include<fstream>
#include<cstdio>
#include<cstring>
#include<stdlib.h>
#include<stdio.h>
#include<iostream>
using namespace std;
bool validate(string name)
{
for(int i=0;i<name.length();i++)
if(isalpha(name[i]))
{
cout<<"There is a number in the input , try again\n";
return 0;
}
return 1;
}
class Directory
{
string name,num,address;
public:
void enter()
{
cout<<"Enter name: "<<endl;
getline(cin,name);
while(!validate(name))
{
getline(cin,name);
}
cout<<"Enter number: "<<endl;
getline(cin,num);
cout<<"Enter address: "<<endl;
getline(cin,address);
}
};
here is you want to also limit the length of the name or number you can make another validator for that

How to solve Run Time Skipping Of a Code snippet cin.getline of C++ [duplicate]

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.

dynamic initalisations of c++ objects based on user input

Hey sorry for the previous Question
OK..My project is to create and run a database for a college using c++
I have to use USN which is a Unique Student Number to access the database :
So i wrote the following program :
#include<iostream>
# include<conio.h>
#include<iomanip>
#include<string.h>
#include<stdlib.h>
int checkinarray(char[],char*);
using namespace std;
class student
{
private :
int sem;
float cgpa;
char password[11];
char passwordtrial[11];
void readdata();
void checkpassword();
void createpassword();
public :
char name[50];
int roll;
void printdata();
char USN[11];
static int number;
void opendatabase();
void firsttime();
public:
student(char n[50]="NONE")
{
number++;
roll=number;
cout<<endl<<"New record under the name of "<<n<<" has been created !"<<endl;
cout<<"Roll number set for new student as : "<<roll<<endl;
cout<<"Use this Roll number for further usage"<<endl<<endl;
};
};
void student::opendatabase()
{
int ch;
cout<<"Enter your name:"<<endl;
cin>>name;
cout<<"Enter your password"<<endl;
cin>>passwordtrial;
if(!strcmp(passwordtrial,password))
{
cout<<"Do you want to read or write";
cin>>ch;
switch(ch)
{
case 0 :
readdata();
break;
case 1 :
printdata();
break;
}
}
else
cout<<"Try Again";
};
void student::readdata()
{
cout <<endl<<"Enter the name of the student : ";
cin >> name;
cout <<endl<<"Enter the semester of the student : ";
cin >> sem;
cout <<endl<<" Enter the cgpa of the student : ";
cin >> cgpa;
};
void student :: printdata()
{
cout << "The name is : " << name << endl;
cout << "The sem is : " << sem << endl;
cout << "The roll is : " << roll << endl;
cout << "The cgpa is : " << cgpa << endl;
}
void student::firsttime()
{
cout<<"Enter your name :";
cin>>name;
cout<<"Hey "<<name<<" Welcome to DBMS "<<endl;
createpassword();
};
void student::createpassword()
{
cout<<"Please enter your 6 character password.. : ";
cin>>password;
cout<<"Please Input your Data here.... :" ;
readdata();
};
int student::number=0;
int main()
{
enum status {existing,newacc,exit};
enum functi{read,update};
char entry1[40];
char entry[10];
char usn[15];
char a[1000][15];
char n[40];
int i,j=0,k;
int s=2;
cout<<endl<<"WELCOME TO COLLEGE NAME"<<endl<<"Press enter to access Database...";
getch();
cout<<endl<<"Welcome to the Main Page of our Database : "<<endl;
while(1)
{//User option
cout<<endl<<"Do you want to access an old entry : "<<endl<<"OR"<<"Create a NEW entry : ";
cin>>entry1;
if(!strcmp(entry1,"old"))
s=existing;
else if(!strcmp(entry1,"new"))
s=newacc;
else
s=exit;
switch(s)
{
case existing:
{
i=1;
break;
}
case newacc:
{ i=1;
cout<<endl<<"Enter your usn : "<<endl;
cin>>usn;
strcpy(a[j],usn);
j++;
strcpy(n,usn);
cout<<n;
student usn(n);
usn.firsttime(); //Start here!! use i to go to next loop or stay in this loop,,change name entry to usn
break;
}
default :{cout<<"Error Input";i=0;break;}
}
if(i)
continue;
cout<<endl<<"What do u want to do??"<<endl<<"Read Entries "<<endl<<"Update entries";
cin>>entry;
if(!strcmp(entry,"read"))
s=read;
else if(!strcmp(entry,"update"))
s=update;
else
s=exit;
cout<<endl<<"Enter your usn : "<<endl;
cin>>usn;
if(checkinarray(a[15],usn))
{
switch(s)
{
case read:{
usn.printdata();
break;
}
case update:{
usn.firsttime();
break;
}
default :
cout<<"Are you sure you want to exit?"<<endl<<"Press 0 to exit"<<endl<<"to back to menu press 1";
cin>>k;
break;
}
if(!k)
break;
}
else cout<<"Invalid Roll number try again!";
}
}
int checkinarray(char a[][15],char b[])
{
int len;
//Finding the length of the string :
len=(sizeof(a)/sizeof(a[0]));
//Checking Conditions for roll number:
for(int k=0;k<len;k++)
{
if(strcmp(a[k],b))
return 1;//stringcompare!!
}
return 0;
}
okay so when i run this i get the following error :
request for member 'printdata' in 'usn', which is of non-class type 'char [15]'
AND
request for member 'firsttime' in 'usn', which is of non-class type 'char [15]'
So please help me overcome this error by suggesting different ways to create and call objects based on user input
OP's problem can be reduced to the following example:
#include<iostream>
#include<string.h>
using namespace std;
class student
{
public:
student(char n[50])
{
}
};
int main()
{
char usn[15];
char n[40];
{
cin >> usn;
strcpy(n, usn);
student usn(n);
}
usn.printdata();
}
This is what is meant by a Minimal, Complete, and Verifiable example. Not everything, but everything needed to reproduce the problem. The beauty of the MCVE is it has reduced the problem to the point where all of it can fit on the screen and probably within the brain, making it easy to analyze and test.
Things to note:
There are two variables named usn
char usn[15];
is an automatic variable within main. It is visible only within main and will expire at the end of main
student usn(n);
is an automatic variable within an anonymous block within main. It is visible only within this block and will expire at the end of the block.
Annotating this block to better explain what is happening, we get
{
cin >> usn; // uses char usn[15];
strcpy(n, usn);
student usn(n); // declares a new variable named usn that replaces char usn[15];for the remainder of this block
} // student usn(n); ends right here and is destroyed.
usn.printdata(); //uses char usn[15]; which has no printdata method.
So how do we fix this?
student usn(n); must have a wider scope.
one of these two variables must change names because once student usn(n); has wider scope it will collide with char usn[15];
Lets give that a quick try.
int main()
{
char usn[15];
char n[40];
student stud(n);
{
cin >> usn;
strcpy(n, usn);
}
stud.printdata();
}
Isn't possible because there is no data in n with which we can make stud. At least not for this minimal example.
int main()
{
char usn[15];
char n[40];
student * stud;
{
cin >> usn;
strcpy(n, usn);
stud = new student(n);
}
stud->printdata();
delete stud;
}
Solves that by dynamically allocating stud so that it is no longer scoped by the braces. Unfortunately that does pick up some extra memory management woes. stud must be deleted. Added a delete, but what if printdata throws an exception and delete stud; is skipped?
int main()
{
char usn[15];
char n[40];
std::unique_ptr<student> stud;
{
cin >> usn;
strcpy(n, usn);
stud.reset(new student(n));
}
stud->printdata();
}
std::unique_ptr takes care of that. But... What about that whole database thing? Why not store stud in a list?
int main()
{
char usn[15];
char n[40];
std::vector<student> studs;
{
cin >> usn;
strcpy(n, usn);
studs.emplace_back(n); // or studs.push_back(student(n));
}
for (student & stud: studs)
{
stud.printdata();
}
}
std::vector solves both problems at once.

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