Printing Dashes/Hyphens ?

///
Last edited on
This is the way that I would probably do it:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
void Print_List(Student* List, int Size) {     // you don't need the &
    // Create a Lambda function to simplify things
    auto printDashes = [](const std::string& id) {
        std::ostringstream out;
        // loop over each character in the string
        for (std::string::const_iterator it = id.begin(); it != id.end(); ++it) {
            switch (std::distance(id.begin(), it)) {
                // if at position 3 or 5, output a dash
                case 3:
                case 5: 
                    out << '-';

                // output the number (no break, so regardless of position)
                default:
                    out << *it;
            }
        }
        // return the string
        return out.str();
    };

    // print out as normal, until line 53
        << setw(17) << printDashes(List[i].Id) << setw(16) << calcAverage(List[i])
    // etc.
}


This may be slightly complicated, and there is probably a better way of doing it, this is just how I thought of doing it off the top of my head. Also, this assumes that "ID" is being stored as a std::string and that you have got #include <sstream> at the top of your code.

EDIT:
Here is a quick example to show that it works:
http://ideone.com/5jgIIg
Last edited on
///
Last edited on
I don't think you should use modulus unless it is a requirement for your assignment.
Topic archived. No new replies allowed.