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;
     }
   }
}
user229044
  • 232,980
  • 40
  • 330
  • 338
matt
  • 33
  • 6

4 Answers4

1

The variable f is an int. When you press the key 'f', the cin istream tries to set the int to 'f', which isn't a number, so the conversion from a character to a number fails.

That failure sets the bad-bit in cin, which breaks out of the while loop.

Eljay
  • 4,648
  • 3
  • 16
  • 27
  • ok thanks for the help but im not sure how to get the conversion working i just want to enter f and have that pattern be on the screen, thanks for the help – matt Nov 22 '17 at 13:56
0

Reading the input into a char is the easy bit: std::cin >> c for a char c will do it.

The fun bit is writing a portable way of printing the letters up to a certain character. Here's one way:

// Prints up to and including 'c'.
// Outputs the alphabet if 'c' is not a lower case letter.
void print(char c)
{
    static const char s[] = "abcdefghijklmnopqrstuvwxyz";
    for (std::size_t i = 0; s[i]; ++i){
        std::cout << s[i];
        if (s[i] == c){
            std::cout << '\n';
            return;
        }
    }
}
Bathsheba
  • 231,907
  • 34
  • 361
  • 483
  • 2
    This function loops until god knows when c is not in the range a-z – Michaël Roy Nov 22 '17 at 14:05
  • Hence my second comment; although we could clamp the output by putting s[i] as the loop conditional. You know I might just do that! – Bathsheba Nov 22 '17 at 14:05
  • im pretty much just trying write a program that inputs a letter from A to Z and displays all the letters similar to the following output (which is based on the input of F): ABCDEF ABCDE ABCD ABC AB A – matt Nov 22 '17 at 14:08
0

If you enter f you cause an error because it is expecting an integer. You can convert the a char to an integer. If you want to turn a result if you enter f you have to options:

1.

char input;
std:cin >> input;
if((int)input == 102){
   .....

2.

char input;
std:cin >> input;
if(input == 'f'){
    .....

EDIT: If you want to print the Alphabet in descending order Michael Roy had a nice solutions but in accesnding order

 if....
    for(char i = input; i >= 'a'; --i)
       cout << i - 32; //the 32 is for converting the lower case char into upper case
    cout << '\n';

So in total it could look something like this:

char input;
std:cin >> input;
if('a' < input < 'z'){
     for(char i = input; i >= 'a'; --i)
        cout << i - 32;
     cout << '\n';
 }else{
     cout << "Not a valid input";
 }
 System("Pause");//so the console doesn't close automatically
Philipp G.
  • 21
  • 1
  • 5
  • im pretty much just trying write a program that inputs a letter from A to Z and displays all the letters similar to the following output (which is based on the input of F): ABCDEF ABCDE ABCD ABC AB A – matt Nov 22 '17 at 14:10
  • I edited my answer hopefully this will bring more carification – Philipp G. Nov 23 '17 at 10:19
0

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;
    }
  }
}
Bathsheba
  • 231,907
  • 34
  • 361
  • 483
Michaël Roy
  • 6,338
  • 1
  • 15
  • 19