help needed computer geeks!! - Sajha Mobile
SAJHA MOBILE
help needed computer geeks!!
Posts 8 · Viewed 5543 · Go to Last Post
kaloVale
· Snapshot
Like · Likedby · 0
hey guys, how to align the integers in right order in visual c++.
I have a matrix to print out with right alignment.I can print out the matrix but the integer values donot align in the right order.
for example | 1  10  -8|
                   | 12 2  3   |
this is what my code does, which is straight forward 
 but the result should be   | 1  10  -8|
                                      |12    2    3| which is more prettier
I know setw() fuction. But I am not able to get it right. Am I not using setw() correctly?
                                                                                         
kaloVale
· Snapshot
Like · Liked by · 0
Ok the example comes out to be horribly wrong here!! the idea is to right align the integer value.
Raxi
· Snapshot
Like · Liked by · 0
may be gotoxy() will help...
kaloVale
· Snapshot
Like · Liked by · 0
hey raxi thanks for the reply, but I think quick google shows gotoxy() is not visual c++ library fuction.
and  I am not trying to move whole matrix around the screen.I just want to align/adjust (right adjust)  the digits of top row and subsequent bottom rows of the matrix in right order. for example if you have 1 on top row and 10 at the bottom row. Normal output is that the 1 of the top row would be on top of the 1 of the bottom row. What I want is aligning that 1 of the top row so that it remains on top of 0 not 1.
techGuy
· Snapshot
Like · Liked by · 0
how did u use setW() ? can you post that piece of code here?
techGuy
· Snapshot
Like · Liked by · 0

Have been out of touch with C++ from long time , but try something like below.
int
main () {
cout <<setw (10);
cout <<right<< 77 << endl;
return 0;
}

src: http://www.cplusplus.com/reference/iostream/manipulators/right.html

LahureDai
· Snapshot
Like · Liked by · 0
KaloVale,

I have made a simple example for ya in order to give you the brief idea about  setw() and how does it work.
Compile this program and run in ur machine :

[code]
int main()
{
  int sales [3][5]={ {1,2,3,4,5},
  {6,7,8,9,10},
  {100,101,102,103,104}};


  cout<<"                Item #0 Item #1 Item #2  Item #3 Item #4  Totals"<<endl;


  //create rows/columns of text representing arrays
  for (int salesperson = 0 ; salesperson <3 ; salesperson++)
  {
    cout <<"Salesperson"<<setw( 2 ) <<salesperson;
    int sum =0;

    for (int item =0; item <5 ; item++)
    {
      cout << setw( 8 )<< sales[salesperson][item];
      sum+=sales[salesperson][item];
    }
    cout<<setw( 8 ) <<sum <<endl;
  }
  cout <<endl;
  cout <<"Totals"<<setw( 14 ); // 14 spaces

  for (int j = 0;j<5;j++)
  {
    int sum = 0;

    for (int i =0; i<3; i++)
    {
      sum+= sales[i][j];
    }
    cout<<sum<<setw( 8 ); // 8 spaces
  }
  cout<<endl;
}[/code]

Hope it helps :)

kaloVale
· Snapshot
Like · Liked by · 0
TechGuy and LahureDai I appreciate it!! And TechGuy that was exactly what I was looking for!! it works.    
LahureDai that works great when u make a output in one line, but if you have to output  multiple line with a' for loop' than data may not align in required order. but techGuy gave what I needed.
Thanks again guys!!
Please log in to reply to this post

You can also log in using your Facebook
View in Desktop
What people are reading
You might like these other discussions...



Travel Partners