Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Differeneces between 2 codes help
New Posts  All Forums:Forum Nav:

Differeneces between 2 codes help

post #1 of 3
Thread Starter 
EDIT: sorry the code windows didn't format right, but its right, disregard the added spaces.

Alright, my teacher gave an assignment and he wants us to validate the data that is coming in. Even though he said that we just have to make sure that all the data is even, he allows us to go above and beyond, for better grades of course. I decided to do this and when he saw my code today he said it was way to cumbersome....

the way I wrote my code with a lot of && and || to get my error codes.
Here is my code:

Code:
if (((firstMonth == 1)
|| (firstMonth == 3)
|| (firstMonth == 5)
|| (firstMonth == 7)
|| (firstMonth == 8)
|| (firstMonth == 10)
|| (firstMonth == 12)
|| (secondMonth == 1)
|| (secondMonth == 3)
|| (secondMonth == 5)
|| (secondMonth == 7)
|| (secondMonth == 10)
|| (secondMonth == 12))
&& ((firstDay > 31)
|| (secondDay > 31))
|| (firstDay < 1)
|| (secondDay < 1))
{
cout << "ERROR: " << setw(2) << setfill('0') << firstMonth << "/" 
 << setw(2) << firstDay << "/" 
 << setw(4) << firstYear
 << " or " << setw(2) << secondMonth << "/"
 << setw(2) << secondDay << "/"
 << setw(4) << secondYear << setfill(' ')
 << "  is invalid, the day needs to be between 1 and 31." << endl;
fout << "ERROR: " << setw(2) << setfill('0') << firstMonth << "/" 
 << setw(2) << firstDay << "/" 
 << setw(4) << firstYear
 << " or " << setw(2) << secondMonth << "/"
 << setw(2) << secondDay << "/"
 << setw(4) << secondYear << setfill(' ')
 << "  is invalid, the day needs to be between 1 and 31." << endl;
error = true;
}
Now he wants me to change it to look something like:

Code:
if (firstDay > 31 && firstMonth >= 8 && firstMonth % 2 == 0)
{
cout << "ERROR: " << setw(2) << setfill('0') << firstMonth << "/" 
 << setw(2) << firstDay << "/" 
 << setw(4) << firstYear
 << " or " << setw(2) << secondMonth << "/"
 << setw(2) << secondDay << "/"
 << setw(4) << secondYear << setfill(' ')
 << "  is invalid, the day needs to be between 1 and 31." << endl;
fout << "ERROR: " << setw(2) << setfill('0') << firstMonth << "/" 
 << setw(2) << firstDay << "/" 
 << setw(4) << firstYear
 << " or " << setw(2) << secondMonth << "/"
 << setw(2) << secondDay << "/"
 << setw(4) << secondYear << setfill(' ')
 << "  is invalid, the day needs to be between 1 and 31." << endl;
error = true;
}
But then I would have to repeat the cout and fout code which in my opinion would be even more cumbersome?

any one have any opinions, or better ways to write this? I have to do it for the months that have 30 days, and the ones with 31. I have already done feb. with the leap years. My leap year code is as follows if any one has anything that can help that:

Code:
if (firstYear % 4 == 0)
valYear4 = true;

if ((valYear4 == true) 
&& (firstYear % 100 == 0))
valYear100 = true;

if ((valYear100 == true) 
&& (firstYear % 400 == 0))
valYear400 = true;

if ((valYear4 == true) 
&& (valYear100 ==  true) 
&& (valYear400 == true))
leapYear = true;
else if ((valYear4 == true) 
&& (valYear100 == false))
leapYear = true;
else
leapYear = false;

if ((leapYear == true) 
&& ((firstMonth == 2)
|| (secondMonth == 2))
&& ((firstDay > 29)
|| (secondDay > 29)
|| (firstDay < 1)
|| (secondDay < 1)))
{
cout << "ERROR: " << setw(2) << setfill('0') << firstMonth << "/" 
 << setw(2) << firstDay << "/" 
 << setw(4) << firstYear
 << " or " << setw(2) << secondMonth << "/"
 << setw(2) << secondDay << "/"
 << setw(4) << secondYear << setfill(' ')
 << "  is invalid, the day need to be between 1 and 29." << endl;
fout << "ERROR: " << setw(2) << setfill('0') << firstMonth << "/" 
 << setw(2) << firstDay << "/" 
 << setw(4) << firstYear
 << " or " << setw(2) << secondMonth << "/"
 << setw(2) << secondDay << "/"
 << setw(4) << secondYear << setfill(' ')
 << "  is invalid, the day needs to be between 1 and 29." << endl;
error = true;
}

if ((leapYear == false)
&& ((firstMonth == 2)
|| (secondMonth == 2))
&& ((firstDay > 28)
|| (secondMonth >28)
|| (firstDay < 1)
|| (secondDay < 1)))
{
cout << "ERROR: " << setw(2) << setfill('0') << firstMonth << "/" 
 << setw(2) << firstDay << "/" 
 << setw(4) << firstYear
 << " or " << setw(2) << secondMonth << "/"
 << setw(2) << secondDay << "/"
 << setw(4) << secondYear << setfill(' ')
 << "  is invalid, the day needs to be between 1 and 28." << endl;
fout << "ERROR: " << setw(2) << setfill('0') << firstMonth << "/" 
 << setw(2) << firstDay << "/" 
 << setw(4) << firstYear
 << " or " << setw(2) << secondMonth << "/"
 << setw(2) << secondDay << "/"
 << setw(4) << secondYear << setfill(' ')
 << "  is invalid, the day needs to be between 1 and 28." << endl;
error = true;
}
    
CPUMotherboardGraphicsRAM
2600K Sandy Bridge 5+Ghz ASUS P8P67 DELUXE x2 EVGA GTX 580 Water Cooled in SLI G.SKILL Ripjaws X Series 16GB 1600 
OSMonitorPowerCase
Duel Boot, Win 7 & Unbuntu 2 x 24in monitors Plus a 1080p projector CORSAIR Professional Series Corsair 600T 
  hide details  
Reply
    
CPUMotherboardGraphicsRAM
2600K Sandy Bridge 5+Ghz ASUS P8P67 DELUXE x2 EVGA GTX 580 Water Cooled in SLI G.SKILL Ripjaws X Series 16GB 1600 
OSMonitorPowerCase
Duel Boot, Win 7 & Unbuntu 2 x 24in monitors Plus a 1080p projector CORSAIR Professional Series Corsair 600T 
  hide details  
Reply
post #2 of 3
I would make functions that take month number and return the number of days in that month, then your error check just checks the day against that function rather than so many things.

Sorry for the short reply, I'm typing this from my phone.
Foldatron
(17 items)
 
Mat
(10 items)
 
Work iMac
(9 items)
 
CPUMotherboardGraphicsGraphics
i7 950 EVGA x58 3-way SLI EVGA GTX 660ti GTX 275 
RAMHard DriveHard DriveHard Drive
3x2GB Corsair Dominator DDR3-1600 80GB Intel X25-M SSD 2TB WD Black 150GB WD Raptor 
Hard DriveOSMonitorKeyboard
2x 150GB WD V-raptor in RAID0 Win7 Home 64-bit OEM 55" LED 120hz 1080p Vizio MS Natural Ergonomic Keyboard 4000 
PowerCase
750W PC P&C Silencer CoolerMaster 690 
CPUGraphicsRAMHard Drive
Intel Core i5 2500S AMD 6770M 8GB (2x4GB) at 1333Mhz 1TB, 7200 rpm 
Optical DriveOSMonitorKeyboard
LG 8X Dual-Layer "SuperDrive" OS X Lion 27" iMac screen Mac wireless keyboard 
Mouse
Mac wireless mouse 
CPUGraphicsRAMHard Drive
i7-2600K AMD 6970M 1GB 16GB PC3-10600 DDR3 1TB 7200rpm 
Hard DriveOptical DriveOSMonitor
256GB SSD 8x DL "SuperDrive" OS X 10.7 Lion 27" 2560x1440 iMac display 
Monitor
27" Apple thunderbolt display 
  hide details  
Reply
Foldatron
(17 items)
 
Mat
(10 items)
 
Work iMac
(9 items)
 
CPUMotherboardGraphicsGraphics
i7 950 EVGA x58 3-way SLI EVGA GTX 660ti GTX 275 
RAMHard DriveHard DriveHard Drive
3x2GB Corsair Dominator DDR3-1600 80GB Intel X25-M SSD 2TB WD Black 150GB WD Raptor 
Hard DriveOSMonitorKeyboard
2x 150GB WD V-raptor in RAID0 Win7 Home 64-bit OEM 55" LED 120hz 1080p Vizio MS Natural Ergonomic Keyboard 4000 
PowerCase
750W PC P&C Silencer CoolerMaster 690 
CPUGraphicsRAMHard Drive
Intel Core i5 2500S AMD 6770M 8GB (2x4GB) at 1333Mhz 1TB, 7200 rpm 
Optical DriveOSMonitorKeyboard
LG 8X Dual-Layer "SuperDrive" OS X Lion 27" iMac screen Mac wireless keyboard 
Mouse
Mac wireless mouse 
CPUGraphicsRAMHard Drive
i7-2600K AMD 6970M 1GB 16GB PC3-10600 DDR3 1TB 7200rpm 
Hard DriveOptical DriveOSMonitor
256GB SSD 8x DL "SuperDrive" OS X 10.7 Lion 27" 2560x1440 iMac display 
Monitor
27" Apple thunderbolt display 
  hide details  
Reply
post #3 of 3
Readability Readability Readability

Can't even understand what the first code segment you posted even checks for in it's if statement. The number of couts you have is irrelevent - go crazy with them as long as I can read your code in the end.
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Coding and Programming
Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Differeneces between 2 codes help