displaying variables from a function class

Hello, I am very new to C++ can some one help with the following problem. I am trying to display a variable from a class function the code works in debug but the variable is not displayed. Here is my code so far.

#include <iostream>
#include <string>

using namespace std;

class dayType
{
public:
string day;
void setday(string);
void setupday();
private:
};

void dayType::setday(string)
{
cout << "Please enter a day of the week." << endl;
cin >> day;
}
void dayType::setupday()
{
cout << "You have entered: " << day << endl;
}
int main()
{

dayType setday;
setday.setday();

dayType setupday;
setupday.setupday();


system ("PAUSE");
return 0;
}
You've created two different instances of of your class.
setday.setday() asks the user to enter a value which is stored in setday.day.

setupday.setupday() displays the value of setupday.day, which is a different uninitialized instance.

BTW, it's poor practice to name your variables andf functions the same thing. Very confusing to the reader and possibly to the compiler.

Try this instead:
1
2
3
4
5
6
7
8
int main()
{  dayType dt;

    dt.setday();
    dt.setupday();
    system ("PAUSE");
    return 0;
}


PLEASE USE CODE TAGS (the <> formatting button) when posting code.
Last edited on
Thanks for your help that worked I am also changing the variables and function names. They make sense to me but I see what you mean.
Topic archived. No new replies allowed.