• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 207
  • Last Modified:

help with code

This is the code I have.

// string2.cpp

#include <string.h>
#include <iostream.h>
#include <conio.h>
#include <stdio.h>
int main()
{
      char string[80];
      char *s0 = string;
      char *s1;
      int boundary;
      int line_no;

      line_no = 1;
      boundary = 10;

      clrscr();
      cout << "Enter string: ";
      gets(string);
      while( strlen(s0) > boundary && (s1 = strchr(s0,' ')) ){
      char *s2;
      while( (s2 = strchr(s1+1,' ')) && s2-s0 <= boundary ){ s1=s2; }
      *(s0 = s1) = '\n';
      line_no++;
      }


      cout << string;
      cout << endl << "line_no = " << line_no;
      return 0;
}



If you enter: Hello this is a test for width

It would output

Hello this
is a test
for width
line_no = 3

My problem is I need it to output the string at x 20
example
                    Hello this
                    is a test
                    for width

If I use
gotoxy (20,5);
cout << string;

I get

                  Hello this
is a test
for width

Can any fix my code to do what i need it to do?

Thanks
0
onestar
Asked:
onestar
  • 10
  • 7
  • 2
  • +3
1 Solution
 
nietodCommented:
answer follows.
0
 
alexoCommented:
What's wrong with:

    const char[] spaces = "                    ";

    // ...

    cout << spaces << string;
0
 
trestanCommented:
gotoxy (20,5);
for(int i=0;i<=strlen(string);i++)
{
putchar(string[i]);
if (string[i]=='\n')
gotoxy (20,5);
}
0
Technology Partners: 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!

 
trestanCommented:
gotoxy (20,5);
for(int i=0;i<=strlen(string);i++)
{
putchar(string[i]);
if (string[i]=='\n')
gotoxy (20,5);
}
0
 
trestanCommented:
Sorry for press the submit for twice.
Although someone block the question before me, I would like to give you answer as well. Hope you are satisfied with my answer.
0
 
nietodCommented:
As I believe I suggested in your previous question, you need to write a procedure to ouput the string that does the indenting and the line wrapping.  For example

OutputIndented(const char *Str, int Indent, int LinLen)
{
    const char *BgnPtr = Str;
    const char *ChrPtr = Str;
    const char *SpcPtr = Str;
    char             CurChr;

    while (true)
   {
         CurChr = *ChrPtr++;
         if (BgnPtr - ChrPtr) > LinLen || !CurChr)
         {
             for (int i = 0; i < Indent; ++i)
                  cout.put(' ');
             if (SpcPtr = BgnPtr)
                SpcPtr = ChrPtr;
             cout.write(BgnPtr,SpcPtr-BgnPtr);
             BgnPtr = SpcPtr;
         };
         if (CurChr == ' ')
            SpcPtr = ChrPtr - 1;
         if (CurChr)
            break;
   }
}


Sorry this is taking so long.  I'm having connection problems.
0
 
nietodCommented:
trestan's approach has some merits, but you need to alter the y value each time you output or you will overwrite. But what do you do when the string is so long that the wrapped string is longer than the screen?  If you just stick with stream output it will scroll the screen for you.
0
 
trestanCommented:
Try my code and you will know that the screen will scroll as well. No problem. Keep simple, concise and easy to understand is the principle of programming.
By the way, there are already complains about people block the question before giving a solution. See the service area.
0
 
nietodCommented:
How is your code coing to make the screen scroll?  It outputs every line on top of each other so only the last line is visible.
0
 
trestanCommented:
To nietod, sorry, i make a mistake about it. but the problem canbe solved like this:
cout<<"     "<<flush;
for(int i=0;i<=strlen(string);i++)
{
putchar(string[i]);
if (string[i]=='\n')
cout<<"     "<<flush;
}


0
 
nietodCommented:
That will work, but I'm not sure why you need the flushes.

I don't like that solution though, not because of it, but because of the code it relies on.  This comes from another question, but it starts with a a string entered from the user and then places the newlines in the string to make it wrap.  In general, I don't like altering the string that way.  Data display issues should be seperate from data storage issues.  That sort of algorithm that alters the data for display purposes often results in problems.  For example, what do you do when you want to display the string again, but with a different line length.

0
 
trestanCommented:
To nietod,
I agree with you about the original idea. But, you know, we are not sure about what he intend to do with the algorithm. That is not our bussiness unless he present it as a question for enquiring our idea.

0
 
VEngineerCommented:
It's actually not a bad plan to use flush and endl.
I think it makes the program clearer than using \n.

Ex:
cout << "I want\nmultiple lines\nof text\n";

is harder to read than

cout << "I want" << endl << "multiple lines" << "of text" << endl;

or better yet

cout << "I want" << endl;
cout << "multiple lines" << endl;
cout << "of text" << endl;

0
 
onestarAuthor Commented:
First I would like to say I am just a beginner with c++ and I haven't really used pointers much.  So when things don't work, it is hard for me to fix them because i don't really understand how they work.
I am having problems.  I understand the concepts you are both trying to do but i can't quite get it to work.  Can you put your answer's in my code so i can just cut and paste and get it to work.  So it is one simply program in main (not a function outside of main) that will ask the user for the string and everything.  Sorry if i am asking to much but I am writing a program and this is the last thing i can't get to work and i am getting fustrated.

I will try both methods and see what i like better.

To: nietod
I can't use true in my compiler so can you change that and i think you are missing a ( in your first if statement.
0
 
nietodCommented:


   // string2.cpp

   #include <string.h>
   #include <iostream.h>
   #include <conio.h>
   #include <stdio.h>
//-------------------------------------------
OutputIndented(const char *Str, int Indent, int LinLen)
     {
         const char *BgnPtr = Str;
         const char *ChrPtr = Str;
         const char *SpcPtr = Str;
         char             CurChr;

         while (true)
        {
              CurChr = *ChrPtr++;
              if ((BgnPtr - ChrPtr) > LinLen || !CurChr)
              {
                  for (int i = 0; i < Indent; ++i)
                       cout.put(' ');
                  if (SpcPtr = BgnPtr)
                     SpcPtr = ChrPtr;
                  cout.write(BgnPtr,SpcPtr-BgnPtr);
                  BgnPtr = SpcPtr;
              };
              if (CurChr == ' ')
                 SpcPtr = ChrPtr - 1;
              if (CurChr)
                 break;
        }
     }
//-------------------------------------------------
   int main()
   {
   char string[80];
   char *s0 = string;
   int boundary;
   int line_no;

   line_no = 1;
   boundary = 10;

   clrscr();
   cout << "Enter string: ";
   gets(string);

   OutputIndented(string, 5,25);

   cout << endl << "line_no = " << line_no;
   return 0;
   }

Note that outputs the string indented 5 and with up to 25 characters on a line.
0
 
trestanCommented:
// string2.cpp

#include <string.h>
#include <iostream.h>
#include <conio.h>
#include <stdio.h>
int main()
{
char string[80];
char *s0 = string;
char *s1;
int boundary;
int line_no;

line_no = 1;
boundary = 10;

clrscr();
cout << "Enter string: ";
gets(string);
while( strlen(s0) > boundary && (s1 = strchr(s0,' ')) ){
char *s2;
while( (s2 = strchr(s1+1,' ')) && s2-s0 <= boundary ){ s1=s2; }
*(s0 = s1) = '\n';
line_no++;
}

//My code starts here.
cout<<"     "<<flush;
for(int i=0;i<=strlen(string);i++)
{
putchar(string[i]);
if (string[i]=='\n')
cout<<"     "<<flush;
}
//My code ends here.

cout << endl << "line_no = " << line_no;
return 0;
}

//I am really glad to see that a question will be answered here.
//Please mark where it does not well so that I can make change //correspondingly.
0
 
nietodCommented:
Opps.   Two mistakes in mine (at least).  Change the function to

OutputIndented(const char *Str, int Indent, int LinLen)
          {
              const char *BgnPtr = Str;
              const char *ChrPtr = Str;
              const char *SpcPtr = Str;
              char             CurChr;

              while (1)   // true is 1 by the way.
             {
                   CurChr = *ChrPtr++;
                   if ((BgnPtr - ChrPtr) > LinLen || !CurChr)
                   {
                       for (int i = 0; i < Indent; ++i)
                            cout.put(' ');
                       if (SpcPtr = BgnPtr)
                          SpcPtr = ChrPtr;
                       cout.write(BgnPtr,SpcPtr-BgnPtr);
                       cout << endl;  // I forgot this.  It is the key!.
                       BgnPtr = SpcPtr;
                   };
                   if (CurChr == ' ')
                      SpcPtr = ChrPtr - 1;
                   if (CurChr)
                      break;
             }
          }
0
 
ozoCommented:
while( strlen(s0) > boundary && (s1 = strchr(s0,' ')) ){
   char *s2;
   while( (s2 = strchr(s1+1,' ')) && s2-s0 <= boundary ){ s1=s2; }
   *s1 = '\0';
   cout << "     " << s0 << endl;
   *(s0 = s1) = ' ';
   s0++;
   line_no++;
}
cout << "     " << s0 << endl;

0
 
trestanCommented:
NOTE:
You may wish to reopen the question before grading and tell us which expert's answers/comments you prefer.  Then the relevant expert can propose an answer for you.
0
 
onestarAuthor Commented:
Just want to say thanks for all your answer's.

neitod:
I can't get your program to run properly.

trestan:
Your code works fine but it does change the string which in the case i am using it for doesn't make a difference to me.

ozo:
Your change works probably the best and it doesn't change the value of string.  I think you had an advantage since you wrote most of the orginal code.

Thanks
0
 
trestanCommented:
I understand you requirment is not limited to the fixed length in out put.
Now try this code:

0
 
trestanCommented:

// string2.cpp

#include <string.h>
#include <iostream.h>
#include <conio.h>
#include <stdio.h>
//extern "C"
int main()
{
char string[80];
char *s0 = string;
char *s1;
int boundary;
int line_no;

line_no = 1;
boundary = 10;

//clrscr();
cout << "Enter string: "<<flush;
gets(string);

      cout<<"    "<<flush;
for(int i=0;i<=strlen(string);i++)
{
      //gotoxy (20,5);
      putchar(string[i]);
      if (i>=line_no*boundary&&string[i]==' ')
      {
            line_no++;
            cout<<"\n";
            cout<<"    "<<flush;
      }


}
 
cout << endl << "line_no = " << line_no;
return 0;
}

//You do not need to declare the additional S0,S1,S2 at all.
0

Featured Post

Technology Partners: 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!

  • 10
  • 7
  • 2
  • +3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now