- #1
faust9
- 692
- 2
Ok, I've been programming with C on and off for a while but I decided to take a C++ class to learn something new. We finally got to classes, and strings which are plaguing me to no end. Here's the problem, I have to write at program which creates a blank person a compile time. The software user will then enter the required information (name and age) for the fictional person. The user will then have the option to view the created person. The user may not view the created person until the initial data has been entered(not too worried about this part.
My problem is my program asks for the name and age but only the ag updates. Also, if I include a constructor the program won't replace the information supplied by the constructor. I'd like to set the default age to zero to act as the flag for the verification process. Anyway, here's the code:
main
person.h
person.cpp
I commented out the verification portion. It should work (shouldn't it?).
stdafx.h
I have a menu class that works so I'm not going to post that code.
I think my problem with the name portion is with the getline(cin, name) function. Am I using it correctly? It's new to me, so I'm probably way out in left field.
here's th output:
Notice how the age didn't update not the name either. I'm using gcc, mingw on my wifes XP box because the class I'm taking requires win32 console apps.
Thanks for any help BTW.
My problem is my program asks for the name and age but only the ag updates. Also, if I include a constructor the program won't replace the information supplied by the constructor. I'd like to set the default age to zero to act as the flag for the verification process. Anyway, here's the code:
main
Code:
#include "stdafx.h" //basic run of the mill include
#include "menu.h" //required to create menus
#include "person.h" //header file which supplies the person class
int main()
{
Menu personMaker; //creates a new menu object.
int selection; //used to determine which menue item was selected
//three menu line items created by using the menu class.
personMaker.addItem ("Create a person");
personMaker.addItem ("View person information");
personMaker.addItem ("I bid thee a fond farewell");
//loop used to allow person creations.
do
{
//menu title
personMaker.displayMenu("David T Platt's Homework Extravaganva");
selection= personMaker.getChoice(); //gets menu input from user.
person hwPerson; //creates instance of person.
//used to select which function to call
switch (selection)
{
case 1:
{
hwPerson.getdata(); //calls function for daata input
break;
}
case 2:
{
hwPerson.showdata(); //calls function for data output
break;
}
case 3:
{
break;
}
}
}while(selection !=3);
return 0;
}
person.h
Code:
//obligatory includes
#include "stdafx.h"
class person
{
private:
int age;
string name;
public:
void getdata();
void showdata();
person();
};
person.cpp
Code:
//obligatory includes
#include "stdafx.h"
#include "person.h"
person::person() : age(0), name("nonoame")
{
}
void person::getdata()
{
cout << "\nHow old is this person? ";
cin >> age;
cout << "Please enter the name of the person you would like to create: ";
cin.ignore(10, '\n');
getline(cin, name);
return;
}
void person::showdata()
{
/*if (age==0)
{
cout << "Sorry please enter the information prior to viewing it:";
return;
}*/
cout << name << " is " << age;
return;
}
I commented out the verification portion. It should work (shouldn't it?).
stdafx.h
Code:
#include <iostream>
#include <tchar.h>
#include <string>
#include <conio.h>
using namespace std;
I have a menu class that works so I'm not going to post that code.
I think my problem with the name portion is with the getline(cin, name) function. Am I using it correctly? It's new to me, so I'm probably way out in left field.
here's th output:
Code:
1) Create a person
2) View person information
3) I bid thee a fond farewell
Enter Choice: 1
How old is this person? 25
Please enter the name of the person you would like to create: monty python
1) Create a person
2) View person information
3) I bid thee a fond farewell
Enter Choice: 2
nonoame is 0
1) Create a person
2) View person information
3) I bid thee a fond farewell
Enter Choice:
Notice how the age didn't update not the name either. I'm using gcc, mingw on my wifes XP box because the class I'm taking requires win32 console apps.
Thanks for any help BTW.