Hi

I am new to C++ and I would be grateful for some help with the following:

#pragma argsused
int main(int argc, char **argv)
{
 AnsiString Line;
 int Index;

Line = ReadStringPr("Enter your text. It must not finish with a space character." );
Index = 1;
 while (Index <= Length(Line))
 if (Line[Index] != ' ');
 {
 WriteChar(Line[Index]);
 Index = Index + 1;
 }
 else
 WriteStringCr(' ');

 while (Line[Index] == ' ');
        Index = Index + 1;

 getchar();  // to keep the display on until you press Enter key
 return 0;
 }

Using Builder I get a misplaced else statement but I cannot seem to locate the source of the problem. I am trying to remove multiple character spaces entered by the user and output their entered text with only 1 character space between words.

Many thanks!!

Recommended Answers

All 14 Replies

Remove the semicolon here and you should have no problems:

if (Line[Index] != ' ');

Hi

What a quick replay thanks! The program compiles no problem but the screen is blank and does not reproduce what you input (containing multiple spaces between words) with a string containing only 1 space character between words :sad:. Is my loop flawed?!?

Thanks again.

Remove the semicolon here and you should have no problems:

if (Line[Index] != ' ');

Is this more what you're trying to do

//read each char of Line
while (Index <= Length(Line))
{
   //if current char is not a space
   if (Line[Index] != ' ')//;
   {  
      //write current char to screen
      WriteChar(Line[Index]);
      Index = Index + 1;
   }
   else //if current char is a space
   {
      //have no idea what this does
      WriteStringCr(' ');

      //ignore all consecutive spaces 
      while (Line[Index] == ' ')//;  this semicolon may have created an infinite loop not allowing anything to print to screen
        Index = Index + 1;
    }
}

Hi

That is exactly right although the corrected sentence is broken up with individual words being written out on separate lines rather than in one continuous sentence.

Thanks!!

Is this more what you're trying to do

//read each char of Line
while (Index <= Length(Line))
{
   //if current char is not a space
   if (Line[Index] != ' ')//;
   {  
      //write current char to screen
      WriteChar(Line[Index]);
      Index = Index + 1;
   }
   else //if current char is a space
   {
      //have no idea what this does
      WriteStringCr(' ');

      //ignore all consecutive spaces 
      while (Line[Index] == ' ')//;  this semicolon may have created an infinite loop not allowing anything to print to screen
        Index = Index + 1;
    }
}

Commenting what you want your code to do will help you immensely in figuring out where you go wrong. When you're starting out it's almost impossible to comment too much. (As you get more experienced you can leave out mundane comments, though you should leave in high level comments and even detailed comments if what you write isn't self evident.)

Do you mean that with the corrections provided you get a single string of char without spaces and you want each word isolated in a single line instead? If so, figure out what tells you that you've come to the end of a word, and how would you go to a new line if you've come to the end of a word.

The functions and variables used in the program you posted are not standard and I have no idea what they do or where they come from and why the program compiles unless somehow all the functions and variables you're using are somehow available through that (to me) weird looking pragma statement.

Hello

Some good advice which I will heed. The corrections print out each word of the sentence entered on a seperate line. One word per line. I am trying to get the program to remove multiple spaces entered between words but print out the new sentence with only 1 space per word (all on the same line). I still think there may be a problem with my loop.

Thanks again.

Commenting what you want your code to do will help you immensely in figuring out where you go wrong. When you're starting out it's almost impossible to comment too much. (As you get more experienced you can leave out mundane comments, though you should leave in high level comments and even detailed comments if what you write isn't self evident.)

Do you mean that with the corrections provided you get a single string of char without spaces and you want each word isolated in a single line instead? If so, figure out what tells you that you've come to the end of a word, and how would you go to a new line if you've come to the end of a word.

The functions and variables used in the program you posted are not standard and I have no idea what they do or where they come from and why the program compiles unless somehow all the functions and variables you're using are somehow available through that (to me) weird looking pragma statement.

When you find the first space after an alphbetical character you ignore it, place a null char at Line[Index], print Line, which is now a C style string, to the screen followed by sending a newline char to the screen and set Index back to zero to start filling in the next word value in Line and end up going back to the top of the while() loop after ignoring any consecutive spaces.

The corrections print out each word of the sentence entered on a seperate line. One word per line. I am trying to get the program to remove multiple spaces entered between words but print out the new sentence with only 1 space per word (all on the same line). I still think there may be a problem with my loop.

Probably. But I don't think anyone here has perfected psychic debugging yet. Without knowing what the code looks like, what can we say?

The code is above!

The code is above!

You mean after all these suggestions you haven't made any changes to the code you posted? No wonder it's not working. :rolleyes:

commented: What the heck, you deserve some more rep after all your hard work... --joeprogrammer +8

Here's an algorhythm to write each word in Line to the screen one word per line, printing one char at a time to the screen. Each comment corresponds to at most a few lines of actual code (in some cases it's one line of code per comment). Since you are using commands I'm not familiar with it's up to you to write the actual code. You've already done most of it. Figuring out how to do the new line thing is all that's really left. Since you are writing one char to screen at a time instead of one word to the screen at a time, you don't need to use the null char as I suggested in my last post. Post your updated code and list of questions, errors, etc, if you are having problem.

//use a loop to read each char of Line
   //if current char is not a space
         //write current char to screen
         //go to next char in Line
   //if current char is a space
      //go to next line of screen by sending newline char to screen
      //ignore all additional consecutive spaces 
      //go to next char in Line
//go back to top of loop

The final solution is as follows:

AnsiString Line;
 int Index;

Line = ReadStringPr("Enter your text. It must not finish with a space character." );
Index = 1;
//read each char of Line

while (Index <= Length(Line))
{
   //if current char is not a space
   if (Line[Index] != ' ')//;
      {
      //write current char to screen
      WriteChar(Line[Index]);
      Index = Index + 1;
      }
   else //if current char is a space
      {
      //to place a space after each word is written
      WriteChar(' ');

      //ignore all consecutive spaces
      while (Line[Index] == ' ')//;  this semicolon may have created an infinite loop not allowing anything to print to screen
        Index = Index + 1;
      }
}
 getchar();  // to keep the display on until you press Enter key
 return 0;
 }

My problem was the use of WriteCharCP which was placing each word on a different line. Essentially this works perfectly. Sincere thank to Learner for all your constructive comments.

Here's an algorhythm to write each word in Line to the screen one word per line, printing one char at a time to the screen. Each comment corresponds to at most a few lines of actual code (in some cases it's one line of code per comment). Since you are using commands I'm not familiar with it's up to you to write the actual code. You've already done most of it. Figuring out how to do the new line thing is all that's really left. Since you are writing one char to screen at a time instead of one word to the screen at a time, you don't need to use the null char as I suggested in my last post. Post your updated code and list of questions, errors, etc, if you are having problem.

//use a loop to read each char of Line
   //if current char is not a space
         //write current char to screen
         //go to next char in Line
   //if current char is a space
      //go to next line of screen by sending newline char to screen
      //ignore all additional consecutive spaces 
      //go to next char in Line
//go back to top of loop

Hi All

I have another question. I need to remove the leading spaces in any line input so that the output of this program displays the string without a leading space. I have come up with the following code but it only outputs the first letter of the first word and not the whole string:

int main(int argc, char **argv)
{
 AnsiString Line;
 int Index;

Line = ReadStringPr("Enter your text. It must not finish with a space character." );
Index = 1;
//read each char of Line

while ((Index <= Length(Line) && Line[Index] == ' '))
        Index = Index + 1;
{
   //if current char is not a space
   if (Line[Index] != ' ')//;
      {
      //write current char to screen
      WriteChar(Line[Index]);
      Index = Index + 1;
      }
   else //if current char is a space
      {
      //to place a space after each word is written
      WriteChar(' ');

      //ignore all consecutive spaces
      while (Line[Index] == ' ')//;  this semicolon may have created an infinite loop not allowing anything to print to screen
        Index = Index + 1;
      }
}
 getchar();  // to keep the display on until you press Enter key
 return 0;
 }

Can you see where I have done wrong?

while ((Index <= Length(Line) && Line[Index] == ' '))
        Index = Index + 1;
{

The line Index = Index + 1; shouldn't be there.

I believe Ansistrings are based on C style character arrays which have indexes that start at zero, not 1, so you may not be evaluating the first char of Line if you start evaluating at Line[1]. Otherwise I think the program should already deal with leading spaces.

You might want to clean up some of the comments from the program before you turn it in.

I'd suggest you add an additional condition in the inner while loop to prevent reading off the end of the array/Line like you do in the outer loop. This would allow the program to handle terminal spaces.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.