Class Project Suggestions

I have a class project that I am doing and wanted some personal feedback as t what I can add for an everyday user or how I can improve a users input. I am still learning C++ so any tips and resources is much appreciated.

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
81
82
83
84
85
86
87
88
89
90
91
#include <iostream>

using namespace std;
int main() {

    int school, social, physical, mental, love, spiritual;

    double sixStar;



    cout << "---Six Star Man Evaluator---" << endl;
    //system("Pause");
    cout << "Please give a number from 1-6 on each quality you believe you are at the moment." << endl;
    cout << "One being very bad and 6 meaning very good\n";

//This part asks the user for their rating in each field.
//The 'while' statements test for validity.
    cout << "How would you rate yourself at school right now? Education? Studies? Knowledge?" << endl;
    cin >> school;
    while (school < 1 || school > 6){
        cout << "Please enter a valid number.\n";
        cin>> school;
    }

    cout << "How would you rate yourself socially? Talking to strangers? Making friends?" << endl;
    cin >> social;
    while (social < 1 || social > 6){
        cout << "Please enter a valid number.\n";
        cin>> social;
    }

    cout << "How would you rate yourself physically? Body? Face? Style?" << endl;
    cin >> physical;
    while (physical < 1 || physical > 6){
        cout << "Please enter a valid number.\n";
        cin>> physical;
    }

    cout << "How would you rate your love and sacrifice for others? Gratitude? Kindness?" << endl;
    cin >> love;
    while (love < 1 || love > 6){
        cout << "Please enter a valid number.\n";
        cin>> love;
    }

    cout << "How would you rate your yourself mentally? Willpower? Self-esteem?" << endl;
    cin >> mental;
    while (mental < 1 || mental > 6){
        cout << "Please enter a valid number.\n";
        cin>> mental;
    }
    //Might replace this with financial
    cout << "How would you rate yourself spiritually? Practicing the Word of God? Reading?" << endl;
    cin >> spiritual;
    while (spiritual < 1 || spiritual > 6){
        cout << "Please enter a valid number.\n";
        cin>> spiritual;
    }


    sixStar = (school + social + physical + mental + love + spiritual) / 6;
    //This part checks the number, gives feedback, and stars.
    if (sixStar == 6){
        cout << "You are the 6 star man! Your number is " << sixStar << " ******!" << endl;
    }
    else if (sixStar == 5){
        cout << "You are a 5 star man! Just one more star! Your number is " << sixStar << " *****!" << endl;
    }
    else if (sixStar == 4){
        cout << "You are a 4 star man! May not be 5 but you're close! Your number is " << sixStar << " ****!" << endl;
    }



    else if (sixStar == 3){
        cout << "You are a 3 star man! You need to start improving now! Your number is " << sixStar << " ***!" << endl;

    }
    else if (sixStar == 2){
        cout << "You are a 2 star man! This is not good. work harder! Your number is " << sixStar << " **!" << endl;
    }
    else

    {
        cout << "Work hard to grow your self and learn from your mistakes. You have " << sixStar << " *!" << endl;
    }
    system("Pause");
    cout << "Thank you for taking the evaluation. We hope you succeed in becoming a six star man.\n";
    return 0;
}
The first thing I see is a lot of code duplication. A few functions could probably remove most of the duplication.

For example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
...
    cout << "How would you rate yourself at school right now? Education? Studies? Knowledge?" << endl;
    cin >> school;
    while (school < 1 || school > 6){
        cout << "Please enter a valid number.\n";
        cin>> school;
    }

    cout << "How would you rate yourself socially? Talking to strangers? Making friends?" << endl;
    cin >> social;
    while (social < 1 || social > 6){
        cout << "Please enter a valid number.\n";
        cin>> social;
    }
...


Could probably be replaced with something like:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
...
    cout << "How would you rate yourself at school right now? Education? Studies? Knowledge? ";
    int school = get_user_input(cin);
 
   cout << "How would you rate yourself socially? Talking to strangers? Making friends? " ;
    int social = get_user_input(cin);
...

// Get input from the user from any input stream (ie std::cin).
int get_user_input(std::istream& fin)
{
    int value;
    fin >> value;
    while (!cin || (value < 1 || value > 6)){
        cout << "Please enter a valid number.\n";
        fin.clear(); // Clear any error flags (if any).
        fin.ignore(1000, '\n'); // Remove any input still in the input buffer.
        fin >> value;
    }

    return value;
}


Topic archived. No new replies allowed.