-2

I have a simple program but it's running weirdly. Basically the code runs fine but when the numbering at the beginning of the line comes into play, int x++ displays the same number as the first line then continues. Why does this happen?

Code:

#include "stdafx.h"
#include <iostream>
#include <windows.h>
#include <string>
#include "logo.h"

int main()
{
    SetConsoleTitle("plains.exe");

    displayLogo();

    int number;
    int addTotal = 0;
    int numbersEntered = 0;

    std::cout << " [1] enter your first number: ";
    std::cin >> number;

    while (number != -1) {
        addTotal = addTotal + number;
        numbersEntered++;

        std::cout << " [" << numbersEntered << "]" << " enter your next number or type '-1' to add them: ";
        std::cin >> number;
    }

    if (number == -1) {
        std::cout << " " << std::endl;
        std::cout << " --------------------------------" << std::endl;
        std::cout << " " << std::endl;
        std::cout << " the sum of your numbers is " << addTotal << "." << std::endl;
        std::cout << " you entered a total of " << numbersEntered << " numbers." << std::endl;
        std::cout << " " << std::endl;
        std::cout << " the average of your numbers is " << addTotal / numbersEntered << "." << std::endl;
        std::cout << " " << std::endl;
    }

    return 0;
}
melpomene
  • 84,125
  • 8
  • 85
  • 148
  • 1
    Possible duplicate of [What is the difference between prefix and postfix operators?](https://stackoverflow.com/questions/7031326/what-is-the-difference-between-prefix-and-postfix-operators) – APerson Nov 02 '17 at 03:33
  • 3
    *int x++ displays* -- I'm looking, and I see no sign of this code you speak of. – PaulMcKenzie Nov 02 '17 at 03:37
  • 3
    *int x++ displays the same number as the first line then continues* ?? That made no sense to me. Perhaps you can elaborate with real input and observed output. – R Sahu Nov 02 '17 at 03:37
  • can you give the part of the code that you think is the source of the issue? – O2Addict Nov 02 '17 at 03:39
  • My bad I used that as an example. I know what the prefix and postfix do and their difference. Also the code is the numbersEntered++. – official abxss Nov 02 '17 at 03:54
  • I have it printed between the brackets and the first line is set to [1] but when the program runs, the second line starts with 1 when it should be 2. – official abxss Nov 02 '17 at 03:55
  • @officialabxss You initialize `numbersEnetered` to zero, increment it once before printing the second line, and expect the printed result to be 2? – Daniel H Nov 02 '17 at 04:01
  • @official abxss: That makes no sense. Your `numbersEntered` is set to `0` initially. So the second line in your program will strat with `[1]` (after `numbersEntered++`), exactly as it should. What made you think it should be `[2]`? – AnT stands with Russia Nov 02 '17 at 04:02
  • Because wouldnt the numbersEntered++ make it 1 instead of 0 then when I input the first number it adds one making it two? – official abxss Nov 02 '17 at 05:44
  • Also, the code works fine if I change the `std::cout << " [" << numbersEntered<< "]" << " enter your next number or type '-1' to add them: ";` to `std::cout << " [" << numbersEntered + 1 << "]" << " enter your next number or type '-1' to add them: ";` Why would that work and not ++? – official abxss Nov 02 '17 at 05:49

1 Answers1

1

You initialized numbersEntered to 0. The first time through the while loop, it does numbersEntered++, which sets it to 1. So the first prompt in the loop contains [1]. This is the same as what you printed before the loop with:

std::cout << " [1] enter your first number: ";

so you see [1] twice.

To prevent this duplication, add 1 to the variable when displaying the prompt:

    std::cout << " [" << (numbersEntered++) << "]" << " enter your next number or type '-1' to add them: ";
Barmar
  • 741,623
  • 53
  • 500
  • 612