Solved

display

Posted on 1998-10-11
9
170 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
 

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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
C language IDE – Compilers installation 14 67
mixing C++ & C# in Vis Studio 2013 7 140
Grammars for C C++ and java 1 113
c++ syntax question 9 43
Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

919 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

19 Experts available now in Live!

Get 1:1 Help Now