You are here:

C++/c ++ programing

Advertisement


Question
respected sir............
is it possible to show our output from right-side of our output screen?
generally in output screen output starts from left side. if is it possible than give me program for start output from right-side of output screen.

Answer
Hi, Hiren.

I'll start by saying that it's possible to do pretty much anything you want when programming, whether the system is designed to allow it or not.  Sometimes it just takes little tricks to make it happen.

Now, I'm assuming you're writing a console application, and not a graphical application.  What you have to do to right-justify text in a console application is basically to fill the first part of the line with spaces.  I've done up a quick application that has three functions, one for left justify, one for right justify, and one for center justify:

   #include <iostream>
   using std::cout;
   using std::endl;

   void rightJustify(const char* _text)
   {
       char introStr[80];
       memset(introStr, ' ', sizeof(char) * 79);
       introStr[79] = '\0';

       int len = 80 - strlen(_text);
       introStr[len] = '\0';

       cout << introStr << _text << endl;
   }

   void centerJustify(const char* _text)
   {
       char introStr[80];
       memset(introStr, ' ', sizeof(char) * 79);
       introStr[79] = '\0';

       int len = 80 - strlen(_text);
       len /= 2;
       introStr[len] = '\0';

       cout << introStr << _text << endl;
   }

   void leftJustify(const char* _text)
   {
       cout << _text << endl;
   }

   void main()
   {
       rightJustify("This is a test");
       centerJustify("This is a test");
       leftJustify("This is a test");
   }

There is no error checking in this code, so it's easy to break, but it demonstrates the principle, anyway.

If you have further questions, do not hesitate to ask.  Thanks!

C++

All Answers


Answers by Expert:


Ask Experts

Volunteer


Joseph Moore

Expertise

I've been programming in one form or another since my brother taught me BASIC when I was 6. I've been programing professionally since I was 20, first web development with HTML, JS, DHTML, CSS, etc., then I became a video game developer, writing code in C, C++, C#, SQL, assembly, and various scripting languages. I've even written my own scripting languages, custom designed for the games I was making. I also dabble in Java, PHP, and Perl. I've worked on pretty much every aspect of game development, including graphics, audio, gameplay, tool, UI, input, animation, and physics.

Experience

I've been writing C++ code for 12 years, both on my own in my spare time and professionally.

Organizations
IGDA

Education/Credentials
Bachelor of Science in Game Design and Development, Full Sail University, Winter Park, FL

Awards and Honors
Salutatorian and Advanced Achiever Awards at Full Sail; Independent Games Festival Student Showcase winner, 2004; Featured article on Gamasutra about an experimental game developed in 2004

©2016 About.com. All rights reserved.