Class Objects
Jun 9, 2014 at 7:38pm UTC
When I enter 3 different presidents and their numbers/quotes into the for loop, it only displays the last entered president in all iterations of the display member function loop.
How can I fix this?
Thank you!
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 92 93 94
// MAIN
#include "Presidents.h"
int main()
{
Presidents president;
string name;
string quote;
int number;
for (int i = 0; i < 3; i++)
{
cout << "Enter the first president's number: " ;
cin >> number;
president.setNumber(number);
cout << "Enter his name: " ;
cin >> name;
president.setName(name);
cout << "Enter his quote: " ;
cin >> quote;
president.setQuote(quote);
cout << "\n" ;
}
president.displayPresidents();
return 0;
}
// PRESIDENTS.H
#include <iostream>
#include <string>
using namespace std;
#ifndef PRESIDENTS_H_
#define PRESIDENTS_H_
class Presidents
{
private :
int _number;
string _name;
string _quote;
public :
Presidents();
~Presidents();
void setNumber(int number);
void setName(string name);
void setQuote(string quote);
void displayPresidents()const ;
};
#endif
//PRESIDENTS.CPP
#include "Presidents.h"
Presidents::Presidents()
{
_number = 0;
_name = "" ;
_quote = "" ;
}
//--------------------------------------------------------------------------
Presidents::~Presidents(){}
//--------------------------------------------------------------------------
void Presidents::setNumber(int number)
{
_number = number;
}
//--------------------------------------------------------------------------
void Presidents::setName(string name)
{
_name = name;
}
//--------------------------------------------------------------------------
void Presidents::setQuote(string quote)
{
_quote = quote;
}
//--------------------------------------------------------------------------
void Presidents::displayPresidents()const
{
for (int i = 0; i < 3; i++)
{
cout << "The Presidents are:\n"
<< _name << ", " << _number << " President, said: "
<< "\"" << _quote << ".\"\n\n" ;
}
}
//--------------------------------------------------------------------------
Last edited on Jun 9, 2014 at 7:38pm UTC
Jun 9, 2014 at 7:49pm UTC
You only create one president object in main, so the code is just updating the same president object with different values. So when you print out, you're printing the details for the same president three times.
Jun 9, 2014 at 8:26pm UTC
As wildblue stated.
A possible example with vectors:
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
#include <iostream>
#include <vector>
using std::cout;
using std::cin;
using std::endl;
class Presidents{
private :
std::string name, quote;
int number;
public :
Presidents(){
number=0;
}
void setNumber(int n){
number = n;
}
void setName(std::string str){
name = str;
}
void setQuote(std::string str){
quote = str;
}
void displayPresidents(){
cout<<number<<' ' <<name<<' ' <<quote<<'\n' ;
}
};
int main()
{
Presidents president;
std::vector<Presidents> presidents;
std::string name;
std::string quote;
int number;
for (int i = 0; i < 3; i++)
{
cout << "Enter the first president's number: " ;
cin >> number;
cin.ignore(255,'\n' );
president.setNumber(number);
cout << "Enter his name: " ;
getline(cin,name);
president.setName(name);
cout << "Enter his quote: " ;
getline(cin,quote);
president.setQuote(quote);
cout << "\n" ;
presidents.push_back(president);
}
for (int i = 0; i < 3; ++i)
{
presidents[i].displayPresidents();
}
return 0;
}
Be careful when reading string data from the user, you must clear the buffer before you operate with cin again; if using cin.
Topic archived. No new replies allowed.