Extracting infomation from line

I need to identify what type of ship is the "row".
For example, I have this data in the file:
1
2
3
A1;A2;A3=Y
B1;C1;D1=Z
C5;C6=X

Assuming:
Y symbolize "Battlecruiser"
Z symbolize "Fishing-Boat"
X symbolize "Destroyer"

Before this, I've done an extraction of data by extracting the individual coordinates out like "A1","A2","A3" and store them in an array. And I compare the input of the user with the array and if the input and coordinate matches, it will show "Hit!". But now what I want to do is that, the system knows what the user has hit when he "fired" the shot e.g "A3".

Instead of "Hit!", I want to do something that identify it as a BattleCruiser. So it will cout "You've hit a Battlecruiser!" and subsequently for the other types of ship. How can I do this?

Thanks in advanced...
closed account (1yvXoG1T)
A simple set of if-elses would do the trick

1
2
3
4
5
6
7
8
9
// Assume shipType is the matched value you got from the user
cout << "You've hit a ";
if(shipType == "X") {
    cout << "Destroyer!";
} else if(shipType == "Y") {
    cout << "Battlecruiser!";
} else {
    cout << "Fishing-Boat!";
}
Hmm because the format is like this:

A1;A2;A3=Y

So when u extract the coordinate it'll store them like this in the array - "A1" , "A2" , "A3"
So i need think of a way to extract that "Y" out and do a if-else condition..
Currently that portion i'm doing is like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
// Extract the "Ship-type"
	ifstream infile;
	infile.open("battleship.txt");
		if (!infile)
	    	{
			cout << "Cannot open file" << endl;
			return 0;
	    	}
	    	else
	    	{
//extracts data from file and compare 
	
		      while(getline(infile, line))
		      {
			 //cout<< line <<endl<<endl;	 
			 //coordinateNum = (line.length() - 1) / 3;	 
			 for(int i = 0; i < line.length(); i++)
			 {
			    //x = line.substr(sp, 2);
			    //array[counter] = x;
			   // counter++;
			   // sp += 3;
                           //To be implemented here..
			 }
			  //sp = 0;
		      }

		     infile.close();
		   }


File format in textfile-
1
2
3
A1;A2;A3=Y
B1;C1;D1=Z
C5;C6=X


Those i commented off is the code I used to extract out the coordinates. Now, I need to extract the last letter of the line for each row...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main()
{
   string x;
   int sp = 0;
   string line;
   int counter = 0;
   string array[100];
   int coordinateNum;
   char submarine = "S";
   char destroyer = "D";
   char patrolboat = "P";
   char battleship = "B";
   char ACCarrier = "A";
   string subFound;
   string desFound;
   string boatFound;
   string bShipFound;
   string carrierFound;
   
   ifstream myfile ("battleship.txt");
   
   if(myfile.is_open())
   {
      while(getline(myfile, line))
      {
      coordinateNum = (line.length() - 1) / 3;
         for(int i = 0; i < line.length; i++)
         {
            // Find submarine
            subFound = line.find(submarine);
            // Find destoyer
            desFound = line.find(destroyer);
            // Find boat
            boatFound = line.find(patrolboat);
            // Find battleship
            bShipFound = line.find(battleship);
            // Find AC carrier
            carrierFound = line.find(ACCarrier);
            
            // Extracting coords
            	x = line.substr(sp, 2);
	        array[counter] = x;
	        counter++;
	        sp += 3;
         }
          sp = 0;
      }
      
      for(int k = 0; k < counter;k++)
      {
         cout << array[k];
         
      }
         
      myfile.close();
   }
   else
   cout<< "Unable to open file!";
   return 0;
}


What I did now was to declare a variable for each of the letter symbols like this A1;A2;A3=S
So its "S" in this case, which will resembles submarine. Then i do a "find() to find those letter in the lines and store each of these find() into a variable for example subFound = line.find(submarine);.

So right now, I want to do a condition as followed:
1. If the user-input is = any of the variable in the array
AND
2. If the line matches with .e.g subFound (find() for submarine)

Then it will cout "You hit a submarine!"

Something like that, that's my logic on doing this.. still trying to figure..
Topic archived. No new replies allowed.