I had a homework assignment to write a program that will determine if a character entered is a digit. If it is a digit, return an integer. Determine if the character is a character, if it is, change it's case. I wrote the program below and put in a do while loop to make it loop until th e user decides to exit. As klong as I enter integers or characters, all is well, but if I enter a number with a decimal, the program loops continuously. I have read, googled and searched, but I am missing my error.
/*
This program utilizes user input of a character or digit and then checks to see if a
character or digit was entered. If a digit was entered, the program outputs that a
a digit was entered. If a character was entered, the program changes the case of the
character and outputs that character to the screen. If neither a character or digit
was entered, thay is output to the screen.
*/
#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;
int main()
{
char ch; //declare identifier ch
int x=1; //declare integer x
do //user controlled do loop as long as x=1, loop
{ //open brace for do loop
cout << "Please enter a character or digit."; //query user for input
cin >> ch; //assign user input to identifier ch
if (ch>='0'&&ch<='9') //check ch to see if it is number
{
static_cast <int> (ch);
cout << "Value entered is a digit: " << ch <<endl; //output if ch is a number
}
else if (ch>='A'&&ch<='Z') //check if ch is a uppercase character
{
ch=ch+32; //change uppercase character to lowercase
cout << "Value entered is a character: " << ch <<endl; //output after changing uppercase to lowercase letter
}
else if (ch>='a'&&ch<='z') //check if ch is lowercase letter
{
ch=ch-32; //change lowercase letter to uppercase letter
cout << "Value entered is a character: " << ch <<endl; //output after changing lowercase letter to uppercase
}
else
{
cout << "Value entered is not a character or a digit." <<endl; //output that input is not character or digit
}
cout <<endl;
cout << "Press 1 to continue or 0 to exit. " <<endl;
cin >> x;
} //close brace for do loop
while (x==1); //user loop control
Didn't read your code, but I think I know what the problem is.
"8.1" is not a character, it is a string. This should break your program, and it does for me. Try doing something like if(cin >> ch) to try avoiding that issue. This should prevent you from using anything that is not a character. Then again, I've never tried.