d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > C++ Validation
Add Reply New Topic New Poll
Member
Posts: 18,191
Joined: May 31 2010
Gold: 149.27
Mar 27 2016 07:34pm
I need to find a way to validate the users input in the constructor/class rather than the main()
the area i need to validate is the ::setDay. I tried using a for(;;) around the for loop with an if statement nested in it but couldnt get it to work out. any ideas?
dayType.h
Code
/**Exercise 5



#include <iostream>
#include <string>
using namespace std;

class dayType
{
public:
void setDay(string dayOfWeek);
void printDay() const;
string returnDay() const;
string returnNextDay() const;
string returnPreviousDay() const;
string returnDayByAdding(int num);
dayType();
private:
string dayList[7];
int numberOfDay;
};



void dayType::setDay(string dayOfWeek)
{

for (int index = 0; index < 7; index++)
if (dayList[index] == dayOfWeek)
{
numberOfDay = index;
break;
}
}





void dayType::printDay() const
{
cout << dayList[numberOfDay] << endl;
}

string dayType::returnDay() const
{
return dayList[numberOfDay];
}

string dayType::returnNextDay() const
{
return dayList[numberOfDay + 1];
}

string dayType::returnPreviousDay() const
{
return dayList[numberOfDay - 1];
}

string dayType::returnDayByAdding(int num)
{
int tempDay = numberOfDay + num;
numberOfDay = tempDay % 7;
return dayList[numberOfDay];
}

dayType::dayType()
{
dayList[0] = "Sunday";
dayList[1] = "Monday";
dayList[2] = "Tuesday";
dayList[3] = "Wednesday";
dayList[4] = "Thursday";
dayList[5] = "Friday";
dayList[6] = "Saturday";
numberOfDay = 0;
}



main.cpp

Code
#include "dayType.h"
#include <string>
#include <iostream>
using namespace std;

int main()
{

dayType myWeek;
string weekDay;
int increaseDay;


cout << "Today is ";
myWeek.printDay();


cout << "Enter the day you would like to begin with.";
cin >> weekDay;
myWeek.setDay(weekDay);

cout << "After setting the day to your desired day, the day is ";
myWeek.printDay();

cout << "The day of the week is " << myWeek.returnDay() << endl;
cout << "The next day is " << myWeek.returnNextDay() << endl;
cout << "Yesterday was " << myWeek.returnPreviousDay() << endl;

cout << "Enter the number of days to increase.";
cin >> increaseDay;

cout << "After " << increaseDay << " days, the day of the week is " << myWeek.returnDayByAdding(increaseDay) << endl;

system("PAUSE");
return 0;

}
Member
Posts: 23,261
Joined: Dec 17 2006
Gold: 18,129.00
Mar 28 2016 01:32pm
It seems to work for me on cpp.sh

Make sure you're typing the days of week with a capital letter just as you did in your constructor.

edit: Just as a tip, I'd avoid using 'break' as it is considered bad practice. Structure your loops to avoid it, such as:

Code
while( index < 7){
if(dayList[index] == dayOfWeek)
numberOfDay = index;
else
++index;
}


Also, avoid system("pause). There are settings to keep your terminal open to read it so you don't have to clutter your code with pauses.

This post was edited by PureOwnage2 on Mar 28 2016 01:37pm
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Mar 28 2016 02:22pm
Quote (PureOwnage2 @ Mar 28 2016 03:32pm)
edit: Just as a tip, I'd avoid using 'break' as it is considered bad practice.


I haven't heard that before. Is that just a c++ thing? It's good practice in the other languages in familiar with

This post was edited by carteblanche on Mar 28 2016 02:27pm
Member
Posts: 23,261
Joined: Dec 17 2006
Gold: 18,129.00
Mar 28 2016 02:46pm
Quote (carteblanche @ Mar 28 2016 03:22pm)
I haven't heard that before. Is that just a c++ thing? It's good practice in the other languages in familiar with


After googling around and seeing what the gurus of stack overflow had to say (lmao), it seems that breaks aren't a bad practice like I was taught. The issue that my professors always had with it is that it is easy to lose track of the purpose of breaks in multiple nested loops. When you start putting breaks and gotos everywhere, you're obstructing the flow of logic making it unreadable. So, I guess they're good and bad on a case by case basis. If they're used in a concise way, they're fine. I guess my professors just spoke too much in absolutes. They probably just wanted to avoid reading spaghetti code :P Also, they probably spoke in absolutes in order to force us to learn the concepts behind different loops. Otherwise, we'd have some assembly-type c++ code that is terrible to read. To avoid using breaks, I've always used something like

Code

While(x){
if(condition is true)
x = false
}


But, the system pause thing is something I was always taught too. Sigh... guess I gotta go find out if that is nonsense too.

edit: It seems that system pause is generally frowned upon as it is platform specific and generally an insecure way of allowing you to read your ouput. Consider using a breakpoint and then debug from there. In case anyone reads this and was wondering, I'll save you the google.

This post was edited by PureOwnage2 on Mar 28 2016 02:55pm
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Mar 28 2016 04:25pm
Deliberately setting a variable solely to exit out of the loop is a terrible idea. It's less readable,can have side effects, and can involve adding extra variables in cases where you can't easily manipulate the condition.

For example, how would you break out of this loop without a break?

Code
for (String item in array){}


Not so easy to manipulate the condition, so you'd go out of your way to make it less readable
Member
Posts: 9,834
Joined: Nov 13 2007
Gold: 771.00
Apr 26 2016 07:59am
Quote (carteblanche @ Mar 28 2016 10:25pm)
Deliberately setting a variable solely to exit out of the loop is a terrible idea. It's less readable,can have side effects, and can involve adding extra variables in cases where you can't easily manipulate the condition.

For example, how would you break out of this loop without a break?

Code
for (String item in array){}


Not so easy to manipulate the condition, so you'd go out of your way to make it less readable



What about just changing that if statement within the setDay func to a while loop and remove the break? My data structures professor would throw a fit if we used break outside of switch statements. I think that's still ingrained unfortunately lol.
Member
Posts: 13,425
Joined: Sep 29 2007
Gold: 0.00
Warn: 20%
Apr 26 2016 08:09am
Thanks for reviving a long dead thread just to insert you 2cents.
Member
Posts: 9,834
Joined: Nov 13 2007
Gold: 771.00
Apr 26 2016 08:17am
Quote (AbDuCt @ Apr 26 2016 02:09pm)
Thanks for reviving a long dead thread just to insert you 2cents.



Anytime bud!
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll