Counter component in 'while' control structure

Disclaimer: I am a student taking an intro to programming. I have some experience in Python because that's what I'll be using/have used in the Geographic Information Systems industry. I recently went back to school to make my knowledge in GIS official. I do enjoy learning C++, but this has me frustrated!

I looked through this thread and couldn't find a similar topic. My instructor saw fit to do most of his examples in C++. I do not need a solution to my problem, just guidance. The first question relates to the example code in my online module:

1
2
3
4
5
6
  counter = 0;
while (counter < 5)
  {
  cout << "\nI love computers!";
  counter ++;
  } 


In the fifth line, there are two plus signs after the 'counter' variable. Why?

My second question has to do with the simple code we are to turn in tomorrow. The cout is a silly line "I love computers!" I cannot get it to print on my screen. The pseudocode my professor gave us to write our code from reads like this:

output
if age greater than 17
display "You can vote."
else
display "You can't vote."
endif

counter is assigned 0
while counter is less than 5
display "\nI love computers!"
increment counter
endwhile

pause so the user can see the answer

The 'if then else' part works fine in my code, but the I love computers never shows up. Is there something missing from this pseudocode? I am using a Dev-C++ Bloodshed compiler and it turned in no errors after I ran it.

Could someone point me in the right direction? I tried to use a simple header file with pause, delay, and clear screen functions. I'm wondering if clearing the screen after the 'if then else' part runs, but there is no input afterward. Below is what I have so far, I'm sorry if I posted too much information, I am new at this!


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
56
57
58
59
60
61
62
63
64
65
66
#include <iostream>  
using namespace std;

// Function Prototypes

void pause(void);

// Variables

int         age;
int         counter;


//******************************************************
// main
//******************************************************

int main(void)
  {
  // Input	
  cout << "\nHow old are you? --->: ";
  cin >> age;
  
  // No Processing
  
  // Output
  // Endif
  
if (age > 17)
  
  cout << "You can vote.";
  
else
  
  cout << "You can't vote.";
  
  pause();
  return 0;

  //While

counter = 0;
while (counter < 5)
  
  cout << "\nI love computers!";
  counter ++;

  pause();
  return 0;
  }

//******************************************************
// pause
//******************************************************

void pause(void)
  {
  cout << "\n\n";
  system("PAUSE");
  cout << "\n\n";
  return;
  }

//******************************************************
// End of Program
//****************************************************** 
For the first question, the counter ++; is an abbreviated version of counter += 1;, which is in turn an abbreviated version of counter = counter + 1;. In other words, it increments the variable by one. One thing you have to note, however, is that the placement of the increment operator does matter. If it is preceding the variable, the variable will increment before being used. If placed after, the variable will be used prior to the incrementation. Here, they both work.

As for your second question, look at line 38. If main returns, how can line 42 ever be reached?
Thank you Ispil! I deleted the return line on 38 and upon executing, it returned 'I love computers' *infinity.

Earlier in the class, we did learn +=, -=, etc shortcuts. The ++ was new to me. My prof will introduce things early in class and explain them later. I never fully understood what 'return' does either. It clicks now.
For the record, the infinite loop was a bad thing. I deleted line 37 as well as 38 and the loop stopped at five "I love computers" like it was intended to.
Topic archived. No new replies allowed.