Hey all,

I have this for loop and it keeps printing a '}' at the end of my alphabet and I can't figure out why.

Please help me figure out how to get rid of it! :)

Thanks!

- EngneerNitemare

// PREPROCESSOR DIRECTIVES
#include <iostream>
using namespace std ;

// main FUNCTION BEGIN PROGRAM EXECUTION
int main()
{
	// ALPHABET ARRAY DECLARATIONS
	int count[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' } ;
	count[0] = 'a' ; // 'a' Character = '97'
	count[1] = 'b' ; // 'b' Character = '98'
	count[2] = 'c' ; // 'c' Character = '99'
	count[3] = 'd' ; // 'd' Character = '100'
	count[4] = 'e' ; // 'e' Character = '101'
	count[5] = 'f' ; // 'f' Character = '102'
	count[6] = 'g' ; // 'g' Character = '103'
	count[7] = 'h' ; // 'h' Character = '104'
	count[8] = 'i' ; // 'i' Character = '105'
	count[9] = 'j' ; // 'j' Character = '106'
	count[10] = 'k' ; // 'k' Character = '107'
	count[11] = 'l' ; // 'l' Character = '108'
	count[12] = 'm' ; // 'm' Character = '109'
	count[13] = 'n' ; // 'n' Character = '110'
	count[14] = 'o' ; // 'o' Character = '111'
	count[15] = 'p' ; // 'p' Character = '112'
	count[16] = 'q' ; // 'q' Character = '113'
	count[17] = 'r' ; // 'r' Character = '114'
	count[18] = 's' ; // 's' Character = '115'
	count[19] = 't' ; // 't' Character = '116'
	count[20] = 'u' ; // 'u' Character = '117'
	count[21] = 'v' ; // 'v' Character = '118'
	count[22] = 'w' ; // 'w' Character = '119'
	count[23] = 'x' ; // 'x' Character = '120'
	count[24] = 'y' ; // 'y' Character = '121'
	count[25] = 'z' ; // 'z' Character = '122'
	/*count[26] = ' ' ;*/ // ' ' Character = '32'

	

	
	// CHARACTER DISPLAY for LOOP
	int i ; // Counting Variable 'i' Declaration
	char a = 'a' ;
	for( i = 0 ; i < count[i] ; i++ )
	{
		
		cout << a++ << " " ; 

	} // ***END CHARACTER for DISPLAY LOOP***

	cout << endl ; // Print blank line to screen
	cout << endl ; // Print blank line to screen

	return 0 ; // Indicate sucessful termination

} // ***END main FUNCTION***

Recommended Answers

All 2 Replies

1) delete lines 10-36 because they are redundent. The array counts was already populated with the correct values in line 9 and there is no point doing it all over again.


2) line 44 -- that's almost an infinite loop. On every loop iteration the value in counts changes. When i is 0, counts[0] is 97. When i = 1, counts[1] = 98, ... when i is 25 counts[25] = 122, from there on the counts is undefined because counts only has 26 elements.

Compare your code with just this :

//print 'a' through 'z'
for(char ch = 'a' ; ch <= 'z'; ch++){
   cout << ch << endl;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.