dimanche 9 juillet 2017

How do a compare a char to a number? C++

in my current program I have the user input a number that gets stored in a char list. After that I go through the list and try to assign that number to an int variable. That is where I run into an error. This is an excerpt of my code.

        if (list1[j]=='1');
            z=1;
        if (list1[j]=='2');
            z=2;
        if (list1[j]=='3');
            z=3;
        if (list1[j]=='4');
            z=4;

The issue is that z always becomes 4 even if list[j]=3. I know I am making a mistake my comparisons but I've been unable to locate it. I would appreciate your help.

This is how I define the list:

    char list1[32];

And this is how I fill it up:

    for(int i=0;i<(2*c);i+=2)
{
    cin>>list1[i]>>list1[i+1];
}

Aucun commentaire:

Enregistrer un commentaire