A beginners code




//This program takes input from user i.e. Grades A or B, then calculate the No. of A grades and give comments.

#include<iostream.h>
#include<stdlib.h>
#include<conio.h>
using namespace std;

main()
{

//Declaring and intializing the variables to be used.

char Grade;
int GradeCountA=0;
int GradeCountB=0;
int ClassStrength=10;
int CurrentStudent=0;

//Telling the user to enter grades, either A or B.

cout<<"Please note that you should enter Grade 'A' or 'B'."<<endl<<endl;

//Using while loop to take the inputs from user of Grades upto the number of students i.e. ClassStrength.
while (ClassStrength>0)
{


//The following code is to get the number of student whome grade is being entered.
CurrentStudent++;

//Prompting the user to enter the Grade.

cout<<endl<<"Please enter the Grade of Student "<<CurrentStudent<<endl;
cin>>Grade;

//Checking with if statment, that A is entered and adding to number of Grade A.
//User may enter in small letters so, this is also taken in consideration by adding it with OR Operator.

if(Grade=='A'||Grade=='a')
{
GradeCountA++;
ClassStrength--;
}
//Checking with another nested if statment, that B is entered and adding to number of Grade B. Grade B is not the required Grade to check but for the sake of reusablity this statement is added.
//User may enter in small letters so, this is also taken in consideration by adding it with OR Operator.

else if(Grade=='B'||Grade=='b')
{
GradeCountB++;
ClassStrength--;
}
//Checking the user input and asking to type the correct grade.
else
{


cout<<endl<<"Please enter 'A' or 'B' only!"<<endl;

//Setting the CurrentStudent number to back as it was incremented before if statements. Its ommision will create an error if the user types 1 or 2 times wrong then the number of student whom grade is being entered would become wrong.

CurrentStudent--;
}
}

//Displaying the number of Grades.

cout<<"Total number of 'A' Grades in your class "<<GradeCountA<<endl;

//Checking with if Statement either the Number of 'A' Grades is less or equal to '2' and displaying comment.

if(GradeCountA<=2)
{
cout<<"Your class is poor!"<<endl;
}
//Checking with if Statement either the Number of 'A' Grades is less or equal to '7' and displaying comment.

else if(GradeCountA<=7&&GradeCountA>2)
{
cout<<"Your class is good!"<<endl;
}
//If the two nested if statements are skipped, then '8' or Above than '8' will remain, else statement is displaying result for the remaining one.

else
{
cout<<"Your class is brilliant!"<<endl;
}




getche();
}








All is good, but the problem I am facing is that,
If I enter a Character two or more times as grades e.g. "Az", The program executes two statements automatically. One for input 'A' (Please enter the Grade of Student 2), and other for 'z' (Please enter 'A' or 'B' only!).

How can get rid of it. I mean if user misstypes two characters as one input it should give again a single entry error.

Thnx.
You could put cin.get(); somewhere just after the while statement, but it won't quite work as I just tried it. If you replace
cin >> Grade; with
1
2
Grade=cin.get();
cin.ignore(256,'\n');

it should work.

http://www.cplusplus.com/reference/iostream/istream/get/
http://www.cplusplus.com/reference/iostream/istream/ignore/
Last edited on
Thanks again. Mr. HooklessFastner.

The 1 and 2 really helped.

Good for now. For a begginer.

But actually I was looking how could give an error on typing more then one character, not moving to the next process.
1. Declare a string called input, and include the <cstring> library.
2. replace cin >> Grade; with this:

1
2
3
4
5
6
7
8
9
10
11
 while (true) {
   cout << "Please enter 1 char: ";
   getline(cin, input);

   if (input.length() == 1) {
     Grade = input[0];
     break;
   }

   cout << "Invalid input length, please try again" << endl;
 }


http://www.cplusplus.com/forum/articles/6046/
Last edited on
Thank you very much for you help.

I'll try it.
Topic archived. No new replies allowed.