I've searched through the already asked questions, but I haven't found what I'm looking for. I'm working with structures, where I store a person's details (first name, last name, birth date and so on...). The goal is then to create an array of pointers to these structures and make a list containing a group of people's information. the main function is vary basic so far.
#include <iostream>
#include "methods.h"
using namespace std;
void printMenu() {
cout << "\n Welcome to the Person program!" << endl
<< " Choose what you would like to do: " << endl
<< " 1. Insert person" << endl
<< " 2. Show person" << endl
<< " 3. Exit" << endl;
}
int main() {
char choice;
person p;
do {
printMenu();
cout << "\n Your choice: ";
cin >> choice;
cin.ignore();
switch(choice) {
case '1': setPerson(p); break;
case '2': getPerson(p); break;
default: cout << "\n Exiting..." << endl;
}
} while(choice == '1' || choice == '2');
return 0;
}
here's the methods.cc file
// methods.cc
#include <iostream>
#include "methods.h"
using namespace std;
static char dummy; // catches the '\n' left in the stream after the usage of cin >>
void setFirstName(char s[]) {
cout << "\n First name: ";
cin.getline(s, 25);
}
void getFirstName(const char s[]) {
cout << "\n First name: " << s << endl;
}
void setLastName(char s[]) {
cout << "\n Last name: ";
cin.getline(s, 25);
}
void getLastName(const char s[]) {
cout << "\n Last name: " << s << endl;
}
void setAddress(address & a) {
cout << "\n Street: ";
cin.getline(a.street, 25);
cout << "\n Number: ";
cin.getline(a.number, 6);
cout << "\n Town: ";
cin.getline(a.town, 25);
cout << "\n Zip code: ";
cin.getline(a.zip, 25);
cout << "\n Province: ";
cin.getline(a.province, 25);
}
void getAddress(const address & a) {
cout << "\n Street: " << a.street << endl;
cout << "\n Number: " << a.number << endl;
cout << "\n Town: " << a.town << endl;
cout << "\n Zip code: " << a.zip << endl;
cout << "\n Province: " << a.province << endl;
}
void setBirthDate(birthdate & bd) {
cout << "\n Day: ";
cin >> bd.day;
cin.get(dummy);
cout << "\n Month: ";
cin >> bd.month;
cin.get(dummy);
cout << "\n Year: ";
cin >> bd.year;
cin.get(dummy);
}
void getBirthDate(const birthdate & bd) {
cout << "\n Day: " << bd.day << endl;
cout << "\n Month: " << bd.month << endl;
cout << "\n Year: " << bd.year << endl;
}
void setGender(char & g) {
cout << "\n Gender: ";
cin >> g;
cin.get(dummy);
}
void getGender(const char & g) {
cout << "\n Gender: " << g << endl;
}
void setPerson(person & p) {
setFirstName(p.firstName);
setLastName(p.lastName);
setAddress(p.location);
setBirthDate(p.bd);
setGender(p.gender);
}
void getPerson(const person & p) {
getFirstName(p.firstName);
getLastName(p.lastName);
getAddress(p.location);
getBirthDate(p.bd);
getGender(p.gender);
}
The program works fine, but, after entering all the information and prompting the program to display the entered data, the first three fields (first name, last name and street) all show the same output, that is, the street's name... somehow the first and last names are not saved into the respective char arrays...
for example, say I enter A and B as first and last name then C as the street name, the output will be
First name: C
Last name: C
Street: C
... then from here, the output is correct...
EDIT: btw, I haven't used the string type on purpose. Being it object-oriented, it is not covered in the programming course, so I'm stuck with the c string type (arrays of chars or pointers to char)
EDIT 2: here's the methods.h file. I've found the error myself. see comments in the code in struct person
// methods.h
#ifndef METHODS_H
#define METHODS_H
struct address {
char street[15];
char number[6];
char town[15];
char zip[6];
char province[3];
};
struct birthdate {
int day;
int month;
int year;
};
struct person {
char firstName[25]; // error due to me omitting the array length.
char lastName[25]; // same here, I had written char firstName[] & char lastName[]
address location;
birthdate bd;
char gender;
};
// base methods
void setFirstName(char s[]);
void getFirstName(const char s[]);
void setLastName(char s[]);
void getLastName(const char s[]);
void setAddress(address & a);
void getAddress(const address & a);
void setBirthDate(birthdate & bd);
void getBirthDate(const birthdate & bd);
void setGender(char & g);
void getGender(const char & g);
void setPerson(person & p);
void getPerson(const person & p);
#endif
thanks for all your advice!