user3687358
user3687358

Reputation: 43

C++ for loop with char type

>The character 'b' is char('a'+1),'c' is char('a'+2),etc. Use a loop to write out a table of characters with their corresponding integer values.
I cannot finish this exercise because of this error.
error: lvalue required as increment operand

  for(char a='a'; a<24; ++a)
    {
    cout<<char('a'++);
    }

Upvotes: 2

Views: 38070

Answers (6)

Aviel AKUETE
Aviel AKUETE

Reputation: 1

hey through troubleshooting i obtained a sample that worked , i have yet to perfectly understand how my code works but as the solutions that were proposed to me here seemed too technical for my level i figured that i should publish mine

#include"header files . h"//i use the libraries given in the book
int main () {
    char a ='a';
    int i = 0 ;
    while (i <= 25)//for (i = 0 ; i <= 25 ; i++)
//for those who used for
{
        cout << a << '\t' << 'a' + i << endl;
        a += 1; // augments the character value of a to b ... then to z

        i++; // augments the value of i allowing us thus to make the augmentation,if you are using the for structure do not put I in your code
        }
    }

Upvotes: 0

What Does This Do
What Does This Do

Reputation: 11

I know this has been closed for a while but since the exercise was about while loops and not for loops, I thought I would offer my solution. I'm just going through the book myself and someone in the future might stumble over this.

int i = 0;
char n = 'a';   // this will list the alphabet
int conv = 0;   
conv = n;       // this converts the alphabet into integer

while (i < 26) {        // starts from 0 and goes to 25
    cout << char(n + i) << '\t' << conv + i << '\n';
    ++i;
}

Upvotes: 1

haccks
haccks

Reputation: 106092

The loop body will never execute with the controlling expression a < 24 because you have initialized variable a with character a and all printable characters are not less than ASCII value 32.
Try this:

for(char a='a'; a < 'a' + 24; ++a)
{
    cout << a;
}

Upvotes: 4

Joseph Mansfield
Joseph Mansfield

Reputation: 110698

I think you would be less confused if you named your variable letter instead of a, because it only represents the letter 'a' at the very beginning.

for(char letter='a'; letter<24; ++letter)
{
  cout<<char('a'++);
}

I'm going to assume you actually want to print out the entire alphabet, not just the first 24 letters.

It looks from here like you tried to do a mix of two possible approaches. In the first approach, you increment a char from a to z with each iteration of the for loop and print it out each time. In the second approach, you increment some offset from 0 to 25 and print out 'a' + offset.

You mix these two approaches up in the first line. You're starting the loop with letter set to 'a', which you do not know the numerical value of. You then compare letter to see if it is less than 24. Well in any ASCII-compatible character set, the character 'a' has value 97, so this condition will never pass.

You then misuse ++ on the cout line. The ++ operator attempts to modify its operand, yet 'a' is a literal and so cannot be modified. Have a look at what your assignment told you. You can do 'a' + 1 to get 'b', for example, so this assumes you have an offset (which you don't with your current approach).

So to repeat, you have two options. First: keep letter as a char starting at 'a' and fix the condition so that it checks if letter is less than or equal to the value of 'z' and then just print out letter. Second: change letter to offset and start it off at 0 and increment it while it is less than 26, and then print out 'a' + offset.

Note, however, that both of these approaches assume that the letters have consecutive values in the execution character set. This is not necessarily true.

Upvotes: 2

Jarod42
Jarod42

Reputation: 217810

You may use the following: (http://ideone.com/akwGhl)

#include <iostream>

int main()
{
    for (char c = 'a'; c <= 'z'; ++c) {
        std::cout << "letter " << c << " has value " << int(c) << std::endl;
    }
    return 0;
}

Upvotes: 0

Peter - Reinstate Monica
Peter - Reinstate Monica

Reputation: 16039

The ++ operator is a "hidden assignment" (the operand's value is changed by it). But you can only assign to variables, which 'a' is not.

Upvotes: 1

Related Questions