0

Hi All,

The program below is returning the following data...

A
1
AB
(empty line)
3
9

...when it should be returning...

A
1
AB
2
ABCDEFG
7

I've tried changing the assignment code statements to read MyString = MyString + "B\n"'; and MyString = MyString + "CDEFG\n"; , but that, expectedly, doesn't change any of the output, and I don't see any extra end-of-line characters. :confused: Can someone point me in the right direction?

// File Name: abc.cpp
// Project 4.2
// Author: (aeisntein, from text exercise)
// Date: 06.03.2006

#include <iostream> // necessary for cout command
#include "apstring.h" // point to apstring header file 

int main()
{
     // Declare empty string object MyString
     apstring MyString;

     // Assign value "A" to string object MyString
     MyString = 'A';

     // Output initial value of string object MyString
     cout << MyString << endl;

     // Output the length of initial value of string object MyString
     cout << MyString.length() << endl;

     // Concatenate character "B" to end of current value of string object MyString.
     MyString += "B\n";

     // Output current value of string object MyString
     cout << MyString << endl;
 
     // Output length of current value of string object MyString
     cout << MyString.length() << endl;

     // Concatenate string "CDEFG" to end of current value of string object MyString.
     MyString += "CDEFG\n";

     // Output length of current value of string object MyString
     cout << MyString.length() << endl;

     return 0;
}

TIA

1
Contributor
1
Reply
2
Views
10 Years
Discussion Span
Last Post by aeinstein
0

Well, guess what? There was an extra end-of-line character in each of the last two string assignments, and that was the issue all along. Funny, I read and re-read that code listing a dozen times or so and didn't pick it up! :mad: Then I decided it might be a glitch in the compiler, so I closed all of my programs, logged off and shutdown the PC. Then I got up and got a handful of grapes from the refrigerator and talked to my sis for a few minutes; came back to the PC and rebooted it, and then recompiled the program, and guess what? NO CHANGE!!! :mad: Then I re-launched NotePad++ (I'm a new user, but so far I like NotePad++ very much!), and started re-reading the code list again, and saw the extra "\n"'s right away! :eek: Just goes to show that taking a break every once in awhile really does pay off! ;)

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.