Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/148.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 使用数组和布尔值在输出未知时从输出中删除逗号_C++ - Fatal编程技术网

C++ 使用数组和布尔值在输出未知时从输出中删除逗号

C++ 使用数组和布尔值在输出未知时从输出中删除逗号,c++,C++,如何删除输出末尾的逗号。但是这里我不知道最终的输出是什么,因为数组的元素是由用户输入的。所以最后一个数组可以是奇数或偶数,它是未知的。我可以使用布尔运算、数组和决策。我不允许使用指针或结构,因为我没有学习它 #include<iostream> using namespace std; int main() { int array[100], Number; cout << "\nEnter the size of an array (1-20):";

如何删除输出末尾的逗号。但是这里我不知道最终的输出是什么,因为数组的元素是由用户输入的。所以最后一个数组可以是奇数或偶数,它是未知的。我可以使用布尔运算、数组和决策。我不允许使用指针或结构,因为我没有学习它

#include<iostream>
using namespace std;

int main()
{
    int array[100], Number;

    cout << "\nEnter the size of an array (1-20):";

    cin >> Number;

    if (Number <= 20 && Number > 0)
    {
        cout << "Enter the elements of the array: \n";

        // For loop execution  
        // i start at 0. as long as i < Number. i++ 
        for (int i = 0; i < Number; i++)
        {
            cout << "array element " << i << ":";
            cin >> array[i];
        }

        cout << "\nEven Numbers are : ";

        // For loop execution
        for (int i = 0; i < Number; i++)
        {
            // condition and execution
            if (array[i] % 2 == 0)
            {
                cout << array[i];
                cout << " , ";
            }
        }
        cout << endl;

        cout << "odd Numbers are: ";

        // For loop execution
        for (int i = 0; i < Number; i++)
        {
            // condition and execution
            if (array[i] % 2 != 0)
            {
                cout << array[i];
                cout << " , ";
            }
        }

        cout << endl;
        cout << "-------------------------------------------------";
    }
    else
    {
        cout << "size is invalid" << endl;
    }

    system("pause");
    return 0;
}
#包括
使用名称空间std;
int main()
{
整数数组[100],数字;
数量;
如果(数字0)
{

cout只需在元素前打印逗号并检查第一个元素:

    bool is_first = true;
    for (int i = 0; i < Number; i++)
    {
        // condition and execution
        if (array[i] % 2 == 0)
        {
            if(!is_first)
            {
                cout << " , ";
            }
            cout << array[i];
            is_first = false;
        } 
    }
bool is_first=true;
for(int i=0;icout只需在元素前打印逗号并检查第一个元素:

    bool is_first = true;
    for (int i = 0; i < Number; i++)
    {
        // condition and execution
        if (array[i] % 2 == 0)
        {
            if(!is_first)
            {
                cout << " , ";
            }
            cout << array[i];
            is_first = false;
        } 
    }
bool is_first=true;
for(int i=0;icout删除尾随逗号的最佳方法是不打印它

重新将阵列打印为类似以下内容:

std::string dlm;
for (int i = 0; i < Number; i++)
{
    // condition and execution
    if (array[i] % 2 == 0)
    {
        cout << dlm << array[i];
        dlm = " , ";
    }
}
std::string-dlm;
for(int i=0;icout删除尾随逗号的最佳方法是不打印它

重新将阵列打印为类似以下内容:

std::string dlm;
for (int i = 0; i < Number; i++)
{
    // condition and execution
    if (array[i] % 2 == 0)
    {
        cout << dlm << array[i];
        dlm = " , ";
    }
}
std::string-dlm;
for(int i=0;i每次打印不能执行两个循环:首先收集所有要打印的条目,然后打印该向量,这样很容易知道何时有最后一个元素。@Aziuth谢谢,但我还没有学习向量。但是谢谢你回答我的问题。每次打印都执行两个循环:首先收集所有要打印的条目在向量中,然后打印该向量,这样就很容易知道什么时候有最后一个元素了。@Aziuth谢谢,但我还没有学习向量。但是谢谢你回答我的问题谢谢你的帮助谢谢你的帮助