简体   繁体   English

不会只打印一个字母就打印所有字母,但需要在输入的字母后打印每个字母

[英]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 到目前为止,它只是在a之后打印出z,而在a是b之后打印出来,所以我希望它打印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. 我只是在寻找一些帮助,然后在那之后我需要输入2个字母并用2个字母来做,但这对我来说我知道这不是一个代码编写服务,只是在寻找有关如何做的帮助那。 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. 否则,仅在循环完成后才运行cout语句。

Sadly though this code is not portable since the C++ standard mandates few requirements as to how the alpha characters are encoded . 可悲的是,尽管此代码不可移植,因为C ++标准对字母字符的编码方式几乎没有要求。 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: 需要将cout放入循环中:

#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; 并删除了using namespace std; as it's not recommended. 因为不建议这样做。

Only thing executed in while is a++; 只有在while中执行的东西才是a ++; because there are no brackets surrounding statements that belongs to while. 因为在属于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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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