Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Is there a way i could end a loop with a char in c++?

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;
}

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Yes, of course. In C/C++, char is a type that can be represented either by its character value ('A', for example) or its numeric value (65). However, a digit’s numeric value does not equal to itself.

'1' == 49

120 is the ASCII value of lower case x and since you loop with an int, it is logical that it will end up working in ways you do not expect it to work. You can change your

for (num = 0; num != stop; num++)

to

for (num = 0; ch != stop; num++)

and of course ch should be declared as char before your loop. Also, make sure that you replace cin >> num; with cin >> ch;

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading