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;
}