-3

I did not write this code. i'm on my 3rd day of coding in C++ and i'm having a hard time understanding how incremnent works in general.

int main()
{
  int antal_ord {};
  double medellangd {};
  string kort_ord;
  string langt_ord;
  int min_length {100}; 
  int max_length {};
  string S;

 cout << "Mata in en text:\n" << endl;

  while (cin >> S)
    {
      if (S.length() > max_length)
    {
      max_length = S.length();
      langt_ord = S;
    }
      
      if (S.length() < min_length)
    {
      min_length = S.length();
      kort_ord = S;
    }
      

      medellangd+=S.length();
      antal_ord++;
      
    }
  

  if (antal_ord == 0)
    {
      cout << "Inga ord matades in." << endl;
    }
  else {
    medellangd = (medellangd / antal_ord);
    round(medellangd);
    
    cout << "Texten innehöll " << antal_ord << " ord." << endl;
    cout << "Det kortaste ordet var " << '"' << kort_ord << '"' << " med "
     << kort_ord.length() << " tecken." << endl;
    cout << "Det längsta ordet var " << '"' << langt_ord << '"' << " med "
     << langt_ord.length() << " tecken." << endl;
    cout << "Medelordlängden var "<< fixed << setprecision(1) << medellangd << " tecken.";
  }
  return 0; 
}

antal_ord is the variable for the amount of words written in this scenario. In the line where it says "cout << "Texten innehöll " << antal_ord << " ord." << endl;" how does it know how many words have been written? The only time this variable is used before this line is when the variable gets incremented, but how does that let the variable know how many words have been written in total? and also the .length command, does it basically just count the amount of letters written?

Zeptuz
  • 103
  • 7

2 Answers2

0

There's really nothing special going on here. Every time you read one word with cin >> S, you increment antal_ord by one. Since you started with zero words written and antal_ord==0, at the end antal_ord will equal the number of words read from cin.

Similarly, S.length() returns the number of letters currently in S. In your case, that is exactly the number of letters read from cin since you didn't chance S after reading. But if you did S += " some extra letters, then S.length() will of course change.

MSalters
  • 173,980
  • 10
  • 155
  • 350
  • Yea I just realized the antal_ord++; was still in the loop, what actually confused me was that I thought it was outside the loop. – Zeptuz Dec 10 '22 at 13:13
0

When you'll learn about most programming languages, you'll start off with basics: syntax, data types, declarations (vars + funcs as well as other possible concepts), loops, calls, math operations and other code-control techniques relevant to each programming language.

What you'll see about most (and I;ll try to "rewind" from the generalization I started with and back down to C/C++) is that you have the following type of math operation variations when it comes to addition (let's focus on this, as it's more on point with the question).

  1. result in a separate variable, in our case b: b = a + 1;
  2. result in the same variable: a += 1;
  3. incrementing the value of the variable: a++;

Expanding on it:

  1. In the first case, b will have its value overwritten and is dependent on a different values (in this case the value of a and 1). What you need to focus on here is that a is NOT changed.

  2. In this case, a receives a new value and is incremented by the right-side-value, in our case 1. a is changed by adding one (not incrementing)

  3. In our case, similar to #2, the value of 8a* is updated, but the incrementation is done by 1.

Apart from syntactic sugar or code style preference, the difference between each is also in the way the variables are assigned their values (more formally said, in the assembly code "underneath"). This topic is a lot more complicated for someone that started programming, but focusing on the question, the answer is simply that ++ increments the value by 1.

Also note that there is a difference in certain coding flows between ++a and a++. Mainly in loops. For ++a the value is set before executing the code, using the already incremented value in the code, while a++ uses the current value of a first, then increments it.

Try it like this:

int i = 0;
while (++i < 100)
{
     std::cout << i << std::endl;
}

... versus...

int i = 0;
while (i++ < 100)
{
     std::cout << i << std::endl;
}

Then count how many lines each case wrote.

There is also a small caveat you should be aware of, it's a bit more advanced, so it's just a little "FYI" for you. There are two C++ techniques called "function overloading" and "operator (re)definition". Let's focus on the second one. You could build your own data type (for example a struct or class) and implement your own operators that do something other than what their arithmetic counterparts do. You'll see this in iterator definitions. In that case ++ is not "actual value incrementation" (so it's not a math calculation), but rather switching to the next item in a list. Once you reach std::vector lessons you'll encounter that.

TheNomad
  • 892
  • 5
  • 6