0% found this document useful (0 votes)
3 views2 pages

code

The document contains a C++ program that prompts the user to input a specified number of characters to build a character array. It includes a function to collect the characters and display them once the input is complete. The program also validates the user's input to ensure it is between 2 and 20 characters.

Uploaded by

kylerharmon99
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views2 pages

code

The document contains a C++ program that prompts the user to input a specified number of characters to build a character array. It includes a function to collect the characters and display them once the input is complete. The program also validates the user's input to ensure it is between 2 and 20 characters.

Uploaded by

kylerharmon99
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

#include <iostream> // main cpp library

#include <strings.h> // string library


using namespace std; // Makes writing easier
using str = string; // uses str instead of string

/* Function uses parameters passed by main to build and paste the char array.

For loop used for pushing back characters inside the pattern (char) array.

If statement used to only paste the whole array before the for loop completely ends.
*/

int ptrn_build(char ptrn[], int num)


{
for(int i = 0; i != num; i++)
{
cout << "Enter 1 character: ";
cin >> ptrn[i];
if(i == num - 1)
{
for(int j = 0; j < num; j++)
{
cout << ptrn[j] << endl;
}
}
}
return 0;
}

/*
Main function used to declare variables for the next function to use.
*/

int main()
{

int number;
char pattern[]{}; // declares an array

cout << "How many letters do you want in your pattern?" << endl;
cout << "(Pick a number from 2 - 20)";

cin >> number; // Puts user input inside of the number variable
if(number < 2 || number > 20) // Calls an error in case user enters an invalid number
{
cout << "Error: too high or low. Try again.";
return 0;
}

ptrn_build(pattern, number); // passes user handled variables to the pattern builder.


return 0;
}

You might also like