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
|
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
void AskForInfoRefs(string &rName, int &rAge);
void AskForInfoPoints(string *pName, int *pAge);
string WriteInfo(string name, int age);
int main()
{
string name, infoRefs, infoPoints;
int *age;
AskForInfoRefs(name, *age); //added asterisk
infoRefs = WriteInfo(name, *age); //added asterisk
AskForInfoPoints(&name, age);
infoPoints = WriteInfo(name, *age); //added asterisk
cout << "\nThis is the info from the references:\n"
<< &infoRefs << endl;
cout << "\nThis is the info from the pointers:\n"
<< infoPoints << endl;
cin.get();
return 0;
}
void AskForInfoRefs(string &rName, int &rAge)
{
cout << "Please enter someone's name: ";
getline(cin, rName);
cout << "Please enter someone's age: ";
cin >> rAge; //removed asterisk
cin.ignore();
}
void AskForInfoPoints(string *pName, int *pAge)
{
cout << "Please enter someone else's name: ";
getline (cin, *pName);
cout << "Please enter someone else's age: ";
cin >> *pAge;
cin.ignore();
}
string WriteInfo(string name, int age)
{
stringstream ss;
ss << "Hi, " << name << "! I wish I were " << age << "!\n";
return ss.str();
}
|