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
Related
I'm trying to create a program that in the main() function enters to a menu using a switch. In each case invoques another functions. In the function createFile(), which it is supposed to create a binary file, fill it out with 0's and then add the info that is required in the first space is returning (1) and it says that the file couldn't be created. What am I doing wrong? What was my mistake?
#include <iostream>
#include <fstream>
#include <iomanip>
#include <cstdlib>
using namespace std;
struct athletesInfo
{
int heartRate;
int time;
};
void createFile(fstream&);
void addInfo(fstream&);
void showInfo(fstream&);
int n;
int main(){
int op;
fstream fileA;
do
{
cout<<"MENU"<<endl;
cout<<"(1) Enter the first Heart Rate of an Athlete"<<endl;
cout<<"(2) Add Another Athlete"<<endl;
cout<<"(3) Show Info Athletes"<<endl;
cout<<"(4) Exit"<<endl;
cout<<"Choose an option: ";
cin>>op;
switch (op)
{
case 1:
createFile(fileA);
break;
case 2:
addInfo(fileA);
break;
case 3:
showInfo(fileA);
break;
default:
cout<<"Choose a correct option."<<endl;
break;
}
} while (op!=4);
return 0;
}
void createFile(fstream &fileA){
athletesInfo athle;
fileA.open("athletes.dat", ios::in|ios::out|ios::binary);
if (fileA.fail())
{
cout<<"The file couldn't be created"<<endl;
exit(1);
}
cout<<"How many registers would you like?"<<endl;
cin>>n;
for (int i = 0; i < n; i++)
{
fileA.seekg(i* sizeof(athletesInfo));
athle.heartRate=0;
athle.time= 0000;
fileA.write((char *)&athle, sizeof(athletesInfo));
}
cout<<"Enter the info of the first athlete"<<endl;
cout<<"Heart rate: ";
cin>>athle.heartRate;
cout<<"Time (00:00): ";
cin>>athle.time;
while (athle.heartRate<0 && athle.time>2359 && athle.time<0000)
{
cout<<"Wrong Inputs."<<endl;
cout<<"Enter the info of the first athlete"<<endl;
cout<<"Heart rate: ";
cin>>athle.heartRate;
cout<<"Time (00:00): ";
cin>>athle.time;
}
fileA.write((char *)&athle, sizeof(athletesInfo));
fileA.close();
}
void addInfo(fstream& fileAthletes){
};
void showInfo(fstream& fileAthletes){
};
I'm trying to develop a program that, in case 2, after an array has been "filled" it adds another space so you are able to add another value to your original array. I tried my best but failed miserably. I cannot use std::copy to just copy the array and add it to a new one with more space in it. Any help is appreciated, thanks!
#include <iostream>
#include <string>
using namespace std;
void stampa (string x[], int q){ //Cout all inserted names
int i;
cout<<"Your names are: "<<endl;
for (i=0; i<q; i++){
cout<<x[i]<<endl;
}
}
void carica(string x[], int q){ //Function to insert the names
int i;
for(i=0; i<q; i++){
cout<<"Insert the "<<i+1<<" name:"<<endl;
cin>>x[i];
}
}
void controllo (int &x){ //Error if a negative number is inserted
while (x<=0) {cout<<"Error! Insert a positive number:"<<endl;
cin>>x;
}
}
int main(){
int scelta, n;
string nome;
bool exit=false;
cout<<"How many names do you want to enter?"<<endl;
cin>>n;
controllo(n);
string a[n];
carica(a,n);
do{
system("cls");
cout<<"1) Print your names"<<endl;
cout<<"2) Insert a new name"<<endl;
cout<<"3) Exit"<<endl<<endl;
cin>>scelta;
switch (scelta){
case 1:
stampa(a,n);
system("pause");
break;
case 2:
cout << "Enter new name to insert: " << endl;
cout<<"Done!"<<endl;
system("pause");
break;
case 3:
exit=true;
cout<<"Goodbye!"<<endl;
break;
}
}while (!exit);
system("pause");
}
Try using std::vector instead of a simple array, or create an array through pointers.
vector: https://www.youtube.com/watch?v=Jh2urtP00Zg
Pointers: https://www.programiz.com/cpp-programming/memory-management
the question asks me to repeat this program until users enter X. what should I do to loop this program? should I use while?
question: repeat the program until the user enter X for package code
#include <iostream>
using namespace std;
int main(){
int price_package;
char package_code;
int num;
int no_of_adults;
int no_of_childs;
cout<<"Enter package code: ";
cin>>package_code;
if(package_code=='A'){
cout<<"Enter number of adults: ";
cin>>no_of_adults;
cout<<"Enter number of childs: ";
cin>>no_of_childs;
price_package= ((40*no_of_adults)+(21*no_of_childs));
cout<<"price package: RM"<<price_package<<endl;
}
else if(package_code=='B'){
cout<<"Enter number of adults: ";
cin>>no_of_adults;
cout<<"Enter number of childs: ";
cin>>no_of_childs;
price_package=((23*no_of_adults)+(14*no_of_childs));
cout<<"price package: RM"<<price_package<<endl;
}
else if(package_code=='C'){
cout<<"Enter number of adults: ";
cin>>no_of_adults;
cout<<"Enter number of childs: ";
cin>>no_of_childs;
price_package=((38*no_of_adults)+(18*no_of_childs));
cout<<"price package: RM"<<price_package<<endl;
}
else{
cout<<"ERROR";
}
}
You could use a while loop or a do while loop. Both of which would look like the following:
// import and variables here
do{
// logic to loop
} while(package_code!='X')
Or
// imports and variables
//declare package_code with some value other than 'X'
while(package_code!='X'){
//loop logic
}
Try this
#include <iostream>
using namespace std;
void Output(int no_of_adults, int no_of_childs, int price_package)
{
cout<<"Enter number of adults: ";
cin>>no_of_adults;
cout<<"Enter number of childs: ";
cin>>no_of_childs;
cout<<"price package: RM"<<price_package<<endl;
}
int main()
{
int price_package;
char package_code;
int num;
int no_of_adults;
int no_of_childs;
while (1)
{
cout<<"Enter package code: ";
cin>>package_code;
if(package_code=='A')
{
price_package= ((40*no_of_adults)+(21*no_of_childs));
Output(no_of_adults, no_of_childs, price_package);
}
else if(package_code=='B')
{
price_package=((23*no_of_adults)+(14*no_of_childs));
Output(no_of_adults, no_of_childs, price_package);
}
else if(package_code=='X')
{
break;
}
else
{
cout<<"ERROR"<<endl;
}
}
}
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.
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.