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

Click For Summary
SUMMARY

The forum discussion centers on a C++ programming issue where a user encounters an "undeclared" error for a character variable when checking it against letters of the alphabet. The user is attempting to implement a vehicle type selection based on user input. Key errors identified include the need to use single quotes for character literals (e.g., 'c' instead of c) and incorrect syntax in the if statement. The discussion highlights the importance of proper character declaration and syntax in C++ programming.

PREREQUISITES
  • Understanding of C++ syntax and variable declaration
  • Familiarity with character data types in C++
  • Knowledge of control structures such as if statements and switch cases
  • Experience with function prototypes and parameter passing in C++
NEXT STEPS
  • Review C++ character data types and their usage
  • Learn about proper syntax for if statements and switch cases in C++
  • Investigate function prototypes and parameter passing techniques in C++
  • Explore debugging techniques for common C++ compilation errors
USEFUL FOR

C++ developers, programming students, and anyone troubleshooting character handling and syntax errors in C++ applications.

Townsend
Messages
240
Reaction score
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
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.
 
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
 

Similar threads

  • · Replies 3 ·
Replies
3
Views
1K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 5 ·
Replies
5
Views
3K
Replies
12
Views
3K
  • · Replies 3 ·
Replies
3
Views
4K
  • · Replies 4 ·
Replies
4
Views
6K
  • · Replies 7 ·
Replies
7
Views
2K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
Replies
10
Views
2K