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:
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 :)
#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 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.
#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.
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.