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.

Kareem

  • Visual Studio 2012 Ultimate

Recommended Answers

All 2 Replies

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.