Hello Guest, please login or register.
Did you miss your activation email?
Login with username, password and session length.

Pages: [1]   Go Down

Author Topic: [Request / Listing] Could you tell me if I made a mistake here? (C++)  (Read 2806 times)

0 Members and 1 Guest are viewing this topic.
[Request / Listing] Could you tell me if I made ...
« on: May 10, 2006, 06:01:20 pm »
  • You don't know me.
  • *
  • Reputation: +0/-0
  • Offline Offline
  • Gender: Male
  • Posts: 234
Code: [Select]
// Gold Fish Feeding Simulator
// Another Ranom Product from Vortex Inc.
// Wait, hold on, I could turn this into a game...

#include <iostream>  // What we have to do to be able to show anything.

using std::cout;  // This is how we show things.
using std::cin;  // This is how we get things to influence our show things.

#include <iomanip>  // No idea, going to have to read my C++ book again. And maybe finish it. >.>

using std::setw;

#include <cstdlib>  // I THINK this is for something random.

#include <ctime>  // I know this is the time function that we use for true randomness.

int main() // The main program, this is what determines what we see.
{  // I got this far and then I realized game potential. >.>
   
   enum Status { CONTINUE, LOST, SSSS }; // Enumeration for the game status.
   
   int aFeed; // This is the amount you feed the gold fish on your turn.
   int aTake; // This is the amount the gold fish can take total, it's seperate because I don't want it to
              // randomize each turn.
   int rTake; // What I'll use to determine how much it can take. It will get a random amount, and that aTake will
              // Recieve it's value.
   int hTake; // This is how much the goldfish has already taken. When this is equal to or above aTake, the goldfish
              // will... err... explode. >.>
   int cTake; // How much the player enters in the while loop, this is added to hTake.
   int aNeed; // You still have to feed the gold fish enough. This is the non-random part.
   int rNeed; // This is the random variable from which aNeed gets it's value.
   
   Status game;
   
   srand( time( 0 ) ); // I'm using this to make the amount random.
   
   rNeed = ( 20 + rand() % 25 ); // Find out how much the gold fish needs.
   aNeed = rNeed; // Assign the value to aNeed.
   
   rTake = ( 24 + rand() % 26 ); // Find out when the gold fish will explode.
   aTake = rTake; // Assign the value to aTake.
   
   cout << "Gold Fish Feeding Game!\n"; // The title.
   cout << "Feed the gold fish enough food to survive, but \n"; // Instructions.
   cout << "Don't overfeed it or it will explode!\n"; // Some more instructions.
   cout << "When it asks you, enter how much food you feed the goldfish.\n"; // Instructions.
   cout << "Enter 7777 to not feed the goldfish any more.\n"; // How to break out of feeding.
   cout << "\n"; // To put some space in between the game and the instructions.
   
   cout << "How much will you feed the fish?\n"; // First prompt.
   cin >> hTake; // Give hTake the value of the amount.
   
   game = CONTINUE;  // Do this so we don't skip the while loop.
   
   while ( game == CONTINUE ) {  // The while loop.
         cout << "How much more will you feed the fish?\n"; // The second occurence of the prompt.
         cin >> cTake; // The player enters the amount.
         if ( cTake == 7777 )// If the player wants to stop feeding the fish...
             game = SSSS;
         else
             hTake + cTake; // Add the amount of cTake to hTake.
         if ( hTake >= aTake )
            game = LOST;
         }
   if ( game == LOST )
      cout << "Oh no! You exploded the fish! Now \nLittle Joey will throw a tempertantrum.\n"; // Tell the player it exploded!
   else if ( game == SSSS ) { // I'm using an else if so that I can have an error message option.
        if ( hTake > aNeed ) // If you fed the fish enough...
           cout << "You fed the fish enough and it survived! Yaayyy!\n"; // Give a victory message.
        else // Otherwise...
            cout << "You didn't feed the fish enough and it died. \nShame on you!\n"; // The fish died! Punish the player!
            }
   else // If the game isn't experiencing either of these options, we have a problem.
        cout << "This game has had an unexpected error\n and needs to close.\n"; // Give the error message.
   
   system("PAUSE"); // Allow the player to read the message he got before closing.
   
   return 0; // Signal success.
   
} // End the program. That's it, folks!

Oklay, I think everything else is going fine, but when I put in an amount I know is enough for the fish, it still says I didn't feed it enough. I know I fed it enough because I temporarily made hte variable of how much it needs a certain amount (22). Any suggestions? Comments? Code that would optimize the game but is cryptic to me?
« Last Edit: February 24, 2012, 06:33:24 pm by Niek »
Logged


No one located in this area of the internet remembers me.
Re: Could you tell me if I made a mistake here? ...
« Reply #1 on: May 10, 2006, 06:06:46 pm »
  • *
  • Reputation: +3/-0
  • Offline Offline
  • Gender: Male
  • Posts: 6629
Code: [Select]
cin >> hTake; // Give hTake the value of the amount.
Shouldnt that be.

Code: [Select]
cin >> cTake; // Give hTake the value of the amount.


BTW why are you using such an obscure naming convention? (i mean why is Take prefixed with h(Handle) and c(Class) ?)

Why not just use hungarian notation?
Logged
Re: Could you tell me if I made a mistake here? ...
« Reply #2 on: May 10, 2006, 06:16:04 pm »
  • You don't know me.
  • *
  • Reputation: +0/-0
  • Offline Offline
  • Gender: Male
  • Posts: 234
I tried your suggestion and made the amount it needs 22. I entered 1 on the first time, and in the loop I entered 21. It gave the exploded message. >.>

And I named them like this:

a = amount

r = random

c = cin = input

h = how much


... Yeah.


Edit: I just realized I have an unused variable. >.>
Logged


No one located in this area of the internet remembers me.
Re: Could you tell me if I made a mistake here? ...
« Reply #3 on: May 10, 2006, 06:19:30 pm »
  • *
  • Reputation: +3/-0
  • Offline Offline
  • Gender: Male
  • Posts: 6629
Well that sort of makes the point of notation pointless. The idea of notation is so other programmers can understand your work, and what varaibles do what. My advice to you is either stop notating your varaibles or read up on hungarian notation :).
Logged
Re: Could you tell me if I made a mistake here? ...
« Reply #4 on: May 10, 2006, 06:25:04 pm »
  • You don't know me.
  • *
  • Reputation: +0/-0
  • Offline Offline
  • Gender: Male
  • Posts: 234
Wow, the wikipedia article for hungarian notation is kind of complicated. I think I'll stop notating. O_O
Logged


No one located in this area of the internet remembers me.
Re: Could you tell me if I made a mistake here? ...
« Reply #5 on: May 10, 2006, 09:09:24 pm »
  • The Broken King
  • *
  • Reputation: +0/-0
  • Offline Offline
  • Gender: Male
  • Posts: 1259
the variables are commented well enough that it's not a huge problem.

I tried using your code, but the headers were unliked by my compiler =D
« Last Edit: May 10, 2006, 09:14:11 pm by therabidwombat »
Logged
  • Broken Kings [Temp Site]
Re: Could you tell me if I made a mistake here? ...
« Reply #6 on: May 10, 2006, 09:59:02 pm »
  • *
  • Reputation: +3/-0
  • Offline Offline
  • Gender: Male
  • Posts: 6629
the variables are commented well enough that it's not a huge problem.

I tried using your code, but the headers were unliked by my compiler =D

Yeh i know, its really just me being pickey. Its a good idea to use correct notation anyway, just so you don't develop any coding habits.
Logged
Re: Could you tell me if I made a mistake here? ...
« Reply #7 on: May 12, 2006, 04:10:43 am »
  • *
  • Reputation: +1/-0
  • Offline Offline
  • Gender: Male
  • Posts: 4588
Cant be bothered reading the post... But why do you use prefixes for Classes? You HAVE no classes. Perhaps prefix with fish? (fishNeed; fishTake; etc.)
Logged
the a o d c
Re: Could you tell me if I made a mistake here? ...
« Reply #8 on: May 12, 2006, 06:37:53 am »
  • *
  • Reputation: +3/-0
  • Offline Offline
  • Gender: Male
  • Posts: 6629
Cant be bothered reading the post... But why do you use prefixes for Classes? You HAVE no classes. Perhaps prefix with fish? (fishNeed; fishTake; etc.)

Read the last few posts :)
Logged
Re: Could you tell me if I made a mistake here? ...
« Reply #9 on: May 12, 2006, 08:38:49 pm »
  • *
  • Reputation: +0/-0
  • Offline Offline
  • Gender: Male
  • Posts: 1141
I guess I know where's the error:

Code: [Select]
if ( hTake >= aTake )
      game = LOST;

it should be:

Code: [Select]
if ( hTake > aTake )
      game = LOST;
Logged
Pages: [1]   Go Up

 


Contact Us | Legal | Advertise Here
2013 © ZFGC, All Rights Reserved



Page created in 0.142 seconds with 54 queries.

anything