Help me explain this code

ello guys,

Could you explain me this code? Especially the part where the loop is. And im wondering what should you do if the user gives you bad entry and you want them to type new number again?

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
  #include <iostream>
#include <string>
#include <cctype>
#include <algorithm>
#include <map>

using namespace std;

int main () {
    map<string,int> m;
    m["zero"] = 0;
    m["one"] = 1;
    m["two"] = 2;
    m["three"] = 3;
    m["four"] = 4;
    m["five"] = 5;
    m["six"] = 6;
    m["seven"] = 7;
    m["eight"] = 8;
    m["nine"] = 9;

    cout << "Enter number from 0-9 with word:" << endl;
    string input;
    getline(cin, input);
    transform(input.begin(), input.end(), input.begin(), ::tolower);

    for (int i = 1; i <= m[input]; i++) {
        for (int j = 1; j <= i; j++) {
            cout << input << ", ";
        }
        cout << endl;
    }

    cin.get();
    return EXIT_SUCCESS;
}
http://ideone.com/IeeWLY

Which lines specifically do you want explained? For the loop, it just depends on your understanding of nested loops.
lolek4551 wrote:
And im wondering what should you do if the user gives you bad entry and you want them to type new number again?
26
27
28
29
if(m.find(input) == m.end())
{
    cout << "Invalid input" << endl;
}
More of your code would need changing to have them try again - do you want me to elaborate?
Could you help me please because i really need that part for bad entries because my teacher like to mess with you .

For the loop what i want is:
which loop is it that does input + input in new line?
Here is a traditional input validation loop:
1
2
3
4
5
6
int x;
while(std::cout << "Please enter a non-negative integer only: " << std::flush
   && std::cin >> x
   && x < 0)
{
}


lolek4551 wrote:
which loop is it that does input + input in new line?
I don't understand the bolded part of your question?
Last edited on
Sorry for bad english but what i mean is that i dont understand is that.

three
three, three
three, three, three

Where in that loop does one more three adds if u know what i mean?

fixed 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
#include <iostream>
#include <string>
#include <cctype>
#include <algorithm>
#include <map>

using namespace std;

int main () {
    map<string,int> m;
    m["zero"] = 0;
    m["one"] = 1;
    m["two"] = 2;
    m["three"] = 3;
    m["four"] = 4;
    m["five"] = 5;
    m["six"] = 6;
    m["seven"] = 7;
    m["eight"] = 8;
    m["nine"] = 9;

    string input;
    while (true) {
        cout << "Enter number from 0-9 with word:" << endl;
        getline(cin, input);
        transform(input.begin(), input.end(), input.begin(), ::tolower);
        if (m.find(input) == m.end()) {
            cout << "Wrong entry!" << endl;
        } else break;
    }

    for (int i = 1; i <= m[input]; i++) {
        for (int j = 1; j <= i; j++) {
            cout << input <<", ";
        }
        cout << endl;
    }

    cin.get();
    return EXIT_SUCCESS;
}
Last edited on
lolek4551 wrote:
Where in that loop does one more three adds if u know what i mean?
32
33
34
35
36
37
    for (int i = 1; i <= m[input]; i++) {
        for (int j = 1; j <= i; j++) {
            cout << input <<", ";
        }
        cout << endl;
    }
Look at the underlined code on line 33.
Ok this is final code. What do you think?

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
#include <iostream>
#include <string>
#include <cctype>
#include <algorithm>
#include <map>


using namespace std;

int main () {
    map<string,int> m = {{"zero", 0}, {"one", 1}, {"two", 2}, {"three", 3}, {"four", 4},
                        {"five", 5}, {"six", 6}, {"seven", 7}, {"eight", 8}, {"nine", 9}};

    string input;
    while (true) {
        cout << "Enter number from 0-9 with word:" << endl;
        getline(cin, input);
        transform(input.begin(), input.end(), input.begin(), ::tolower);
        if (m.find(input) == m.end()) {
            cout << "Wrong entry!" << endl;
        } else break;
    }

    for (int i = 1; i <= m[input]; i++) {
        for (int j = 1; j <= i; j++) {
            cout << input;
            if (j != i) cout << ", ";
        }
        cout << endl;
    }

    cin.get();
    return EXIT_SUCCESS;
}
I think it's fine, other than line 32:
http://www.cplusplus.com/forum/beginner/1988/
Topic archived. No new replies allowed.