Can someone help me with my program?

Hello, I am new to programming and working on a small program just testing something, this should raise b by one every 20 ticks but when I run it nothing happens.

#include <iostream>
using namespace std;


int x = 0;
int b = 0;
int main()
{
if(x==20){
b = b + 1;
x = 0;
}
else{
x = x++;
}
while(x==10){
cout << "Bacon count: " << b << endl;
}
return 0;
}
I think you have a conceptual misunderstanding - statements are executed in order once unless they are in a looping structure. Your if-else is not in a looping structure, so it is run once and never again. Because of this, x never changes to 10 and your while loop never runs either.

Also, x = x++; does not do what you think it does, in fact it does nothing at all. Just write ++x; by itself.

And for future reference, surround your code with code tags:

[code]
1
2
3
4
5
6
#include <iostream>

int main()
{
    std::cout << "Syntax highlighting makes code easier to read" << std::endl;
}
[/code]
Last edited on
Under what circumstances will it increase b by one?

Under what circumstances won't it increase b by one?

Given the initial values of x and b, do you think b will ever be increased by one?

Given the initial values of x and b, do you think the value of b will ever be written to the standard output?
Last edited on
Thanks L B and Mikey I intially thought that it would loop thus increasing x everytime x didn't equal 20 so it would've if it didn't work
Last edited on
Topic archived. No new replies allowed.