visual c++ – filling up an array in c++

visual c++ – filling up an array in c++

Using C++11

#include <algorithm>
#include <iostream>

int main() {
    char array[80];
    std::fill(std::begin(array),std::begin(array)+10,r);
}

Or, as mentioned in the comments you can use std::fill(array,array+10,r).

You can use the [] operator and assign a char value.

char y[80];
for(int b=0; b<10; ++b)
    y[b] = r;

And yes, std::fill is a more idiomatic and modern C++ way to do this, but you should know about the [] operator too!

visual c++ – filling up an array in c++

// ConsoleApp.cpp : Defines the entry point for the console application.
//

#include stdafx.h
#include <iostream>
#include <string>

using namespace std;

int fun(bool x,int y[],int length);
int funx(char y[]);
int functionx(bool IsMainProd, int MainProdId, int Addons[],int len);
int _tmain(int argc, _TCHAR* argv[])
{
    int AddonCancel[10];

    for( int i = 0 ; i<4 ;++i)
    {
        std::fill(std::begin(AddonCancel)+i,std::begin(AddonCancel)+i+1,i*5);
    }
    bool IsMainProduct (false);
    int MainProduct =4 ; 
    functionx(IsMainProduct,MainProduct,AddonCancel,4);

}

int functionx(bool IsMainProd, int MainProdId, int Addons[],int len)
{
    if(IsMainProd)
        std::cout<< Is Main Product;
    else
    {
        for(int x = 0 ; x<len;++x)
        {
          std::cout<< Addons[x];
        }
    }

    return 0 ; 
}

Leave a Reply

Your email address will not be published.