Solved

help with code

Posted on 1998-06-17
22
194 Views
Last Modified: 2010-04-01
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
Comment
Question by:onestar
  • 10
  • 7
  • 2
  • +3
22 Comments
 
LVL 22

Expert Comment

by:nietod
ID: 1166076
answer follows.
0
 
LVL 11

Expert Comment

by:alexo
ID: 1166077
What's wrong with:

    const char[] spaces = "                    ";

    // ...

    cout << spaces << string;
0
 
LVL 8

Expert Comment

by:trestan
ID: 1166078
gotoxy (20,5);
for(int i=0;i<=strlen(string);i++)
{
putchar(string[i]);
if (string[i]=='\n')
gotoxy (20,5);
}
0
 
LVL 8

Expert Comment

by:trestan
ID: 1166079
gotoxy (20,5);
for(int i=0;i<=strlen(string);i++)
{
putchar(string[i]);
if (string[i]=='\n')
gotoxy (20,5);
}
0
 
LVL 8

Expert Comment

by:trestan
ID: 1166080
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
 
LVL 22

Expert Comment

by:nietod
ID: 1166081
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
 
LVL 22

Expert Comment

by:nietod
ID: 1166082
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
 
LVL 8

Expert Comment

by:trestan
ID: 1166083
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
 
LVL 22

Expert Comment

by:nietod
ID: 1166084
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
 
LVL 8

Expert Comment

by:trestan
ID: 1166085
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
 
LVL 22

Expert Comment

by:nietod
ID: 1166086
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
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 8

Expert Comment

by:trestan
ID: 1166087
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
 
LVL 2

Expert Comment

by:VEngineer
ID: 1166088
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
 
LVL 1

Author Comment

by:onestar
ID: 1166089
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
 
LVL 22

Expert Comment

by:nietod
ID: 1166090


   // 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
 
LVL 8

Expert Comment

by:trestan
ID: 1166091
// 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
 
LVL 22

Expert Comment

by:nietod
ID: 1166092
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
 
LVL 84

Expert Comment

by:ozo
ID: 1166093
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
 
LVL 8

Expert Comment

by:trestan
ID: 1166094
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
 
LVL 1

Author Comment

by:onestar
ID: 1166095
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
 
LVL 8

Accepted Solution

by:
trestan earned 50 total points
ID: 1166096
I understand you requirment is not limited to the fixed length in out put.
Now try this code:

0
 
LVL 8

Expert Comment

by:trestan
ID: 1166097

// 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
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 viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

758 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

14 Experts available now in Live!

Get 1:1 Help Now