What does this code do?

This is a discussion on What does this code do? within the C++ Programming forums, part of the General Programming Boards category; 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. ...

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    3

    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
    Code:
    int
    main()
    instead of just
    Code:
    main()

  2. #2
    The Richness... Richie T's Avatar
    Join Date
    Jan 2006
    Location
    Ireland
    Posts
    469
    >>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
    No No's:
    fflush (stdin); gets (); void main ();


    Goodies:
    Example of fgets (); The FAQ, C/C++ Reference


    My Gear:
    OS - Windows XP
    IDE - MS Visual C++ 2008 Express Edition


    ASCII stupid question, get a stupid ANSI

  3. #3
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,638
    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.

  4. #4
    Registered User
    Join Date
    May 2006
    Posts
    3
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Extended ASCII Characters in an RTF Control
    By JustMax in forum C Programming
    Replies: 18
    Last Post: 04-03-2009, 08:20 PM
  2. Enforcing Machine Code Restrictions?
    By SMurf in forum Tech Board
    Replies: 21
    Last Post: 03-30-2009, 07:34 AM
  3. Obfuscated Code Contest
    By Stack Overflow in forum Contests Board
    Replies: 51
    Last Post: 01-21-2005, 03:17 PM
  4. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM
  5. Replies: 0
    Last Post: 02-21-2002, 05:05 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21