Solved

display

Posted on 1998-10-11
9
169 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
Comment Utility
Edited text of question
0
 
LVL 3

Expert Comment

by:NullTerminator
Comment Utility
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
Comment Utility
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
 

Accepted Solution

by:
clance earned 20 total points
Comment Utility
#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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 

Author Comment

by:CG
Comment Utility
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
Comment Utility
// 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
Comment Utility
// 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
Comment Utility
// 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
Comment Utility
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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

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…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

772 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now