Able to use correct syntax as a beginner

This code took me while to figure out and a few re-reads of the explanations of each line of code for me to fully understand. Because this is new to me I don't fully understand on how and when to use the things I've been introduced to.
My question is that will it take me a while to know when and how to correctly use code? And does it come naturally to some people or is every beginner in the same boat as they learn x programming language?
Also what would be another example of use an . (dot) because I'm still not 100% confident on that.
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
// String Tester
// Demonstrates string objects

#include <iostream>
#include <string>

using namespace std;

int main()
{
    string word1 = "Game";
    string word2("Over");
    string word3(3, '!');

    string phrase = word1 + " " + word2 + word3;
    cout << "The phrase is: " << phrase << "\n\n";

    cout << "The phrase has " << phrase.size() << " characters in it.\n\n";

    cout << "The character at position 0 is: " << phrase[0] << "\n\n";

    cout << "Changing the character at position 0.\n";
    phrase[0] = 'L';
    cout << "The phrase is now: " << phrase << "\n\n";

	for (unsigned int i = 0; i < phrase.size(); ++i)
	{
        cout << "Character at position " << i << " is: " << phrase[i] << endl;
	}

    cout << "\nThe sequence 'Over' begins at location "; 
    cout << phrase.find("Over") << endl;

    if (phrase.find("eggplant") == string::npos)
	{
        cout << "'eggplant' is not in the phrase.\n\n";
	}

    phrase.erase(4, 5);
    cout << "The phrase is now: " << phrase << endl;

    phrase.erase(4);
    cout << "The phrase is now: " << phrase << endl;

    phrase.erase();
    cout << "The phrase is now: " << phrase << endl;

    if (phrase.empty())
	{
        cout << "\nThe phrase is no more.\n";
	}

	return 0;
}
Last edited on
The . in phrase.erase()?

I'm not exactly sure the proper terms for this, but it's basically a way to access the members of an object. Compare this little code to the way you use the string object:
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include <iostream>
using namepsace std;

class Data
{
public:

  int number;

  Data()
  {
    number = 0;
  }
  
  Data(int n)
  {
    number = n;
  }

  void increase()
  {
    number++;
  }
  
  void decrease()
  {
    number--;
  }
};

int main()
{
  Data myData01;
  Data myData02(10);

  cout << "myData01's number = " << myData01.number << endl;
  cout << "myData02's number = " << myData02.number << endl;

  myData01.increase();
  myData02.decrease();

  cout << "myData01's number = " << myData01.number << endl;
  cout << "myData02's number = " << myData02.number << endl;

  return 0;
}


If you don't know about functions, then this code probably makes zero sense.

As for your other questions. I think we're all in the same boat as far as knowing how/when to use certain C++ things. However, when I became a beginner to C++, I was already pretty strong with calculus and linear algebra which meant that I generally thought about questions in a more efficient way in the first place.
Last edited on
Topic archived. No new replies allowed.