This code shows you how to split one text the user typed into two or multiple strings, can be useful if you are making an application or game and you want the user to type something like "equip sword" or something like that. This application will split the word into two words, "equip" and "sword" and you can check the action which is "equip" and perform certain actions like in this case it will equip a sword.
Splitting two strings:
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
|
#include <iostream>
using namespace std;
#define NUM 20
inline void splitString(char c[])
{
//These are used to hold the temporary words like "equip" or "sword"
char tempchar1[NUM] = " ";
char tempchar2[NUM] = " ";
//This checks through the whole text typed until it reaches a space
for(int i = 0; i < NUM;i++)
{
//Assigns the first word to the tempchar1
tempchar1[i] = c[i];
//Checks if it has reached a space
if(c[i + 1] == ' ')
{
//Used to assign the second word to tempchar2
for(int r = i + 2;r < NUM;r++)
{
tempchar2[r - (i + 2)] = c[r];
}
i = NUM;
}
}
//Prints the two characters
cout << "tempchar1 = \"" << tempchar1 << "\"" << endl <<
"tempchar2 = \"" << tempchar2 << "\"" << endl;
}
int main()
{
char Char[NUM];
cin.getline(Char, NUM);
splitString(Char);
system("pause");
return 0;
}
| |
If you want to have more than one item you can do something like this
Splitting three strings:
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
|
#include <iostream>
using namespace std;
#define NUM 20
inline void splitString(char c[])
{
char tempchar1[NUM] = " ";
char tempchar2[NUM] = " ";
char tempchar3[NUM] = " ";
for(int i = 0; i < NUM;i++)
{
tempchar1[i] = c[i];
if(c[i + 1] == ' ')
{
for(int r = i + 2;r < NUM;r++)
{
tempchar2[r - (i + 2)] = c[r];
if(c[r + 1] == ' ')
{
for(int q = r + 2; q < NUM;q++)
{
tempchar3[q - (r + 2)] = c[q];
}
r = NUM;
}
}
i = NUM;
}
}
cout << "tempchar1 = \"" << tempchar1 << "\"" << endl <<
"tempchar2 = \"" << tempchar2 << "\"" << endl <<
"tempchar3 = \"" << tempchar3 << "\"" << endl;
}
int main()
{
char Char[NUM];
cin.getline(Char, NUM);
splitString(Char);
system("pause");
return 0;
}
| |
An output can look something like this -
This can be very useful when creating worded games and you want them to write spell then the target or something like that, There are endless possibilities with this.
This is one example program of how to use this unique function -
Example:
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 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
|
#include <iostream>
#include <string>
using namespace std;
#define CHAR 30
//Prototypes
void getUserI();
int main();
void catchErrors(char error[CHAR]);
//Convention - c infront of a name = current
int maxhp = 100, pHp = maxhp;
int maxmana = 100, cmana = maxmana;
int playerChoice, numWeapsInInventory = 3;
string tagWeap = "";
int lvl = 12, strength = 1, intelligence = 3, dexterity = 8, agility = 4, maxWeapons = 3;
string cweapon = "Fist", playerJob = "Farmer", playerName = "Demon Slayer";
bool exitGame = false;
void showStat()
{
system("cls");
cout << "\tName : " << playerName << "\n\nWeapon : " << cweapon << "\n\n" << "\tJob : " << playerJob << "\n";
cout << "\tLvl : " << lvl << "\n\tHealth : " << pHp << "/" << maxhp << "\n";
cout << "\tMana : " << cmana << "/" << maxmana << "\n\n";
cout << "Strength : " << strength << "\n" << "Intelligence : " << intelligence << "\n";
cout << "Dexterity : " << dexterity << "\n" << "Agility : " << agility << "\n\n";
}
void catchErrors(int error)
{
switch(error)
{
case 1:
cout << "You did not type an action, please try again" << endl;
break;
default:
cout << "ERROR in catchErrors. You should not see this!!" << endl;
break;
}
getUserI();
}
void decipherChar(char c[CHAR])
{
//Temporary holding characters to hold the action and another variable needed to split
char tempchar[CHAR] = " ";
char tempchar2[CHAR] = " ";
//Checks the char c letter by letter
for(int i = 0; i < CHAR; i++)
{
//Assigns the letters one by one to the tempchar
tempchar[i] = tolower(c[i]);
//Checks if it has reached a space or the null character - used for just actions etc exit
if(c[i + 1] == ' ' || c[i + 1] == '\0')
{
// equip
if(tempchar[0] == 'e' && tempchar[1] == 'q' && tempchar[2] == 'u' && tempchar[3] == 'i' && tempchar[4] == 'p')
{
//Goes through the second part of the string
for(int w = 6;w < CHAR; w++)
{
//Makes sure that it wont add any weird characters for the other 20 or so elements of the array
if(c[w] != '\0')
{
//Assigns the first array in tempchar2 to the weapon
tempchar2[w - 6] = tolower(c[w]);
}
else break;
}
cout << "You eqipped " << tempchar2 << endl;
//Assigns the weapon
cweapon = tempchar2;
}
// stat
else if(tempchar[0] == 's' && tempchar[1] == 't' && tempchar[2] == 'a' && tempchar[3] == 't')
{
showStat();
}
// exit
else if(tempchar[0] == 'e' && tempchar[1] == 'x' && tempchar[2] == 'i' && tempchar[3] == 't')
{
exitGame = true;
main();
}
else
{
catchErrors(1);
}
//sets i to char so the for loop ends
i = CHAR;
}
}
//Goes back to asking the user another choice
getUserI();
}
void getUserI()
{
char choice[CHAR];
//Gets the whole line that the user has typed in including spaces and everything
cin.getline(choice, CHAR);
decipherChar(choice);
}
int main()
{
if( exitGame != true)
{
getUserI();
}
system("pause");
return 0;
}
| |
By the way, the exit does not work properly and some variables may have been unused but this is an example of how to implement this function into your program, There may be better ways and i may post that up another time maybe using classes but this way is pretty good for now. Have fun.
By Sean Genge