Looping Issues

Good Afternoon,

I am currently trying to write a console application that allows a user to input a character such as 'e'. It will go through a for-loop until it reaches 'z'. That loop will output all the values up to 'z'. Although my issue seems to be that it goes past 'z' regardless of the input. Any help would be greatly appreciated. The issue seems to be in the for-loop.

The swap() function i created works fine, so i commented it out.

Header file im using from stroustrup
http://www.stroustrup.com/Programming/PPP2code/std_lib_facilities.h

MY CODE
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include "std_lib_facilities.h";

// swap - swaps a given char value by + 1
// ex: char_value'a' temporarily becomes 'b' and 
// char_value 'd' temporarily becomes 'e'.
// the program exits by printing the swapped value, 
// but never changes the value passed.
void swap(char char_value)
{
	// ensure we don't swap past 'z' 
	// we don't want any input past z 
	if (char(char_value) == 'z') {} // don't do a swap
	else {
		// create a temp variable that contains the swapped value. 
		char temp = char(char_value + 1);

		// output the new temp char and int value. 
		cout << "SWAPED VALUE:\n";
		cout << char(temp) << '\t' << int(temp) << '\n';
	}
}

int main() try {
	// integer value of the character 'z' 
	constexpr int char_z = 122;
	// integer value of alphabetic letter we start with
	int starting_letter = 97; // 97 = a , 98 = b, ..., 122 = z.
	
	// Welcome and prompt the user for input
	cout << "Please enter a character value. The program does not\n"
		 << "accept integers, doubles, ect. ONLY character values.\n"
		 << "ex: a, b, e, h, d\n"; 
	cout << "Input a vlaue please: "; // prompt 

	//variable used for gathering user input
	char users_letter;
	// input a char, any integer values will break the loop
	// fix?: when we input 98 it does a char check, '9' then '8'. 
	// not a big issue just slightly ugly. 
	while (cin >> users_letter) {
		// check to make sure our input is alphabetical and valid.
		if (!isalpha(users_letter) || users_letter > 'z' || users_letter < 'a') { // error case 
			cout << "Input is non alphabetical < " << users_letter << " >\n";
			cout << "Input another value please: "; // ugly but usable.
		}
		else { // create our starting letter and move on to loop to 'z'.
			starting_letter = int(users_letter);
			break;
		}
	}
	
	// our character counter, to ensure we increment 
	// the starting letter properly each time. 
	int char_type = 0; 

	// ex: input = a, input becomes 98, i = 98.. ect
	for (int i = int(starting_letter); i < (char_z + 1); ++i) {
		cout << "ORIGONAL VALUE:\n";
		cout << char(i + char_type) << '\t' << int(i + char_type) << '\n';
		//swap(char(char(i) + char_type)); // optional function, comment out if you don't want it. 
		++char_type;
	}

	// await user input before closing program 
	keep_window_open("~");

}
catch (runtime_error e) {
	cout << e.what() << endl;
	keep_window_open("~");
}


Thanks for any help :)
Last edited on
SOLVED
The char_type variable I had for incrementing
wasn't actually doing what I expected it to do.
I took it out and everything works fine now. I cout'ed i to see
that i + char_type was allowing it to go past z. Works good now.

1
2
3
4
5
6
// ex: input = a, input becomes 98, i = 98.. ect
for (int i = int(starting_letter); i < (char_z + 1); i++) { 
	cout << "ORIGONAL VALUE:\n";
	cout << char(i) << '\t' << i << '\n';
	swap(char(i)); // optional function, comment out if you don't want it. 
}
Last edited on
Registered users can post here. Sign in or register to post.