joler
joler

Reputation: 37

Separating blank spaces from getline vs cin >> string

Context: This was an exercise for a "course", where I was given a file with ASCII numbers and was told to decode the ASCII numbers into words. Each word was written in one line, with each ASCII letter separated by spaces. Example:

107 97 119 97
112 105 101 115
100 111 109

In the terminal, it did return the decoded words, but my code was marked as incorrect and I was told to use file >> line instead of (getline(file, line)) in the main function by one course admin, after which another admin told me to use (getline(file, line)), which my code already does have... I've been unable to get any feedback helpful to me so I wanted to ask here instead. Is using getline inefficient for this and is file >> line ideal? How would you separate the decoded words into separate lines, when file >> line doesn't take in blank characters?

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

int stringToInt(string numberSet)
{
    int position = 1;
    int number = 0;

    for (int i = numberSet.length() - 1; i >= 0; i--)
    {
        number = number + (numberSet[i] - 48) * position;
        position = position * 10;
    }
    return number;
}

string convertToWord(string line)
{
    string numberSet = "";
    string word = "";
    int number;

    for (int i = 0; i < line.length(); i++)
    {
        if (line[i] != ' ' && line[i] != '/n')
        {
            numberSet = numberSet + line[i];
        }
        else
        {
            number = stringToInt(numberSet);
            char letter = number;
            word = word + letter;

            numberSet = "";
        }
    }

    number = stringToInt(numberSet);
    char letter = number;
    word = word + letter;

    return word;
}

int main()
{
    ifstream file;
    ofstream result;
    file.open("slowa1.txt");
    result.open("wyniki2.txt");

    string line;
    bool isEmpty = true;

    while (getline(file, line))
    {
        isEmpty = false;
        cout << convertToWord(line) << endl;
        result << convertToWord(line) << endl;
    }

    if (isEmpty)
    {
        cout << "No solutions" << endl;
        result << "No solutions" << endl;
    }

    file.close();
    result.close();
    return 0;
}

I originally used (getline(file, line)) in main function, but was told that it's wrong with no further context. For the example input above my code returned

kawa
pies
dom

Is the code wrong because I didn't use file >> line instead? How can you add '/n' or endl after each word like above by using file >> line, so the output doesn't become kawapiesdom without spaces?

Upvotes: 0

Views: 102

Answers (1)

Caleth
Caleth

Reputation: 63142

Is the code wrong because I didn't use file >> line instead?

We can't know the marking scheme, but given that the course admins disagree, I would suggest it isn't. You've certainly noticed the problems that file >> line has over getline(file, line), and it doesn't have benefits in this case.

What may have been marked as incorrect is your stringToInt and convertToWord functions, which can be simplified by using a std::stringstream

string convertToWord(string_view letters)
{
    istringstream in{ letters };
    string word;
    int letter;

    while(in >> letter)
    {
        word.push_back(static_cast<char>(letter));
    }

    return word;
}

Another issue is that you call convertToWord twice when writing to the two output streams.

while (getline(file, line))
{
    isEmpty = false;
    string word = convertToWord(line);
    cout << word << endl;
    result << word << endl;
}

Upvotes: 1

Related Questions