xMas.cpp

my first attempt using vectors! =P
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#include <iostream>
#include <vector>
#include <ctime>

std::string cantBeSimpleNowCanIt(int);
inline void mySleep(clock_t);

int main() {
    
    std::string xStr[] = {
        "glitch in a pearl tree",
        "algorithms",
        "French tens",
        "calling methods",
        "golden strings",
        "modulos a-dividing",
        "arrays a-summing",
        "bits a-byting",
        "loops a-looping",
        "longs a-heaping",
        "pipelines piping",
        "dumpers dumping",
    };
    std::vector<std::string> xMas (xStr, xStr + sizeof(xStr) / sizeof(std::string) );
    
    for(std::vector<std::string>::iterator n = xMas.begin(); n < xMas.end(); n++) {
        
        int nDay = std::distance(xMas.begin(), n) + 1;
        
        std::cout << "\nOn the " << nDay << cantBeSimpleNowCanIt(nDay) 
                  << " day of xMas, my coding sent to me!\n";
        mySleep(1);
        
        for(std::vector<std::string>::iterator i = n; i >= xMas.begin(); i--) {
            
            if(i == xMas.begin()) {
                if (n == xMas.begin())
                    std::cout << "A";
                else
                    std::cout << "And a";
            } else {
                std::cout << nDay;
            }
            
            std::cout << " " << *i << "\n";
            
            nDay--; //high precision operation!
            mySleep(1);
        }
    }
    
    return 0;
}

std::string cantBeSimpleNowCanIt(int iDay) {
    
    switch (iDay) {
        case 1:
            return "st";
            break;
            
        case 2:
            return "nd";
            break;    
        
        case 3:
            return "rd";
            break;
            
        default:
            return "th";
    }
}

inline void mySleep(clock_t sec) //thanks Denis!
{
    clock_t start_time = clock();
    clock_t end_time = sec * 1000 + start_time;
    while(clock() != end_time);
}

improvements? ideas? lets hear!

for the search engine users: we are iterating a vector both up and down simultaneously, also accessing its position integer and applying a sleep function kindly provided by denis in another cplusplus post
Last edited on
inline void mySleep(clock_t sec) //thanks Denis!

I don't know who Denis is, but he should be shot.

http://en.cppreference.com/w/cpp/thread/sleep_for

Line 34 results in undefined behavior. You cannot decrement the iterator that compares equal to the iterator returned by begin().

You need to #include <string>

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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#include <chrono>
#include <iostream>
#include <string>
#include <thread>
#include <vector>

std::string ordinal(int);

int main() {

    std::vector<std::string>  items = {
        "glitch in a pearl tree",
        "algorithms",
        "French tens",
        "calling methods",
        "golden strings",
        "modulos a-dividing",
        "arrays a-summing",
        "bits a-byting",
        "loops a-looping",
        "longs a-heaping",
        "pipelines piping",
        "dumpers dumping",
    };

    for (std::size_t i = 0; i < items.size(); ++i)
    {
        std::cout << "\nOn the " << ordinal(i + 1) << " day of Christmas, my coding sent to me...\n";
        std::this_thread::sleep_for(std::chrono::milliseconds(750));

        for (std::size_t j = i + 1; j > 1; --j)
        {
            std::cout << j << ' ' << items[j-1] ;
            std::this_thread::sleep_for(std::chrono::milliseconds(200));

            for (std::size_t k = 0; k < 3; ++k)
            {
                std::cout << '.' << std::flush;
                std::this_thread::sleep_for(std::chrono::milliseconds(200));
            }

            std::cout << '\n';
        }

        std::cout << (i == 0 ? "A " : "And a ") << items[0] << '\n';
        std::this_thread::sleep_for(std::chrono::milliseconds(1200));
    }
}

std::string ordinal(int iDay) {
    std::string result = std::to_string(iDay);

    if (iDay > 10 && iDay < 20)
        result += "th";
    else
    {
        switch (iDay % 10) {
        case 1:
            result += "st";
            break;

        case 2:
            result += "nd";
            break;

        case 3:
            result += "rd";
            break;

        default:
            result += "th";
            break;
        }
    }

    return result; 
}
Last edited on
I used cire's sleep and tidied your code.
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include <iostream>
#include <vector>
#include <chrono>
#include <thread>

std::string suffix(int i);
int day(int idx) { return idx + 1; }

int main()
{
    const std::vector<std::string> xStr {
        "glitch in a pearl tree",
        "algorithms",
        "French tens",
        "calling methods",
        "golden strings",
        "modulos a-dividing",
        "arrays a-summing",
        "bits a-byting",
        "loops a-looping",
        "longs a-heaping",
        "pipelines piping",
        "dumpers dumping"
    };

    for (size_t i = 0; i != xStr.size(); ++i)
    {
        std::cout << "On the " << day(i) << suffix(day(i)) << " day of xMas, my coding sent to me!\n";
        std::this_thread::sleep_for(std::chrono::seconds(1));

        for (size_t j = i; j > 0; --j)
            std::cout << day(j) << " " << xStr[j] << "\n";
        std::cout << (i == 0 ? "A " : "And a ") << xStr[0] << "\n\n";
        std::this_thread::sleep_for(std::chrono::seconds(1));
    }

    return 0;
}

std::string suffix(int i)
{
    switch (i)
    {
    case 1:
        return "st";

    case 2:
        return "nd";

    case 3:
        return "rd";

    default:
        return "th";
    }
}
Last edited on
interesting xD the mySleep function worked for me
on the other hand, when i tried including chrono, the compiler didnt like it pointing me to:
1
2
3
#error This file requires compiler and library support for the \
ISO C++ 2011 standard. This support is currently experimental, and must be \
enabled with the -std=c++11 or -std=gnu++11 compiler options. 

and also complained about the vector declaration, telling me to initialize it via constructor instead of "{...}"

so i experimentally switched to C++11
but even then the compiler complains about std::this_thread and std::chrono not being declared (also does with C++98 and 14)

which also brings me to a further question, is C++11 still experimental? (looking at ++14)
and whats the most stable/reliable version to use for productive projects?

also, are there advantages/disadvantages in using begin/end compared to the size variant? - apart from begin/end being mono directional and having slightly more overhead

cheerio!
interesting xD the mySleep function worked for me


http://codereview.stackexchange.com/questions/42506/using-a-for-loop-instead-of-sleeping


which also brings me to a further question, is C++11 still experimental?

No. It's not. Update your compiler.


also, are there advantages/disadvantages in using begin/end compared to the size variant? - apart from begin/end being mono directional and having slightly more overhead

Using an index has the advantage that you have it when you need it, and since you need the index it makes sense to loop on it.
Topic archived. No new replies allowed.