Home > Back-end >  How can i make it display 5 different asterisk using functions
How can i make it display 5 different asterisk using functions

Time:03-18

I need to make a program to that display five different asterisk using functions C for a test and currently, its saying too many arguments and also cant take one arguments

#include <iostream>
using namespace std;

void asterisk();

int main()
{
    int k;
    int i;
    // Asking the user to input 5 random between 1 to 30
    cout << "Enter 5 numbers between 1 to 30" << endl;
    cin >> k;

    asterisk(i);

    return 0;
}

void display(int a)
{

    // Using for loop to get the asterisk and print it out to the console
    // How many numbers its looking for the program

    for (int i = 1; i <=  a;   i) // How many asterisk is getting printing out at a time
        cout << '*'; // asterisk
    cout << '\n'; // new line
}

CodePudding user response:

If i have not understood wrong you are looking something like this

#include<iostream>
#include<vector>

using namespace std;
void display(int a);
vector<uint> asterisks = vector<uint>(5);


int main()
{

    // Asking the user to input 5 random between 1 to 30
    cout << "Enter "<<asterisks.size()<<" numbers between 1 to 30" << endl;
    for(int i=0;i<asterisks.size();i  ) {
        cout << i << ":\t";
        cin >> asterisks[i];
    }
    for(int i=0;i<asterisks.size();i  )
    display(asterisks[i]);

    return 0;

}

void display(int a) {
for (int i = 1; i <=  a;   i) cout << '*'; // asterisk
    cout << '\n'; // new line
}

Now you store what the user chooses in a vector and then print the content of the vector using the display function Hope it will be helpful

CodePudding user response:

Your question is not quite clear but if you are looking for a program ask a user how many stars he/she wants from 1 to 30, and instead of the number replaced with '*', you should do something like this:

#include <iostream>
using namespace std;

string display(int numberOf)
{
    string outPut = "";
    for (int i = 0; i < numberOf; i  )
    {
        outPut  = "*";
    }

    return outPut;
}

int main()
{

    int numberOfStar;
    cout << "Enter number of star between 1 to 30: ";
    cin >> numberOfStar;

    if (numberOfStar >=1 && numberOfStar <= 30)
    {
        cout << "You have entered " << numberOfStar << " star(s)" << endl;
        string result = display(numberOfStar);
        cout << "\nThe output is: \n" << result;
    }
    else {
        cout << "You have entered a number which is out of range (1 to 30)";
    }


}
  •  Tags:  
  • c
  • Related