Output char into square
Feb 23, 2020 at 12:50am UTC
Write your question here.
I am trying to output a square that is the same height and length as the users input Num1. But I want the square to be made of a symbol NumC that the user inputs.for example if the user inputs 4 as Num1 and ! and NumC the output should be
!!!!
!!!!
!!!!
!!!!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
#include <iostream>
using namespace std;
int main() {
int Num1;
int NumC;
cout << "what is the number" << endl;
cin>> Num1;
cout << "what is the character" <<endl;
cin>> NumC;
char symbol = NumC;
for (int i=0; i<Num1; i++)/////
{
for (int j=0; j<=(Num1); j++){ //
cout << symbol;
}
cout <<endl;
}
return 0;
}
Last edited on Feb 23, 2020 at 12:50am UTC
Feb 23, 2020 at 1:08am UTC
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
#include <iostream>
using namespace std;
int main() {
int Num1;
char NumC; // <---
cout << "what is the number" << endl;
cin>> Num1;
cout << "what is the character" <<endl;
cin>> NumC;
char symbol = NumC;
for (int i=0; i<Num1; i++)/////
{
for (int j=0; j<=(Num1); j++){ //
cout << symbol;
}
cout <<endl;
}
return 0;
}
Feb 23, 2020 at 1:09am UTC
Oops! I forgot the output. Also your indexing needs closer examination (<= etc) to adjust so 7 gives 7 and not 8 :)
what is the number
7
what is the character
b
bbbbbbbb
bbbbbbbb
bbbbbbbb
bbbbbbbb
bbbbbbbb
bbbbbbbb
bbbbbbbb
Last edited on Feb 23, 2020 at 1:12am UTC
Feb 23, 2020 at 1:21am UTC
Also, self-documenting variable names helps readability. something like this ...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
#include <iostream>
using namespace std;
int main()
{
int square_size;
cout << "What size is the square? " ;
cin>> square_size;
char symbol;
cout << "What is the character? " ;
cin>> symbol;
for (int row = 0; row < square_size; row++)
{
for (int column = 0; column < square_size; column++)
{
cout << symbol;
}
cout << '\n' ;
}
return 0;
}
Feb 24, 2020 at 3:55am UTC
Thanks so much @againtry
Topic archived. No new replies allowed.