fgetc() for proccessing entered chars - char by char. The function returns integer value of entered char.
I need to check these:
- Each town shouldn't exceed 30 characters
- There exists a "-"(dash) character on each line (splitting the whole line to two cities)
I've defined 31-int-array and one integer for counting chars. When user enter "-", i need to save the chars as a string to another array (actually map).
printf("Enter route (city - city):\n");
char town[31];
int character, charsOnRow = 0, i = 0;
map<int, string> route;
while (!feof(stdin)) {
character = fgetc(stdin);
if (character != '-' && character != '\n') {
if (charsOnRow < 30) {
town[charsOnRow] = character;
charsOnRow++;
} else {
printf("Invalid input.\n");
}
} else if (route.size() == 0 && character == '\n') {
printf("Invalid input.\n");
} else {
town[charsOnRow] = '\0';
route[i] = town;
charsOnRow = 0;
}
}
So example of input would be this:
Syracuse - Utica
Binghamton - Syracuse
Binghamton - Utica
How should i fix it and be able to save int-array to map<int, string>?