Is there any way i could make this code simple as possible.

I need tit to be beginner friendly if anyone can help to make this simplistic as possible.

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 <vector>

using namespace std;

int main()
{
    std::string str;
	const char* data[] = { "First", "Last" };
    int j;

    for (int i = 0; i < 2; i++) {
        cout << "Type " << data[i] << " Name: ";
        cin >> str;

        if (isupper(str[0])) {

            for (j = 1; j < str.size(); j++) {
                if (!islower(str[j]))
                {
                    cout << "InCorrect!" << endl;
                    break; // Exit the loow
                }
            }
            if(j==str.size())
                cout << "Correct!" << endl;
        }
        else {
            cout << "InCorrect!" << endl;
        }
    }

    system("pause");
    return 0;
}
It is already rather simple. You could add comments to explain the logic.

Alternatively, you could move the validity check into separate function. That way it would be more obvious that the main program asks for a word (twice) and each time responds either 'Correct' or 'Incorrect'.


There is a style issue; the array 'data' has some number of elements, but the loop is hardcoded to iterate exactly two times. That is an unnecessary maintenance hazard and thus a bad example for beginners. The loop should iterate as many times as there are elements in the array.

Other bad examples are on lines 5 and 34. Nobody should learn those by default. Both can be avoided. One can learn to know and to think, or one can learn to blindly copy-paste suicidal tendencies. That naturally leads to the ethical question, which is worse: to be friendly to beginners by apparently hurting them, to be friendly to beginners by letting them hurt themselves?
Also remove line 3. You don't use vector so it isn't needed.
I think that one is clearer:
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
#include <cctype>
#include <iostream>
#include <string>

bool is_correct(const std::string& str) {
    // is incorrect (!str.empty() call is not needed here, std::cin won't accept just enter, 
    // but it's a good practise)
    if (!str.empty() && isupper(str[0])){
        for (auto i = str.begin() + 1, end = str.end(); i != end; ++i)
            if (!islower(*i))
                return false;
    }
    else
        return false;
   
   // if not incorrect, return true
    return true;
}

int main() {
    std::string str;

    std::cout << "Type first name: ";
    std::cin >> str;

    if (is_correct(str))
        std::cout << "Correct!\n";
    else
        std::cout << "Incorrect!\n";
        
        
    std::cout << "Type last name: ";
    std::cin >> str;

    if (is_correct(str))
        std::cout << "Correct!\n";
    else
        std::cout << "Incorrect!\n";
        
    std::cin.get(); // one for getting enter left in input by std::cin
    std::cin.get(); // and one to pause the screen
    return 0;
}


EDIT: edited code, thanks to andywestken
Last edited on
@TheHardew

Your validation code is not quite right?

Type first name: super
Correct!
Type last name: man
Correct!


(Doge1's version correctly rejects names without leading capitals.)

Andy
You're right, sorry.
Topic archived. No new replies allowed.