Page 1 of 1

Phel's Poopheaded Proggies

Posted: Tue Sep 13, 2005 9:59 pm
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;
 }

Posted: Tue Sep 13, 2005 10:05 pm
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.

Posted: Thu Sep 15, 2005 9:08 pm
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.

Posted: Thu Sep 15, 2005 9:26 pm
by Falco Girgis
:shock:



:shock:



:shock:


*dies*

Posted: Thu Sep 15, 2005 10:32 pm
by MarauderIIC
I mentioned that to him over aim... arce...

Posted: Fri Sep 16, 2005 7:08 am
by spideyspiderman2000
Thank God. I almost had a heart attack.