Question: Incrementing Program

First things first, here's the code:

1
2
3
4
5
6
7
8
9
#include "std_lib_facilities.h"

int main(){
	int a = 1;
	cout<<"a == "<<a<<'\n';
	while (a<26){
		cout<<"a"<<" == "<<++a<<'\n';
	}
}


What I want it to do is assign 'b' to 2, c to '3', and so forth. My problem is that as a beginner I don't understand how to (if there even is a way to) increment characters. As in the alphabet a-z, 1-26. I am teaching myself, so this is a slow and tedious process.

Help is much appreciated.

-Incliine
If I have correctly understood you all what you need is to make some minor changes

1
2
3
4
5
6
7
8
9
#include "std_lib_facilities.h"

int main(){
	int a = 0;
	cout<<"a == "<<a<<'\n';
	while (a<26){
		cout<<"a"<<" == "<< char( a++ + 'a' ) <<'\n';
	}
}


Or you can write even simpler

1
2
3
4
5
6
7
#include "std_lib_facilities.h"

int main(){
	for ( char c = 'a'; c <= 'z'; c++ ) std::cout << c;
	cout << '\n';
	
}
I dont know if you know what arrays are or what the for loop is but I will explain my code to you and If you have questions just ask them.

So first I make an array of chars to represent the different letters in the alphabet. Then I use the for loop to loop through each letter in my array and get the corresponding number to the letter.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include<iostream>
using namespace std;

int main(){
    //make an array of all the letters in the alphabet.
	char letters[26]={'a','b','c','d','e','f','g','h','i','j','k','l',
                                 'm','n','o','p','q','r','s','t','u','v','w','x','y','z'};

   //loop through the array of letters and get the corresponding number.
	for(int i = 0; i<26; i++){
           cout << letters[i] << " is equal to " << i + 1 << endl;
	}

}


Output


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
a is equal to 1
b is equal to 2
c is equal to 3
d is equal to 4
e is equal to 5
f is equal to 6
g is equal to 7
h is equal to 8
i is equal to 9
j is equal to 10
k is equal to 11
l is equal to 12
m is equal to 13
n is equal to 14
o is equal to 15
p is equal to 16
q is equal to 17
r is equal to 18
s is equal to 19
t is equal to 20
u is equal to 21
v is equal to 22
w is equal to 23
x is equal to 24
y is equal to 25
z is equal to 26

Process returned 0 (0x0)   execution time : 0.013 s
Press any key to continue.

WOW! Great help! I'm trying to only use the tools I currently understand, but arrays look cool!

THANKS:
@vlad from moscow
@Hertz


-Incline
Topic archived. No new replies allowed.