C++ check a type char variable against letters of the alphabet

In summary, The conversation involves someone getting an error while trying to compile their code and asking for help. They are trying to check a type char variable against letters of the alphabet in an if function and are not sure what they are doing wrong. They also have some prototype declarations and a switch statement for different cases. However, the program is not finished yet and they need to fix the problem before proceeding. Someone suggests using single quotes for characters in C++ and another person agrees.
  • #1
Townsend
232
0
I am getting an error I cannot seem to figure out. whenever I try to compile it, it says c is undeclared. I want to check a type char variable against letters of the alphabet in an if function. Maybe someone could explain what I am doing wrong.
Code:
#include <iostream>
#include <cctype>


//Prototype Declaration
void caseOne (char& vehicle);

//Prototype Declaration
void caseTwo (char& vehicle);

using namespace std;

int main()
{
    char vehicle;                                               //vehicle type
    int hourEnter=0;                                            //Hour entered lot
    int minuteEnter=0;                                          //minute entered lot
    int hourLeft=0;                                             //Hour left lot
    int minuteLeft=0;                                           //minute left lot
    int r=0;                                                    //dummy variable
    
    cout<<endl<<endl<<endl
    <<"Please enter the type of "
    <<"vehicle you drive. "      
    <<endl<<endl                                                                                     
    <<"Please enter C for car, "
    <<"B for bus and T for truck."
    <<endl<<endl;     
    
    cin>>vehicle;                                             
    
    int islower(vehicle);
     
    if (islower=1)
    {   
        r=1;
    }
    else
        r=2;
        
    switch (r)
           {
                  case 1: caseOne (vehicle);
                          break;
                  
                  case 2: caseTwo (vehicle);
                          break;
                          
                  default: cout<<endl<<endl
                              <<"Input error please start over.";
                          break; 
           }
        
    cout<<endl<<endl
    <<"Please enter the hour, between 0 and 23,"
    <<" that you entered the lot."
    <<endl<<endl;  
    
    cin>>hourEnter;                                           //Prompt user for hour                                                                         
    
    cout<<endl<<endl
    <<"Please enter the minute, between 0 and 59, "
    <<"that you entered the lot."
    <<endl<<endl;
    
    cin>>minuteEnter;
        
    cout<<endl<<endl
    <<"Please enter the hour, between 0 "
    <<"and 23, that you left the lot."
    <<endl<<endl;
    
    cin>>hourLeft;
    
    cout<<endl<<endl
    <<"Please the minute, between 0 and 59, "
    <<"that you left the lot."
    <<endl<<endl;
    
    cin>>minuteLeft;
    
    cout<<endl<<endl                                        //program output
    <<"\tPARKING LOT CHARGE"
    <<endl<<endl
    <<"Type of vehicle:          "
    <<vehicle
    <<"\t\tTIME-IN "
    <<endl
    <<"                                              "
    <<"\t\t       "
    <<hourEnter<<minuteEnter
    <<endl<<endl<<endl;
    
 
    return 0;
}  //main




/*================================caseOne=======================================
     Determines type of vehicle from user input
     Pre veh contains vehicle type to be determined
     post nothing
*/

void caseOne (char& vehicle)
{
     char x=c;
     char y=b;
     char z=t;
     if (vehicle==x)
     {
                vehicle=Car;
     }
     else if (vehicle==y);
     {
          vehicle=Bus;
     }
     else if (vehicle==z)
     {
          vehicle=Truck;
     }
     
     return;
}


/*================================caseTwo=======================================
     Determines type of vehicle from user input
     Pre veh contains vehicle type to be determined
     post nothing
*/
void caseTwo (char$ vehicle)
{
     if (vehicle==C)
     {
              vehicle=Car;
     }
     else if (vehicle==B)
     {
          vehicle=Bus;
     }
     else if (vehicle==T)
     {   
         vehicle=Truck;
     }
           return;
}

Of course the program is not finished yet but I need to fix this problem before I go any futher.

Thanks in advance

Townsend
 
Physics news on Phys.org
  • #2
Don't you need to put characters within single quotes in C++?

Like this: char x = 'c';

Otherwise, the compiler will think c is another variable.
 
  • #3
letters of the alphabet have to be in single quotes

ie: char x = 'c';

edit: oops, sorry nylex, didn't see you posted it already
 

1. How can I check if a C++ char variable is a letter of the alphabet?

To check if a C++ char variable is a letter of the alphabet, you can use the isalpha() function from the cctype header. This function returns a non-zero value if the character is a letter and a zero value if it is not.

2. Can I use operators to check if a char variable is a letter of the alphabet?

Yes, you can use comparison operators such as == and != to compare a char variable with individual letters of the alphabet. However, this method may not be as efficient as using the isalpha() function.

3. How do I check if a char variable is a lowercase letter?

You can use the islower() function from the cctype header to check if a char variable is a lowercase letter. This function returns a non-zero value if the character is lowercase and a zero value if it is not.

4. How can I check if a char variable is an uppercase letter?

The isupper() function from the cctype header can be used to check if a char variable is an uppercase letter. This function returns a non-zero value if the character is uppercase and a zero value if it is not.

5. Is there a way to check if a char variable is a letter without using any functions?

Yes, you can manually check if a char variable is a letter by comparing its ASCII value to the ASCII values of uppercase and lowercase letters. For example, if the ASCII value of the char variable is between 65 and 90, it is an uppercase letter, and if it is between 97 and 122, it is a lowercase letter.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
3
Views
754
  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
1K
  • Programming and Computer Science
Replies
12
Views
1K
Replies
10
Views
958
  • Programming and Computer Science
Replies
5
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
24
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
8
Views
841
  • Programming and Computer Science
2
Replies
66
Views
4K
Back
Top