-
What does this code do?
I was trying the calculator challange at http://www.cprogramming.com/complete/calcans.html and I seen this bit of code that I dont quite understand. I sorta remember reading but couldn't find where I thought I read it. Any help would be appreciated.
Code:
{ char op='c'; int x, y;
while(op!='e') {
Also i was wondering why it says to do
instead of just
-
>>char op='c'; int x, y;
This creates a variable of char called op and gives it an initial
value of the character c. then it creates two integers x and y.
>>while(op!='e') {
then the program will be entering a loop ( because op is 'c', not
'e', the looping condition is true and it will execute the code in it).
also it says to use int main instead of just main because C/C++
are standardised - they must return an integer to the operating
system according to that standard.
next time, look up a tutorial
-
Code:
int
main() {
char op='c'; // create a character called op and initialise it t the letter 'c'.
int x, y; // create to integers for later called x and y
while(op!='e') { // while op isn't 'e' do...
op is just a sentinal variable that gets changed every time the loop runs through. It lets the program know what the user wants to do. When the user types e the loop will break, and the program exits.
As for your other question, main returns an int. Even though this is commonly known by programmers, you should type it because you are defining main. All functions, including main need a return type.
-
i looked all through the tutorial for about 30 minutes before giving up and posting on here, also review my notes that i made while going through the tutorial.