-2

Quick question. I'm doing my portfolio C++ question for a uni assignment. It is standard deviation. My question is to do with reading multiple strings from a file, see text here;

Design and write a c++ program that reads a set of scores from the file scored.dat, and outputs their mean and standard deviation to cout.

I'm not going to bother with the actual equation, I have that part nearly sorted. My query is based directly on outputting the text read from the file into strings. For example, if the document had these three scores:

10
15
11

Instead of outputting the text as it is, it would put them into three strings;

Score_One (Which would be 10)
Score_Two (Which would be 15)
Score_Three (Which would be 11)

I hope I am making sense here guys. Thanks.

Lol4t0
  • 12,444
  • 4
  • 29
  • 65
John Brown
  • 151
  • 3
  • 14
  • How are you outputting the numbers now? (post your code) – Caribou Nov 15 '12 at 20:09
  • I haven't actually got to that part at the minute, I wanted to get this bit sorted before moving to the next as it is basically the most important part. - Thanks for the edit lol4t0 - I need the strings before I can start the standard deviation equation. – John Brown Nov 15 '12 at 20:10
  • Why do you want to put values to strings, if you should output them to standard output?n And what is the problem? all this is not clear at all. – Lol4t0 Nov 15 '12 at 20:12
  • Basically, for the equation I need to have two individual values for "X". Those values will come from the file. - I was going to store the information from the file into strings, and then output them as depicted from the standard deviation question. – John Brown Nov 15 '12 at 20:13
  • 1
    @JohnBrown this question is seriously confused - "Design and write a c++ program that reads a set of scores from the file" - there are hundreds of students asking similar questions on SO - search and find one that does something similar - then if it doesn't work as you want try asking a question again. – Caribou Nov 15 '12 at 20:18
  • If you want I could write down the whole question with the equation? – John Brown Nov 15 '12 at 20:22
  • I can already read stuff from a file and output it, but I need to store the data in strings. – John Brown Nov 15 '12 at 20:33

2 Answers2

1

Here is a solution which was just fun to write and which hardly a solution I would expect to show up. It may have some entertainment and/or educational value as well. The basic idea is that values a written as

    std::cout << 10 << 15 << 11 << reset << '\n';
    std::cout << 1 << 2 << 3 << reset << '\n';

To achieve this, a bit of machinery is needed but it isn't that bad, really. The code is blow:

#include <locale>
#include <iostream>
#include <algorithm>

static int index(std::ios_base::xalloc());
static std::string const names[] = { "One", "Two", "Three", "Four", "Five" };
static std::string const score("Score_");
static std::string const other(" (Which would be ");
std::ostream& reset(std::ostream& out)
{
    out.iword(index) = 0;
    return out;
}

struct num_put
    : std::num_put<char>
{
    iter_type do_put(iter_type to, std::ios_base& fmt, char_type fill,
                     long v) const {
        to = std::copy(score.begin(), score.end(), to);
        if (fmt.iword(index) < 5) {
            to = std::copy(names[fmt.iword(index)].begin(),
                           names[fmt.iword(index)].end(), to);
            ++fmt.iword(index);
        }
        else {
            throw std::runtime_error("index out of range!");
        }
        to = std::copy(other.begin(), other.end(), to);
        to = this->std::num_put<char>::do_put(to, fmt, fill, v);
        *to++ = ')';
        *to++ = ' ';
        return to;
    }
};

int main()
{
    std::cout.imbue(std::locale(std::locale(), new num_put));
    std::cout << 10 << 15 << 11 << reset << '\n';
    std::cout << 1 << 2 << 3 << reset << '\n';
}
Dietmar Kühl
  • 150,225
  • 13
  • 225
  • 380
1

you need to do something like this:

int raw_score_one = 11; //you have already set this but for the sake of clarity
std::stringstream output;
output << raw_score_one;
std::string Score_One = output.str(); 

for each score...

Caribou
  • 2,070
  • 13
  • 29