Jump to content


Photo

Help w/ code


  • Please log in to reply
11 replies to this topic

#1 Florida_Funk

Florida_Funk

    Americas Wang

  • Dedicated Member
  • PipPipPipPipPipPip
  • 1,218 posts

Posted 09 October 2008 - 02:03 PM

me and halcomb are in the programming 1 class and we had to right a code for how many class days are left in the class. we keep getting an answer of 1, anyone know what we did wrong here's what we got so far. We did this in an hour cause class is tonight and we need this done.




( the if then statement is correct it just looks weird cause it didnt copy and paste right.)




#include <iostream>
using namespace std;

//Declarations
int AUG_21 = 1;
int AUG_27 = 2;
int SEP_04 = 3;
int SEP_11 = 4;
int SEP_18 = 5;
int SEP_25 = 6;
int OCT_02 = 7;
int OCT_09 = 8;
int OCT_16 = 9;
int OCT_23 = 10;
int OCT_30 = 11;
int NOV_06 = 12;
int NOV_13 = 13;
int NOV_20 = 14;
int DEC_04 = 15;
int DEC_11 = 16;

int week = 16;



int main()
{
int week, date, num;

cout << "Enter current date as first 3 letters of month, underscore and numeric date." << endl;
cout << "Example SEP_13" << endl;
cin >> date; //collecting data

if (date = AUG_21)
{
num = week - date;
cout << "The number of Classes left is " << num << endl;
}
else
{
if (date = AUG_27)
{
num = week - date;
cout << "The number of Classes left is " << num << endl;
}
else
{
if (date = SEP_04)
{
num = week - date;
cout << "The number of Classes left is " << num << endl;
}
else
{
if (date = SEP_11)
{
num = week - date;
cout << "The number of Classes left is " << num << endl;
}
else
{
if (date = SEP_18)
{
num = week - date;
cout << "The number of Classes left is " << num << endl;
}
else
{
if (date = SEP_25)
{
num = week - date;
cout << "The number of Classes left is " << num << endl;
}
else
{
if (date = OCT_02)
{
num = week - date;
cout << "The number of Classes left is " << num << endl;
}
else
{
if (date = OCT_09)
{
num = week - date;
cout << "The number of Classes left is " << num << endl;
}
else
{
if (date = OCT_16)
{
num = week - date;
cout << "The number of Classes left is " << num << endl;
}
else
{
if (date = OCT_23)
{
num = week - date;
cout << "The number of Classes left is " << num << endl;
}
else
{
if (date = OCT_30)
{
num = week - date;
cout << "The number of Classes left is " << num << endl;
}
else
{
if (date = NOV_06)
{
num = week - date;
cout << "The number of Classes left is " << num << endl;
}
else
{
if (date = NOV_13)
{
num = week - date;
cout << "The number of Classes left is " << num << endl;
}
else
{
if (date = NOV_20)
{
num = week - date;
cout << "The number of Classes left is " << num << endl;
}
else
{
if (date = DEC_04)
{
num = week - date;
cout << "The number of Classes left is " << num << endl;
}
else
{
if (date = DEC_11)
{
num = week - date;
cout << "The number of Classes left is " << num << endl;
}



}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
system("PAUSE");
return 0;
}

Edited by Florida_Funk, 09 October 2008 - 02:05 PM.

  • 0

#2 Novahawk

Novahawk

    Beast

  • Dedicated Member
  • PipPipPipPipPipPipPipPip
  • 2,896 posts

Posted 09 October 2008 - 02:25 PM

The problem is that you defined week first, and then initialized it again.

Change this:

int week, date, num;

to:

int date, num;

or

int week = 16, date, num;

(and remove the first "int week = 16;")
  • 0

#3 Florida_Funk

Florida_Funk

    Americas Wang

  • Dedicated Member
  • PipPipPipPipPipPip
  • 1,218 posts

Posted 09 October 2008 - 03:07 PM

thanks, fixed that. now instead of getting 1 it gets 15 for everything. I guess it's using the week=16 but only subtracting 1 for date.

by saying: if (date = AUG_27)
{
num = week - date;
cout << "blah" << num << endl;
}

Date = AUG_27 which was declared as 2, so shouldn't the expression go ? = 16 - 2 right?

for some reason I've f*cked it somewhere.

-halcomb
  • 0

#4 IWantWhiteCastle

IWantWhiteCastle

    I Know the Slyder Secret

  • Dedicated Member
  • PipPipPipPipPipPipPipPip
  • 2,363 posts

Posted 09 October 2008 - 07:53 PM

shouldn't you use else if's instead of what you have? I'm a bit rusty on my c++ and programming in general but idk, maybe you can't even do that in c++ lol

ex.

if (date = AUG_21)
{
num = week - date;
cout << "The number of Classes left is " << num << endl;
}

else if (date = AUG_27)
{
...
}
.
.
.
  • 0

#5 Halcomb

Halcomb

  • Dedicated Member
  • PipPipPipPipPipPipPip
  • 1,633 posts

Posted 09 October 2008 - 09:53 PM

sh*t, that very well could be it man. I looked a few things up and yeah, if it's not actually going through the if then statement that could solve it. It's either that or someone said that date might not be resetting to 0 and is simply carrying along the 1.
  • 0

#6 IWantWhiteCastle

IWantWhiteCastle

    I Know the Slyder Secret

  • Dedicated Member
  • PipPipPipPipPipPipPipPip
  • 2,363 posts

Posted 10 October 2008 - 03:55 PM

Does that actually work that way you had it? Because you have the date as an integer but what they enter is never an integer, its a char.
  • 0

#7 Halcomb

Halcomb

  • Dedicated Member
  • PipPipPipPipPipPipPip
  • 1,633 posts

Posted 10 October 2008 - 05:09 PM

son of a b*tch, you're right. I forgot it was alpha-numeric. I'll try that when I get back to funk's house. good call, I didn't even notice.
  • 0

#8 monster

monster

    You live on pen island

  • Dedicated Member
  • PipPipPipPipPipPipPip
  • 1,653 posts

Posted 10 October 2008 - 10:17 PM

Your IF statements are actually ASSIGNMENTS. Use == if you are comparing (IF), and use = if you are assigning.

Your if statements should look like this:

if (date == AUG_21)
{
..
}
Also throw those if-else statements in a switch so that you don't have spaghetti code.
  • 0

#9 Halcomb

Halcomb

  • Dedicated Member
  • PipPipPipPipPipPipPip
  • 1,633 posts

Posted 16 October 2008 - 02:45 PM

Alright, I ripped everything out and started new with a switch statement. Unfortunately it now tells me that my dates are not declared. (IE, the date October16 is undeclared.)

source:

#include <iostream>
using namespace std;

int main()
{

char month;
unsigned short int day;


cout << "Enter current or projected month." << endl;
cout << "Example September." << endl;
cin >> month; //collecting Month value
cout << "Enter current or projected day." << endl;
cout << "Example 01 or 10." << endl;
cin >> day; //collecting Day value
char date = month + day;

switch (date) //switch to evaluate date variable
{
case October16: cout << "8 Days left in class.\n"; //switch to output desired date response
break; //break to kill case and apply next instance
case October23: cout << "7 Days left in class.\n";
break;
case October30: cout << "6 Days left in class.\n";
break;
case November06: cout << "5 Days left in class.\n";
break;
case November13: cout << "4 Days left in class.\n";
break;
case November20: cout << "3 Days left in class.\n";
case November27: cout << "Happy Holidays! See you next week.\n"; //break left out intentionally
break;
case December04: cout << "2 Days left in class.\n";
break;
case December11: cout << "1 Day left in class.\n";
break;
case December18: cout << "Good luck on your final.\n";
break;
default: cout << "Incorrect Data Applied to Switch Function. User error Detected.\n"; //default case to show incorrect input.
}



system("PAUSE");
return 0;
}


Any ideas?
  • 0

#10 Halcomb

Halcomb

  • Dedicated Member
  • PipPipPipPipPipPipPip
  • 1,633 posts

Posted 16 October 2008 - 02:51 PM

Fixed my input to:

cout << "Enter current or projected month." << endl;
cout << "Example September." << endl;
cout << "Then Enter a day." << endl;
cout << "Example 01 or 10." << endl;
cin >> month >> day; //collecting Month and Day value.
  • 0

#11 Novahawk

Novahawk

    Beast

  • Dedicated Member
  • PipPipPipPipPipPipPipPip
  • 2,896 posts

Posted 16 October 2008 - 04:12 PM

You can't have a char in a switch. You also need to have whatever the case is in ' ' (so like 'October16').

You also can't add a string and an integer together.

I doubt you covered string comparisions (we haven't gotten anywhere close), so you probably don't want to have any strings in your program. Your best bet would be to have the month input as a number (like 11 for November) and also a number for the day. Then you can make the switch work. You can have your date still be month + day, but in the switch instead of like October16 have 26 as the case. Just make sure the numbers don't repeat (like november 15 would also be 26) . If they do then just do something different.

Edited by Novahawk, 16 October 2008 - 04:40 PM.

  • 0

#12 Florida_Funk

Florida_Funk

    Americas Wang

  • Dedicated Member
  • PipPipPipPipPipPip
  • 1,218 posts

Posted 16 October 2008 - 04:59 PM

many thanks from some drunk bastards
  • 0


0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users