• 0

Help with CLASSES and a SWITCH statement


Question

Hi guys, I'm trying to run this program that asks the user to select 1 - 7 from a menu then output it in a printData() function member from my class DaysofTheWeek.

I can get the porgram to compile with no errors but my output is not what i want or expect. I'm still new and don't fully understand the grammer of CLASSES and how to fully use them.

Any help would be awesome!

#include "stdafx.h"

#include <iostream>

#include <string>

using namespace std;

class DayofTheWeek

{

string day;

int selection;

public:

void setdata(string, int);

string getdata();

void printdata();

};

int main()

{

// int selection

DayofTheWeek today;

// today.setdata;

today.printdata();

system("pause");

return 0;

}

void DayofTheWeek::setdata(string temp, int temp2)

{

day = temp;

selection = temp2;

cout << "Select todays' day: " << endl;

cout << "1: Monday" << endl;

cout << "2: Tuesday" << endl;

cout << "3: Wednesday" << endl;

cout << "4: Thursday" << endl;

cout << "5: Friday" << endl;

cout << "6: Saturday" << endl;

cout << "7: Sunday" << endl;

switch(temp2)

{

case 1:

cout << "Yesterday was Sunday, Tomorrow will be Tuesday, and today is Monday." << endl;

break;

case 2:

cout << "Yesterday was Monday, Tomorrow will be Wednesday, and today is Tuesday." << endl;

break;

case 3:

cout << "Yesterday was Tuesday, Tomorrow will be Thursday, and today is Wednesday." << endl;

break;

case 4:

cout << "Yesterday was Wednesday, Tomorrow will be Friday, and today is Thursday." << endl;

break;

case 5:

cout << "Yesterday was Thursday, Tomorrow will be Saturday, and today is Friday." << endl;

break;

case 6:

cout << "Yesterday was Friday, Tomorrow will be Sunday, and today is Saturday." << endl;

break;

case 7:

cout << "Yesterday was Saturday, Tomorrow will be Monday, and today is Sunday." << endl;

break;

default:

cout << "Sorry, that selected isn't listed. Try again" << selection << endl;

}

}

string DayofTheWeek::getdata()

{

return day;

}

void DayofTheWeek::printdata()

{

cout << selection << day << endl;

}

Link to comment
Share on other sites

10 answers to this question

Recommended Posts

  • 0

From what I'm understanding your switch should be in the printData method rather than the setData one.

Your program flow also seems to have problems, when do you read which day theuser inputed? You are setting day before printing the options.

Link to comment
Share on other sites

  • 0
From what I'm understanding your switch should be in the printData method rather than the setData one.

Your program flow also seems to have problems, when do you read which day theuser inputed? You are setting day before printing the options.

Well thats my problem, I'm having trouble tryin to understand how to use Classes.

I essentially need the user to be presented with the menu and have them make a selection. How the program is coded is not the problem. I just need to show that I can make it use of implementing CLASSES correctly. I'm trying to learn, but I suppose my learning curve is a little off on this.

Link to comment
Share on other sites

  • 0

Classes are usually used to represent specific objects that hold data. They also provide a set of useful functions; some of which are used to manipulate data stored in the object. The set and get functions are usually used to assign or retrieve the value of variables in an object.

If I were gonna use this to example of how to implement a class I might leave the menu and user prompt stuff in main and maybe put the switch case print statements in the printData function. Then have functions in the class like setDay which assigns a name to the day like "Monday", "Tuesday", etc. You could have another string variable called "notes". Then have a addNote function where the user could assign something like "Monday sucks".

The printData function could be used show the user all of the data that was stored in that object. Like

"Today is Monday"

"Yesterday is Sunday...."

"Notes: Monday sucks.."

So the main would just show the menu and get user input. Then the user could set the day, add notes or whatever other crap.

Maybe not the best but I guess that's just one way you could go about doing this.

Edited by Rob2687
Link to comment
Share on other sites

  • 0

Maybe something like this?

You pretty much had it. You just have to prompt the user to get the input from them.

#include "stdafx.h"
#include &lt;iostream&gt;
#include &lt;string&gt;

using namespace std;


class DayofTheWeek
{
	private:
		string day;
		int selection;

	public:

		void setData(int);
		void printdata();
		void displayMenu();

};

int main()
{
	DayofTheWeek today;

	today.displayMenu();
	today.printdata();

	system("pause");

	return 0;

}


void DayofTheWeek::printdata()
{
	switch(selection)
	{
		case 1:
			cout &lt;&lt; "Yesterday was Sunday, Tomorrow will be Tuesday, and today is Monday." &lt;&lt; endl;
			break;

		case 2:
			cout &lt;&lt; "Yesterday was Monday, Tomorrow will be Wednesday, and today is Tuesday." &lt;&lt; endl;
			break;

		case 3:
			cout &lt;&lt; "Yesterday was Tuesday, Tomorrow will be Thursday, and today is Wednesday." &lt;&lt; endl;
			break;

		case 4:
			cout &lt;&lt; "Yesterday was Wednesday, Tomorrow will be Friday, and today is Thursday." &lt;&lt; endl;
			break;

		case 5:
			cout &lt;&lt; "Yesterday was Thursday, Tomorrow will be Saturday, and today is Friday." &lt;&lt; endl;
			break;

		case 6:
			cout &lt;&lt; "Yesterday was Friday, Tomorrow will be Sunday, and today is Saturday." &lt;&lt; endl;
			break;

		case 7:
			cout &lt;&lt; "Yesterday was Saturday, Tomorrow will be Monday, and today is Sunday." &lt;&lt; endl;
			break;

		default:
			cout &lt;&lt; "Sorry, that selected isn't listed. Try again" &lt;&lt; selection &lt;&lt; endl;

	}
}

void DayofTheWeek::displayMenu()
{
	int temp2;

	cout &lt;&lt; "Select todays' day: " &lt;&lt; endl;
	cout &lt;&lt; "1: Monday" &lt;&lt; endl;
	cout &lt;&lt; "2: Tuesday" &lt;&lt; endl;
	cout &lt;&lt; "3: Wednesday" &lt;&lt; endl;
	cout &lt;&lt; "4: Thursday" &lt;&lt; endl;
	cout &lt;&lt; "5: Friday" &lt;&lt; endl;
	cout &lt;&lt; "6: Saturday" &lt;&lt; endl;
	cout &lt;&lt; "7: Sunday" &lt;&lt; endl;

	cout &lt;&lt; "Selection: ";
	cin &gt;&gt; temp2;

	setData(temp2);
}

void DayofTheWeek::setData(int choice)
{
	selection = choice;
}

Didn't really test it though so may be a few bugs! You could also make getting the user data a separate function. Infact that's probably how I should have done it, but for quickness :p

Hope it helps. With regards to your code, you might want to put your actual Class declaration into a header file, and then include it into the cpp file. You could also try adding some input validation next, to ensure the user enters not only an integer, but one that is in the correct range.

Link to comment
Share on other sites

  • 0

Wow vizion, yeah i ran it and it works perfect. And at the moment we're not learning or trying to learn how to make header files till later on so I know it could be done for less coding, but I wanted to stick to what he is teaching right now.

Also, I have a quick question. I don't really understand how string are created from text.

But now, i'm tinkering into adding the current time possible using the localtime() function. But when the time is outputed I would like to grab it and do something like hours++ and hours -- aswell for minutes++ and minutes -- that is assuming that i can grab the hours and minutes and pass it to a variable.

Anyone have any insight on how this is done?

Link to comment
Share on other sites

  • 0
What do you mean by "how string are created from text"? Where are you learning to program?

Well i mean when a user CIN something grab what ever he/she inputs and created a string variable from it so that i can manipulate it later on.

I'm not good with the terminology sorry

Link to comment
Share on other sites

  • 0
Well i mean when a user CIN something grab what ever he/she inputs and created a string variable from it so that i can manipulate it later on.

I'm not good with the terminology sorry

You could do something like this:

string input;
cin &gt;&gt; input;

Make sure you put #include <string> at the top of the document if you want to use strings. [EDIT: I see you've already done this, sorry]

Link to comment
Share on other sites

  • 0
You could do something like this:

string input;
 cin &gt;&gt; input;

Make sure you put #include <string> at the top of the document if you want to use strings. [EDIT: I see you've already done this, sorry]

Yep that's probably how to do it if you want to manipulate it later on.

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.