restricting string input to numbers and letters only

Pages: 12
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
92
93
94
#include<iostream>
#include<iomanip>


using namespace std;

int main (){
     unsigned short numJudges, numCeleb;
     float score, addScore, finalScore = 0, min, max, max2=0;
     string name, winner;
    do {
     cout << "\n\n\tEnter number of judges: ";
     cin >> numJudges;
     if (numJudges < 3 || numJudges > 6)
        cout << "\n\n\tERROR! Number of judges must be between 3 and 6" << endl;
    }
     while (numJudges < 3 || numJudges > 6);


    do {
     cout << "\n\n\tEnter number of paricipants: ";
     cin >> numCeleb;
     if (numCeleb < 10 || numCeleb > 15)
        cout << "\n\n\tERROR! Number of participants must be between 10 and 15" << endl;
    }
    while (numCeleb < 10 || numCeleb > 15);


     for (unsigned short i = 1; i <= numCeleb; i++){


bool test;
do {
        test = true;
            cout << "\n\n\tEnter name of celebrity " << i << " : ";
//getline (cin, name, '\n');
getline (cin >> ws, name);
            for (unsigned short n = 0; n < name.length(); n++){
                if(!(isdigit(name.at(n)) || isalpha(name.at(n))))
                test = false;
            }
                if (!test)
                cout << "\n\n\tERROR! Name can consist of letters and numbers only.\n";
}
while (!test);

            addScore = 0,
            min = 6,
            max = 0;

            for (unsigned short a = 1; a <= numJudges; a++){
                do {
                    cout << "\n\n\tEnter score from judge number " << a << " : ";
                    cin >> score;
                    if (score < 0 || score > 6)
                        cout << "\n\n\tERROR! Score must be between 0 and 6." << endl;
                    else if ((((int) (score * 100.0)) % 10) != 0 && ((int) (score * 100.0) % 10) != 9)
                        cout << "\n\n\tERROR! Score can have only one deciamal place" << endl;
                }
                while ((score < 0 || score > 6) || (((int) (score * 100.0)) % 10) != 0 && ((int) (score * 100) % 10) != 9);

                    if (score > max)
                        max = score;

                    if (score < min)
                        min = score;

                        addScore+=score;
                }


                    finalScore = (addScore - max - min) / (numJudges - 2);

                    if (i==1){

                        max2=finalScore;
                        winner=name;
                    }

                    else if(max2<finalScore){

                             max2=finalScore;
                             winner=name;
                           }



                    cout << "\n\n\tFinal score is " << finalScore << endl;
         }

          cout << setw(51) << "And the winner is\n\n" << setw((80+winner.length())/2) << winner << "\n\n"<< setw(42) << max2 << endl;

           return 0;
    }


try now, thats without changing line 59
Like MiiNiPaa said, changing <= to < will give some result as well as adding cin.ignore after line
35.
Your updated code still skips over one a set number of your input buffers(meaning, if you're the user, and you type 2 characters or integers, you're going to skip over 2 input buffers). Try adding cin.ignore after line 35
Last edited on
Also on line 51, it would be better if you change the variable a to zero and <= numjudges to < numjudges.
Last edited on
that's my almost final code, now I just have to make it look pretty, I'm supposed to put the
Winner is
name
score

in the middle of the screen and make a decorative box around it, I was given notes on
#include<iomanip> and I should be able to work with 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
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
92
93
94
95
96
97
98
99
#include<iostream>
#include<iomanip>
#include<cmath>


using namespace std;

int main (){
     static double score;
     static double epsilon(0.000001);
     static double allowedShift(10.0);
     unsigned short numJudges, numCeleb;
     float addScore, finalScore = 0, min, max, max2=0;
     string name, winner;
    do {
     cout << "\n\n\tEnter number of judges: ";
     cin >> numJudges;
     if (numJudges < 3 || numJudges > 6)
        cout << "\n\n\tERROR! Number of judges must be between 3 and 6" << endl;
    }
     while (numJudges < 3 || numJudges > 6);


    do {
     cout << "\n\n\tEnter number of paricipants: ";
     cin >> numCeleb;
     if (numCeleb < 10 || numCeleb > 15)
        cout << "\n\n\tERROR! Number of participants must be between 10 and 15" << endl;
    }
    while (numCeleb < 10 || numCeleb > 15);


     for (unsigned short i = 1; i <= numCeleb; i++){


bool test;
do {
        test = true;
            cout << "\n\n\tEnter name of celebrity " << i << " : ";
cin.ignore();
getline (cin >> ws, name);
            for (unsigned short n = 0; n < name.length(); n++){
                if(!(isdigit(name.at(n)) || isalpha(name.at(n))))
                test = false;
            }
                if (!test)
                cout << "\n\n\tERROR! Name can consist of letters and numbers only.\n";
}
while (!test);

            addScore = 0,
            min = 6,
            max = 0;

            for (unsigned short a = 1; a <= numJudges; a++){
                do {
                    cout << "\n\n\tEnter score from judge number " << a << " : ";
                    cin >> score;
                    if (score < 0 || score > 6)
                        cout << "\n\n\tERROR! Score must be between 0 and 6." << endl;

                   else if  (fabs(trunc(score * allowedShift) - score * allowedShift) > epsilon)
                        cout << "\n\n\tERROR! Score can have only one deciamal place" << endl;
                }
                while  (fabs(trunc(score * allowedShift) - score * allowedShift) > epsilon);

                    if (score > max)
                        max = score;

                    if (score < min)
                        min = score;

                        addScore+=score;
                }


                    finalScore = (addScore - max - min) / (numJudges - 2);

                    if (i==1){

                        max2=finalScore;
                        winner=name;
                    }

                    else if(max2<finalScore){

                             max2=finalScore;
                             winner=name;
                           }



                    cout << "\n\n\tFinal score is " << finalScore << endl;
         }

          cout << setw(51) << "And the winner is\n\n" << setw((80+winner.length())/2) << winner << "\n\n"<< setw(42) << max2 << endl;

           return 0;
    }


I'll do it tomorrow, I have had enought..
It is not. cpp.sh has some problem with output flushing in interactive mode. This makes prompt appear after name is entered. It is cpp.sh bug

std>>ws is enough. In fact you should never use ignore() (at least in this way) to get rid of newline.
Last edited on
True, cpp.sh does have some hiccups I will admit that(like taking a really long time to compile).
Last edited on
thanks I removed cin.ignore()
Topic archived. No new replies allowed.
Pages: 12