two questions

1. When do I use {} brackets to close things off. I never know when to use them.

2. Can microsoft Express c++ be used to modify existing programs such as video games.if so how do I modify an executable?
1: Use braces to inclose statements. After if/while/else/if else/ function declarations / class declarations ... The first brace opens the statement, the 2nd closes it. e.g.

if(x == 1)
{//Open statement.
//Do some action(s)
} //Close statement

while(x > y)
{
//Do some action(s)
}

int main()
{
//write a program
}

You don't have to use them for statements that are only one line long, but it is good practice to do so, as it makes your code a little more readable.

2) Only if you can get hold of the source code and the appropriate libraries. In short, you cannot modify an .exe file.
I have one more question. How do I keep the console window open? Even if I write something allowing user input the console window closes as soon as the input is entered.
One way to do it is to call system("PAUSE"); before your return 0.
Don't use that. This code will suffice:
1
2
cin.sync();
cin.ignore();
Just press enter to close.

If you want to be safe, use this:
1
2
3
cin.clear();
cin.sync();
cin.ignore(std::numeric_limits<std::streamsize>::max());
Last edited on
Even the second variant isn't all that safe, though.
Consider:

1
2
3
4
5
int i;
cin >> i;
cin.clear();
cin.sync();
cin.ignore(std::numeric_limits<std::streamsize>::max(),'\n');


That still won't keep the window open (although it depends on the standard library implementation).
what about cin.get()? and you forgot about structs and do while loops
Same for cin.get(). It'll only work when the input buffer is completely empty, while the other variant can handle characters besides newlines.
Edit: see here for a fully working solution: http://www.daniweb.com/software-development/cpp/tutorials/90228
Last edited on
Athar, what? What is wrong with it? The cin.sync(); gets the new line from the cin >> i; so there should be no problem.
Topic archived. No new replies allowed.