Hello everyone,

I'm studying the while statement now, and there is an example code and I tried to make it and see what it does. When I wanted to start it said it has errors and when I started with debugging I get a weird statement on my cmd program.
The code:

// While statement program

#include <iostream>

int main()
    int sum = 0, val = 1;
    while (val <= 10) {
        sum += val;
        ++= val;
    std::cout << "The sum of 1 to 10 inclusive is" << sum << std::endl;
    return 0;

What have I done wrong? There are also some screenschots of Visual Studio* and the CMD in the attachments.


  • Visual Studio 2012 Ultimate
Attachments While_statement_error.jpg 286.91 KB While_Statement_VS.png 106.24 KB
4 Years
Discussion Span
Last Post by andreas.bjorn

Change ++= val; to val++;. (Or if you want harder to read code, remove ++= val; and change sum += val; to sum += val++;)


(Or if you want harder to read code ...

Why would you even suggest that? ...

Like he said, the compiler has no clue what you're doing here: ++= val; ..
The proper syntax to add One to your val-variable is to use val++;
In another case, you might want to add more than one to your val-variable, in that case do like so:
val += x; , where x is the amount you wish to increase val with.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.