Can some tell me how to display words vertically c++?

Home » Programming & Design » Can some tell me how to display words vertically c++?
Programming & Design No Comments

Other answer:

Pawanjit:
Sure, just write a newline between each character assuming you are making a command line program.

cout << "H \ni \n" ;

Timothy:
Here is a simple code example.

Note: the … and …… are showing indentation in code (thus not a literal part of code)

#include <iostream>
#include <string>
using namespace std;
int main() {
…std::string str; // declares a string variable
…std::cin >> str; // takes input and stores it in the variable str

…int y = 0; // use in condition loop to extract char @ position y of string

…// Prints character of str @ position y then new line
…while (y != str.length()) {
……std::cout << str[y] << "\n";
…….y++; // increase y by 1 to get next position in string
…}
…std::cout << "press enter to exit…\n";
…std::cin.ignore(256,'\n');
…std::cin.get(); // pauses the console
…return 0;
}

Regards,

Timothy

galaxy:
To display student Id vertically:

If you use string to store it:

for(uint i=0;i<studentID.size();i++)
{
cout<<studentID[i]<<endl;
}

char array is the same way, but you need to know the size beforehand.

Now if you want to display three labels side by side:

string label1,label2,label3;
// Know which string has the largest size(number of characters)
// Suppose label3 has the largest size
for(uint i=0; i<label3.size();i++)
{
if(i<label1.size())
cout<<label1[i]<<" ";

if(i<label2.size())
cout<<label2[i]<<" ";

cout<<label3[i]<<endl;
}

cja:
You really just need one line of code to do that, like this:

#include <iostream>
#include <string>
#include <algorithm>
#include <iterator>

using namespace std;

int main(int argc, char *argv[]) {
. . string in;

. . while (true) {
. . . . cout << endl << "> ";
. . . . getline(cin, in);
. . . . copy(in.begin(), in.end(), ostream_iterator<char>(cout, "\n"));
. . }
. . return 0;
}

#if 0

Sample run:

> hello
h
e
l
l
o

> good bye
g
o
o
d

b
y
e

#endif

Dawn M:
yes

LEAVE A COMMENT