searching method using certain keyword to give desired output - c++

here i want enter the patient data, and then i want to be able to give output after key in the id, this means that i use searching method, here i use linear search. i use array, to save the patient data, such as name and age. so in pharmacy department i want to be able to search the patient data before approving giving medicine.
#include<iostream.h>
int linear(int patient[],int size,int key);
int linear(int patient[],int size,int key) {
int i;
for(i=0;i<2;i++) {
if(key==patient[i].id) {
return i;
}
}
return -1;
}
void main() {
int i=0;
int size=2;
struct patientRecord {
char name[20];
int id,age;
int *patient;
};
patientRecord patient[i];
int choice;
cout<<"Welcome to Hospital Sejahtera\n";
cout<<"Please enter your choice:";
cout<<"1.Patient Registration\n2.Pharmacy\n3.Doctor Appoinment";
cin>>choice;
if(choice==1) {
for(i=0;i<2;i++) {
cout<<"Please enter patient name";
cin>>patient[i].name;
cout<<"Please enter patient id";
cin>>patient[i].id;
cout<<"Please enter patient age";
cin>>patient[i].age;
}
} else if(choice==2) {
int i;
for(i=0;i<2;i++) {
cout<<"Enter the patient id to search:";
cin>>patient[i].id;
linear(patient,size,patient[i].id);
cout<<"Location at index:";
cout<<endl;
}
}
}
i would be glad if you all can point out my mistake. im still learning. please help.

You might get a compile warning here that the variable size is not used.
You should check out what that is and where it should go.
int linear(int patient[],int size,int key) {
int i;
for(i=0;i<2;i++) {
if(key==patient[i].id) {
return i;
}
}
return -1;
}

Related

Compiler is throwing error in main that this is not declare before

#include<iostream>
#include<string>
using namespace std;
class customerNode{
public:
int c_id;
int quantity;
string c_name;
string type;
customerNode* next_node;
};
class Queue{
public:
customerNode* front=NULL;
customerNode* rear=NULL;
int getc_id();
string getc_name();
int getquantity();
int setc_id(int c_id);
string setc_name(string c_name);
int setquantity(int quantity);
void display();
void enqueue(int c_id,int quantity,string c_name);
void dequeue();
int nor_queue,exp_queue;
};
int Queue::getc_id(){
int c_id;
cout<<"enter customer id:"<<endl;
cin>>c_id;
return c_id;
}
int Queue::getquantity(){
int quantity;
cout<<"enter quantity customer purchased:"<<endl;
cin>>quantity;
return quantity;
}
string Queue::getc_name(){
string c_name;
cout<<"enter customer name:"<<endl;
cin>>c_name;
return c_name;
}
int Queue::setc_id(int c_id){
return c_id;
}
int Queue::setquantity(int quantity){
return quantity;
}
string Queue::setc_name(string c_name){
return c_name;
}
void Queue:: enqueue(int c_id,int quantity,string c_name){
int exp_queue,nor_queue;
cout<<"enter customer information"<<endl;
customerNode* new_node=new customerNode;
new_node->c_id=c_id;
new_node->c_name=c_name;
new_node->quantity=quantity;
new_node->next_node=NULL;
if(front==NULL){
rear=front;
rear=new_node;
rear->next_node=NULL;
}
else{
while(rear->next_node!=NULL)
rear=rear->next_node;}
rear->next_node=new_node;
rear=new_node;
if(new_node->quantity<=5)
{
new_node->type="express";
exp_queue++;
cout<<"customer entered in express queue"<<endl;
cout<<"total customer in express queue="<<exp_queue<<endl;
}
else{
new_node->type="normal";
nor_queue++;
cout<<"customer entered in normal queue"<<endl;
cout<<"total customer in normal queue="<<nor_queue<<endl;
}
}
void Queue::display(){
customerNode* ptr=front;
cout<<"normal queue customer information"<<endl;
while(ptr!=NULL)
{
if(ptr->type=="normal"){
cout<<"custumer name:"<<setc_name(ptr->c_name)<<endl;
cout<<"custumer id:"<<setc_id(ptr->c_id)<<endl;
cout<<"item puchased by custumer :"<<setquantity(ptr->quantity)<<endl;
nor_queue--;
cout<<"total customer in normal queue:"<<nor_queue<<endl;
}
ptr=ptr->next_node;
}
cout<<"express queue customer information"<<endl;
while(ptr!=NULL)
{
if(ptr->type=="normal"){
cout<<"custumer name:"<<setc_name(ptr->c_name)<<endl;
cout<<"custumer id:"<<setc_id(ptr->c_id)<<endl;
cout<<"item puchased by custumer :"<<setquantity(ptr->quantity)<<endl;
nor_queue--;
cout<<"total customer in normal queue:"<<exp_queue<<endl;
}
}
}
main(){
Queue q;
char i;
do{
q.enqueue(c_id,quantity,c_name );
cout<<"do you want to enter another customer?input y or Y for yes and n or N for no:";
cin>>i;
}
while(i=='y'||i=='Y');
q.display();
return(0);
};`
in mian fuction i m getting error c_id,quantity,c_name is not declare before,when i use int c_id,int quantity,string c_name than it shows expected primary expression befor int and strinng..i dont know which expression is missing or how to resolve the error,
please help me to solve this i hve to submit assing as soon as possible.
A much simpler example with similar error is:
#include <iostream>
struct foo {
int x = 0;
int y = 0;
void assign(int a, int b){
x = a;
y = b;
}
};
int main()
{
foo f;
f.assign(x,y);
}
The error is:
<source>: In function 'int main()':
<source>:14:14: error: 'x' was not declared in this scope
14 | f.assign(x,y);
| ^
<source>:14:16: error: 'y' was not declared in this scope
14 | f.assign(x,y);
| ^
x and y are declared in the scope of the class. Instances of foo have members of that name. To access those members you would write f.x or f.y.
c_id,quantity, and c_name are not declared in your main. I am not eniterly sure what you want to do and it is too much code to adress all its issues. Though, if you want to declare variables of that name in main then you need to do that:
int main(){ // main must return int
Queue q;
char i;
int c_id = 42;
int quantity = 0;
string c_name{"some fancy name"};
q.enqueue(c_id,quantity,c_name );
// ...
}
It is a little surprising that you write code with advanced stuff like pointers, classes and what not, but don't know about scope. Try to search for "scope" and read about it.
There are more issues in your code. For example int Queue::setquantity(int quantity){ return quantity;} does not set anything. Though, as I wrote before, this is just way too much code to adress all of them. I can only advise you to start with less code and only write more when you know the code you have already does compile and passes your tests. And thats not just an advise for a beginner, but anybody is making mistakes and you rather want to fix one problem then many at the same time.

strcpy is copying sth character onwards i string . How to resolve this error?

My complete code is at pastebin.
There is a train database , and user enters train number to book a ticket.The function updt_tick should copy the values of train's name,source and destination into passenger's reservation object.
But the problem is that 5th character onwards are only being copied.
Here is the function's code sample.Train database is entered by user.
void updt_tick()
{
fstream f;
f.open("train.dat",ios::in | ios::binary);
while(f.read((char*)&t,sizeof(t)))
{
if (tno==t.tno)
{
strcpy(bp,t.source);
strcpy(dest,t.dest);
strcpy(tname,t.tname);
amt=SeatNum*t.PerSeatFare;
break;
}
}
f.close();
}
The train class is ->
class train
{
public:
int tno;
char tname[100];
char source[100];
char dest[100];
int PerSeatFare;
public:
void getdetail();
void showdetail();
}t;
The reserv class is ->
`
class reserv
{
//Assume that cust select train according to his source and destination.
public:
int pnr;
int tno;
char tname[100];
char pnames[10][100];
int ages[10];
int SeatNum;
int i;
int d,m,y;
float amt;
char bp[100],dest[100];
void updt_tick()
{
fstream f;
f.open("train.dat",ios::in | ios::binary);
while(f.read((char*)&t,sizeof(t)))
{
if (tno==t.tno)
{
strcpy(bp,t.source);
strcpy(dest,t.dest);
strcpy(tname,t.tname);
amt=SeatNum*t.PerSeatFare;
break;
}
}
f.close();
}
public:
void getresdet()
{
cout<<"Enter the details as follows\n";
cout<<"Train no:";
cin>>tno;
cout<<"No of seats required:";
cin>>SeatNum;
cin.ignore();
for(i=0; i<SeatNum ; i++)
{
cout<<"Passenger name:";
gets(pnames[i]);
cout<<"Passenger age:";
cin>>ages[i];
cin.ignore();
}
cout<<"Date of travel:";
cin>>d>>m>>y;
cout<<"Details Accepted\n";
pnr=rand();
updt_tick();
}
void showresdet()
{
cout<<"Pnr no:"<<pnr;
cout<<"\nTrain no:"<<tno;
cout<<"\nTrain name:";
puts(tname);
cout<<"Boarding point:";
puts(bp);
cout<<"Destination pt:";
puts(dest);
cout<<"No of seats reserved:"<<SeatNum;
for(i=0; i<SeatNum; i++)
{
cout<<"Passenger name:";
puts(pnames[i]);
cout<<"Passenger age:"<<ages[i];
}
cout<<"\nDate of reservation:"<<d<<"-"<<m<<"-"<<y;
cout<<"\nYou must pay:"<<amt<<endl;
}
int getpnr()
{
return pnr;
}
};
Edit: There was nothing wrong with strcpy or any other code. I made
the foolish mistake of giving the file name as "train.dat" instead of
"trains.dat:.
Im not sure how did you manage to read 5 characters, because there is a problem is in your code. The file which you use to store train details is "trainS.dat", not "train.dat".

structure name not declared and swap function swaps without pass by refrence

I have this code,which takes input of 3 students from division a and b each.
those 2 divions are sorted and merged in a 3rd array according to birth dates of students.
the swap function ,I have not passed anything by refrence still its swapping and sort output is correct !!!.
NOTE:the line below #include..
void swap(struct a,struct b)
it should be
void swap(struct student a,struct student b)
but without changing that the program is runnong and giving correct outputs !! how ??
#include<iostream>
#include<string.h>
using namespace std;
void swap(struct a,struct b);
void findweek(struct student ar[10],int l,int bd1,int bm1);
struct student
{
//m is for month and b is for birthdate prn=prn number,name=name of student
int m,bd;
char prn[10],name[10];
};
int main()
{
//2 divisions a and b declared and will be merged into c
struct student a[3], b[3], c[6];
//division a input
for(int i=0;i<3;i++)
{
cout<<" Enter name of student "<<endl;
cin>>a[i].name;
cout<<"Enter prn no. "<<endl;
cin>>a[i].prn;
cout<<"Enter birth day "<<endl;
cin>>a[i].bd;
cout<<"Enter birth month "<<endl;
cin>>a[i].m;
}
//sorting of a
for(int i=0;i<3;i++)
{
for(int j=i;j<3;j++)
{
if(a[i].m>a[j].m)
{
swap(a[i],a[j]);
}
else if(a[i].m==a[j].m)
{
if(a[i].bd>a[j].bd)
{
swap(a[i],a[j]);
}
}
}
}
//division b input
for(int i=0;i<3;i++)
{
cout<<" Enter name of student "<<endl;
cin>>b[i].name;
cout<<"Enter prn no. "<<endl;
cin>>b[i].prn;
cout<<"Enter birth day "<<endl;
cin>>b[i].bd;
cout<<"Enter birth month "<<endl;
cin>>b[i].m;
}
//sorting of b
for(int i=0;i<3;i++)
{
for(int j=i;j<3;j++)
{
if(b[i].m>b[j].m)
{
swap(b[i],b[j]);
}
else if(b[i].m==b[j].m)
{
if(b[i].bd>b[j].bd)
{
swap(b[i],b[j]);
}
}
}
}
cout<<"-----------------------"<<endl;
cout<<"Division A"<<endl;
int count=0;
for(int i=0;i<3;i++)
{ //c has merged array , a being filled first
c[i]=a[i];
count++;
cout <<c[i].name<<"\t"<<c[i].prn<<"\t"<<c[i].bd<<"|"<<c[i].m<<endl;
}
cout<<"Division B"<<endl;
for(int i=0;i<3;i++)
{
//resume filling the array from count
c[count]=b[i];
cout <<c[count].name<<"\t"<<c[count].prn<<"\t"<<c[count].bd<<"|"<<c[count].m<<endl;
count++;
}
int bd1,bm1;
cout<<"Enter date to find birthdays in that week "<<endl;
cin>>bd1;
cout<<"Enter corresponding month "<<endl;
cin>>bm1;
findweek(c,count,bd1,bm1);
return 0;
}
//to swap the structure student arrays for sorting
void swap(struct student a,struct student b)
{
struct student t;
t=a;
a=b;
b=t;
}
void findweek(struct student ar[10],int l,int bd1,int bm1)
{
int count=0;
for(int i=0;i<l;i++)
{
int month_end=30;
int next_month=bm1+1;
//if(bd1>=23)
int end_date=bd1+7-month_end;
// else
int endofweek=bd1+7;
//l is length of ar , ar=copy of merged array, bd1&bm1 are date and month to search for birthday in that week
if((ar[i].m==bm1&&ar[i].bd>=bd1&&ar[i].bd<=endofweek)||ar[i].m==bm1+1&&ar[i].bd<=end_date)
{
if(month_end-bd1>7)
cout <<ar[i].name<<"\t"<<ar[i].prn<<"\t"<<ar[i].bd<<"|"<<ar[i].m<<endl;
else
{
if((ar[i].m==bm1&&ar[i].bd>=bd1)||(ar[i].m==next_month&&ar[i].bd<=end_date))
{
cout <<ar[i].name<<"\t"<<ar[i].prn<<"\t"<<ar[i].bd<<"|"<<ar[i].m<<endl;
}
}
count++;
if(count>7)
break;
}
}
}
It compiles even if you remove this line:
void swap(struct a,struct b);
Also, it compiles if you remove the whole swap function.
How is it?
Quite simple.
You are defining a function that takes two arguments: an incomplete type struct a and an incomplete type struct b.
That function is simply discarded from the overload set while searching the one to be used at function call.
Your main is not using your swap function. Instead, it's using the one from std:: namespace.
It is probably introduced by iostream or string, it's implementation defined.
Try changing the name of the function or putting a throw in your implementation of swap. In the second case, your runtime won't be affected.
Minimal, (not-)working example to reproduce the issue:
void f(struct s);
struct S {};
int main() { f(S{}); }
void f(S) {}
As you can see, the error is that you are referring to an incomplete type struct s.
swap is kinda of a somehow misleading example that compiles for the reasons above.
Reproducing an issue with a minimal example is often helpful.

Quiz program always evaluating answer to be wrong

class Question{
protected:
int op1;
int op2;
string operate;
public:
Question();
};
class generateRandomQuiz:Question{
public:
generateRandomQuiz();
int getp1();
int getp2();
string getOp();
};
class checkAnswer:generateRandomQuiz{
private:
int Ans;
public:
checkAnswer(int ans);
};
Question::Question()
{
op1=23;
op2=12;
operate="/";
}
generateRandomQuiz::generateRandomQuiz():Question()
{
op1=rand()%50;
op2=rand()%50;
string s="+-/*";
int n=rand()%4;
operate=s[n];
}
int generateRandomQuiz::getp1()
{
return op1;
}
int generateRandomQuiz::getp2()
{
return op2;
}
string generateRandomQuiz::getOp()
{
return operate;
}
checkAnswer::checkAnswer(int ans):generateRandomQuiz()
{
Ans=ans;
string operate=getOp();
int op1=getp1();
int op2=getp2();
if (operate=="+")
{
if (op1+op2==Ans)
{
cout<<"Your answer is correct."<<endl;
}
else
{
cout<<"You can do better next time."<<endl;
}
}
if (operate=="-")
{
if (op1-op2==Ans)
{
cout<<"Your answer is correct."<<endl;
}
else
{
cout<<"You can do better next time."<<endl;
}
}
if (operate=="*")
{
if (op1*op2==Ans)
{
cout<<"Your answer is correct."<<endl;
}
else
{
cout<<"You can do better next time."<<endl;
}
}if (operate=="/")
{
if (op1/op2==Ans)
{
cout<<"Your answer is correct."<<endl;
}
else
{
cout<<"You can do better next time."<<endl;
}
}
}
int main()
{
cout<<"This quiz is about evaluating an expression which is being generatedrandomly"
<<endl;
generateRandomQuiz Q;
int answer;
int op1=Q.getp1();
int op2=Q.getp2();
string opr=Q.getOp();
cout<<"What is: "<<op1<<op2<<op2<<"=?"<<endl;
cin>>answer;
checkAnswer A(answer);
system("PAUSE");
return 0;
}
I am writing a program which generates a quiz randomly and asks the user for an answer answer like this: What is : 15 / 43 = ? The operator and numbers are randomly generated.But when i givw the correct answer,even then the comment for the wrong answer is printed.I have written conditions quite clear.Can someone please point it out?thanks
To check the answer you are relying on the fact that checkAnswer inherits from generateRandomQuiz.
But when you actually check the answer, you use a different instance than the random generated quiz, so you cannot get the generated quiz by doing stuff like int op1=getp1();.
By doing this you will get default constructed values only, and since your constructor does the randomizing, you will get a different question altogether. So the checking mechanism is right, its just checking a different question. I suggest you rethink your code structure, it looks very awkward.

C++ strange "format" of output string

I am writing a C++ applications that is working with files. I have implemented standard operations like writing and reading from the file, some search functions, etc.
After doing some operations, my output strings come in a very strange way. For example the text "Address -", comes as "?п&&..&":
Is there any buffer in C++ that could be full by the file operations and it is doing this output so i need to empty it or clean it?
Edit: The problem appeared when I write a functions that is reading the records from my file, choosing some of them due to user criteria and then sorting them.
Here is my code:
Stucture:
struct Appartament
{
char address[50];
char telephoneNumber[20];
char view[10];
double price;
double distanceFromCenter;
int roomCount;
};
Function working with the file:
//Search by room count and order by price (rising)
void AdvanceSearch()
{
clrscr();
Appartament currentRecord;
int recordsCount=0;
fstream currentFile("Records.dat",ios::binary|ios::in);
if(!currentFile)
{
cout<<"Error - the file could not be opened."<<endl;
return;
}
else
{
//Array with apartments records
Appartament CurrentRecords[MaxRecords];
currentFile.seekg(0L,ios::end);
long int length=currentFile.tellg();
currentFile.seekg(0L,ios::beg);
int isAppartamentFound=0;
if(length==0)
{
cout<<"The file is empty."<<endl;
return;
}
else
{
int userRoomCount;
do
{
clrscr();
cout<<"Enter apartment room count - ";
cin>>userRoomCount;
}while(userRoomCount<0);
clrscr();
cout<<endl<<"Apartments with "<<userRoomCount<<" rooms order by price:";
currentFile.read((char*)(&currentRecord),sizeof(Appartament));
while(!currentFile.eof())
{
if(currentRecord.roomCount==userRoomCount)
{
CurrentRecords[recordsCount]=currentRecord;
recordsCount++;
isAppartamentFound=1;
}
currentFile.read((char*)(&currentRecord),sizeof(Appartament));
}
currentFile.close();
}
if(isAppartamentFound==0)
{
cout<<endl<<"There are no matches!"<<endl;
}
else
{
//If only one apartment is found
if(recordsCount==1)
{
cout<<endl;
ShowRecord(currentRecord);
}
else
{
//Sort the records
Appartament tempApartament;
int isChangeMade=1;
for(int index=0;index<recordsCount;index++){
//ShowRecord(CurrentRecords[index]);
}
while(isChangeMade==1)
{
isChangeMade=0;
for(int index=0;index<recordsCount-1;index++)
{
if(CurrentRecords[index].price>CurrentRecords[index+1].price)
{
isChangeMade=1;
tempApartament=CurrentRecords[index];
CurrentRecords[index]=CurrentRecords[index+1];
CurrentRecords[index+1]=tempApartament;
}
}
}
for(index=0;index<recordsCount;index++)
{
ShowRecord(CurrentRecords[index]);
}
}
}
}
}
Function for showing a record:
void ShowRecord(Appartament Record)
{
cout<<"Apartment Information"<<endl;
cout<<"The address is - "<<Record.address<<" and it is only "<<Record.distanceFromCenter<<" miles away from the center."<<endl;
cout<<"The price is "<<Record.price<<" $ for "<<Record.roomCount<<" rooms with "<<Record.view<<" view."<<endl;
}
Тhis is possible output:
I have run my program on other machine with other version of Borland and it is working perfectly. That's why the questions is what is wrong with mine compiler :?