Jump to content


Please Help


  • Please log in to reply
13 replies to this topic

#1 _*D34DM347_*

_*D34DM347_*
  • Guests

Posted 18 February 2005 - 11:29 AM

I am making a game where the character types which monster they attack and then a switch statement does the damage (which is the current time in seconds, 0-59). Unfortunately whenever i run it, the program stops after the user input and doesn't run the switch statement. Please help fix this!

#include <iostream.h>
#include <conio.h>
#include <time.h>

void main( )
{
struct tm *local;
time_t t;
t = time(NULL);
local = localtime(&t);

int hp1, hp2, hp3, hpmain, answer;
hp1 = 100;
hp2 = 100;
hp3 = 100;
hpmain = 250;

cout << "Do you attack skeleton 1, 2, or 3?" << endl;
cin >> answer;

switch (answer) {
case '1': cout << "You attack monster 1." << endl;
cout << local->tm_sec << " damage was dealt to monster 1." << endl;
hp1 = hp1 - local->tm_sec;
cout << hp1 << " hit points left." << endl;
if (local->tm_sec = 0);
cout << "You missed!" << endl;
break;
case '2': cout << "You attack monster 2." << endl;
cout << local->tm_sec << " damage was dealt to monster 2." << endl;
hp2 = hp2 - local->tm_sec;
cout << hp2 << " hit points left." << endl;
if (local->tm_sec = 0);
cout << "You missed!" << endl;
break;
case '3': cout << "You attack monster 3." << endl;
cout << local->tm_sec << " damage was dealt to monster 3." << endl;
hp3 = hp3 - local->tm_sec;
cout << hp3 << " hit points left." << endl;
if (local->tm_sec = 0);
cout << "You missed!" << endl;
break;
}


}

#2 xunhandmex

xunhandmex

    Young Padawan

  • Members
  • Pip
  • 44 posts

Posted 19 February 2005 - 11:35 PM

if it was any language OTHER then C++ i would be able to tell you :-\ ... though all i can say is maybe check your logic... sometimes all it is is that the logic might be a little off and you might be double adding/subtracting when you don't mean too thats what i run across 1/2 the time. ... i could try and help if you post saying what the Error states.

#3 _*D34DM347_*

_*D34DM347_*
  • Guests

Posted 21 February 2005 - 11:26 AM

There is no error, it just doesn't do anything.

#4 _*D34DM347_*

_*D34DM347_*
  • Guests

Posted 28 February 2005 - 11:33 AM

Well, it doesn't matter that no one helped me, because I fixed it myself.

#5 Jordan

Jordan

    Young Padawan

  • Members
  • Pip
  • 28 posts
  • Location:Kansas

Posted 28 February 2005 - 01:32 PM

Well, it doesn't matter that no one helped me, because I fixed it myself.

I took the time to edit the code you posted above and it compiles with some warnings but no errors and runs fine. You should really check into some of the warnings and try fixing them.

I would like to show you a few improvements I made to your code.

I noticed you had a variables hp1, hp2, hp3 all equal 100. You had it like this,

int hp1, hp2, hp3, hpmain, answer;
hp1 = 100;
hp2 = 100;
hp3 = 100;

If they are all going to be the same value just set them to the value, "100" when you defined them like so,

int hp1, hp2, hp3, hpmain, answer = 100;

I also saw in your switch statement that you used hp1, hp2, hp3. Why not just use one variable for this? hp1 for example. Unless you plan on putting this in a while loop you should just use one variable for the whole process.

#include <iostream.h>
#include <conio.h>
#include <time.h>

using namespace std; 

int main() { 
     
     struct tm *local; 
     time_t t; 
     t = time(NULL); 
     local = localtime(&t); 
     
     int hp1, hp2, hp3, hpmain, answer; 
     hp1, hp2, hp3 = 100; 
     hpmain = 250;
     
     
     cout << "Do you attack skeleton 1, 2, or 3?" << endl; 
     cin >> answer; 
     
     switch(answer) { 
                    case 1: 
                         cout << "You attack skeleton 1." << endl; 
                         cout << local->tm_sec << " Damage was dealt to monster 1." << endl; 
                         hp1 = hp1 - local->tm_sec; 
                         if(local->tm_sec=0) { cout << "You missed!" << endl; }
                         break; 
                         
                    case 2: 
                         cout << "You attack monster 2." << endl; 
                         hp2= hp2 - local->tm_sec; 
                         cout << hp2 << " hit points left." << endl;
                         if(local->tm_sec = 0) { cout << "You missed!" << endl; } 
                         break; 
                              
                    case 3:
                         cout << "You attack monster 3." << endl; 
                         hp3 = hp3 - local->tm_sec; 
                         cout << hp3 << " hit points left." << endl; 
                         if(local->tm_sec =0 ){ cout << "You missed!" << endl; }
                         break; 
                              
                    }
          
     }
I

#6 _*D34DM347_*

_*D34DM347_*
  • Guests

Posted 09 March 2005 - 11:34 AM

Thanks for your advice, but like I said, I already fixed it and that project is completed.

#7 .iCY7.

.iCY7.

    Simplicity With A Twist

  • Members
  • PipPipPipPip
  • 1,045 posts
  • Gender:Male
  • Location:England

Posted 10 March 2005 - 11:40 AM

I'm increasing my post count!

i think this is gonna end up in a ban <3

#8 _*D34DM347_*

_*D34DM347_*
  • Guests

Posted 10 March 2005 - 11:42 AM

I'm increasing my post count!

#9 _*D34DM347_*

_*D34DM347_*
  • Guests

Posted 10 March 2005 - 11:43 AM

I'm increasing my post count!

#10 _*D34DM347_*

_*D34DM347_*
  • Guests

Posted 10 March 2005 - 11:45 AM

I'm increasing my post count!

#11 Faken

Faken

    Pimpmaster G

  • Admin
  • 5,966 posts
  • Gender:Male
  • Location:Montreal, Canada

Posted 16 March 2005 - 10:59 PM

I'm increasing my post count!

i think this is gonna end up in a ban :)

It sure did!

Faken

#12 Indigo

Indigo

    Official Alien

  • Members
  • PipPipPip
  • 617 posts
  • Gender:Male
  • Location:Trondheim, Norway
  • Interests:Computing in general, especially design and programming of all kinds.

Posted 17 July 2005 - 07:30 AM

Well, it doesn't matter that no one helped me, because I fixed it myself.


Could you post your final code too? I'm curious how you did it..!

#13 Jaymz

Jaymz

    Retired P2L Staff

  • Members
  • PipPipPipPip
  • 4,104 posts

Posted 17 July 2005 - 10:23 AM

Well, it doesn't matter that no one helped me, because I fixed it myself.


Could you post your final code too? I'm curious how you did it..!

No, he can't, he's been banned :P

#14 Indigo

Indigo

    Official Alien

  • Members
  • PipPipPip
  • 617 posts
  • Gender:Male
  • Location:Trondheim, Norway
  • Interests:Computing in general, especially design and programming of all kinds.

Posted 17 July 2005 - 11:00 AM

I saw that... But can somebody please delete ALL the "I'm increasing my post count!" posts? Man, they're anoying!




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users