-1

I want to display output like this:

First Name: Max Last Name: Payne City: Brisbane Phone Number: 155478635

I have tried with \t and setting field width using cout.width() and setw(), but this width gets changed and are not displayed in columns properly and depends on my input length.

My code is given below:

 #include <iostream>
 #include <iomanip>
 using namespace std;



 class BillSys{

       char fname[20], lname[20], city[20], phone[11];

       public:

       void accept(){

             cout<<"Enter the name of the customer: ";
             cin>>fname>>lname; cout<<endl;
             cout<<"Enter city: ";
             cin>>city; cout<<endl;
             cout<<"Enter contact number: ";
             cin>>phone;
             cout<<endl<<endl;

      }

      void display(){

            cout<<"***Customer Details***"<<endl<<endl;
            cout<<"First Name: ";
            cout<<fname;

           //cout<<setw(10); 

           cout<<"\t";

           cout<<"Last Name: "<<lname;
           cout<<endl;

           cout<<"City: "; 
           cout<<city;
           cout<<"\t";

          //cout<<setw(10); 

          cout<<"Phone Number: "<<phone;
          cout<<endl;
      }

  };

  int main(int argc, char** argv) {

     BillSys obj;

     obj.accept();
     obj.display();
     return 0;
  }
πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
  • Did you try you search online for an example? – ZivS Sep 29 '15 at 07:41
  • Use `printf` with formatting. `cout` is for students. – i486 Sep 29 '15 at 07:41
  • 4
    @i486 And I guess you are not a student, but a battle-hardened code veteran? :) –  Sep 29 '15 at 07:43
  • @Boris Yes, you guess. I am programmer from more than 25 years. Do you remember Windows 3.0 and do you know Win16 API? – i486 Sep 29 '15 at 07:48
  • @i486 Well at least I know why you think that "`printf` is for real programmers" :) –  Sep 29 '15 at 07:51
  • 4
    @i486 Rubbish. Using `std::cout` and `<<` is the idiomatic way to print to standard output in C++. The common conception that using low level stuff is better, is hardly progressive. – Felix Glas Sep 29 '15 at 07:52
  • `cout` and `<<` are "demo" for beginners in C++. To show them example for predefined operators. And from students' years some people keep using it 10+ years later. – i486 Sep 29 '15 at 07:55
  • 3
    @i486 Again, utter subjective rubbish. – Felix Glas Sep 29 '15 at 08:09

1 Answers1

0

I would gather all the information in one std::string and then modify/trim the string. You might find some inspiration in http://www.cplusplus.com/reference/string/string/ and What's the best way to trim std::string?

Community
  • 1
  • 1
Anders Schou
  • 184
  • 1
  • 13