-3

I want to print a big letter (R) into a block. I've got the letter but how can I add a block ?

#include <iostream>
#include <string>
#include <cctype>


int main(){
    for( int r = 0; r < 7; ++r ){
        for(int c = 0; c<5;++c){
            if ((c == 0) || (c == 4 && (r != 0 && r != 3)) || ((r == 0 || r == 3) && (c > 0 && c < 4))){

                std::cout << "R" << "" ;
            }
            else{
                std::cout << " ";
            }
        }
        std::cout << std::endl;

    }
    return 0;
}

What I want:

enter image description here

Output:

enter image description here

  • 4
    out of curiosity, is this from a online coding site? There is quite some hype around this big R recently – 463035818_is_not_an_ai Jan 17 '20 at 15:44
  • 1
    Try converting your images of text to text. That might help you solve the problem. – 1201ProgramAlarm Jan 17 '20 at 15:52
  • your code is not producing the result you claim in your question. Please add the real expected and actual output. – 463035818_is_not_an_ai Jan 17 '20 at 15:56
  • 1
    I still believe having glyphs, 2d arrays representing the character, is a better method than having an `if` statement constraining the character printing. The `if` statement is not portable for other letters. Try printing `S`. – Thomas Matthews Jan 17 '20 at 16:05
  • @ThomasMatthews indeed. I dont get the point of the exercise, because if done properly you just need a single string in your code that looks exactly like the desired output. Learning to code should not be about making things more complicated than necessary – 463035818_is_not_an_ai Jan 17 '20 at 16:15
  • If done properly, you have a couple of functions that are table driven. You don't need to test the code every time you add a new glyph. You don't have to add new code for adding another letter, such as T or Z. The code will be less complicated because it is generic. Your code is complicated because there are different exceptions based on the row and column. Printing 5x7 or 3x5 cells requires no exceptions; a simple loop. – Thomas Matthews Jan 17 '20 at 21:35
  • If you want very simple, then define the glyph as a `static const char` array of characters, including a newline where appropriate. Next use `std::cout.write()` to output the glyph C-String. No explicit loops required. You can also add the border characters to C-String as well. – Thomas Matthews Jan 17 '20 at 21:37

2 Answers2

1

Not exactly the R you want, but that should be straightforward to adjust:

#include <iostream>
#include <string>

int main(){
    std::string output = "---------|\n"
                         "|  RRRRR |\n"
                         "|  R   R |\n"
                         "|  RRRRR |\n"
                         "|  R R   |\n"
                         "|  R  R  |\n"
                         "|  R   R |\n"
                         "----------\n";
    std::cout << output;
}

Note that code is not just to be written once, but much more often it is read and modified. Hence, making something more complicated than necessary will bite you in the long run. I could imagine that the exercise is about loops and conditions, but without further requirements I would reject anything more complicated than the above. If you have the chance to represent something in code exactly the way it appears to the real world then you should take that opportunity.

It even teaches you something useful: multiline character literals. The compiler will simply concatenate the several string literals into one. For more on that see here.

463035818_is_not_an_ai
  • 109,796
  • 11
  • 89
  • 185
0

I didn't modify your logic inside the loops. I just added the border around the big letter. This should print your desired output:

#include <iostream>

int main(){
    std::cout << " ___________" << std::endl;
    for( int r = 0; r < 7; ++r ){
        std::cout << "|   ";
        for(int c = 0; c<5;++c){
            if ((c == 0) || (c == 4 && (r != 0 && r != 3)) || ((r == 0 || r == 3) && (c > 0 && c < 4))){
                std::cout << "R" << "" ;
            }
            else{
                std::cout << " ";
            }
        }
        std::cout << "   |" << std::endl;
    }
    std::cout << " ___________" << std::endl;
    return 0;
}

Output:

 ___________
|   RRRR    |
|   R   R   |
|   R   R   |
|   RRRR    |
|   R   R   |
|   R   R   |
|   R   R   |
 ___________
rndElxment
  • 31
  • 4