-1

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?

Ted Lyngmo
  • 93,841
  • 5
  • 60
  • 108
Andrew
  • 31
  • 5
  • 1
    Use a function split from string to split input string respect to ' '(space char) – BIg G Mar 11 '22 at 08:12
  • 2
    @Andrew In your expected output why are the scores of different people jumbled? I mean, the score for `Jelly, Kelly:` should be `93` and not `87` since inside the input file, the score for kelly jelly is `93`. Is it intentional? – Jason Mar 11 '22 at 08:14
  • @Blg G How do I do that? – Andrew Mar 11 '22 at 08:31
  • @Anoop Rana Objective of the assignment is to achieve that output. – Andrew Mar 11 '22 at 08:32
  • @Andrew Don't learn C++ by trial and error. Instead use a [good C++ book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). Also, if you expect people to help you, clarify the problem when needed. We already know the objective is to achieve that output. But is there any logic involved in getting that output? For example, inside the input file, the score for `Roberta Robertson` is `63` but inside the output file the score for `Robertson, Roberta:` is `86`. You should clarify what is going on there. – Jason Mar 11 '22 at 08:36
  • _" Objective of the assignment is to achieve that output"_ - So, you than need to know a set of rules to do that mapping. What makes reading the lines `86 Bill Fun` and `63 Roberta Robertson` result in the output `Fun, Bill: 63` and `Robertson, Roberta: 86`? What rules say that they should swap `score`s with eachother? – Ted Lyngmo Mar 11 '22 at 09:29

1 Answers1

0

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;
}
Guruweryn
  • 1
  • 2