C++ checking errors

Hi all, i have this average grade code.
My input is only Letter (A/B/C/D/F), is it possible that anyone can help me input lines that shows error message if the user input other letter beside the a/b/c/d/f ?

appreciate you guys help !

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

using namespace std;

int main()
{
	char name [100];
    int n, ta, tb, tc, td, tf;
    ta=tb=tc=td=tf=0;

    char array[100];
	string s;

	cout <<"This is an Average Point Calculator. Just put in your 1-5 modules grade"<< endl;
	cout<<"What is your name?"<< endl;
	cin.getline (name,100);
	cout<<"Hello, " << name << "."<< endl;
    cout << "How many modules total ? ";
    cin >> n;

    for(int i=1; i<=n; i++)
    {
        do
        {
            cout << "Enter Module" << i << "'s grade in LETTER : ";
            cin >> s;
			if(s.length()>1)
				break;
			else array[i] = s[0];
			if(isalpha(array[i]))
            {
                if(tolower(array[i])=='a'){ ta++; break; }

                if(tolower(array[i])=='b'){ tb++; break; }

                if(tolower(array[i])=='c'){ tc++; break; }

                if(tolower(array[i])=='d'){ td++; break; }

                if(tolower(array[i])=='f') break;
            }
        } while(1);
    }
    cout << "Average grade of " << n << " subjects is : " << (ta*4+tb*3+tc*2+td)*1.0/(ta+tb+tc+td+tf);
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <ctype.h>
#include <string>
using namespace std;

int main() {
    //initialize to null to the while loop will run
    char ans = '\0';
    //make a condition that accepts a to f then reverse the it
    //using ! operator
    while( !((ans>='a' && ans<='d') || ans=='f') ) {
        cout << "Enter grade: ";
        cin >> ans;
        //make the input lower case so the condition will accept uppercase input
        ans = tolower(ans);
    }

    return 0;
}


hope it helps..
hi blackcoder41, thanks for the reply. as i am new to c++, can i know whether the codes which lines do i input into my int main ? :)
Topic archived. No new replies allowed.