I am new to programming, and I am currently trying to figure my way around C++. I am working on a program that will display basic information about employees to the users, and I want the program to end when the users entered a char. However, while working through it, I encountered a problem. When I enter the char (x), it somehow looped from 1-120 (as you seen in the counter below), but when I entered its ASCII number (120), it ended the program as intended. Can anyone explain it to me and what are the possible solutions?
#include <iomanip>
#include <iostream>
using namespace std;
class Employee
{
public:
int Age;
string Name;
int ID;
void information()
{
cout << "Name: " << Name << endl;
cout << "Age: " << Age << endl;
cout << "ID: " << setfill('0') << setw(3) << ID << endl;
}
Employee(string name, int age, int id)
{
Name = name;
Age = age;
ID = id;
}
};
void createProfile()
{
int age;
int id;
string name;
cout << "Enter the Employee Name: ";
cin >> name;
cout << "Enter the Employee Age: ";
cin >> age;
cout << "Enter the Employee ID: ";
cin >> id;
}
int main()
{
int num, counter;
const char stop = 'x';
cout << "Choose an Employee " << endl;
cout << "001 Jack" << endl;
cout << "002 Susan" << endl;
cout << "003 to create profile" << endl;
cout << "Press x to Exit";
for (num = 0; num != stop; num++)
{
cout << "\n\nEnter the Employee ID: ";
cin >> num;
counter += 1;
if (num == 001)
{
Employee jack = Employee("Jack", 25, 001);
jack.information();
}
else if (num == 002)
{
Employee jack = Employee("Susan", 23, 002);
jack.information();
}
else if (num == 003)
{
createProfile();
}
else
{
cout << "Input invalid";
}
}
cout << "\n" << counter;
}