'How do I avoid printing the numbers within the string of this file io program?

Here's my code:

#include <fstream>
#include <iostream>
#include <string>
using namespace std;

string substring(string a, int b, int c) {
    string result = "";
    for (int i = b; i < b + c; i++) {
        result += a[i];
    }
    return result;
}

int main() {
    ifstream in_stream;
    in_stream.open("HW3Test.txt");
    ofstream output_stream("HW3output.txt");

    string result[100];
    int i = 0;
    while (in_stream >> result[i]) {
        i++;
    }
    string check[i];

    for (int k = 0; k < i; k++) {
        check[k] = substring(result[k], 0, 2);
    }

    string scores[i];

    for (int k = 0; k < i; k++) {
        if (check[k][0] >= '0' && check[k][0] <= '9') {
            scores[k] = check[k];
        }
    }
    for (int k = i; k >= 0; k--) {
        output_stream << scores[k] << endl;
    }
    for (int k = 0; k < i; k++) {
        if (!(result[k][0] >= '0') && !(result[k][0] <= '9')) {
            output_stream << result[k];
        }
    }
}

In this problem, I'm given this input:

86 Bill Fun
93 Kelly Jelly
74 Bob Squee
81 Jim Flim
72 John Fraggle
87 June Prune
63 Roberta Robertson

and trying to achieve this output:

Fun, Bill:  63
Jelly, Kelly:  87
Squee, Bob:  72
Flim, Jim:  81
Fraggle, John:  74
Prune, June:  93
Robertson, Roberta:  86

So, I first got the scores in from the text file, and stored it in a scores array. Now, I need to get only the names, but for some reason, the program is not outputting any text when I check if the first character in the string is not a number. How can I get it to print out only the strings that start with a letter?



Solution 1:[1]

The following code may not be the most optimal, but it seems to work as expected.

#include <fstream>
#include <iostream>
#include <string>
#include <vector>
#include <cstdlib> // for atoi function to convert to int, 
//since c++ 11 you can use std::stoi(string) without this #include (but I didn't check it)

struct Leaderboard
{
    std::string FirstName;
    std::string Surname;
    int score;  
};

int main() 
{   
    std::ifstream in_stream;
    in_stream.open("HW3Test.txt");
    if(!in_stream)
    {
        throw "Input File not Found";
    }
    
    std::ofstream output_stream("HW3output.txt");

    //std::string result[100]; why only 100?
    std::vector<Leaderboard> tLeaderBoardVector; // it can be bigger with for example vector
    
    std::string tLine;
    while (in_stream >> tLine) 
    {
        Leaderboard tmpLeaderboard;
        tmpLeaderboard.score = atoi(tLine.c_str());
        in_stream >> tLine;
        tmpLeaderboard.FirstName = tLine;
        in_stream >> tLine;
        tmpLeaderboard.Surname = tLine;
        tLeaderBoardVector.push_back(tmpLeaderboard);
    }
    
    //reverse points output to get the same output as in question output, but I don't know why reverse it
    for(int i = 0; i < tLeaderBoardVector.size() / 2; i++)
    {
        int tmp = tLeaderBoardVector[i].score;
        tLeaderBoardVector[i].score = tLeaderBoardVector[tLeaderBoardVector.size() - 1 - i].score;
        tLeaderBoardVector[tLeaderBoardVector.size() - 1 - i].score = tmp;
    }

    for (int i = 0; i < tLeaderBoardVector.size(); i++) 
    {
        output_stream << tLeaderBoardVector[i].Surname << ", ";
        output_stream << tLeaderBoardVector[i].FirstName << ":  ";
        output_stream << tLeaderBoardVector[i].score << "\n";
    }
    return 0;
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Guruweryn