Thread: battleship program

  1. #1
    Registered User
    Join Date
    May 2018
    Posts
    6

    battleship program

    I need some help I am currently trying to code a program that reads data ( degrees, and velocity) from a file. the degrees has to be converted to radians first then everything needs to be converted to distance. constants known are the location of each ship and when the file compiles anything hit within 1meter of the known location is a hit. I also have max hits for each ship but thats for later. There is alot to do with this program but I am literally drained so I have decided to ask for help. My main problem right now is data isnt being read from file which I am assuming is because of the placement in the file explorer. I can upload what the whole program is asking for if it makes it easier. this is my code so far. most stuff is commented out until I can figure out the file issue.


    Code:
        // Libraries:
        #include<iostream>
        #include<iostream>
        #include<iomanip>
        #include<fstream>
        #include<string>
        #include<cmath>
    
    
        using namespace std;
    
    
    
    
           // Constants: 
           float const RAD_DEGREES = 180.0f;
           float const FEET_PER_SECOND = 32.2f;
           float const PI = 3.14159265f;
           int SUB = 1000;
           int DESTROYER = 1900;
           int BATTLESHIP = 2000;
           int AIRCRAFT = 3030;
    
    
    
    
           float GetDistance(float velocity, float ang);
           float CalcRadians(int &angle, float PI);
           bool I........();
           int main()
           {
    
    
               // Delcare all variables and state what they contain here:
               ifstream inFile;     //input file variable
               string inFileName;  //name of the input file
               ofstream outFile;    //otput file variable
               string outFileName;    //name of output file
               int count = 0;    //the number of values in the input file (unknown)
               int angle;    //angle input from file
               float velocity;   //velocity of projectile from file
               float distance;    //calculated distance
               float radians;     //calculated radians
               bool hit;           //returns true if hit and false if not
               int missCt = 0;        //counts misses
               int hitCt = 0;    // count hits
    
    
               cout << fixed << setprecision(2);
    
    
               inFile.open(inFileName);
               outFile.open("battle.log");
               do
               {
                   cout << "Please enter the name of the input file" << endl;
                   cin >> inFileName;
                   if (!inFile)
                   {
                       cout << "File entered " << inFileName << " is invalid, please try again " << endl;
                   }
               } while (!inFile);
               
              
            
    
    
                
                    inFile >> angle;
                    inFile >> velocity;
    
    
                
    
    
               radians = CalcRadians(angle, PI);
    
    
    
    
    
    
              
               //GetDistance;
               cout << "The radians is " << radians << endl;
               //cout << "The distance is " << GetDistance << endl;
    
    
               system("pause");
               return 0;
    
    
           } // end main
    
    
    
    
             /*float GetDistance(float velocity, float radians)
             { float distance;
             float sinTotal = (2 * radians);
             distance = ((velocity*velocity) * sin(sinTotal)) / 32.2;
             return distance;
             }*/
    
    
           float CalcRadians(int& angle, float PI)
           {
               float radians;
               radians = ((float)angle * PI) / RAD_DEGREES;
               return radians;
           }
         /*  bool I........(float distance)
           {
               bool hit = false;
               if (distance >= 3029 && distance <= 3031)
                   AIRCRAFT = true;
               else if (distance >= 1999 && distance <= 2001)
                   BATTLESHIP = true;
               else if (distance >= 1899 && distance <= = 1901)
                   DESTROYER = true;
               else if (distance >= 999 && distance <= 1001)
                   SUB = true;
               else
    
    
                   return hit;
    
    
           }
           */
    If this isnt the right way to display the code I'm sorry, I am new here.

  2. #2
    Registered User
    Join Date
    Dec 2017
    Posts
    1,633
    You're "opening" the file before you've got the filename from the user. Try this:
    Code:
        do {
            cout << "Please enter the name of the input file\n";
            cin >> inFileName;
            inFile.open(inFileName);       // try opening the file here
            if (!inFile)
                cout << "File entered " << inFileName
                     << " is invalid, please try again\n";
        } while (!inFile);
    A little inaccuracy saves tons of explanation. - H.H. Munro

  3. #3
    Registered User
    Join Date
    May 2018
    Posts
    6
    I really feel like an idiot I knew it something simple i kept moving the location of the actual files instead of moving the declaration to open it. Thanks so much!

  4. #4
    Registered User
    Join Date
    May 2018
    Posts
    6
    so I am thinking the bool isnt the way to go for determining if the distance was a hit or not. Can someone let me know if I was on the right track or if i am way off. i am also trying to figure out the best way to determine if the distance is within 1m +- of the ships location for it to be a hit then i have to count the hits and count the misses along with counting how many rounds of ammo were expelled and the percentage of hits to rounds expelled.

  5. #5
    Registered User
    Join Date
    Dec 2017
    Posts
    1,633
    I don't understand your task. You call it battleship, but it doesn't seem to be much like the game that I know by that name.

    What exactly is your program supposed to do?
    What does the input file look like?
    What are the numbers SUB, DESTROYER, BATTLESHIP, and AIRCRAFT?
    A little inaccuracy saves tons of explanation. - H.H. Munro

  6. #6
    Registered User
    Join Date
    May 2018
    Posts
    6
    thats the thing its called battleship but its not an actual game the main part of the code it to determine whether or not projectiles launched at a given velocity and angle of inclination will hit the known targets. the input files are a list of about 7 sets of numbers left column is angle and right is velocity. i have to compute the distance each given set of values travels ( which I have done) compare the distance with a list of known target distances which are my constants. if it lands within 1m+- stated meters for each ship its a direct hit otherwise its a miss. And then compare number of hits per vessel with the max number of hits given and determine whether or not the vessel has been sunk, and add a trailing message to file stating whether or not a vessel has been sunk and include number of hits on all vessels still afloat. along with the other things that have to be sent to a report which i said above.
    aircraft carrier 3030m max hit 6
    battleship 2000m max hit 5
    destroyer 1900m max3
    sub 1000m max2
    i know everything that has to be done im just stuck as to what to do next and i think i need to determine the distance of projectile vs the known target a different way.

  7. #7
    Registered User
    Join Date
    Dec 2017
    Posts
    1,633
    I see. Maybe something like this:
    Code:
    const int   SubDist         = 1000,
                DestroyerDist   = 1900,
                BattleshipDist  = 2000,
                AircraftDist    = 3030;
    
    enum ShipType {NONE, AIRCRAFT, BATTLESHIP, DESTROYER, SUB};
    
    ShipType is_hit(float distance) {
        if (distance >= AircraftDist-1 && distance <= AircraftDist+1)
            return AIRCRAFT;
        else if (distance >= BattleshipDist-1 && distance <= BattleshipDist+1)
            return BATTLESHIP;
        else if (distance >= DestroyerDist-1 && distance <= DestroyerDist+1)
            return DESTROYER;
        else if (distance >= SubDist-1 && distance <= SubDist+1)
            return SUB;
        return NONE;
    }
    
    // in main
        switch (is_hit(distance)) {
        case AIRCRAFT:
            break;
        case BATTLESHIP:
            break;
        case DESTROYER:
            break;
        case SUB:
            break;
        case NONE:
            break;
        }
    A little inaccuracy saves tons of explanation. - H.H. Munro

  8. #8
    Registered User
    Join Date
    May 2018
    Posts
    6
    this is what mine looks like so far but my while (inFile) loop is repeating constantly.
    Code:
    // Libraries:
    	#include<iostream>
    	#include<iostream>
    	#include<iomanip>
    	#include<fstream>
    	#include<string>
    	#include<cmath>
    
    
    	using namespace std;
    
    
    
    
    	   // Constants: 
    	   float const RAD_DEGREES = 180.0f;
    	   float const FEET_PER_SECOND = 32.2f;
    	   float const PI = 3.14159265f;
    	   const int   SubDist = 1000,
    		const int DestroyerDist = 1900,
    		const int BattleshipDist = 2000,
    		  const int AircraftDist = 3030;
    
    
    
    
    	   
    	   float CalcRadians(int &angle, float PI);
    	   float GetDistance(float velocity, float ang);
    	   
    	   int main()
    	   {
    
    
    		   // Delcare all variables and state what they contain here:
    		   ifstream inFile;     //input file variable
    		   string inFileName;  //name of the input file
    		   ofstream outFile;    //otput file variable
    		   string outFileName;    //name of output file
    		   int count = 0;    //the number of values in the input file (unknown)
    		   int angle;    //angle input from file
    		   float velocity;   //velocity of projectile from file
    		   float distance;    //calculated distance
    		   float radians;     //calculated radians
    		   //bool directHit;           //returns true if hit and false if not
    		   int missCt = 0;        //counts misses
    		   int hitCt = 0;    // count hits
    
    
    		   cout << fixed << setprecision(2);
    
    
    
    
    		   outFile.open("battle.log");
    		   do {
    			   cout << "Please enter the name of the input file\n";
    			   cin >> inFileName;
    			   inFile.open(inFileName);
    			   if (!inFile)
    				   cout << "File entered " << inFileName
    				   << " is invalid, please try again\n";
    		   } while (!inFile);
    
    
    		   inFile >> angle;
    		   inFile >> velocity;
    		   if (angle < 0 || velocity < 0)
    			   inFile.ignore(20, '\n');
    		   
    		   radians = CalcRadians(angle, PI);
    
    
    		   distance = GetDistance(velocity, radians);
    		   
    		   while (inFile)
    		   {
    			   cout << "The distance is " << distance << endl;
    		   }
    
    
    
    
    		   if (distance >= 999 && distance <= 1001)
    		   {
    			   cout << "you hit the submarine" << endl;
    			  
    		   }
    		   else if (distance >= 1899 && distance <= 1901)
    		   {
    			   cout << "you hit the Destroyer" << endl;
    			
    		   }
    		   else if (distance >= 1999 && distance <= 2001)
    		   {
    			   cout << "you hit the Battleship" << endl;
    			   
    		   }
    		   else if (distance >= 3029 && distance <= 3031)
    		   {
    			   cout << "you hit the Aircraft Carrier" << endl;
    			 
    		   }
    		   else
    			   cout << "You missed" << endl;
    		   cout << missCt++ << endl;
    	   
    			   cout << "The radians is " << radians << endl;
    
    
    			   cout << "The distance is " << distance << endl;
    		   
    
    
    		   system("pause");
    		   return 0;
    
    
    	   } // end main
    
    
    
    
    		
    
    
    	   float CalcRadians(int& angle, float PI)
    	   {
    		   float radians;
    		   radians = ((float)angle * PI) / RAD_DEGREES;
    		   return radians;
    	   }
    
    
    	   float GetDistance(float velocity, float radians)
    	   {
    		   float distance;
    		   float sinTotal = (2 * radians);
    		   distance = ((velocity*velocity) * sin(sinTotal)) / 32.2;
    		   return distance;
    	   }

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > but my while (inFile) loop is repeating constantly.
    Perhaps you should do something inside the loop to actually modify the file state in some way, like for example reading some values.
    Code:
               while (inFile)
               {
                   cout << "The distance is " << distance << endl;
               }
    This loops forever, because you don't do anything with inFile.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  10. #10
    Registered User
    Join Date
    May 2018
    Posts
    6
    I fixed the continuous loop but it is only calculating the first set of numbers and outputting the same number for each set

  11. #11
    Registered User
    Join Date
    Dec 2017
    Posts
    1,633
    You need to post the code for someone to help. Otherwise we can only guess what might be wrong.
    A little inaccuracy saves tons of explanation. - H.H. Munro

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Battleship program, two questions?
    By Sam Maiden in forum C Programming
    Replies: 1
    Last Post: 11-25-2014, 06:51 PM
  2. help with battleship program
    By howardqd in forum C++ Programming
    Replies: 12
    Last Post: 12-15-2010, 08:40 PM
  3. question on battleship program.
    By newbc in forum C Programming
    Replies: 0
    Last Post: 12-07-2010, 08:32 AM
  4. Help with a Battleship Program
    By HBlakeH in forum C Programming
    Replies: 1
    Last Post: 12-05-2010, 11:13 PM
  5. Battleship program
    By swgh in forum C++ Programming
    Replies: 4
    Last Post: 04-07-2008, 09:02 AM

Tags for this Thread