Solved

display

Posted on 1998-10-11
9
175 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying 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

Suggested Solutions

Title # Comments Views Activity
Find boost header files using gnu make (MinGW) on Win 7 11 108
Move constructor only called if marked noexcept? 6 121
Find Visual Studio Tools 2 134
cmake and message 1 21
Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

739 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