Search code examples
c++charcmath

input the letter f or any letter and cout an alphabet pattern like a abc abcd abcde abcdef but it only works if i press 0


So far nothing happens when you enter f it only works when 0 is entered but I want it so when you press f you get this a ab abc abcd abcde abcdef

#include <iostream>
using namespace std;
int main()
{
int f = 0;
int z;
cout << "";
while(cin >> f)
{
    if(f == 0)
    {
        cout << "ABCDEF\nABCDE\nABCD\nABC\nAB\nA";
        break;
     }
   }
}

Solution

  • Here's one way to make your program do what you want:

    #include <iostream>
    using namespace std;
    int main()
    {
      char c = 0;  // we want chars, not integers.
      int z;
      cout << "";
      while (cin >> c)
      {
        if ('a' <= c && c <= 'z') // test for the range we want
        {
          // print all glyphs from a to 'c'
          for (char i = 'a'; i <= c; ++i)
            cout << i;
          cout << '\n';
          break;
        }
      }
    }