C++ Array of Struct: Functions aren't working [closed] - c++

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
Hey guys I'm pretty new into coding...
so my code won't run and i don't know what is wrong.
one of the errors that Im getting is that in my
void animal::petDataInfo(animal maxEntry[], int counter)
the counter is saying that it isn't being initialized...
basically i don't know whats wrong....
animal.h
#ifndef Database_animal_h
#define Database_animal_h
struct animal
{
char petName[20];
char lastName[30];
char color[12];
int month, day, year;
void petDataInfo(animal [], int &counter);
void displayInfo(animal [], int &counter);
}; animal maxEntry[100];
#endif
main.cpp
#include <iostream>
#include <string>
using namespace std;
#include "animal.h"
enum animalSpecies {Dog, Cat, Bird, Reptile, Other};
animalSpecies getAnimal();
void print(animalSpecies entered);
void menuInfo(animal [], int &counter);
void petDataInfo(animal [], int &counter);
void displayInfo(animal [], int &counter);
void quitInfo(int &counter);
int main()
{
int counter=0;
counter++;
menuInfo(maxEntry, counter);
system ("PAUSE");
return 0;
}
/********************************************************************************************************/
void menuInfo(animal maxEntry[], int& counter)
{
char letter;
do
{
cout<<"\nWhat would you like to do?"<<endl;
cout<<"(E)nter"<<endl;
cout<<"(S)earch"<<endl;
cout<<"(D)isplay"<<endl;
cout<<"(C)lear"<<endl;
cout<<"(Q)uit"<<endl;
cout<<"\n";
cout<<"Please enter a letter: "<<endl;
cin>>letter;
cout<<"\n";
cout<<"\n";
if (!((letter=='e')|| (letter=='E') || (letter=='s') || (letter=='S') || (letter=='d') || (letter=='D') || (letter=='c') || (letter=='C') || (letter=='q') || (letter=='Q')))
{
cout<<"Not a valid letter. Try again."<<endl;
cout<<"\n";
}
}
while (!((letter=='e')|| (letter=='E') || (letter=='s') || (letter=='S') || (letter=='d') || (letter=='D') || (letter=='c') || (letter=='C') || (letter=='q') || (letter=='Q')));
{
switch (letter)
{
case'e': case 'E':
petDataInfo(maxEntry, counter);
if ((letter=='e') || (letter=='E'))
{
cout<<"Entry number: "<<counter++;
}
break;
case 's': case 'S':
cout<<"goes to search database"<<endl;
break;
case 'd': case 'D':
displayInfo(maxEntry, counter);
break;
case 'c': case 'C':
if ((letter=='c') || (letter=='C'))
{
counter--;
}
break;
case 'q': case 'Q':
quitInfo(counter);
break;
default:
cout<<"Try again. \n";
}
}
}
/********************************************************************************************************/
void animal::petDataInfo(animal maxEntry[], int counter)
{
for(int i=0; i<counter; i++)
{
cout<<"What is the pets first name? "<<endl;
cin>>maxEntry[i].petName;
cout<<"What is the owners last name? "<<endl;
cin>>maxEntry[i].lastName;
getAnimal();
cout<<"What month is your pets date of birth? "<<endl;
cin>>maxEntry[i].month;
cout<<"What day is your pets date of birth? "<<endl;
cin>>maxEntry[i].day;
cout<<"What year is your pets date of birth? "<<endl;
cin>>maxEntry[i].year;
cout<<"What color is your pet? "<<endl;
cin>>maxEntry[i].color;
}
menuInfo(maxEntry, counter);
}
/********************************************************************************************************/
animalSpecies getAnimal()
{
animalSpecies entered = Dog;
char species;
cout<<"Species of Animal:\n (D)og\n (C)at\n (B)ird\n (R)eptile\n (O)ther\n "<<endl;
cin>>species;
switch (species)
{
case 'd':
case 'D':
entered = Dog;
break;
case 'c':
case 'C':
entered = Cat;
break;
case 'b':
case 'B':
entered = Bird;
break;
case 'r':
case 'R':
entered = Reptile;
break;
case 'o':
case 'O':
entered = Other;
break;
default:
cout<<"Error: Try again. "<<endl;
}
return entered;
}
/********************************************************************************************************/
void print(animalSpecies entered)
{
switch (entered)
{
case Dog:
cout<<"Dog"<<endl;
break;
case Cat:
cout<<"Cat"<<endl;
break;
case Bird:
cout<<"Bird"<<endl;
break;
case Reptile:
cout<<"Reptile"<<endl;
break;
case Other:
cout<<"Other"<<endl;
break;
}
}
/********************************************************************************************************/
void animal::displayInfo(animal maxEntry[], counter)
{
for(int i=0; i<100; i++)
{
cout<<"Pet name: "<<maxEntry[i].petName<<"\n";
cout<<"Owners last name: "<<maxEntry[i].lastName<<"\n";
cout<<"Pet species: ";
animalSpecies entered;
entered = getAnimal();
print(entered);
cout<<"Pets DOB: ";
if (month<10)
{
cout<<"0"<<maxEntry[i].month<<"/"; //if month is less than 10 it will add a '0' in front
}
else
{
cout<<maxEntry[i].month<<"/";
}
if (day<10)
{
cout<<"0"<<maxEntry[i].day<<"/";
}
else
{
cout<<maxEntry[i].day<<"/";
}
cout<<maxEntry[i].year<<"\n";
menuInfo(maxEntry, counter); //goes back to the menu
}
}
/********************************************************************************************************/
void quitInfo(int& counter)
{
int ans;
do
{
cout<<"Are you sure you want to quit? (y/n)"<<endl;
cin>>ans;
if ((ans=='n') || (ans=='N'))
{
menuInfo(maxEntry, counter); //goes back to the menu function
}
else if ((ans=='y') || (ans=='Y'))
{
exit(0); //exits the program
}
else
{
cout<<"Error: Try again. "<<endl;
}
}
while (!(ans=='n') || !(ans=='N') || !(ans=='y') || !(ans=='Y'));
}

Issues I found in your code:
You have declared petDataInfo and displayInfo in two places, once inside the definition of animal and once in main.cpp. Looking at your code, I don't think you need them inside animal.
You are missing the type of counter in the definition of displayInfo.
void animal::displayInfo(animal maxEntry[], counter)
You need to add the type in that line. Also, since the function is not a member function of animal, it can be written as:
void displayInfo(animal maxEntry[], int& counter)
The type of counter is different in the declaration and the definition of petDataInfo. I the declaration, you are using int& counter. In the definition, you are using int counter. Make sure that they match. Also, the class scope can be removed from the definition since it's no longer a member function of animal.
void petDataInfo(animal maxEntry[], int& counter)
You are missing the object when trying to access the month and day. Change the line
if (month<10)
to
if (maxEntry[i].month<10)
and change
if (day<10)
to
if (maxEntry[i].day<10)
I haven't verified the logic of your program. If there are any, hopefully you can track and fix them.

Related

Reverse Polish notation Calculator

I am currently working on a RPN calculator, it takes an infix expression converts it to postfix and shows the answer. I mostly got it right, but when I pop the answer from the stack if shows only the last digit of the result
ex
Enter infix: (1+1)*13+10/2
Postfix: 11+13*102/+
Result is: 1
Enter infix: 2*13+10/2
Postfix: 213*102/+
Result is:1
It gets it right for this kind of inputs
Enter infix: 3*2+5
Postfix: 32*5+
Result is : 11
#include<iostream>
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<stdlib.h>
using namespace std;
class infix2postfix
{
public:
void push(int symbol);
int pop();
void infix_to_postfix();
int priority(char symbol);
int isEmpty();
int white_space(char);
int eval_post();
};
char infix[100], postfix[100];
int stack[100];
int top;
int main()
{
infix2postfix ip;
top=-1;
cout<<"Enter infix : ";
gets(infix);
ip.infix_to_postfix();
cout<<"Postfix : "<<postfix<<endl;
cout<<"Result is : "<<ip.eval_post()<<endl;
return 1;
}
void infix2postfix :: infix_to_postfix()
{
int i,p=0;
char next;
char symbol;
for(i=0; i<strlen(infix); i++)
{
symbol=infix[i];
if(!white_space(symbol))
{
switch(symbol)
{
case '(':
push(symbol);
break;
case ')':
while((next=pop())!='(')
postfix[p++] = next;
break;
case '+':
case '-':
case '*':
case '/':
case '%':
case '^':
while( !isEmpty( ) && priority(stack[top])>= priority(symbol) )
postfix[p++]=pop();
push(symbol);
break;
default: /*if an operand comes*/
postfix[p++]=symbol;
}
}
}
while(!isEmpty( ))
postfix[p++]=pop();
postfix[p]='\0'; /*End postfix with'\0' to make it a string*/
}
/*This function returns the priority of the operator*/
int infix2postfix :: priority(char symbol)
{
switch(symbol)
{
case '(':
return 0;
case '+':
case '-':
return 1;
case '*':
case '/':
case '%':
return 2;
case '^':
return 3;
default :
return 0;
}
}
void infix2postfix :: push(int symbol)
{
if(top>100)
{
cout<<"Stack overflow\n";
exit(1);
}
stack[++top]=symbol;
}
int infix2postfix :: pop()
{
if( isEmpty() )
{
cout<<"Stack underflow\n";
exit(1);
}
return (stack[top--]);
}
int infix2postfix :: isEmpty()
{
if(top==-1)
return 1;
else
return 0;
}
int infix2postfix :: white_space(char symbol)
{
if( symbol == ' ' || symbol == '\t' )
return 1;
else
return 0;
}
int infix2postfix :: eval_post()
{
int a,b,i,temp,result;
for(i=0; i<strlen(postfix); i++)
{
if(postfix[i]<='9' && postfix[i]>='0')
push(postfix[i]-'0');
else
{
a=pop();
b=pop();
switch(postfix[i])
{
case '+':
temp=b+a;
break;
case '-':
temp=b-a;
break;
case '*':
temp=b*a;
break;
case '/':
temp=b/a;
break;
case '%':
temp=b%a;
break;
case '^':
temp=pow(b,a);
}
push(temp);
}
}
result=pop();
return result;
}
Consider what happens when eval_post() is given 213*102/+ to work with. Let's start in the middle, with the '1' after the asterisk. The '1' is a digit, so push it [stack ends with: 1]. Similarly, the 0 and 2 get pushed [stack ends with: 1, 0, 2]. Then the division symbol is encountered, so pop 2 and 0, then push 0/2 = 0 [stack ends with: 1, 0]. Finally, the addition symbol is encountered, so pop 0 and 1, then push 1+0=1, which is then popped as your answer.
One symptom of your problem is that, if things work, the stack should be empty when eval_post() returns. However, it is not empty when your infix includes numbers with more than one digit. Note that "10" gets pushed onto the stack as two numbers: "1" followed by "0". You want the value 10 pushed.
There are also some style problems with the code, but this appears to be the main functional problem.

Repeat statement for switch case

So I want make the program that can input data many times, for example I choose case 1 again after I use case 1 to input data. Or I choose case 2 many times to see all data as I want.
Mostly said break statement bring the solution. But, when I using break statement after case, the program was terminated. So how I can make repeat switch statement without make terminating program?
If I make this, the switch case happen sequentially. Example, I can input case 1 normally but when I input case 1 again it will go to next case instead case 1 and vice versa until exit case. This is my source code:
#include <iostream>
#pragma hdrstop
using namespace std;
struct ig
{
int id;
string name;
ig *next;
ig *prev;
};
ig *head= NULL;
ig *tail= NULL;
//---------------------------------------------------------------------------
int main(int argc, char** argv)
{
cout<<"+---------------------------------------+"<<endl;
cout<<"| Instagram User Data |"<<endl;
cout<<"+---------------------------------------+"<<endl;
cout<<"|1. Add Instagram ID |"<<endl;
cout<<"|2. Look All Data |"<<endl;
cout<<"|3. Look Previous Data |"<<endl;
cout<<"|4. Look Next Data |"<<endl;
cout<<"|5. Exit |"<<endl;
cout<<"+---------------------------------------+"<<endl;
int choose,l;
cout<<"your choices (1/2/3/4/5): ";cin>>choose;
switch(choose)
{
case 1:
{
cout<<"Input limit : ";cin>>l;
for(int a=1;a<=l;a++)
{
ig *new= new ig;
new->next=NULL;
new->prev=NULL;
cout<<"Entry Number : ";cin>>new->id;
cout<<"Instagram ID : ";cin>>new->name;
cout<<"===================="<<endl;
if(head== NULL)
{
head=new;
tail=new;
}
else
{
new->prev=tail;
tail->next=new;
tail=new;
}
}
}
cout<<"your choices (1/2/3/4/5)";cin>>choose;
case 2:
{
while(head!= NULL)
{
cout<<head->id<<endl;
cout<<head->name<<endl;
head=head->next;
}
}
cout<<"your choices (1/2/3/4/5)";cin>>choose;
case 3:
{
if(tail=NULL)
{
ig *show;
show=head;
while(show!= NULL)
{
cout<<head->id<<endl;
cout<<head->name<<endl;
head=head->prev;
}
}
else
{
cout<<"Data belum terisi"<<endl;
}
}
cout<<"your choices (1/2/3/4/5)";cin>>choose;
case 4:
{
ig *show;
show=tail;
while(show!= NULL)
{
cout<<tail->id<<endl;
cout<<tail->name<<endl;
tail=tail->next;
}
}
cout<<"your choices (1/2/3/4/5)";cin>>choose;
case 5:
{
return 0;
}
}
}
I tried to learn the logic of statement and the solution many times, but still unsuccessful. So I need help here, thank you
Put a while around the switch:
while (true) {
int choose = askChoice();
switch (choose)
{
case 1: AddInstagramID(); break;
case 2: LookAllData(); break;
case 3: LookPreviousData(); break;
case 4: LookNextData(); break;
case 5: return 0;
}
}

Sorting array of pointers

I am trying to sort an array of pointers, which are pointing to a character array of names. I can get the program to sort the first two names but not the rest. Also the names are part of a structure.
void sort( Class* ptrs[], int num_classes){
bool swap=false;
do
{
swap=false;
for (int i=0; i<=num_classes-1; i++) {
if (ptrs[i]->title[i]>ptrs[i+1]->title[i]) {
Swap(&ptrs[i], &ptrs[i+1]);
swap=true;
}
}
}while(swap);
}
void Swap(Class** num1,Class** num2){
Class* temp=*num1;
*num1=*num2;
*num2=temp;
}
struct Class{
char title[MAX];
int units;
char grade;
};
int main(){
int choice,num_classes=0;
char class_selection[MAX];
Class* ptrs[MAX];
char* class_ptr[MAX];
bool Continue=false;
Class classes[MAX];
for (int i = 0; i < MAX; i++){
ptrs[i]=&classes[i];
}
cout<<" 1. Add new class"<<endl<<" 2. Edit an existing class"<<endl<<" 3. Display a class"<<endl<<" 4. List all classes"<<endl<<" 5. Display GPA"<<endl<<" 6. Delete all classes"<<endl<<" 7. Quit"<<endl<<"Enter selection number: ";
cin>>choice;
if (choice!=7) {
Continue=true;
}
switch (choice) {
case 1:
add(ptrs,num_classes);
num_classes++;
break;
case 2:
edit(ptrs,num_classes);
cin.ignore();
break;
case 3:
sort(ptrs,num_classes);
cin.ignore();
cout<<"Enter the name of the class to display: ";
cin.getline(class_selection, MAX);
for (int j =0; j<MAX; j++) {
class_ptr[j]=&class_selection[j];
}
Bin_search(ptrs,num_classes,class_ptr);
break;
case 4:
sort(ptrs,num_classes);
display(ptrs,num_classes);
break;
case 5:
// display_GPA();
break;
case 6:
// delete_end();
break;
}
if (choice == 7) {
// delete_end();
//system('pause");
return 0;
}
}
This code is comparing i-th character of corresponding titles, not the whole titles:
if (ptrs[i]->title[i]>ptrs[i+1]->title[i]) {
// ^^^ ^^^
Possible ways to solve:
Change this
char title[MAX];
to
std::string title;
And after that you'll be able to write
if (ptrs[i]->title > ptrs[i+1]->title) {
Only if you can't use std::string for some reason, use strcmp:
if (strcmp(ptrs[i]->title, ptrs[i+1]->title) > 0) {

How does the output display twice when I did not call it twice? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 9 years ago.
Improve this question
This is the code for some sorting methods written in turbo c++(I know the program method of writing is old)I have a problem in the ascending part of bubble sort .Apparently the 2 functions are connected(I debugged it) and so the function is called twice and I don't know why.
Could you please tell the reason
This is the code:
#include<iostream.h>
#include<conio.h>
#include<limits.h>
void display(int ar[],int sz)
{
cout<<"Displaying arranged array\n";
for(int i=1;i<sz;++i)
cout<<ar[i]<<'\n';
}
void selectasd(int ar[],int sz)
{
for(int i=1;i+1<sz;++i)
for(int j=i+1,p=i;j<sz;++j)
if(ar[j]<ar[p])
{
p=j;
ar[i]+=ar[p];
ar[p]=ar[i]-ar[p];
ar[i]-=ar[p];
}
}
void selectdsd(int ar[],int sz)
{
for(int q=1;q+1<sz;++q)
for(int w=q+1,p=q;w<sz;++w)
if(ar[w]>ar[p])
{
p=w;
ar[q]+=ar[p];
ar[p]=ar[q]-ar[p];
ar[q]-=ar[p];
}
}
void insertasd(int arr[],int sz)
{
arr[0]=INT_MIN;
for(int x=2;x<sz;++x)
{
int tmp=arr[x];
int k=x-1;
while(tmp<arr[k])
{
arr[k+1]=arr[k];
k--;
}
arr[k+1]=tmp;
}
}
void insertdsd(int ar[],int sz)
{
ar[0]=INT_MAX;
for(int q=2;q<sz;++q)
{
int temp=ar[q];
int k=q-1;
while(temp>ar[k])
{
ar[k+1]=ar[k];
k--;
}
ar[k+1]=temp;
}
}
void bubsrtdd(int ar[],int sz)
{
for(int q=1;q+1<sz;++q)
for(int w=1;w+1<sz;++w)
if(ar[w]<ar[w+1])
{
ar[w]+=ar[w+1];
ar[w+1]=ar[w]-ar[w+1];
ar[w]-=ar[w+1];
}
}
void bubsrtad(int ar[],int sz)
{
for(int q=1;q+1<sz;++q)
{
for(int w=1;w+1<sz;++w)
{
if(ar[w]>ar[w+1])
{
ar[w+1]+=ar[w];
ar[w]=ar[w+1]-ar[w];
ar[w+1]-=ar[w];
}
}
}
}
int main()
{
clrscr();
int ar[20],size;
cout<<"This is a program to arrange arrays using selection sort\nInsert the size of the array\n";
cin>>size;
size++;
cout<<"Enter the elements\n";
for(int q=1;q<size;++q)
cin>>ar[q];
cout<<"Menu:\n1.Selection sort\n2.Insertion sort\n3.Bubblesort\n";
cin>>q;
switch(q)
{
case 1:
cout<<"Would you like to display it arranged in\n1.Ascending order\n2.Descending order\n";
cin>>q;
switch(q)
{
case 1:
selectasd(ar,size);
display(ar,size);
break;
case 2:
selectdsd(ar,size);
display(ar,size);
break;
default:cout<<"\nInvalid choice\n";
}
break;
case 2:
cout<<"Would you like to display it arraged in\n1.Ascending order\n2.Descending order\n";
cin>>q;
switch(q)
{
case 1:
insertasd(ar,size);
display(ar,size);
break;
case 2:
insertdsd(ar,size);
display(ar,size);
break;
default:cout<<"\nInvalid choice\n";
}
break;
case 3: {
cout<<"Would you like to display the array in \n1.ascending order \n2.descending order\n";
cin>>q;
switch(q)
{
case 1: {
bubsrtad(ar,size); display(ar,size);
display(ar,size);
break;
}
case 2: {
bubsrtdd(ar,size);
display(ar,size);
break;
}
default:cout<<"\nINVALID CHOICE\n";
}
}
break;
default:cout<<"\nInvalid choice\n";
}
getch();
return 0;
}
Seems to me, you're calling display twice here:
case 3: {
cout<<"Would you like to display the array in \n1.ascending order \n2.descending order\n";
cin>>q;
switch(q)
{
case 1: {
bubsrtad(ar,size); display(ar,size);
display(ar,size);
break;
}

In my program what is the difference between Record* and Databse*?

I am using Dev C++ compiler.
So, what's wrong in here, i am getting errors in the addRecord Module.
The compile log :
Compiler: Default compiler
Executing g++.exe...
g++.exe "C:\SAI,MANI\test_add(09.03.2013).cpp" -o "C:\Users\Ravitheja\Desktop\C and C++\Projects\SAI,MANI\test_add(09.03.2013).exe" -O3 -g3
-I"C:\Dev-Cpp\lib\gcc\mingw32\3.4.2\include" -I"C:\Dev-Cpp\include\c++\3.4.2\backward" -I"C:\Dev-Cpp\include\c++\3.4.2\mingw32" -I"C:\Dev-Cpp\include\c++\3.4.2" -I"C:\Dev-Cpp\include" -L"C:\Dev-Cpp\lib" -g3
C:\SAI,MANI\test_add(09.03.2013).cpp: In function `void addRecord(Record*)':
C:\SAI,MANI\test_add(09.03.2013).cpp:151: error: using typedef-name `Record' after `struct'
C:\SAI,MANI\test_add(09.03.2013).cpp:151: error: cannot convert `Record*' to `Database*' in assignment
C:\SAI,MANI\test_add(09.03.2013).cpp:153: error: expected primary-expression before '*' token
C:\SAI,MANI\test_add(09.03.2013).cpp:153: error: expected primary-expression before ')' token
C:\SAI,MANI\test_add(09.03.2013).cpp:153: error: expected `;' before "malloc"
C:\SAI,MANI\test_add(09.03.2013).cpp:154: error: cannot convert `Record*' to `Database*' in assignment
C:\SAI,MANI\test_add(09.03.2013).cpp: In function `void viewRecords(Record*)':
C:\SAI,MANI\test_add(09.03.2013).cpp:180: error: cannot convert `Database*' to `Record*' in assignment
C:\SAI,MANI\test_add(09.03.2013).cpp: In function `int getDate(date*)':
C:SAI,MANI\test_add(09.03.2013).cpp:187: error: conversion from `date*' to non-scalar type `date' requested
Execution terminated
particularly in this line ,
x->next = (Record*)malloc(sizeof(struct Record));
i am getting an error : cannot convert 'Record*' to ' Database*' in assignment, but what is the difference between Record and Database, as i have typedef-ed them.
I have not written all the modules yet, i just wanted to test my addRecord() and viewRecords() , but it's not working. How to correct these errors ?
#include<iostream>
#include<conio.h>
#include<stdlib.h>
#include<malloc.h>
using namespace std;
// global declarations and function prototypes....
const int TRUE = 1,FALSE = 0,SUCCESS = 1;
const int SUBJECTS = 3;
typedef char String[25];
// date structure , a better way to store dates.
struct date
{
int day,month,year;
};
// Database structure , the main double linked list which stores all the information.
typedef struct DataBase
{
String Name,FatherName,Address,Hometown;
float Marks[SUBJECTS],Total,Percentage;
date DOB;
long int RegNo,PhoneNumber;
struct Database *previous; // the addresses of the next and previous nodes in the dll.
struct Database *next;
}Record;
int main();
int menu(); // for displaying menu.
void process(int,Record*); // for processing the menu.
void addRecord(Record*); // for adding a record.
void delRecord(Record*); // for deleting a record.
void modRecord(Record*); // for modifying values of a record.
void sortRecords(Record*); // for sorting records.
void filterRecords(Record*); // for filtering records.
void searchRecords(Record*); // for searching a record.
void viewRecords(Record*); // for viewing records (all).
int getDate(date*); // for getting input for a date.
void copyDate(date,date*); // for copying two date variables.
int checkDate(date); // for checking wether a given date is correct.
char *toString(int); // for displaying month name given the month number.
void copyRecord(Record*,Record*); // for copying contents of one record into another.
// main function...
int main()
{
Record *r;
r = (Record*) malloc (sizeof(Record));
r->next = NULL;
r->previous = NULL;
while(1)
{
process(menu(),r);
}
}
// the menu function.
int menu()
{
static int choice;
cout<<"\n\t\t\t Menu";
cout<<"\n\t\t\t 1.Add Records ";
cout<<"\n\t\t\t 2.Delete Records";
cout<<"\n\t\t\t 3.Modify Records";
cout<<"\n\t\t\t 4.Sort Records";
cout<<"\n\t\t\t 5.Filter Records";
cout<<"\n\t\t\t 6.Search Records";
cout<<"\n\t\t\t 7.View Records";
cout<<"\n\t\t\t 8.Exit";
cout<<"\n\t\t\t YOUR CHOICE : ";
cin>>choice;
if(choice>=1 && choice<=7) return choice;
else if(choice == 8) exit(0);
else
{
cout<<"\n Sorry, that's an invalid choice.";
cout<<"\n Please Try Again.";
menu();
}
}
void process(int choice,Record *r)
{
switch(choice)
{
case 1:
addRecord(r);
break;
case 2:
delRecord(r);
break;
case 3:
modRecord(r);
break;
case 4:
sortRecords(r);
break;
case 5:
filterRecords(r);
break;
case 6:
searchRecords(r);
break;
case 7:
viewRecords(r);
break;
}
}
void addRecord(Record *x)
{
date *t;
t = (date*) malloc ( sizeof(date) );
fflush(stdin);
Record *temp; temp = (Record*) malloc ( sizeof(Record) );
cout<<"\n Enter the following details ..... "<<endl;
cout<<"\n Name : ";
gets(temp->Name);
cout<<"\n Father's Name : ";
gets(temp->FatherName);
cout<<"\n Address :";
gets(temp->Address);
cout<<"\n Hometown : ";
gets(temp->Hometown);
cout<<"\n Register Number : ";
cin>>temp->RegNo;
temp->Total = 0;
for(int i=0;i<SUBJECTS;i++)
{
cin>>temp->Marks[i];
temp->Total += temp->Marks[i];
}
temp->Percentage = temp->Total/SUBJECTS;
cout<<"\n Total Marks : "<<temp->Total;
cout<<"\n\n Percentage : "<<temp->Percentage;
if(getDate(t) == SUCCESS) // trick!
copyDate(temp->DOB,t);
x->next = (Record*)malloc(sizeof(struct Record));
copyRecord(x,temp);
temp->previous = (Record*)malloc(struct Database);
temp->previous = x;
temp->next = NULL;
return;
}
void viewRecords(Record *x)
{
if(x->next == NULL && x->previous == NULL)
{
cout<<"\n There are no records to view.";
cout<<"\n Please Add some records and then try again!";
return;
}
do
{
cout<<"\n Name : "<<x->Name;
cout<<"\n Father's Name : "<<x->FatherName;
cout<<"\n Address : "<<x->Address;
cout<<"\n Hometown : "<<x->Hometown;
cout<<"\n Register Number : "<<x->RegNo;
for(int i=0;i<SUBJECTS;i++)
cout<<"\n Mark "<<i+1<<" : "<<x->Marks[i];
cout<<"\n Total Marks : "<<x->Total<<endl;
cout<<"\n Percentage : "<<x->Percentage;
cout<<"\n Date Of Birth : "<<x->DOB.day<<"th"<<toString(x->DOB.month)<<" "<<x->DOB.year;
if(x->next == NULL) break;
}while((x=x->next)!=NULL);
}
int getDate(date *t)
{
cout<<"\nDate of birth (dd:mm:yyyy) : ";
scanf("%d:%d:%d",&t->day,&t->month,&t->year);
if(checkDate(t) == SUCCESS)
return SUCCESS;
else
{
cout<<"\n Sorry, that's not a valid Date.";
cout<<"\n Please Try Again,"<<endl;
getDate(t);
}
}
void copyDate(date d1,date *d2)
{
d1.day = d2->day;
d1.month = d2->month;
d1.year = d2->year;
return;
}
int checkDate(date *x)
{
int leap = (x->year%4==0)?TRUE:FALSE;
if( ( x->day<=0 || x->day > 31 ) || (x->month>12 || x->month<=0) || (x->year<1900 || x->year > 2008) )
return FALSE;
else if(leap == TRUE && x->month == 2 && (x->day>29))
return FALSE;
else if(leap == FALSE && x->month == 2 && (x->day>28))
return FALSE;
else if( (x->month == 4 || x->month == 6 || x->month == 9 || x->month == 11) && x->month >= 31 )
return FALSE;
else
return TRUE;
}
char *toString(int m)
{
char *t = (char*) malloc (sizeof(char)*4);
switch(m)
{
case 1 : t = "Jan"; break;
case 2 : t = "Feb"; break;
case 3 : t = "Mar"; break;
case 4 : t = "Apr"; break;
case 5 : t = "May"; break;
case 6 : t = "Jun"; break;
case 7 : t = "Jul"; break;
case 8 : t = "Aug"; break;
case 9 : t = "Sep"; break;
case 10 : t = "Oct"; break;
case 11 : t = "Nov"; break;
case 12 : t = "Dec"; break;
}
return t;
}
I think the root of the problem is that Record is a typedef name for struct Database, and the first error (using the struct keyword with Record) is leading to the later errors.
Since this is C++, dump malloc entirely and use the new operator:
x->next = new Record;
You don't need to use struct keyword for typedef..
so,
x->next = (Record*)malloc(sizeof(struct Record));
should be
x->next = (Record*)malloc(sizeof(Record));
Note:
You better use new instead of malloc
You should make up your mind whether you want to write C or C++, as they are different languages.
The answer to your main question, "what is the difference between Record and Database", is that
typedef struct Database
{
// ...
} Record;
introduces several names for the struct.
In C there would be two: struct Database and Record.
In C++, such as your code, there are three: the same two as in C, plus just Database.
In neither language is there a type called struct Record, which is why you get the first error: "using typedef-name 'Record' after 'struct'".
You should always fix the first error first.
(I'm not sure if this would fix your problem as your g++ lokks a bit dated - if it's really version 3.4.2 it's almost 10 years old and you should consider upgrading).