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

code5

The document describes a C++ program that prompts the user to input a number between 2 and 20, which determines the number of characters in a pattern. The user then enters the specified number of characters, which are displayed at the end. The program includes error handling for invalid inputs and utilizes functions to build and display the character pattern.

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

code5

The document describes a C++ program that prompts the user to input a number between 2 and 20, which determines the number of characters in a pattern. The user then enters the specified number of characters, which are displayed at the end. The program includes error handling for invalid inputs and utilizes functions to build and display the character pattern.

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

In the beginning of the program, the user will type a number no less than 2, and no more than 20.

This tells the program how many singular characters are going to be used in their pattern.

For example:

Type 3, then type ‘.’ 3 times, or ‘.’ ‘/’ ‘.’

They’ll then be prompted to type in any character they want.

#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