简体   繁体   中英

not printing out all alphabet just one letter but it needs to print out every letter after the one that was entered

so far it just prints out z after a but after a is b so i want it to print bcdef g....z

#include <iostream>

using namespace std;

int main() 
{
   char a = 'a';
   while (a < 'z')
   a++; 
  cout << a; 
}

im just looking for some help on how to do that then after that i need to enter 2 letters and do that with 2 letters but that is just for me i know this is not a code writing service just looking for some help on how to do that. thanks any help is good

In the loop, you need to enclose multiple statements in braces:

int main() 
{
    char a = 'a';
    while (a < 'z'){
        a++; 
        cout << a; 
    }
    cout << '\n'; // let's have a line break at the end
}

Otherwise the cout statement is only ran once the loop finishes.

Sadly though this code is not portable since the C++ standard mandates few requirements as to how the alpha characters are encoded . And their being contiguous and consecutive is not a requirement. The portable solution is based on the obvious if ostensibly puerile

int main()
{
    std::cout << "abcdefghijklmnopqrstuvwxyz\n";
}

; if you want to print all letters from a certain value, then use something on the lines of

int main() {
    const char* s = "abcdefghijklmnopqrstuvwxyz";   
    char a = 'k'; // print from k onwards, for example
    for ( ; *s /*to prevent overrun*/ && *s != a; ++s);     
    std::cout << s; 
}

Need to put the cout inside the loop:

#include <iostream>

int main() 
{
   char a = 'a';
   while (a < 'z')
   {
      a++; 
      std::cout << a << " "; 
   }
}

Also added a space to distinguish the different letters. And removed the using namespace std; as it's not recommended.

Only thing executed in while is a++; because there are no brackets surrounding statements that belongs to while. to do multiple statements surround them in brackets. Or like in this case its possible to make them into one statement.

#include <iostream>

int main() 
{
    char a = 'a';
    while (a < 'z')
        std::cout << ++a;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM