is this a typo in C++ for dummies?

Aug 2, 2015 at 1:05am
Write your question here.
Very much a BEGINNER. Can someone explain to me what this line is for in this program? (*realmessage)[6] = 'i'; It seems to have no use at all. Also when [6] is changed to [3] or less it actually does something. i.e. is [4] and above it has no affect at all. Also note that if the line is commented out it changes nothing and the code compiles and runs fine(i think) the code is from C++ all-in-one for dummies 2nd ed

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
<
  #include <iostream>

using namespace std;

void Paranoid(string *realmessage)
{
        (*realmessage)[6] = 'i';
        realmessage->replace(9, 1, "");
        realmessage->insert(18, "ad");
        realmessage->replace(15, 2, "in");
        realmessage->replace(23, 7, "!");
        realmessage->replace(4, 3, "ali");
}

int main()
{
    string message = "The friends are having dinner";
    cout << message << endl;
    cout << endl;
    Paranoid(&message);
    cout << message << endl;

    return 0;
}
>
Aug 2, 2015 at 1:40am
Can someone explain to me what this line is for in this program? (*realmessage)[6] = 'i';

This line changes the character at position 6 in given string to 'i'. As is it has no effect because the character at this position is already 'i'.This explains why commenting out the line changes nothing to the program.
Aug 2, 2015 at 1:53am
First, thank you for responding to my post. I understand what you are saying but when I changed the 'i' to a 'W', it did not change. I then played around with it and through experimenting I found that the only characters I could change were at positions [0] through [3]. This confused me as I thought I was starting to understand what I was reading.
Aug 2, 2015 at 2:02am
Your understanding is fine. The function is (purposefully) confusing.
Line 13 changes whatever you did to message[6] back to an 'i'.

Try commenting out line 13.
Aug 2, 2015 at 2:04am
Oh I think I understand now.This must be cause of the last line
realmessage->replace(4, 3, "ali");
No matter what you change at the first line it will be 'i' again cause of this line replacing substring 'fri' to 'ali'.
Last edited on Aug 2, 2015 at 2:12am
Aug 2, 2015 at 2:12am
closed account (E0p9LyTq)
Try adding intermediate cout statements to see how the string is changed.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
void Paranoid(string *realmessage)
{
   cout << "*** beginning string conversion! ***\n\n";

   (*realmessage)[6] = 'i';
   cout << *realmessage << "\n\n";

   realmessage->replace(9, 1, "");
   cout << *realmessage << "\n\n";

   realmessage->insert(18, "ad");
   cout << *realmessage << "\n\n";

   realmessage->replace(15, 2, "in");
   cout << *realmessage << "\n\n";

   realmessage->replace(23, 7, "!");
   cout << *realmessage << "\n\n";

   realmessage->replace(4, 3, "ali");
   cout << *realmessage << "\n\n";

   cout << "*** string conversion complete! ***\n\n";
}


The friends are having dinner

*** beginning string conversion! ***

The friends are having dinner

The friens are having dinner

The friens are havading dinner

The friens are invading dinner

The friens are invading!

The aliens are invading!

*** string conversion complete! ***

The aliens are invading!


The command to change the string is correct and working, changing an 'i' to an "i'.
Last edited on Aug 2, 2015 at 2:39am
Aug 2, 2015 at 2:38am
to all ok now i get it thank you
Topic archived. No new replies allowed.