简体   繁体   English

如何摆脱 C++ 创建的空行?

[英]How do can I get rid of the blank lines that are created c++?


Edit: Thank you all for the quick and helpful replies.编辑:感谢大家的快速和有用的答复。 I got it working now.我现在开始工作了。 It was because I had to reset the counter.那是因为我必须重置计数器。


I have come to ask for help as my professor is not giving me the help I need.我是来寻求帮助的,因为我的教授没有给我所需的帮助。 I am new to c++ and I am trying to program a program that displays all the integers from 1 to 100 that are divisible by 6 or 7, but not both.我是 C++ 新手,我正在尝试编写一个程序,该程序显示 1 到 100 之间的所有整数,这些整数可以被 6 或 7 整除,但不能同时被 6 或 7 整除。 and I have to display 5 numbers per row.我必须每行显示 5 个数字。 I got it working except I have blank lines forming in certain areas.我得到了它的工作,除了我在某些区域形成了空行。 I don't know if it's because of how I set up the counter or what.不知道是不是我设置柜台的原因还是什么。

Here is what I got.这是我得到的。


#include <iostream>

using namespace std;

int main()
{
    int counter = 0; // Counter for creating new lines after 5 numbers
    for (int numRange = 1; numRange <= 100; ++numRange) // Starts the loop of number 1 to 100
    {
        if (numRange % 6 == 0 || numRange % 7 == 0) // Makes the numbers divisible by 6 and 7
        {
            cout << numRange << " "; // Displays the output of the divisible numbers
            counter++; // Starts the counter

        }
        if (counter % 5 == 0) // using the counter to create new lines after 5 numbers displayed
        {
            cout << endl; // Creates a new line
        }
    }

    return 0;
}

This is what is outputted:这是输出的内容:






6 7 12 14 18


21 24 28 30 35
36 42 48 49 54

56 60 63 66 70

72 77 78 84 90
91 96 98

and this is what it's supposed to look like这就是它的样子

  6   7 12 14 18 
21 24 28 30 35 
36 48 49 54 56 
60 63 66 70 72 
77 78 90 91 96 
98

The problem that you're seeing is due to the fact that you are checking for "5 outputs" on every loop, rather than only on ones where a number has been output!您看到的问题是由于您在每个循环中检查“5 个输出”,而不是仅检查已输出数字的那些! So, to fix this issue (there are others), put the counter % 5 == 0 test inside the preceding if block:因此,要解决问题(还有其他问题),请将counter % 5 == 0测试放在前面的if块中:

    for (int numRange = 1; numRange <= 100; ++numRange) // Starts the loop of number 1 to 100
    {
        if (numRange % 6 == 0 || numRange % 7 == 0) // Makes the numbers divisible by 6 and 7
        {
            cout << numRange << " "; // Displays the output of the divisible numbers
            counter++; // Increments the counter
            if (counter % 5 == 0) // Only need this if we have done some output!
            {
                cout << endl; // Creates a new line
            }
        }
    }

Another problem is that, in this requirement:另一个问题是,在这个要求中:

that are divisible by 6 or 7, but not both可被 6 或 7 整除,但不能同时被 6 或 7 整除

your code doesn't check for the "but not both" part (but that's not the 'title' question, and I'm not going to do all your homework in one fell swoop).您的代码不会检查“但不是两者”部分(但这不是“标题”问题,我不会一举完成您的所有作业)。

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

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