gets() & puts() not declared in scope in dev c++ - c++

This is my simple code for a bookshop
There is nothing wrong with the code. I am using DevC++ to run the code and after compling it gives out an error which says 'gets' was not declared in this scope & the same error for puts. Please help me.
#include<iostream>
#include<conio.h>
#include<stdlib.h>
#include<iomanip>
#include<cstring>
using namespace std;
class Book
{
char *title,*author,*publisher,ans;
int price,quant,quant_ent;
public:
Book()
{
title = new char[50];
author = new char[50];
publisher = new char[50];
price = quant = quant_ent = 0;
}
void getdata()
{
cout<<"\nEnter The Title";
gets(title);
cout<<"\nEnter The Author";
gets(author);
cout<<"\nEnter The Publisher";
gets(publisher);
cout<<"\nEnter The Price";
cin>>price;
cout<<"\nEnter The Quantity";
cin>>quant;
}
void display()
{
cout<<setw(15)<<title<<setw(15)<<author<<setw(15)<<publisher<<setw(10)<<quant
<<setw(10)<<price;
}
void search(char search_title[],char search_author[])
{
if(strcmpi(author,search_author)==0)
{
if(strcmpi(title,search_title)==0)
{
cout<<"\nBook Found!";
cout<<"\nEnter The Quantity: ";
cin>>quant_ent;
if(quant_ent <= quant)
{
cout<<"\nThe Title is: ";
puts(title);
cout<<"\nThe Author is: ";
puts(author);
cout<<"\nThe Publisher is: ";
puts(publisher);
cout<<"\nPrice Of Single Copy: "<<price;
cout<<"\nTotal Price = "<<price*quant_ent;
quant = quant - quant_ent;
}
else
{
cout<<"\nSufficient Quantity Not Available!";
}
}
}
}
};
int main()
{
Book obj[10];
int i=0,ch;
char author[50],title[50];
for(;;)
{
cout<<"\n*******MENU********\n1)Enter Details\n2)Buy Book\n3)Display All Books\n4)Exit";
cin>>ch;
switch(ch)
{
case 1:
obj[i].getdata();
i++;
break;
case 2:
cout<<"\nEnter The Authors Name: ";
gets(author);
cout<<"\nEnter The Title: ";
gets(title);
for(int j=0;j<i;j++)
{
obj[j].search(title,author);
}
break;
case 3:
cout<<setw(15)<<"TITLE"<<setw(15)<<"AUTHOR"<<setw(15)<<"PUBLISHER"<<setw(15)<<"QUANTITY"<<setw(15)<<"PRICE";
cout<<"\n"<<setw(75)<<"-----------------------------------------------------------------------------------------------------";
for(int j=0;j<i;j++)
{
cout<<"\n";
obj[j].display();
}
case 4:
exit(1);
};
}
}

Because it's declared in stdio.h (cstdio in C++) header and you haven't included it.
But you shall not use gets. It's a hopelessly broken function. Use fgets instead. Even better, ditch the naked pointers to char arrays and use std::string class instead.

Related

if else problem salary formula for loop desire loop user input

asking desire number to become the for loop(how many employee if input is 4 then 4 loop if 3 3 loops), salary formula not working, if else statement for string name to not accept number and vice versa integer to not accept letters. another one of my problem is how can I name the loop for example the question is name hours and rate then the cout should do 1. name hours rate, 2.name hours rate 3.name hours rate... the code is working.. just need some imporvements.
#include <iostream>
#include <cstdlib>
using namespace std;
void displayRules()
{
cout<<"====================="<<endl;
cout<<" EMPLOYEE-SALARY "<<endl;
cout<<"====================="<<endl;
cout<<" "<<endl;
}
int main()
{
char ans;
do
{
system("cls");
displayRules();
struct Employee
{
string name;
double hours;
double rate;
double salary;
Employee *next;
Employee *prev;
};
Employee *head;
head=NULL;
Employee *newEmployee;
Employee *EmpPointer;
Employee *nextEmpPointer;
Employee *prevEmpPointer;
string inpname;
int inpN;
double inphours;
double inprate;
double salary;
salary = (inprate*inphours);
for(int ctr=0; ctr<3; ctr++)
{
cout<<endl;
cout<<"Enter Name: \t\t";
cin>> inpname;
cout<<"Enter # Hours Worked: \t";
cin>> inphours;
if (inphours<0)
{
cout << "Invalid Input! Program Stopped. ";
return 0;
}
cout<<"Enter Rate per Hour: \t";
cin>> inprate;
if (inprate<0)
{
cout << "Invalid Input! Program Stopped. ";
return 0;
}
newEmployee = new Employee;
newEmployee->name=inpname;
newEmployee->hours=inphours;
newEmployee->rate=inprate;
newEmployee->next=NULL;
if (head==NULL)
head=newEmployee;
else
{
EmpPointer=head;
while (EmpPointer->next)
EmpPointer=EmpPointer->next;
EmpPointer->next=newEmployee;
}
}
cout<<endl;
Employee *displayPointer;
displayPointer=head;
system("cls");
cout<<"------------------------------------------------------------"<<endl;
cout<<" =Summary of PAYROLL= "<<endl;
cout<<"------------------------------------------------------------"<<endl;\
cout<<"Employee Name\t"<<"# Hours Worked\t"<<"Rate/Hour\t"<<"Salary\t"<<endl;
while (displayPointer)
{
cout<<displayPointer->name<<"\t\t";
cout<<displayPointer->hours<<"\t\t";
cout<<displayPointer->rate<<"\t\t";
cout<<displayPointer->salary<<endl;
displayPointer=displayPointer->next;
}
cout<<"------------------------------------------------------------"<<endl;
cout<<endl;
cout << "Would you like to run the program again? (Y/N) ";
cin>>ans;
}
while (ans == 'y' or ans == 'Y');
return 0;
}
Note: The salary wasn't being calculated so I fix that.
I broke your code into small functions in which each function only does one thing and one thing only (Single Responsibility Principle).
Also, I introduce function templates that allows you to reuse a function when you provide the type.
Finally, the code is missing a clean up of pointers to prevent memory leaks. Each time you use the keyword new to obtain a pointer to memory, you need later to check if the pointer contains null and if doesn't then use the keyword delete to free that memory, else you end with memory leaks in your code. Therefore, I leave you with the task to write the function that should iterate your employee list and free the memory to prevent memory leaks.
I hope this is useful.
#include <iostream>
#include <cstdlib>
using namespace std;
struct Employee {
string name;
double hours;
double rate;
double salary;
Employee *next;
Employee *prev;
};
void displayRules() {
cout<<"====================="<<endl;
cout<<" EMPLOYEE-SALARY "<<endl;
cout<<"====================="<<endl;
cout<<" "<<endl;
}
// Here we create a function template to make this code more reusable
template <typename T>
T consoleInput(const std::string& prompt) {
T value;
std::cout << prompt;
std::cin >> value;
return value;
}
// Lets create our own assert to exit the app.
void assertGreaterEqualThanZero(const double value, const std::string& prompt){
if (value < 0) {
cout << prompt;
exit(1);
}
}
// Small functions that do one thing only makes coding easy to debug
Employee* createEmployee(string name, int hours, int rate) {
Employee *newEmployee = new Employee;
newEmployee->name=name;
newEmployee->hours=hours;
newEmployee->rate=rate;
newEmployee->salary = (rate * hours);
newEmployee->next=NULL;
// You need to set and maintain ->prev
// if you are thinking on using a double linked list
// else remove it from the structure since is unused.
return newEmployee;
}
// This is a helper function to add new employees to a list
Employee* addToEmployeeList(Employee* list, Employee* newEmployee){
if (list==NULL) {
list = newEmployee;
} else {
Employee *EmpPointer = list;
while (EmpPointer->next)
EmpPointer=EmpPointer->next;
EmpPointer->next=newEmployee;
}
return list;
}
// The only purpose of this function is to print the list provided
void printEmployeList(Employee* employeeList){
Employee *currentEmployee = employeeList;
system("cls");
cout<<"------------------------------------------------------------"<<endl;
cout<<" =Summary of PAYROLL= "<<endl;
cout<<"------------------------------------------------------------"<<endl;
while (currentEmployee){
cout<<"Employee Name\t"<<"# Hours Worked\t"<<"Rate/Hour\t"<<"Salary\t"<<endl;
cout<<currentEmployee->name<<"\t\t";
cout<<currentEmployee->hours<<"\t\t";
cout<<currentEmployee->rate<<"\t\t";
cout<<currentEmployee->salary<<endl;
cout<<"------------------------------------------------------------"<<endl;
currentEmployee=currentEmployee->next;
}
}
// I leave you with this piece that is missing.
// TODO: create function that delete each employee in the list,
// then deletes the list in order to prevent memory leaks
int main() {
char ans;
do {
system("cls");
displayRules();
Employee *employeeList;
employeeList=NULL;
for(int ctr=0; ctr<3; ++ctr) {
// Lets declare and instantiate when we need it.
string name = consoleInput<string>("Enter Name: \t\t");
// No need to use inp (as inphours) in front of your variables
// It makes it harder to read. Just put hours as a name.
double hours = consoleInput<double>("Enter # Hours Worked: \t");
assertGreaterEqualThanZero(hours, "Invalid Input! Program Stopped.");
double rate = consoleInput<double>("Enter Rate per Hour: \t");
assertGreaterEqualThanZero(rate, "Invalid Input! Program Stopped. ");
Employee *newEmployee = createEmployee(name, hours, rate);
employeeList = addToEmployeeList(employeeList, newEmployee);
}
cout << endl;
printEmployeList(employeeList);
cout << "Would you like to run the program again? (Y/N) ";
cin>>ans;
} while (ans == 'y' or ans == 'Y');
return 0;
}

Program goes into infinite loop, But if removed some functions or lines, it works

A basic student database using classes, I accept
name,
roll number, and
sgpa(cgpa equivalent but out of 10).
I used while loop, and switch case,but if the code for sgpa validation, and code for displaying all students with same sgpa is removed, the program works neatly, but if not, the program goes into continuous loop, the while loop inside the main, as it accepts the options itself and keeps doing it.
The code fails when i accept the sgpa(while taking the student data) or call the displaySGPA fucntion(option 3)
int totalStudents=0;
class database{
float sgpa;
int roll;
string name;
public:
void getSGPA();
void getData(database []);
friend void displaySGPA(int,database []);
};
//Display Students with same SGPA
void displaySGPA(int temp,database students[]){
int i,sameSGPA=0;
for(i=0;i<totalStudents;i++){
if(students[i].sgpa==temp){
sameSGPA+=1;
}
}
if(sameSGPA>1){
cout<<"\nStudents with SGPA "<<temp<<"."<<endl;
for(i=0;i<totalStudents;i++){
if(students[i].sgpa==temp){
cout<<" "<<students[i].roll<<" "<<students[i].name;
}
}
}
else if(sameSGPA==1){
cout<<"Only one student with the SGPA , "<<temp<<". :"<<endl;
for(i=0;i<totalStudents;i++){
if(students[i].sgpa==temp){
cout<<" "<<students[i].roll<<" "<<students[i].name;
break;
}
}
}
else{
cout<<"No Student with given SGPA."<<endl;
}
}
void database :: getData(database students []){
cout<<"\nEnter Name : ";
cin>>name;
cout<<"Enter Roll. ";
cin>>roll;
getSGPA();
}
//SGPA validation
void database :: getSGPA(){
int x=1,temp;
while(x==1){
cout<<"Enter SGPA : ";
cin>>temp;
if(temp<=10){
sgpa=temp;
break;
}
else{
cout<<"Please enter a valid SGPA.";
}
}
}
//main Loop
int main() {
int x,temp;
database students[50];
while (x!=5){
cout<<"\n1.Enter a New Student.\n3.Display students with same SGPA.\n5.Exit.\n\nYour Choice : ";
cin>>x;
switch(x){
case 1:
students[totalStudents].getData(students);
totalStudents++;
break;
case 3:
cout<<"\nEnter the SGPA.";
cin>>temp;
displaySGPA(temp,students);
break;
case 5:
cout<<"Exiting the program.....";
break;
default:
cout<<"Please select a valid option."<<endl;
}
}
}
I found the bug, it was a type conversion bug, i was accepting a float value but passing it as a int,just worked, don't know why.But thanks for each comment and suggestion.

Program compiles but I think switch is ignored.

I am currently studying c++ but I fell behind a little bit, so I apologize if my question is obvious.
I have to create a program that asks for a student's name, GPA, Year of admission, and get a random 5 digit number generated for that person. The number of students will not exceed 42.
My program compiled (somehow) and I am able to get the error for invalid menu selection, however, whenever I give a valid selection (currently 1) nothing happens.
Maybe I am missing something, this is why I need help.
Here is my code.
#include <iostream>
#include <stdlib.h>
#include <time.h>
using namespace std;
//print all the menu options
void print_menu()
{
cout<<"\nRCNJ Registrar Menu:"<<"\n"
<<"\n"
<<"[1] Add a student"<<"\n"
<<"[2] Display all students"<<"\n"
<<"[3] Display by year"<<"\n"
<<"[4] Display statistics"<<"\n"
<<"[5] Quit"<<"\n";
}
//get and return the student's name
void get_name(string& student_name) //call student_name after that.
{
cout<<"Please enter the sudent's name: ";
cin >> student_name;
cout<<"\n";
}
//validate and return gpa
double get_gpa()
{
double student_gpa = 0;
cout<<"Please enter the GPA: ";
cin >>student_gpa;
cout<<"\n";
while (student_gpa > 4 || student_gpa < 0)
{
cout<<"Please enter a valid GPA for the student (0.00 - 4.00): ";
cin >> student_gpa;
cout<<"\n";
}
return student_gpa;
}
//validateand return year
int get_year()
{
int student_year = 0;
cout<<"Please enter the year: ";
cin >> student_year;
cout<<"\n";
while (student_year >2016 || student_year <1972)
{
cout<<"Please enter a valid year (min 1972, max 2016): ";
cin >> student_year;
cout<<"\n";
}
return student_year;
}
//generate the student's R#
int generate_number()
{
int r_number;
srand (time(NULL));
r_number = rand() % 89999 + 10000;
return r_number;
}
//save info. Include get_name, get_gpa, get_year
void input_new_student()
{
string student_name;
double student_gpa;
int student_year;
int r_number;
int s_name, s_gpa, s_year, r_num;
get_name(student_name);
get_gpa();
get_year();
generate_number();
}
//display all students in the proper format
void print_all()
{
}
//get a year as selection and print all students that are the same year
void print_by_year()
{
}
//display statistics based on entered students
void print_statistics()
{
}
//validate and return the menu option selected by the user.
//it should call print_menu defined earlier
int get_selection(int menu_choice)
{
menu_choice = 0;
cout<<"\n"
<<"Selection: ";
cin >> menu_choice;
cout<<"\n";
while (menu_choice > 5 || menu_choice< 1)
{
cout<<" Menu choice is invalid. Please re-enter (1 - 5): ";
cin>> menu_choice;
cout<<"\n";
}
return menu_choice;
}
int main()
{
string student_name;
double student_gpa;
int student_year;
int r_number;
int menu_choice;
int s_name=0;
int s_gpa=0;
int s_year=0;
int r_num=0;
string nameArray[42];
s_name++;
double gpaArray[42];
s_gpa++;
int yearArray[42];
s_year++;
int ramapoArray[42];
r_num++;
print_menu();
get_selection(menu_choice);
switch (menu_choice)
{
case 1:
input_new_student();
nameArray[s_name] = student_name;
gpaArray[s_gpa] = student_gpa;
yearArray[s_year] = student_year;
ramapoArray[r_num] = r_number;
break;
}
return 0;
}
I dont have permission to comment, hence adding it here.
In you main(),
get_selection(menu_choice);
switch (menu_choice)
You return menu_choice, but there is none to take the value, you end you using garbage value as it is uninitialized.
So two ways you can do it, either by passing the address/reference of menu_choice or by return value. try either of these it should work, though I have not gone through the rest of your program.
As suggested by others, try a debugger e.g. gdb?

Syntax error in a turbo c++ program

I get declaration syntax error in my code but i do not find any mis-typings or anything.Please help.I am using turbo c++ for my school level programming.
I get it where i have typed -void issue() (inside the main function)
#include <iostream.h>
#include <conio.h>
#include <stdio.h>
#include <string.h>
class book {
char bookname[20];
char isbn[20];
char author[20];
char category[20];
float price;
int noc;
public:
void accept(){
cout<<"Enter book name :- \n";
gets(bookname);
cout<<"Enter isbn no of the book:- \n";
gets(isbn);
cout<<"Enter authour name:- \n";
gets(author);
cout<<"Enter category of book:- \n";
gets(category);
cout<<"Enter price of the book :- \n";
cin>>price;
cout<<"Enter no of copies of book available in the library :- \n";
cin>>noc;
}
void display() {
cout<<"Name of the book is :- ";puts(bookname);cout<<endl;
cout<<"ISBN :- ";puts(isbn);cout<<endl;
cout<<"Author :- ";puts(author);cout<<endl;
cout<<"Category :- ";puts(category);cout<<endl;
cout<<"Price :- ";cout<<price<<endl;
cout<<"No of copies available :- ";cout<<noc<<endl;
}
void issue();
}b[5];
int main() {
for(int i=0;i<5;++i) {
b[i].accept();
}
void issue() {
int flag=1;
char booksearch[20];
cout<<"Enter name of book member wants to issue :- \n";
gets(booksearch);
for(int i=0;i<5;++i) {
flag=strcmp(booksearch,b[i].bookname);
if(flag==0)
break;
}
if(flag==0) {
cout<<"Book found \n";
b[i].display();
b[i].issue();
} else {
cout<<"Book not available:- \n";
}
}
getch();
return 0;
}
main does not have a closing brace Put one in before void issue()
Now change void issue() to void book::issue()
Fix this then tell us any further errors along with the line that it occurs on
EDIT
Instead of 1) move
getch();
return 0;
}
To that location

Program on cricket scoreboard

Why is the following code not giving results and how to get results?
Whenever I run the code, it first asks for the names of the players of two teams playing the match, then it shows the menu from which if we select any one of the option it again asks for the batsman name which is not according to the program designed. My research on the code and the problem is that I think buffer memory is full but I don't know how to free it, any help would be beneficial. Thank you
#include<iostream>
#include<string.h>
#include<conio.h>
using namespace std;
class scorecard{
char batname[11][20];
int runscored[11];
char situation[11][10];
char mode[11][15];
char bowlername[11][20];
float oversplayed[11];
int maiden[11];
int runsgiven[11];
int wicketstaken[11];
public:
void updatebatsman(void);
void updatebowler(void);
void displaybat(void);
void displaybowl(void);
void menu(void);
scorecard()
{for(int n=0;n<12;n++)
{
runscored[n]={0};
oversplayed[n]={0};
maiden[n]={0};
runsgiven[n]={0};
wicketstaken[n]={0};
}
}
};
int main()
{
int jb=0;
scorecard s1;
int kb;
s1.menu();
do
{
cout<< "Enter the option"<<endl;
cout<<"(1) Display batting score"<<endl<<"(2) Display Bowling score"<<endl<<"(3) Update batting score"<<endl;
cout<<"(4) Update Bowling score"<<endl;
cin >>kb;
switch(kb)
{
case 1 : s1.displaybat();
break;
case 2 :s1.displaybowl();break;
case 3:s1.updatebatsman();break;
case 4:s1.updatebowler();break;
default:cout<<"Wrong choice";
}
}while (jb<1);
}
void scorecard::updatebowler(void)
{char bowlname[20];
int str,k,option,overnumbers,maidenumb,uprun,upwicket;
cout<<"Enter Bowler name:";
cin.getline(bowlname,20);
for( k=0;k<11;k++)
{str= strcmp(bowlername[k],bowlname);
if (str== 0)
{
cout<<"Menu for Bowler information update "<<endl;
cout<<"(1) Update Number of overs"<<endl<<"(2) Update maiden overs"<<endl<<"(3) Update runs given"<<endl;
cout<<"(4) Update wickets taken"<<endl;
cin >> option;
switch(option)
{
case 1:{cout<<"Enter Numbers of overs to be updated:";
cin >>overnumbers;
cout<<endl;
oversplayed[k]+=overnumbers;
break;
}
case 2:{cout <<"Enter the number of maiden overs to be updated:";
cin>>maidenumb;
cout<<endl;
maiden[k]+=maidenumb;
break;
}
case 3:{cout <<"Enter the number of runs to be added:";
cin>>uprun;
cout<<endl;
runsgiven[k]+=uprun;
break;
}
case 4: {cout<<"Enter number of wickets to be updated:";
cin >>upwicket;
cout<<endl;
wicketstaken[k]+=upwicket;
}
default:cout<<"wroung choice";
}
break;
}
}
if (str!=0)
cout <<"You entered wrong player."<<endl;
}
void scorecard::updatebatsman(void)
{char batsmaname[20];
int str,k;
cout<<"Enter Batsman name:";
cin.getline(batsmaname,20);
for( k=0;k<11;k++)
{str= strcmp(batname[k],batsmaname);
if (str== 0)
{
cout<<"enter runs scored:";
cin>>runscored[k];
cout<<endl<<"enter weather out or not out:";
cin>>situation[k];
cout<<endl<<"enter mode(if batsman out) by which batsman was out:";
cin>>mode[k];
break;
}
}
if (str!=0)
cout <<"You entered wrong player."<<endl;
}
void scorecard::displaybat(void)
{
cout << "Batsman name"<<'t'<<"Runs scored"<<'t'<<"situation"<<'t'<<"mode"<<endl;
for(int j=0;j++;j<12)
{
cout<<batname[j]<<'t'<<runscored[j]<<'t'<<situation[j]<<'t'<<mode[j]<<endl;
}
}
void scorecard::displaybowl(void)
{
cout << "Bowler name"<<'t'<<"overs played"<<'t'<<"maiden overs"<<'t'<<"wicket taken"<<'t'<<"Runs given"<<endl;
cout<<endl;
for(int j=0;j++;j<12)
{
cout<<bowlername[j]<<'t'<<oversplayed[j]<<'t'<<maiden[j]<<'t'<<wicketstaken[j]<<'t'<<runsgiven[j]<<endl;
}
}
void scorecard::menu(void)
{
cout<<"Enter the name of players of batting team"<<endl;
for (int k=0;k<11;k++)
{
cout <<"Enter name of player "<<k+1<<":";
cin>>batname[k];
}
cout <<"Enter the name of players of bowling team"<<endl;
for (int n=0;n<11;n++)
{
cout <<"Enter name of player "<<n+1<<":";
cin>>bowlername[n];
}
}
This is very wrong:
for(int j=0;j++;j<12)
It should be:
for(int j=0; j < 11; j++)
You are also missing a break in your case 4 statement for the options:
case 4: {cout<<"Enter number of wickets to be updated:";
cin >>upwicket;
cout<<endl;
wicketstaken[k]+=upwicket;
break;
}
default:cout<<"wroung choice";
Without the break you will see also the output wrong choice when the user selects option 4.