Solved

display

Posted on 1998-10-11
9
172 Views
Last Modified: 2010-04-02
Can anybody help me to write a program that display the following strings to standard output. The strings should be displayed one line with each line centered within a field length that is extracted from standard input.
Greetings earth inhabitants
Take me to your leaders

The output should be:
                    Greetings
                        earth
                   inhabitants
                       Take
                         me
                         to
                       your
                    leaders
0
Comment
Question by:CG
  • 4
  • 3
  • 2
9 Comments
 

Author Comment

by:CG
ID: 1174847
Edited text of question
0
 
LVL 3

Expert Comment

by:NullTerminator
ID: 1174848
int i, j;

// get the field width
cin >> i;
// get the string length
j = strlen("greetings");
// set the width
cout.width( ( i + j ) / 2 );
cout << "greetings" << endl;

// repeat for other strings

0
 

Author Comment

by:CG
ID: 1174849
Hi, NullTerminator:
I got an error message when I compiled the following test program using your code:
 'strlen' : cannot convert parameter 1 from 'class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char>>' to 'const char *'
Could you help me fix it? I use Microsoft Developer Studio.

#include<iostream>
#include<string>
using namespace std;
int main(){
    int i, j;
    string inputString;
    cout << "Please enter the field width: ";
    // get the field width
    cin >> i;
    cout  << "Please enter the string:\n";
    while(cin>>inputString){
         // get the string length
         j = strlen(inputString);
         // set the width
         cout.width( ( i + j ) / 2 );
         cout << inputString << endl;
     }
    return 0;
}


0
Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

 

Accepted Solution

by:
clance earned 20 total points
ID: 1174850
#include <iostream.h>
#include <string.h>

#define NUM_WORDS 8
#define PAGE_WIDTH 80

char *aszWords[NUM_WORDS] = {
      "Greetings",
      "earth",
      "inhabitants",
      "Take",
      "me",
      "to",
      "your",
      "leaders."
};

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

      for (int i = 0; i < NUM_WORDS; i++) {
            iPadding = (PAGE_WIDTH - strlen(aszWords[i])) / 2;
            for (int j = 0; j < iPadding; j++)
                  cout << ' ';
            cout << aszWords[i] << '\n';
      }

      return 0;
}

0
 

Author Comment

by:CG
ID: 1174851
I must use type "string", so I change your code(see following program). But it doesn't
work the right way. The output looks like:

This is a test1 <-- input
                                    This is a test 1   <--- output
This is a test2 <-- input
                                    This is a test2    <--- output

It should look like:
This is a test1 <---input
This is a test2 <---input
                                   This is a test1 <-- output
                                   This is a test2 <-- output
Could you help me correct it?


               #include <iostream.h>
       #include <cstring.h>


       #define PAGE_WIDTH 80

       int main()
       {
            string str1;
            int iPadding;
            cout << "Enter your string:" ;

            while ( getline(cin,str1,'\n'))  {
                  iPadding = (PAGE_WIDTH - str1.length()) / 2;
                  for (int j = 0; j < iPadding; j++)
                        cout << ' ';
                  cout << str1 << '\n';
            }
            return 0;
       }
0
 
LVL 3

Expert Comment

by:NullTerminator
ID: 1174852
// namespace declaration didn't compile for me
// any build settings to enable namespaces?

// get the string length the length() member fucntion should do it

j = inputString.length();

// cout seems to choke on setw() and .width() just ignoring me
// I worked around it with the following padding mechanism
cout << setw((i-j)/2) << " " << inputString << endl;

// setw and width worked fine on the iostream.h and iomanip.h versions
// but you are using the non  .h versions

'\0'
0
 
LVL 3

Expert Comment

by:NullTerminator
ID: 1174853
// namespace declaration didn't compile for me
// any build settings to enable namespaces?

// get the string length the length() member fucntion should do it

j = inputString.length();

// cout seems to choke on setw() and .width() just ignoring me
// I worked around it with the following padding mechanism
cout << setw((i-j)/2) << " " << inputString << endl;

// setw and width worked fine on the iostream.h and iomanip.h versions
// but you are using the non  .h versions

'\0'
0
 
LVL 3

Expert Comment

by:NullTerminator
ID: 1174854
// use two for loops
int iCounter;
string inputString[8];
for(iCounter = 0; iCounter <8; iCounter++) {
    // collect the data into your array of strings
    cin >> inputString[i];
    }
for(iCounter = 0; iCounter < 8; iCounter ++) {
    // write them out
    j = inputString[i].length();
    cout << setw((i-j)/2) << " " << inputString[i] << endl;
    }

// is this some kind of homework assignment
// this logic is pretty basic stuff
   
0
 

Expert Comment

by:clance
ID: 1174855
Oh, you want to use std::string?  Okay:

#include <iostream.h>
#include <string>

using namespace std;

#define NUM_WORDS 8
#define PAGE_WIDTH 80

string sWords[NUM_WORDS] = {
      "Greetings",
      "earth",
      "inhabitants",
      "Take",
      "me",
      "to",
      "your",
      "leaders."
};

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

      for (int i = 0; i < NUM_WORDS; i++) {
            iPadding = (PAGE_WIDTH + sWords[i].size()) / 2;
            cout.width(iPadding);
            cout << sWords[i].c_str() << '\n';
      }

      return 0;
}

0

Featured Post

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

832 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question