problem with print ABC pyramid

test
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <iostream>
#include <string>
#include <windows.h>

using namespace std;

void printMeHere(string Str, int iTextPositionX, int iTextPositionY, int iFieldWidth)
{
      int lengthofsting = Str.length();
      iFieldWidth = iFieldWidth + lengthofsting;
      cout.width(iFieldWidth / 2);
      COORD coordPos;
      coordPos.X = iTextPositionX;
      coordPos.Y = iTextPositionY;
      SetConsoleCursorPosition( GetStdHandle( STD_OUTPUT_HANDLE ), coordPos);
      cout << Str;
}

int main ()
{
      cout << " Please type something"<<endl;
      string myString;
      cin >> myString;
      printMeHere(myString, 2, 3, 10);
      printMeHere("bbb", 2, 4, 10);
      printMeHere("ccccc", 2, 5, 10);
      printMeHere("gggggggggg", 2, 6, 10);
      cout << "\n\nHit the enter key to exit.\n";
      cin.get();
      return 0;
}


Take a look at my function here and see if this is what your talking about. I wrote this to plot a position anywhere in the console with cout, with your added idea of centering text at that spot. I personally would not use printf() because that is a C command, not really a C++ command. Just my opinion though. Someone else who knows this stuff better then I do, might have a better way of doing this.

You have the 4 perimeters of the function :

The String itself
X Position
Y Position
The field width that the string is in.

Hopes this helps.
Last edited on
Thanks for the response, the program on purpose written in C.
I could not understand how I can fix the original code. I have a problem with the alignment of the pyramid. I think missing a for loop define the amount of space according to the character is get.


The program can accept any character lowercase ABC not even position (a, c, e, g, i, k...) and print a pyramid untill last char.
Thanks again.
Topic archived. No new replies allowed.