pointers, dynamic arrays questions
Dec 9, 2010 at 5:09pm UTC
Hi Everybody here. I am programming a calendar program using pointers and dynamic arrays. My input likes these:
1/1/2001 one
2/2/2002 two
3/3/2003 three
4/4/2004 four
My output should like these:
1/1/2001 one
2/2/2002 two
3/3/2003 three
4/4/2004 four
and also I will sort the date after I finish all input. It is my later job.
My question is
[1] how to read the slash '/';
[2] how to change an line if I done the each line's input.
[3] and also please give me any advices about my calendar programming.
My code likes this:
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
#include <iostream>
#include "event.h"
using namespace std;
int main()
{
int m_day;
int m_month;
int m_year;
string m_activity;
char slash =' \ ' ;
int size=0;
cout<<"welcome to Project 2: Calendar." <<endl;
const int max = 100;
Event *events[size];
while (size<100)
{
events[size] = new Event;
cin>>m_month>>m_day>my_year
size++;
}
date.print();
return 0;
}
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
#ifndef EVENT_H
#define EVENT_H
#include <iostream>
using namespace std;
class Event
{
public :
Event(int newMonth, int newDay, int newYear, string newActivity);
Event();
~Event();
void print();
int getMonth(){return month;}
int getDay(){return day;}
int getYear(){return year;}
string getActivity(){return activity;}
void setMonth(int newMonth){month = newMonth;}
void setDay(int newDay){day = newDay;}
void setYear(int newYear){year = newYear;}
void setActivity(string newActivity){activity = newActivity;}
private :
int month;
int day;
int year;
string activity;
};
#endif
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
#include <iostream>
#include "event.h"
using namespace std;
Event::Event(int newMonth, int newDay, int newYear, string newActivity )
{
month = newMonth;
day = newDay;
year = newYear;
activity = newActivity;
}
Event::Event()
{
month = 0;
day = 0;
year = 1900;
activity = " " ;
}
Event ::~Event()
{
cout<<"destructor called" <<endl;
}
void Event::print()
{
cout<<"month" <<month<<endl;
cout<<"day" <<day<<endl;
cout<<"year" <<year<<endl;
cout<<"activity" <<activity<<endl;
}
Dec 9, 2010 at 9:03pm UTC
To read you could use scanf
char slash =' \ ' ;
What character is that? If you want a backlash you need to escape it (with a backslash)
'\\'
1 2
int size[0];
Event *events[size];//ISO C++ forbids zero-size array
events[size] = new Event;
Why pointers? why dynamic allocation? Just use objects.
If you aren't going to validate the setters just make the attributes public
Dec 9, 2010 at 11:04pm UTC
Thanks. But I know I can use object. But I wanna practice using pointer. It will be better for me to learn other data structure. thanks
Topic archived. No new replies allowed.