Thread: Problem what I want to do.

  1. #1
    Registered User
    Join Date
    Dec 2005
    Posts
    155

    Problem what I want to do.

    Hi, I am working on a fire gun program but I cant seem to get it right. I want it to check if there is efn. ammo for the gun you pick. Will I got this ok, but I cant get this void right for some reason.
    Code:
    #include "tt.h"
    using namespace std;
    void gunfire(void);
    int main()
    {
        int gunammopistal;
        int gunammoshootgun;
        string whatgun;
        string gunammouseornot;
    gunammopistal=15;
    gunammoshootgun=5;
    bool pQuit = false;
     while( false == pQuit )
     {
    cout<<"Press -F- if you want to fire the gun. Press -R- to reload.
    Also if you want a differnt gun press 1 for pistal and 2 for shoot gun."<<endl;
    cout<<"You have right now "<<whatgun<<endl;
    cin >> whatgun;
    cin >> gunammouseornot;
      if(whatgun=="1"){
         gunfire();}
      else if(whatgun=="2"){
         gunfire();}
     }
    }
    void gunfire()
    {
      if(gunammouseornot=="f"){
         if(gunammopistal==0||gunammoshootgun==0){
            cout<<"Click..."<<endl;}
         else if(gunammopistal==15||gunammoshootgun==5){
            cout<<"Its full."<<endl;}
         else if(gunammopistal>0||gunammoshootgun>0){
              if(whatgun=="1"){
               gunammopistal=gunammopistal-1;
               cout<<"BANG!"<<endl;
               cout<<"You got "<<gunammopistal<<endl;}
              else if(whatgun=="2"){
               gunammoshootgun=gunammoshootgun-1;
               cout<<"BANG!"<<endl;
               cout<<"You got "<<gunammoshootgun<<endl;}}}
      else if(gunammouseornot=="r"){
              if(whatgun=="1"){
               gunammopistal=gunammopistal+15;
               cout<<"Reloading."<<endl;
               cout<<"You got "<<gunammopistal<<endl;}
              else if(whatgun=="2"){
               gunammoshootgun=gunammoshootgun+5;
               cout<<"Reloading."<<endl;
               Sleep(1000);
               cout<<"."<<endl;
               Sleep(1000);
               cout<<"."<<endl;
               cout<<"You got "<<gunammoshootgun<<endl;}
      }
    }
    It gives me errors on that my int and strings were never declared.

  2. #2
    Registered User OnionKnight's Avatar
    Join Date
    Jan 2005
    Posts
    555
    The variables that you declared inside the main function are not visible to your other function so you will either have to send the address of those variables to your function or declare them as global so that everyone can see them.

  3. #3
    Registered User
    Join Date
    Dec 2005
    Posts
    155
    Quote Originally Posted by OnionKnight
    The variables that you declared inside the main function are not visible to your other function so you will either have to send the address of those variables to your function or declare them as global so that everyone can see them.
    Ok I thought I needed to make them global. How would you make them global tho? I have never done that yet with c++.

  4. #4
    Registered User OnionKnight's Avatar
    Join Date
    Jan 2005
    Posts
    555
    Simply put them outside the function, such as
    Code:
    #include <header.h>
    
    int globalvar;
    
    int main ()
    {
    	int localvar;
    
    	return 0;
    }

  5. #5
    Registered User
    Join Date
    Dec 2005
    Posts
    155
    Ok Thanks. In my old lang. I had to tell the program that the int or what ever was a global.

  6. #6
    Supermassive black hole cboard_member's Avatar
    Join Date
    Jul 2005
    Posts
    1,709
    Quote Originally Posted by adr
    Ok Thanks. In my old lang. I had to tell the program that the int or what ever was a global.
    What language was that - just out of interest.
    Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.

    - Mike McShaffry

  7. #7
    Registered User
    Join Date
    Dec 2005
    Posts
    155
    It was call GML-game maker lang. Its where I started on this stuff. I got another q/n if you all dont minde me asking? In my code I want it to reload the gun, but it can add more then what I want to add. So I want to make it stop reloading ones its at the maxs holding. How would I do that? Also I want to do this so when lets say the ammo was at a limit then it would add as meny and - as meny as it needs and stop if ammo would to = 0. Kinda like this: ammo in gun=a and ammo have on side=b"so" a-b=<15 then stop reloading at cap.

  8. #8
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    Ok I thought I needed to make them global.
    You thought wrong. Don't ever make a variable global. Send them to the function instead.

  9. #9
    Registered User
    Join Date
    Nov 2005
    Posts
    14
    I would suggest you look through the C++ tutorials on the site, or better yet, get a proper book on it (I know everyone has their favourite, but I _love_ O'reilly's 'Practical C++ Programming'). You'll need to set up a while() loop, with a currentBullets variable being < max bullets until it stops, ie. while(currentBullets <= maxBullets).

  10. #10
    Registered User
    Join Date
    Dec 2005
    Posts
    155
    Quote Originally Posted by 7stud
    You thought wrong. Don't ever make a variable global. Send them to the function instead.
    Why is it bad to use it? I didnt finde anything wrong with it yet.

  11. #11
    Registered User
    Join Date
    Feb 2006
    Posts
    8
    Its bad to use it because other functions can corrupt the data that the variables hold. If the variables are held inside the function then they are only opperated on by that function

  12. #12
    60% Braindead
    Join Date
    Dec 2005
    Posts
    379
    So then you just need to be careful with whos doing what when. I definantly wouldent reccomend a multithreaded program using a global.

    Theirs always an internal global (Keeps track of data like a global, but only the function can use it)

    Code:
    int main() {
     static int Pi=3.14;
    
     Pi++; //Same as: Pi=Pi+1, Or Pi+=1, but this is the best way to increment by 1.
    }
    Every time that you call main, Pi would increase by one, because statics only initialize once.

    Corrupt is a little strong, other functions can change the data, but I wouldent call that corrupting it. -Or is their something I'm missing about globals?
    Code:
    Error W8057 C:\\Life.cpp: Invalid number of arguments in function run(Brain *)

  13. #13
    Registered User
    Join Date
    Dec 2005
    Posts
    155
    Well then for what I am using it for its ok. Its just a test of how it works. Not going to to put it into nothing els just yet.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help understanding a problem
    By dnguyen1022 in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2009, 04:21 PM
  2. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  3. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  4. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  5. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM