how to add values into an array dynamically .. is there any way where u can add values into array without using vector. The code which i have blocked is used for adding values into reminderObject and while running the program the input is asked twice that is frm enter day to enter min idk why its asking twice .
int reminderCount = 0;
...
reminderObject[reminderCount] = newRem;
++reminderCount;
And yes remove is also possible
There're several problems in your code like line 103 which will crash your program in case of an invalid month. Look here http://www.cplusplus.com/forum/beginner/43617/#msg240465 to learn how to avoid recursion while displaying
when i output the program in case of invalid month it doesnt crash u just have to give valid month number then it asks for next option
the code which u have written should be written in reminderList() in line 193 .?
i am not able to add new reminders into existing list of reminder list .
when i output the program in case of invalid month it doesnt crash u just have to give valid month number then it asks for next option
displayLongFormat() does crash on line 103 (endless recusion) if month is not 1 - 12. So why not replacing default:displayLongFormat(); with default:cout<<"- invalid date -"<<endl;
the code which u have written should be written in reminderList() in line 193 .?
for (int i=0; i<=9; i++)
{
cout<<"\n"<<reminderObject[i].message<<" "<<reminderObject[i].date.day<<"/"<<reminderObject[i].date.month<<"/"<<reminderObject[i].date.year<<","<<reminderObject[i].time.hour<<":"<<reminderObject[i].time.min;
}
since there will be total 10 reminders list ( 3 by default 7 items added ) but when i do this the last reminder which is watch IPL is getting erased and is replaced by new reminder which is added. what should i do to add multiple reminders at a time
how to delete any one item from an existing array
can i do like this
i am finding in difficulty in inherent logic. is there a way to overcome that like is there a book r something like that or i just have to keep doing different program
and while adding an item into existing array this item replaces the last item which is already there in the reminder list
i am finding in difficulty in inherent logic. is there a way to overcome that
Yes. Debugging. Stepping through your code line by line and watching the content of the variables will definitely show you what's going on. It might be tedious at first but that will make you a better programmer because you know what's going in your code.
If I solve all the problems for you then the problems are solved but you still don't know what's going on.
is there a book r something
Usually most of the things you find in those books you can also find in the internet.
and while adding an item into existing array this item replaces the last item which is already there in the reminder list
so if i put reminderCount = 5 then the item will be at reminderObject[4]. and so on so if i want to add the reminder after the existing list then i'll have put it as reminderCount = 4 then in output all the first 3 reminders are shown and then the fourth reminder will get displayed which is at reminderObject[3]
#include <iostream>
usingnamespace std;
void remindersList();
void options();
void newremindersList();
class dateClass
{
public:
int day;
int month;
int year;
void input();
void displayLongFormat();
void displayShortFormat();
};
void dateClass::input()
{
cout<<"\nEnter day (1-31) : "<<endl;
do
{
cin>>day;
cout<<day;
}
while (day>31 || day<1);
cout<<"\nSelect the month : "<<endl;
cout<<"\n1.Jan"<<endl;
cout<<"2.Feb"<<endl;
cout<<"3.Mar"<<endl;
cout<<"4.Apr"<<endl;
cout<<"5.May"<<endl;
cout<<"6.June"<<endl;
cout<<"7.July"<<endl;
cout<<"8.Aug"<<endl;
cout<<"9.Sept"<<endl;
cout<<"10.Oct"<<endl;
cout<<"11.Nov"<<endl;
cout<<"12.Dec\n"<<endl;
cout<<"Enter month"<<endl;
do
{
cin>>month;
cout<<month;
}
while (month>12 || month<1);
cout<<"\n\nEnter Year : "<<endl;
cin>>year;
cout<<year;
}
void dateClass::displayLongFormat()
{
switch (month)
{
case 1:
cout<<day<<" "<<"Jan"<<" "<<year<<endl;
break;
case 2:
cout<<day<<" "<<"Feb"<<" "<<year<<endl;
break;
case 3:
cout<<day<<" "<<"Mar"<<" "<<year<<endl;
break;
case 4:
cout<<day<<" "<<"Apr"<<" "<<year<<endl;
break;
case 5:
cout<<day<<" "<<"May"<<" "<<year<<endl;
break;
case 6:
cout<<day<<" "<<"June"<<" "<<year<<endl;
break;
case 7:
cout<<day<<" "<<"July"<<" "<<year<<endl;
break;
case 8:
cout<<day<<" "<<"Aug"<<" "<<year<<endl;
break;
case 9:
cout<<day<<" "<<"Sept"<<" "<<year<<endl;
break;
case 10:
cout<<day<<" "<<"Oct"<<" "<<year<<endl;
break;
case 11:
cout<<day<<" "<<"Nov"<<" "<<year<<endl;
break;
case 12:
cout<<day<<" "<<"Dec"<<" "<<year<<endl;
break;
default:displayLongFormat();
break;
}
}
void dateClass::displayShortFormat()
{
cout<<day<<"/"<<month<<"/"<<year<<endl;
}
class timeClass
{
public:
int hour;
int min;
void input();
void output();
};
void timeClass::input()
{
cout<<"\nEnter hour (0-23) : "<<endl;
do
{
cin>>hour;
}
while(hour>23 || hour<0);
cout<<"\nEnter min (0-59) : "<<endl;
do
{
cin>>min;
}
while (min>59 || min<0);
}
void timeClass::output()
{
cout<<hour<<":"<<min<<endl;
}
class reminder
{
public:
string message;
void displayItems();
void outputLongFormat();
void outputShortFormat();
dateClass date;
timeClass time;
}reminderObject[5];
int reminderCount = 4;
int reminderNumber;
reminder newRem; // IMPORTANT NOTE
void reminder::displayItems()
{
cout<<"\n"<<reminderObject[0].message<<endl;
date.displayLongFormat();
cout<<reminderObject[0].time.hour<<":"<<reminderObject[0].time.min<<endl;
int select;
cout<<"\nPress 1 to view all the reminders"<<endl;
cin>>select;
switch (select)
{
case 1:
remindersList();
break;
default:displayItems();
break;
}
}
void remindersList()
{
for (int i=0; i<=3; i++)
{
cout<<"\n"<<reminderObject[i].message<<" "<<reminderObject[i].date.day<<"/"<<reminderObject[i].date.month<<"/"<<reminderObject[i].date.year<<","<<reminderObject[i].time.hour<<":"<<reminderObject[i].time.min;
}
int option;
cout<<"\n\nSelect Options\n"<<endl;
cout<<"1.Add Reminder"<<endl;
cout<<"2.Delete Reminder"<<endl;
cin>>option;
switch (option)
{
case 1:
cout<<"Enter Message :"<<endl;
cin>>newRem.message;
cout<<newRem.message;
newRem.date.input(); // IMPORTANT NOTE
newRem.time.input(); // IMPORTANT NOTE
newRem.time.output(); // IMPORTANT NOTE
reminderObject[reminderCount] = newRem;
reminderCount++;
newremindersList();
break;
case 2:
if(reminderCount > 0)
{
cout<<"Enter item number "<<endl;
int n;
cin>>n;
if((n >= 0) && (n < reminderCount)) // Check if the user entered a valid item
{
reminderObject[n] = reminderObject[reminderCount];
reminderCount--;
}
else
{
cout<<"Invalid item number"<<endl;
newremindersList();
}
}
break;
default:remindersList();
break;
}
}
void newremindersList()
{
for (int i=0; i<=reminderNumber; i++)
{
cout<<"\n"<<reminderObject[i].message<<" "<<reminderObject[i].date.day<<"/"<<reminderObject[i].date.month<<"/"<<reminderObject[i].date.year<<","<<reminderObject[i].time.hour<<":"<<reminderObject[i].time.min;
}
}
int main (int argc, constchar * argv[])
{
reminderNumber=4;
reminderObject[0].message="Go and get milk";
reminderObject[0].date.day=28;
reminderObject[0].date.month=5;
reminderObject[0].date.year=2011;
reminderObject[0].time.hour=1;
reminderObject[0].time.min=40;
reminderObject[1].message="Drink Tea";
reminderObject[1].date.day=28;
reminderObject[1].date.month=5;
reminderObject[1].date.year=2011;
reminderObject[1].time.hour=2;
reminderObject[1].time.min=00;
reminderObject[2].message="Sleep";
reminderObject[2].date.day=28;
reminderObject[2].date.month=5;
reminderObject[2].date.year=2011;
reminderObject[2].time.hour=3;
reminderObject[2].time.min=00;
reminderObject[3].message="Watch IPL";
reminderObject[3].date.day=28;
reminderObject[3].date.month=5;
reminderObject[3].date.year=2011;
reminderObject[3].time.hour=6;
reminderObject[3].time.min=00;
reminderObject[0].displayItems();
}
i got the output but when i wanted to delete item 1 then i'll have to give 0 in enter item number , in the output in 1st item it shows as 0/0/0 , rest of items are shown ... is there a way where it shows only remaining items than showing null characters in 1st item . I want to delete an item after adding items . I want to sort these items based on the dates .
case 2:
if(reminderCount > 0) // Check if remove is possible
{
cout<<"Enter item number"<<endl;
int n;
cin>>n;
if((n >= 0) && (n < reminderCount)) // Check if the user entered a valid item
{
--reminderCount;
reminderObject[n] = reminderObject[reminderCount];
}
else
cout<<"Invalid item number"<<endl;
}
else
cout<<"No item to remove"<<endl;
break;
is this how the code works
when the reminderCount is greater than 0 . input the item number , when the number is greater than 0 and when reminderCount is less than 0 decrement reminderCount ( if it is 4 it becomes 3 ) the reminderObject of the array reminderCount is assigned to reminderObject of that item number array . correct me if i am wrong
#include <iostream>
usingnamespace std;
void remindersList();
void options();
void newremindersList();
class dateClass
{
public:
int day;
int month;
int year;
// Add a constructor that intialze the values
void input();
void displayLongFormat();
void displayShortFormat();
};
void dateClass::input()
{
cout<<"\nEnter day (1-31) : "<<endl;
do
{
cin>>day;
cout<<day;
}
while (day>31 || day<1); // Wrong logic
cout<<"\nSelect the month : "<<endl;
...
cout<<"Enter month"<<endl;
do
{
cin>>month;
cout<<month;
}
while (month>12 || month<1); // Wrong logic
cout<<"\n\nEnter Year : "<<endl;
cin>>year;
cout<<year;
}
void dateClass::displayLongFormat()
{
switch (month)
{
...
default:displayLongFormat(); // This will crash, but it looks like you need to observe it yourself
break;
}
}
void dateClass::displayShortFormat()
{
cout<<day<<"/"<<month<<"/"<<year<<endl;
}
class timeClass
{
public:
int hour;
int min;
void input();
void output();
};
void timeClass::input()
{
cout<<"\nEnter hour (0-23) : "<<endl;
do
{
cin>>hour;
}
while(hour>23 || hour<0); // Wrong logic
cout<<"\nEnter min (0-59) : "<<endl;
do
{
cin>>min;
}
while (min>59 || min<0); // Wrong logic
}
void timeClass::output()
{
cout<<hour<<":"<<min<<endl;
}
class reminder
{
public:
string message;
void displayItems();
void outputLongFormat();
void outputShortFormat();
dateClass date;
timeClass time;
};
constint remindeMax = 5 // This is the maximum amount of items (with this you can check whether you'r in range)
reminder reminderObject[remindeMax]; // This are the items with the amount above
int reminderCount = 0; // The number of items used (currently none)
int reminderNumber;reminder newRem; // IMPORTANT NOTE void reminder::displayItems()
{
cout<<"\n"<<reminderObject[0].message<<endl; // You want the information of this object not always the first
date.displayLongFormat(); // ok
cout<<reminderObject[0].time.hour<<":"<<reminderObject[0].time.min<<endl; // Why not time.output();?
// The following should not be here
int select;
cout<<"\nPress 1 to view all the reminders"<<endl;
cin>>select;
switch (select)
{
case 1:
remindersList();
break;
default:displayItems();
break;
}
}
void remindersList()
{
// 3 is not valid anymore
for (int i=0; i<=3; i++)
{
cout<<"\n"<<reminderObject[i].message<<" "<<reminderObject[i].date.day<<"/"<<reminderObject[i].date.month<<"/"<<reminderObject[i].date.year<<","<<reminderObject[i].time.hour<<":"<<reminderObject[i].time.min;
}
// Use (misnamed) newremindersList()
newremindersList(); // Simplier isn't it
int option;
cout<<"\n\nSelect Options\n"<<endl;
cout<<"1.Add Reminder"<<endl;
cout<<"2.Delete Reminder"<<endl;
cin>>option;
switch (option)
{
case 1:
if(reminderCount < remindeMax) // You cannot add an item if your array is full. If we don't check it may crash!
{
// Note that reminderCount is the item after the last valid item
// With the if clause above we made sure that at least 1 item is available
// So you don't need a temporary item like 'newRem' anymore
cout<<"Enter Message :"<<endl;
cin>>reminderObject[reminderCount].message;
cout<<reminderObject[reminderCount].message;
reminderObject[reminderCount].date.input(); // IMPORTANT NOTE
reminderObject[reminderCount].time.input(); // IMPORTANT NOTE
reminderObject[reminderCount].time.output(); // IMPORTANT NOTE
reminderCount++; // Now the so prepared item gets valid!
newremindersList(); // This shows all valid items
}
else
cout<<"No further reminder available!"<<endl;
break;
case 2:
if(reminderCount > 0) // This makes sure that there is actually an item to remove!
{
cout<<"Enter item number "<<endl;
int n;
cin>>n; // Here the user enters the item number we displayed with newremindersList()
// At newremindersList() we do i + 1 so there must be an n - 1 to access the correct item
// Keep in mind the index of an array always starts with 0 (_not_ 1)
if((n > 0) && (n <= reminderCount)) // This checks if the user entered a valid value
{
// The following line makes the last item invalid
reminderCount--; // It is important that this expression comes first
// Now after decrementing the reminderCount is the index to the previously last valid item
reminderObject[n - 1] = reminderObject[reminderCount]; // Note that n - 1 (the user entered the index + 1)
// The line above copies the last item which was made invalid to the position where the item to be deleted is (hence overwriting it)
// It is a trick to keep deletion simple!
newremindersList(); // This shows all valid items
}
else
{
cout<<"Invalid item number"<<endl;
newremindersList();
}
}
break;
default:remindersList();
break;
}
}
void newremindersList() // Better rename it to showremindersList()
{
for (int i=0; i<=reminderNumber; i++)
{
cout<<"\n"<<reminderObject[i].message<<" "<<reminderObject[i].date.day<<"/"<<reminderObject[i].date.month<<"/"<<reminderObject[i].date.year<<","<<reminderObject[i].time.hour<<":"<<reminderObject[i].time.min;
}for (int i=0; i<=reminderCount; i++) // Using reminderCount is important here! Otherwise you wont see the really used item
{
cout<<"\n" << i + 1 << ":";
reminderObject[i].displayItems(); // This simplifies it a lot, don't you think so
}
}
int main (int argc, constchar * argv[])
{
reminderNumber=4; // Sorry not needed
...
reminderCount = 4; // Now you added 4 items at once!
remindersList(); // reminderObject[0].displayItems();
}