How to use "*" to make letters using rows and columns - c++

Here is the code of which I'm writing to create the letter "Z" for example just cant figure out how to connect the rows and columns. Also don't know why its re-writing the code as many times by the user input. ex: user inputs 5 rows, it produces the "letter" 5 times over. if anyone is able to help me resolve this issue, it would be greatly appreciated!
#include <iostream>
#include <string>
#include <cmath>
#include <random>
#include <time.h>
using namespace std; //when compiler sees a name it does not recognize, assume std;
//Program 2 by anon
int main() {
cout<<"This program draws characters. Select character, then height(s)\n";
const string PROMPT{"how many rows tall? (0 to quit): "};
bool running{true};
while (running) {
cout << "\nOption: a)Z b)H /)/ \\)\\ q)quit? (q to quit): ";
char option{}; cin>>option;
if (option=='a') { // box
while (true) {
cout << "([z]) " << PROMPT;
int rows=0;
cin >> rows;
if (rows<=0) break;
for (int col{1}; col<=rows; ++col) { // for each column
for (int row{1}; row<=rows; ++row)
cout << string(rows-row, ' ') << "*\n";
cout << "*";
}
cout<<endl;
}
}
if (option=='b') { // forward slash
while (true) {
cout << "(H) " << PROMPT;
int rows=0;
cin >> rows;
if (rows<=0) break;
for (int row{1}; row<=rows; ++row) {
cout << string(row-row, ' ') << "*\n";
for (int col{1}; col<=rows; ++col);
}
cout<<endl;
}
cout<<endl;
}
if (option=='/') { // forward slash
while (true) {
cout << "(/) " << PROMPT;
int rows=0;
cin >> rows;
if (rows<=0) break;
for (int row{1}; row<=rows; ++row) {
cout << string(rows-row, ' ') << "*\n";
}
cout<<endl;
}
}
else if (option=='\\') { // back slash
while (true) {
cout << "(\\) " << PROMPT;
int rows=0;
cin >> rows;
if (rows<=0) break;
for (int row{1}; row<=rows; ++row) { // backslash
cout << string(row-1, ' ') << "*\n";
}
cout<<endl;
}
}
else if (option=='q') {
break;
}
else {
cout<<" Invalid option, try again.\n";
}
}
cout<<"Goodbye\n";
return 0;
}
desired output:
a) Z b) H /)/ \\)\\ q) quit:a
How many rows tall? (0 to quit): 8
********
*
*
*
*
*
*
*
*
********
how many rows tall? (0 to quit): // loop reruns.

In order to draw the Z you need to add the horizontal "*" line at the beginning and the end. In addition you have to remove the outer loop, to avoid the letter to be drawn multiple times. This loop needs to be seperate at the beginning and the end, to draw the horizontal line:
if (option == 'a')
{ // box
while (true)
{
cout << "([z]) " << PROMPT;
int rows = 0;
cin >> rows;
if (rows <= 0) break;
for (unsigned int i = 0; i < rows; ++i)
cout << "*";
cout << endl;
for (int row{ 1 }; row <= rows; ++row)
cout << string(rows - row, ' ') << "*\n";
for (unsigned int i = 0; i < rows; ++i)
cout << "*";
cout << endl;
}
}

Related

I'm trying to check my array list input if its an int or string but this seems to crash and just go into infinite loop

#include <iostream>
#include <stdio.h>
#include <ctype.h>
using namespace std;
class List{
private:
int A[10];
int i;
public:
List(){
i = 0;
}
void insert(){
int v;
for(int j = 0 ; j < 10 ; j++){
cout << "\nElement you want to insert: (" << i+1 << "). ";
cin >> v;
if(i <= 9){
A[i] = v;
i++;
}
else{
cout << "\nWrong Input" << endl;
break;
}
}
if(i > 9){
cout << "\nYour List Capacity is Full.\n" << endl;
}
}
void display(){
cout << "\n{ ";
for(int j = 0 ; j < i ; j++)
cout << A[j] << " ";
cout << "}\n" << endl;
}
void remove(){
int p;
cout << "\nElement you want to remove (0 - 9): ";
cin >> p;
if (i == 0){
cout << "\nList is empty!\n" << endl;
return;
}
if (p >= 0 && p < i){
for(int j = p ; j < i-1 ; j++)
A[j] = A[j + 1];
i--;
}
}
void size(){
cout << "\nYour list size is: " << i << endl;
}
void checkcapacity(){
int arrSize = sizeof(A)/sizeof(A[0]);
cout << "\nThe Capacity of the array is: " << arrSize << endl;
}
};
int main(){
List l;
int a;
cout << "\t\t\t\t\t\tWelcome to List Program!";
while(a != 4){
int choose;
cout << "\nThe Program have following options:\n1. Insert\n2. Display\n3. Remove\n4. Check Size\n5. Check Capacity\n6. Exit\n\nNote: Your list capacity is 10!";
cout << "\n\nChoose (1 - 5): ";
cin >> choose;
if (choose == 1){
l.insert();
}
else if (choose == 2){
l.display();
}
else if (choose == 3){
l.remove();
}
else if (choose == 4){
l.size();
}
else if (choose == 5){
l.checkcapacity();
}
else if (choose == 6){
a = 4;
}
}
cout << "Thank you for using this program!";
}
I'm using this class in my main function in which I call them but when the user inputs a char or string in the insert function it goes into infinte loop. Int i is my counter and it just contains the size of my array list im just trying to put a check of character that if user input a character is should show an error.
Here's one way to write your insert function
void insert()
{
int v;
for(int j = 0 ; j < 10 ; j++)
{
cout << "\nElement you want to insert: (" << i+1 << "). ";
if (cin >> v)
{
if (i < 10)
{
A[i] = v;
i++;
}
}
else
{
cin.clear(); // clear error
cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); // discard any pending input
}
}
if (i >= 10)
cout << "\nYour List Capacity is Full.\n" << endl;
}
The important part is the recovery from bad input. First cin.clear() is called to clear the stream error state, secondly cin.ignore(...) is called to discard any pending input.
More details here

Print out the alphabet using for loop and setw

When I compile my program, run it and enter values I will get a rather strange and unexpected output:
So if I enter:
Enter width and height: 9 5
Enter characters: X O
1 XOXOXOXOX
2 OXOXOXOXO
3 XOXOXOXOX
4 OXOXOXOXO
5 XOXOXOXOX
A BCDEFGHI
When it's supposed to be:
Enter width and height: 9 5
Enter characters: X O
1 XOXOXOXOX
2 OXOXOXOXO
3 XOXOXOXOX
4 OXOXOXOXO
5 XOXOXOXOX
ABCDEFGHI
When do my void print_alphabet in another program it will work out just fine so I don't know the problem. I believe it has something to do with my other function but I can not seem to get it to work. Why does it act that way? Why does it print out A and then it does setw and prints out the rest?
This is my code:
#include <iostream>
#include <iomanip>
using namespace std;
void print_chess_board (int const height,
int const width,
char const char_1,
char const char_2)
{
int index {};
for (int i = 1; i <= height; ++i)
{
if (i%2)
{
index = 0;
}
else
{
index = 1;
}
cout << left << setw(3) << i;
for (int j {}; j < width; ++j)
{
if (++index%2 == 0)
{
cout << char_2;
}
else
{
cout << char_1;
}
}
cout << endl;
}
}
void print_alphabet (int const width)
{
cout << setfill(' ') << setw(4);
for (int i {}; i < width; ++i)
{
cout << char('A' + i);
}
}
int main()
{
int width {};
int height {};
char char_1 {};
char char_2 {};
cout << "Enter width and height: ";
cin >> width >> height;
cout << "Enter characters: ";
cin >> char_1 >> char_2;
print_chess_board(height,width,char_1,char_2);
print_alphabet(width);
return 0;
}
You need to change
cout << setfill(' ') << setw(4);
to
cout << right << setfill(' ') << setw(4);

Adding conditions to a conditional statement

I am messing around with dynamic arrays for a user defined amount of inputs for an and gate.
The issue I am running into is that I don't know how many inputs the user is going to test and I need to be able to have an if-else statement that tests each input.
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
class logic_gate {
public:
int x = 0;
};
int main() {
int userInput = 0;
cout << "How many inputs do you want on your and gate?: ";
cin >> userInput;
cout << endl;
logic_gate *and_gate = new logic_gate[userInput];
cout << endl << "Please enter the values of each bit below . . ." << endl <<
endl;
int userTest1 = 0;
for (int i = 0; i < userInput; i++) {
cout << "#" << i + 1 << ": ";
cin >> userTest1;
and_gate[i].x = userTest1;
}
return 0;
}
Here is the code that I am currently trying to find a solution for.
To implement an AND gate with n inputs you can simply do:
int output = 1;
for (int i = 0; i < n; ++i)
{
if (!and_gate [i])
{
output = 0;
break;
}
}
// ...
Use Vector data structure, you don't need to tell its size while declaring, unlike array, and it can grow automatically.
To read input till it's arriving, put cin inside while loop condition. I used getline to read whole line and work with it, so that whenever user presses enter button at empty line, program will think that no more input is coming anymore, and will start calculating 'And' of inputs.
//don't forget to import vector
#include <iostream>
#include <vector>
#include <string>
using namespace std;
class logic_gate {
public:
int x = 0;
logic_gate(){ //default constructor
}
logic_gate(int k){ //another constructor needed
x = k;
}
};
int main(){
cout << endl << "Please enter the values of each bit below . . ." << endl;
vector<logic_gate> and_gate; //no need to tell size while declaration
string b;
while(getline(cin, b)){ //read whole line from standard input
if (b == "\0") //input is NULL
break;
and_gate.push_back(logic_gate(stoi(b))); //to convert string to integer
}
if (!and_gate.empty()){
int output = and_gate[0].x;
for (int i = 1; i < and_gate.size(); i++){
output = output & and_gate[i].x;
}
cout << "And of inputs is: " << output << endl;
}
else{
cout << "No input was given!\n";
}
return 0;
}
Feel free to ask if some doubts linger
I figured out what I wanted to do. Thanks to everyone who helped and especially Paul Sanders. Below is my final code.
#include <iostream>
using namespace std;
class logic_gate {
public:
int x = 0;
};
int main() {
int userInput;
int output = 1;
cout << "How many inputs do you want on your and gate?: ";
cin >> userInput;
cout << endl;
logic_gate *and_gate = new logic_gate[userInput];
cout << endl << "Please enter the values of each bit below . . ." << endl <<
endl;
int userTest1;
for (int i = 0; i < userInput; i++) {
cout << "#" << i + 1 << ": ";
cin >> userTest1;
and_gate[i].x = userTest1;
}
if (userInput == 1) {
output = userTest1;
cout << "The test of " << userTest1 << " is " << output << endl << endl;
}
else if (userInput > 1) {
for (int i = 0; i < userInput; i++) {
if (!and_gate[i].x)
{
output = 0;
break;
}
}
cout << "The test of ";
for (int i = 0; i < userInput; i++) {
cout << and_gate[i].x;
}
cout << " is " << output << endl << endl;
}
return 0;
}

How to fix this pyramid pattern in C++?

I'm trying to make this output:
This is my code, what I have so far:
#include <iostream>
using namespace std;
int main() {
int number;
bool flag;
do {
cout << "\t\t\Menu\n";
cout << "Enter a number between 6 and 12.\n";
cin >> number;
if (number > 5 && number < 13) {
flag = true;
for(int index = 1; index <= number; ++index) {
//Loop for spaces.
for(int spaces = index; spaces < number; ++spaces) {
cout << " ";
}
//Loop for numbers.
int counter = index;
int counter2 = 1;
for(int index2 = 1; index2 <= (2 * index - 1); ++index2) {
if (counter > 0) cout << counter--;
else cout << ++counter2;
}
cout << "\n";
}
} else cout << "Enter a valid number!\n";
} while (!flag);
return 0;
}
My output:
How to fix my output with proper spaces, I tried to concatenate with spaces but it doesn't fit good, how to fit it properly?
First, you want to output a few more spaces here:
for(int spaces = index; spaces < number; ++spaces) {
cout << " ";
}
To handle different number length well, I suggest C++'s equivalent of printf() and format string, cout << setw():
#include <iomanip>
cout << setw(4) << number;
... or just use printf:
printf("%4d", number);

How do you get cin to only accept numbers from user input? [duplicate]

This question already has answers here:
How to make cin take only numbers
(2 answers)
Closed 6 years ago.
So the requirements for this program is to be able to increment arrays of the same size (size from 5 to 15 indexes) and increment each element in the array by one using for and while loops. The last task is to take values from the first array and put them in reverse order and assign them to the second array.
So everything works as normal, and the program rejects invalid inputs and does not go into an infinite loop. However, the program accepts some inputs that are not wanted.
For example, I would input something like '12 a' or '7 asdfkla;j lasnfg jasklgn asfg' and it would go through. It is interesting too because the code registers only 12 or 7 and completely ignores the rest. I think it is because once it hits a non-integer character, it would stop ignore the rest.
Why is it ignoring the rest of the input? And is there a way to catch this error from going through?
Also, if you see anything that catches your eye, feel free to critique c: I am always looking to improving.
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int main() {
srand(time(NULL));
int x;
int j = 0;
bool not_valid = true;
system("color f");
cout << "Program will ask for an input for the size of an array.\n"
<< "With the array size defined, program will generate semi-\n"
<< "true random integers from 0 to 8. First array will then\n"
<< "be assigned to the second in reverse (descending) order.\n\n";
do {
cout << "Enter array size (0 - 15): ";
cin >> x;
if (x >= 5 && x <= 15) {
not_valid = false;
cout << "\nArray size: " << x << endl;
}
else {
cout << "Invalid input.\n\n";
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
}
} while (not_valid);
int *arr0;
int *arr1;
arr0 = new int[x];
arr1 = new int[x];
for (int i = 0; i < x; i++) {
arr0[i] = rand() % 9;
}
for (int i = 0; i < x; i++) {
arr1[i] = rand() % 9;
}
cout << "\nARRAY 0 (unmodified, for):\n";
for (int i = 0; i < x; i++) {
cout << arr0[i] << "\t";
}
cout << "\n\nARRAY 0 (modified, for):\n";
for (int i = 0; i < x; i++) {
arr0[i]++;
cout << arr0[i] << "\t";
}
cout << "\n\nARRAY 1 (unmodified, while):\n";
for (int i = 0; i < x; i++) {
cout << arr1[i] << "\t";
}
cout << "\n\nARRAY 1 (modified, while):\n";
while (j < x) {
arr1[j]++;
cout << arr1[j] << "\t";
j++;
}
int second = x - 1;
for (int i = 0; i < x; i++) {
arr1[second] = arr0[i];
second--;
}
j = 0;
cout << "\n\nARRAY 1 (array 0, descending):\n";
while (j < x) {
cout << arr1[j] << "\t";
j++;
}
cout << endl << endl;
system("pause");
return 0;
}
Take input in string and then check if it's a number or not.
Example:
#include<iostream>
#include<sstream>
#include <string>
using namespace std;
int main()
{
string line;
int n;
bool flag=true;
do
{
cout << "Input: ";
getline(cin, line);
stringstream ss(line);
if (ss >> n)
{
if (ss.eof())
{
flag = false;
}
else
{
cout << "Invalid Input." << endl;
}
}
}while (flag);
cout << "Yo did it !";
}