ah I see what you're saying
so the issue is with the way you're doing the whole getadd getname getage
personally, this is what I would suggest to make the least changes to your code
I combined your 3 functions getadd, getname, getage
Instead of using your namectr int, they use a for loop to determine the first available slot based on if the age is 0
Due to checking if the age is 0, I had to change where int age[10]; is declared to int age[10] = {0}; so they'd initially all be at 0.
I also had to change how DisplayRecords works since it uses some variable called linectr or something. Instead I just made a new variable called CurrentRecord inside of that function.
Let me know if this is what you needed or not please, I hate answering and not getting a reply.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
|
#include<iostream>
#include<iomanip>
#include <string>
using namespace std;
string name[10];
int namectr;
int age[10] = {0};
int agectr;
string address[10];
int addctr;
int linenum=1;
void deleterecord()
{
int linedel;
cout<<"What record do you want to delete?: ";
cin>>linedel;
int accu1=linedel-1;
name[accu1]=" ";
age[accu1]=0;
address[accu1]=" ";
}
void editrecord()
{
int line;
cout<<"What record do you want to edit?: ";
cin>>line;
int accu=line-1;
cout<<"New name: ";
cin>>name[accu];
cout<<"New age: ";
cin>>age[accu];
cout<<"New address: ";
cin>>address[accu];
}
void displayrecords()
{
int x;
cout<<"+----------+-----+--------------------+"<<endl;
cout<<"| NAME | AGE | ADDRESS |"<<endl;
cout<<"+----------+-----+--------------------+"<<endl;
int CurrentRecord=1;
for(x=0;x<10;x++)
{
if (age[x]==0)
{
continue;
}
if (age[x]>0)
{
cout<<"|"<<CurrentRecord++<<". "<<setw(10)<<left<<name[x]<<"|"<<setw(5)<<left<<age[x]<<"|"<<setw(20)<<left<<address[x]<<"|"<<endl;
cout<<"+----------+-----+--------------------+"<<endl;
}
}
}
void getrecordinfo()
{
int AvailableSlot=-1; //Initialize to a number not between 0-9 since our array elements subscripts use 0-9
for (int i=0; i<10; i++) //Going to loop to find the first available spot in our array when adding a record
{
if (age[i]==0) //If we found an available spot
{
AvailableSlot=i; //Set AvailableSlot to be our available slot
break; //Exit the for loop once we find it if we do
}
}
if (AvailableSlot==-1) //If there are no available slots
{
cout << "No available slots to add a record." << endl;
return; //By returning, we exit this function ignoring the rest of the code in the case that there's no room to add a record
}
//Once we get to this part in the function, we must have found an AvailableSlot, or else the function will have returned by this point and ended
//We know the first availableslot is = to our int AvailableSlot
cout<<"Enter name: ";
cin>>name[AvailableSlot];
cout<<"Enter age: ";
cin>>age[AvailableSlot];
cout<<"Enter address: ";
cin>>address[AvailableSlot];
}
void menu()
{
int choice;
cout<<"Menu:"<<endl;
cout<<"1-Add record"<<endl;
cout<<"2-View records"<<endl;
cout<<"3-Edit record"<<endl;
cout<<"4-Delete record"<<endl;
cout<<"5-Exit program"<<endl;
cout<<">> ";
cin>>choice;
if (choice==1)
{
getrecordinfo();
menu();
}
if (choice==2)
{
displayrecords();
menu();
}
if (choice==3)
{
linenum=0;
editrecord();
displayrecords();
menu();
}
if (choice==4)
{
deleterecord();
displayrecords();
menu();
}
}
int main()
{
menu();
system("pause");
}
|