Solved

display

Posted on 1998-10-11
9
174 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
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 concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
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…

685 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