Loop with char

Jul 29, 2016 at 9:27pm
This assign, I cant understand how it work.

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:

a 97
b 98
...
z 122

I tried some, but nothing work. It just repeat b and c all the way 100 time. I don't think I learned how to make abc in loop unless I have to write out all myself, like a = 1, b = 2. Did i miss something in the book? I reread, no luck...

1
2
3
4
5
6
7
8
9
10
11
12
13
  #include "std_lib_facilities.h"

int main()

{
	int i = 0;
	char b = 'a'+1;
	char c = 'a'+2;
		while (i<100) {
		cout << b << '\t' << c << '\n';
		++i;
	}
}
Jul 29, 2016 at 9:32pm
Hi,
1
2
3
4
while (i<100) {
		cout << b << '\t' << c << '\n';
		++i;
	}


Should be :
1
2
3
4
while (i < 100) {
		cout << b + (char)i << '\t' << c + (char)i << '\n';
		++i;
	}
Jul 29, 2016 at 9:33pm
Does that help you? :)
Jul 29, 2016 at 9:49pm
It does work, but somehow the abc doesn't show. How do I make abc loop? Is it possible to make loop itself from a to z? Does compiler has it?
Jul 29, 2016 at 9:57pm
Hi,
> How do I make abc loop?

Try this :
1
2
3
4
while (i < 100) {
		cout << b + (char)i << '\t' << c + (char)i << '\t' << c + (char)i + 1 << '\n';
		++i;
	}
Jul 29, 2016 at 9:58pm
(Does that help you?) :)
Jul 29, 2016 at 10:01pm
My brain is cooking. I can't figure what it want me to do.

Does it want me to make compiler loop from A to Z and 97 to 122 itself?

Or does I have to make code << cout "a = 97 then cout << "b = 98?

Problem is, I don't know how to make the a to z without actually code all cout for it.

The loop thing is still new to me.
Jul 29, 2016 at 10:06pm
So in other words, you want to loop from 'a' to 'z' only?
Jul 29, 2016 at 10:27pm
I'm trying to follow this assignment

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:

1
2
3
4
a  97
b  98
...
z  122


Does it want me to make compiler loop from A to Z and 97 to 122 itself?
Jul 29, 2016 at 10:34pm
Really, this book is being unfair, the next page after this assignment actually talk about for. That is what I need to make alphabet thing work.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include "std_lib_facilities.h"

int main()

{
	

for (int i=0;i<26;++i) {

     char ch = 'A' + i;
     
	cout << ch <<'\n';
     char ch1 = 'a' + i;
     

}

 
}
Jul 29, 2016 at 10:40pm
You could simplify your loop a little:
1
2
3
4
for (char i = 'A'; i <= 'Z'; ++i) {
	cout << i << ' ';
}
cout << endl;
Last edited on Jul 29, 2016 at 10:42pm
Jul 30, 2016 at 3:13am
closed account (48T7M4Gy)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>

int main()
{
    //Upper case
    for (int i = 0; i < 26; ++i) {
        char ch = 'A' + i;
        std::cout << ch << '-' << i << '\n'; // <-- one way
    }
    
    // Lower case
    for (int i = 0; i < 26; ++i) {
        char ch = 'a' + i;
        std::cout << ch << " = ASCII code " << (int)ch << '\n'; // <-- another way
    }
}
Topic archived. No new replies allowed.