inputting appointments
Nov 12, 2015 at 3:18am UTC
Hello,
This is homework.
I've made a program that lets me set up appointments
I could enter how many I wants but it only prints out 1 person even though I have put entered 2 or more.
the num part somehow isn't working since that's my counter.
anyone want to give me some hints on what I am missing?
I would greatly appreciate it.
so far I have 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 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
#include <iostream>
using namespace std;
// constants
const int NAME_LEN = 30;
const int AFFIL_LEN = 30;
const int PHONE_LEN = 20;
const int MAX_APPT = 30;
struct date
{
int month;
int day;
int year;
};
struct appt
{
char name[NAME_LEN];
char affiliation[AFFIL_LEN];
char phone[PHONE_LEN];
date when;
};
int main()
{
appt appointments[MAX_APPT];
appt *ptr = appointments;
int num = 0;
cout << "My Appointment Scheduler" << endl;
cout << "Please enter the following data for an appointment or enter ctrl-z when you are done." << endl;
cout << "Enter the customer's name: " ;
while (cin.getline((*ptr).name, NAME_LEN))
{
cout << "Please enter the customer's business affiliation: " ;
cin.getline(ptr->affiliation, AFFIL_LEN);
cout << "Please enter the customer's phone number: " ;
cin.getline(ptr->phone, PHONE_LEN);
cout << "Please enter the date of the appointment in the following format:" << endl;
cout << "Enter the month (1-12): " ;
cin >> ptr->when.month;
if (ptr->when.month < 1 || ptr->when.month > 12)
{
cout << "Invalid month." << endl;
exit(1);
}
cout << "Enter the day (1-31): " ;
cin >> ptr->when.day;
if (ptr->when.day < 1 || ptr->when.day > 31)
{
cout << "Invalid day." << endl;
exit(1);
}
cout << "Enter the year (1998): " ;
cin >> ptr->when.year;
cin.ignore(256, '\n' );
cout << "Please enter the next one \n" << endl;
cout << "Enter the customer's name: " ;
ptr++;
num++;
}
ptr =appointments;
cout << "These are your appointments: " << endl;
if (ptr < appointments + num )
{
cout << "Name: " << ptr->name << endl;
cout << "Business Affiliation: " << ptr->affiliation << endl;
cout << "Phone Number: " << ptr->phone << endl;
cout << "Appointment Date: " << ptr->when.month << "/" << ptr->when.day << "/" << ptr->when.year << endl;
}
system("pause" );
return 0;
}
Nov 12, 2015 at 3:31am UTC
Checkout line 78, you're using the if statement. That means you only print 0 or 1 appointment.
To print more appointments, use a loop. (consider while or for)
Nov 12, 2015 at 3:37am UTC
It looks like you just need to iterate over the a stack of appointments that are made.
Right now, it looks like you have only created a variable that only stores
one value . As a result you will only get one person who has an appointment with you.
All you need to do is use a vector (a vector is useful for an unknown amount of appointment) or a list (a list is only useful if you already know how many appointments you want to store in the program or you have an upper limit of the number of appointments).
For your convenience i have posted the links to the reference pages for both links (the 1
st link) and vectors (2
nd link):
http://www.cplusplus.com/reference/list/list/?kw=list
http://www.cplusplus.com/reference/vector/vector/?kw=vector
Please let us know if there is any other way we can help you.
- Hirokachi
Nov 12, 2015 at 11:57am UTC
Thank you!
I'll post back once I've rewritten my program.
Topic archived. No new replies allowed.