Beginner here so bear with me. This is what I have so far:
What I want this to do is increment x by 1 repeatedly and print the current value of x at any given time until it reaches 1000, in which case it should add the word "finally". When I compile and run this code it increments x all the way up to 1000 but when it reaches 1000 it simply prints "X is equal to 1000.", ignoring the condition of the if statement that encloses it.Code:#include <iostream> using namespace std; int main() { int x = 0; if (x == 1000) { cout<<"X is finally equal to 1000.\n"; } else { for (; x <= 1000; x++) { cout<<"X is equal to " << x << ".\n"; } } cin.ignore(); }
I think the problem is that the for loop wants to increment x until x is less than or equal to 1000, but at that point I want the if function to take over, and print its message. I'm not really sure how to go about doing that.
Thanks in advance.



LinkBack URL
About LinkBacks



