kaloVale
Replies to this thread:

More by kaloVale
What people are reading
Subscribers
:: Subscribe
Back to: Computer/IT Refresh page to view new replies
 help needed computer geeks!!
[VIEWED 6650 TIMES]
SAVE! for ease of future access.
Posted on 02-10-09 10:20 AM     Reply [Subscribe]
Login in to Rate this Post:     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?
                                                                                         

 
Posted on 02-10-09 10:29 AM     Reply [Subscribe]
Login in to Rate this Post:     0       ?    
 

Ok the example comes out to be horribly wrong here!! the idea is to right align the integer value.

 
Posted on 02-10-09 2:00 PM     Reply [Subscribe]
Login in to Rate this Post:     0       ?    
 

may be gotoxy() will help...
 
Posted on 02-10-09 3:18 PM     Reply [Subscribe]
Login in to Rate this Post:     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.

 
Posted on 02-10-09 3:21 PM     Reply [Subscribe]
Login in to Rate this Post:     0       ?    
 

how did u use setW() ? can you post that piece of code here?

 
Posted on 02-10-09 3:33 PM     Reply [Subscribe]
Login in to Rate this Post:     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


 
Posted on 02-10-09 8:00 PM     Reply [Subscribe]
Login in to Rate this Post:     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 :)


 
Posted on 02-10-09 10:20 PM     Reply [Subscribe]
Login in to Rate this Post:     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 be able to reply! If you don't have a login, please register here.

YOU CAN ALSO



IN ORDER TO POST!




Within last 7 days
Recommended Popular Threads Controvertial Threads
I hope all the fake Nepali refugee get deported
All the Qatar ailines from Nepal canceled to USA
MAGA मार्का कुरा पढेर दिमाग नखपाउनुस !
Travel Document for TPS (approved)
MAGA and all how do you feel about Trumps cabinet pick?
Those who are in TPS, what’s your backup plan?
NOTE: The opinions here represent the opinions of the individual posters, and not of Sajha.com. It is not possible for sajha.com to monitor all the postings, since sajha.com merely seeks to provide a cyber location for discussing ideas and concerns related to Nepal and the Nepalis. Please send an email to admin@sajha.com using a valid email address if you want any posting to be considered for deletion. Your request will be handled on a one to one basis. Sajha.com is a service please don't abuse it. - Thanks.

Sajha.com Privacy Policy

Like us in Facebook!

↑ Back to Top
free counters