Detect cin entry of datatype char

How to make code instructions work when the input is of datatype char?

Page 1 of 1

3 Replies - 1942 Views - Last Post: 14 May 2008 - 01:36 AM Rate Topic: -----

#1 skater_00  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 12
  • View blog
  • Posts: 256
  • Joined: 30-April 08

Detect cin entry of datatype char

Post icon  Posted 13 May 2008 - 05:17 PM

I am learning C++ and still going over all of the basics, but now I am having a problem with one of the exercises in a C++ book. Well, actually I already finished the exercise. The executable program is observed bug-free, but I am adding a few more lines of code to protect several stuff. You know: invalid entries etc.

The last thing I would like to add is a protection for entries of datatype char. (Yes, I know that a number can also be stored in a char variable, but I mean letters and other characters/symbols.) Numbers between 0 and 100 (including both) result in a lettergrade from A to F (A - B - C - D - F), this was the actual exercise. I added something when you enter a negative value or a value higher than 100, you get an error message. Now I want to do exactly the same thing when the user enters a letter or another character/symbol. A user entering a letter or another character/symbol is an invalid entry and needs the appropriate error message.

I would like to know how I can detect if the cin << entry is of datatype char (check if the number is NOT an integer) and how I can execute certain lines of code properly when this is the case.

I've tried reacting on char's as input too before, but the result wasn't that great. It continued looping all the time, non-stop.

Here is my code to what I currently have working:

// ****************//
// GRADE VALUES //
// ****************//

// Program Description: The user enters a numeric grade between 0 and 100 (including both) which are percentages,
// and the corresponsing lettergrade (A - B - C - D - F) is printed to the screen. The user cannot enter negative values
// or values higher than 100. Nor can the user enter letters or other characters/symbols. Well, the user can do all of it,
// but will receive an appropriate error message when this happens.
//
// Grade values:
//
// 0 - 60 => F
// 61 - 70 => D
// 71 - 80 => C
// 81 - 90 => B
// 91 - 100 => A

// Include input & output streaming
#include <iostream>

// Use the std namespace
using namespace std;

int main() // Start of main() function
{
	// Set application title
	system("TITLE Grade values");

	// Declare and initialize variables to 0
	short int nNumericGrade(0);
	char cLetterGrade(0);

	while (true) { // Start the looping
		cout << "Enter numeric grade (0 - 100): ";
		
		// Ask the user for numeric grade input
		cin >> nNumericGrade;

		if (nNumericGrade >= 0 && nNumericGrade <= 100) { // Allows me to prevent the user from entering negative values or numbers higher than 100
			if (nNumericGrade >= 0 && nNumericGrade <= 60) cLetterGrade = 'F'; // The letter grade for a score between 0 and 60 is F and assigned to the char variable
			if (nNumericGrade > 60 && nNumericGrade <= 70) cLetterGrade = 'D'; // The letter grade for a score between 61 and 70 is D and assigned to the char variable
			if (nNumericGrade > 70 && nNumericGrade <= 80) cLetterGrade = 'C'; // The letter grade for a score between 71 and 80 is C and assigned to the char variable
			if (nNumericGrade > 80 && nNumericGrade <= 90) cLetterGrade = 'B'; // The letter grade for a score between 81 and 90 is B and assigned to the char variable
			if (nNumericGrade > 90 && nNumericGrade <= 100) cLetterGrade = 'A'; // The letter grade for a score between 91 and 100 is A and assigned to the char variable

			// Print the corresponding letter grade to the screen
			cout << endl << "Corresponding letter grade: " << cLetterGrade << endl << "-----------------------------------" << endl;
		} else
			// Print an error message to the screen when an invalid entry has occured
			cout << endl << "Corresponding letter grade: INVALID ENTRY! Please try again." << endl << "-----------------------------------" << endl;
	} // End the looping

	return 0; // Application exits with code 0
} // End of main() function

This post has been edited by skater_00: 13 May 2008 - 05:23 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Detect cin entry of datatype char

#2 pertheusual  Icon User is offline

  • D.I.C Head

Reputation: 10
  • View blog
  • Posts: 245
  • Joined: 26-January 08

Re: Detect cin entry of datatype char

Posted 13 May 2008 - 11:08 PM

this is a situation where you use the fact that a 'char' is simply a data type. the computer has no way of knowing that it is a character or a number, it is just bits.
they are stored using the ascii codes. http://www.asciitable.com/

as you can see in the table, the letters are in alphabetical order and numeric order. this means that you can use a simple comparison of the characters using greater than or less than, just like numbers.

so, you can do this
if (character >= 'A' && character <= 'F') //then it is a correct grade



you can also use this principal to make number conversion to letters easily

if (nNumericGrade >= 0 && nNumericGrade <= 100) {
            if (nNumericGrade <= 60) nNumericGrade = 50;//skip e, since it's not a grade
            nNumericGrade -= 100;
            cLetterGrade = 'A'-nNumericGrade/10;

/*instead of this
            if (nNumericGrade >= 0 && nNumericGrade <= 60) cLetterGrade = 'F';
            if (nNumericGrade > 60 && nNumericGrade <= 70) cLetterGrade = 'D'; 
            if (nNumericGrade > 70 && nNumericGrade <= 80) cLetterGrade = 'C';
            if (nNumericGrade > 80 && nNumericGrade <= 90) cLetterGrade = 'B';
            if (nNumericGrade > 90 && nNumericGrade <= 100) cLetterGrade = 'A';
*/
            cout << endl << "Corresponding letter grade: " << cLetterGrade << endl << "-----------------------------------" << endl;
} else
            cout << endl << "Corresponding letter grade: INVALID ENTRY! Please try again." << endl << "-----------------------------------" << endl;
} // End the looping




i haven't tested that code, but i think it works. either way, you get the idea.

per

ps, all lower case because my keymap has decided my shift keys don't work. guess it's time to reboot...
Was This Post Helpful? 0
  • +
  • -

#3 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3089
  • View blog
  • Posts: 19,137
  • Joined: 14-September 07

Re: Detect cin entry of datatype char

Posted 14 May 2008 - 01:29 AM

I think he meant to have a user enter either and the program able to accept either a char or a int as input. Not converting an int entry or vice versa after input
Was This Post Helpful? 0
  • +
  • -

#4 no2pencil  Icon User is offline

  • Head MFIC
  • member icon

Reputation: 5072
  • View blog
  • Posts: 26,471
  • Joined: 10-May 07

Re: Detect cin entry of datatype char

Posted 14 May 2008 - 01:36 AM

If you want to detect the value of input to your program, good 'ol isalpha to the rescue!

/* isalpha example */
#include <stdio.h>
#include <ctype.h>
int main () {
  int i=0;
  char str[]="C++";
  while (str[i]) {
    if (isalpha(str[i])) printf ("character %c is alphabetic\n",str[i]);
    else printf ("character %c is not alphabetic\n",str[i]);
    i++;
  }
  return 0;
}


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1