Phel's Poopheaded Proggies

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

Post Reply
Phelios
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 3
Joined: Sat Jul 24, 2004 2:46 am
Contact:

Phel's Poopheaded Proggies

Post by Phelios »

I finally began taking a c++ class (for those who remember me from a long time ago...back when I knew nothing)...well now I know very little extra (yay!).

Anyway, I am attempting to solve the following problem for the Programming Team at this college and, with my horrible lack of programming knowledge, it is rather difficult (I'm sure many of you will laugh at simple the solution probably is, but heck...read the title of the post).

"When I can't sleep, I count sheep. During the first hour, I count 900 sheep (I start at 1 and count up). During the next hour, I count them down (so as to avoid having my numbers of sheep grow too large). As I count down, I count slower than up, therefore only counting 600 sheep down during the second hour. During the third hour, I once again begin counting up. Therefore, it alternates up 900, down 600, etc. ... Write a program that takes the number of minutes I try and get to sleep from a file and return the sheep number that I land on."

-- Assume that every hour contains exactly sixty minutes.
-- Assume that each hour I count evenly: 15 sheep a minute up, 10 sheep a minute down.
-- Inputs are valid if they are minutes between 0 and 720 inclusive.

I will begin by restating the fact that my code will be messy due to lack of knowledge in this field (don't worry, I'll improve!). If you have time and don't mind reading through it, I could use some feedback (I believe the error involves the true/false not switching. I experimented with a few different approaches and none of them work. The program compiles and runs, it just returns the wrong number (it always counts up 900 and never down).

Code: Select all

/* Problems: I think that the true/false test is still not working... */

#include <iostream>
#include <conio.h>
#include <fstream>
using namespace std;

int countSheep(int, int, int);
int intervalTest(bool);

int main()
{
 int sheepPerMinute = 15;
 int sheepTotal = 0;
 int tempSheep = 0;
	
 ifstream File;
 int minutes;

 File.open("minutes.txt");
 File >> minutes;

 while (minutes > 0)
 {
	sheepTotal = countSheep(sheepPerMinute, minutes, sheepTotal);
	minutes = minutes - 60;
 }
 cout << "The final number of sheep equals: " << sheepTotal;

 getch();

 return 0;
}

int intervalTest(bool interval)
{
	int sheepPerMinute;

	if(interval = true)
	{
		sheepPerMinute = 15;
		
	}

	else
	{
		sheepPerMinute = -10;
		
	}
	return sheepPerMinute;
}

int countSheep(int sheepPerMinute, int minutes, int sheepTotal)
{ 
	int tempSheep = 0;
	int newSheep;
	int determine;
	bool interval = true;

	determine = sheepPerMinute;
	
	if (determine == 15)
	{
		interval = false;
	}

	else
	{
		interval = true;
	}
	
	newSheep = intervalTest(interval);

	if(minutes <= 60)
	{
	 tempSheep = (minutes*sheepPerMinute);
	 cout << tempSheep << endl;
	 sheepTotal = sheepTotal + tempSheep;
	}

	else
	{
	 tempSheep = 60*sheepPerMinute;
	 cout << tempSheep << endl;
	 sheepTotal = sheepTotal + tempSheep;
	}

	
	return sheepTotal;
 }
You know, I tried to sign my name here and now I have permanent marker on my computer screen!
User avatar
MarauderIIC
Respected Programmer
Respected Programmer
Posts: 3406
Joined: Sat Jul 10, 2004 3:05 pm
Location: Maryland, USA

Post by MarauderIIC »

interval = true should be interval == true, or just (interval). You used assignment (=) instead of equality (==).

You're testing to see if the assignment is true. Which they always are.

As a side note, you can test return values while executing an assignment or function like so:

Code: Select all

bool tehFunc(bool retVal)
{
    return retVal;
}

class AClass {
};

int main()
{
    bool userInput = /* assign */;
    if (!tehFunc(userInput))
    {
        //tehFunc returned false.
    }
    else
    {
        //tehFunc returned true.
    }
    AClass* aClassPtr;
    if (!(aClassPtr = new AClass)) {
        //assignment failed
    }
}
Also, you have a few redundancies, copying a variable to another when it's unnecessary and unnecessarily initializing variables, since the first use for them is an assignment and not a test.

I'd clean it for you a bit, if you like, but I figure I'd let you do it on your own if you'd rather first.

Edit: A suggestion: Do even/odd testing by using the modulus operator... trying to let you see where I'm going with this w/o telling you outright =)

Edit2 - Added assignment check example.
I realized the moment I fell into the fissure that the book would not be destroyed as I had planned.
User avatar
Marx Chaotix
Chaos Rift Devotee
Chaos Rift Devotee
Posts: 873
Joined: Sat May 22, 2004 11:30 am
Location: Alabama
Contact:

Post by Marx Chaotix »

Basically, after your whole intervalTest function, the sheep per minute never changes. Even if you fixed the '=' to '==' thing, the function still wouldn't fix the problem. If you'll look, your countSheep function takes the sheep per minute as an argument, and you ALWAYS pass 15. That's teh problem.
User avatar
Falco Girgis
Elysian Shadows Team
Elysian Shadows Team
Posts: 10294
Joined: Thu May 20, 2004 2:04 pm
Current Project: Elysian Shadows
Favorite Gaming Platforms: Dreamcast, SNES, NES
Programming Language of Choice: C/++
Location: Studio Vorbis, AL
Contact:

Post by Falco Girgis »

:shock:



:shock:



:shock:


*dies*
User avatar
MarauderIIC
Respected Programmer
Respected Programmer
Posts: 3406
Joined: Sat Jul 10, 2004 3:05 pm
Location: Maryland, USA

Post by MarauderIIC »

I mentioned that to him over aim... arce...
I realized the moment I fell into the fissure that the book would not be destroyed as I had planned.
User avatar
spideyspiderman2000
Chaos Rift Demigod
Chaos Rift Demigod
Posts: 1177
Joined: Fri Sep 17, 2004 9:28 pm
Location: Stupid Kansas
Contact:

Post by spideyspiderman2000 »

Thank God. I almost had a heart attack.
South Park wrote:Mr Garrison: Okay class. Can anybody tell me what "Sexual Harrassment" is?

Cartman: Isn't that when you're having intercourse with a special lady friend, and some other guy comes up behind you and tickles your balls?
I HATE Jack Thompson!
Post Reply