Iterative loops - I'm a bit stuck

Hello folks.

I am learning about loops, and I wrote this program (in Ubuntu, compiled with g++) to do the following:

Display the word "peppermint", and - one character at a time, starting at the first letter, cycle each letter in turn from a-z, before going back to it's original letter, then moving onto the next letter in the word. See example below:


peppermint
aeppermint
beppermint
ceppermint
deppermint

... through ...

zeppermint.


I can't get this to work. Only the first letter in the word cycles from a-z, then it just outputs blanks... any ideas where I am stumbling? I don't care if I have wrong headers, and I don't want to optimise the code, as I am still learning - if you could show me how to fix this in the format I have written it, I would be most humbly grateful. Thank you, Happy 2015 and May God bless you :)


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
#include <iostream>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
using namespace std;

int main(int argc, char *argv[])
{
int outcnt,incnt;
int alphacount=0;
char word[]="peppermint";
char alpha[]="abcdefghijklmnopqrstuvwxyz";

        for(outcnt=0;outcnt<=10;outcnt++)
        {
                for(incnt=0;incnt<=26;incnt++)
                {
                word[outcnt]=alpha[incnt];
                cout << word << endl;
                usleep(200000);

                // line below for debug only - remove from final
                cout << outcnt << " <---outer counter ~~ inner counter---> " << incnt << endl;


                }

        }

}
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
#include <string>

int main()
{
	std::string word = "peppermint";

	std::cout << word << std::endl;
	for (unsigned i = 0; i < 26; ++i)
	{
		char a = 'a';
		word.front() = a + i;
		std::cout << word << std::endl;
	}

	std::cin.ignore(); //keep window open
	return 0;
}
???

I don't understand, as you've left no explanation. That may as well be in Chinese.

Thanks.

[edit]

Also, in Ubuntu, I get:

1
2
3
4
l.cpp: In function ‘int main()’:
l.cpp:12:8: error: ‘std::string’ has no member named ‘front’
   word.front() = a + i;
        ^
Last edited on
I felt the code was self explanatory.

I just iterate through a loop for 26 iterations. 26 is the number of letters in the alphabet.


std::string has a method called front which returns a reference to the first character in the string. This allows us to easily modify the fist character in a string.
word.front()

I assign a + i to the first character in the string. On the first iteration this is equal to a + 0 so a is unchanged, and aeppermint is printed. On the second iteration it is equal to a + 1 so a becomes b, and beppermint is printed . So on and so forth.
The std::string::front is part of the c++11 standard if you compiler doesn't support c++11 then it will not have the front method.
http://www.cplusplus.com/reference/string/string/front/

you can change word.front() to word[0]

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
#include <string>

int main()
{
	std::string word = "peppermint";

	std::cout << word << std::endl;
	for (unsigned i = 0; i < 26; ++i)
	{
		char a = 'a';
		word[0] = a + i;
		std::cout << word << std::endl;
	}

	std::cin.ignore(); //keep window open
	return 0;
}
It was mostly self-explanatory, except this "front" method doesn't exist in my MingW + Netbeans OR my Ubuntu system - I compiled on both, and that's all I use. I even put "using namespace std;" and removed "std::" from each method... nope. No go.
I appreciate your help very much. Did you see this part of my OP?

"if you could show me how to fix this in the format I have written it"

The syntax may be wrong, and you may have better and leaner ways of doing this, but this is the manner in which I am learning - I'm taking it one step at a time, and I am used to how I have written it. Your code now works, using "word[0]" - I still need to learn in the way written, however.

Many thanks though :)
Last edited on
On line 12 of the code in the OP, alpha is defined as an array of char with 27 elements. The last of these elements is the NUL character which signifies the end of a string at index 26.

So, on line 16 when the body of the for loop is allowed to execute when incnt is 26, you assign the NUL character to word[outcnt]. At the end of the first set of iterations for the inner for loop, word[0] is the NUL character which indicates the end of a string. Line 19 then feeds an empty string to cout whenever it is reached subsequently.
Last edited on
@cire

Thank you - your simple diagnosis fixed it :)
Topic archived. No new replies allowed.