Thread: Code explanation..

  1. #1
    Registered User snapshooter's Avatar
    Join Date
    Sep 2004
    Posts
    37

    Unhappy Code explanation..

    Could someone explain that code:
    int main()
    int x;
    while (cin >> x){ <-------(explain this!)
    for (int i=x; i>0; i--) {
    cout << "*";
    }
    cout << endl;
    }
    return 0;
    }
    thanks in advance.

  2. #2
    Hello,
    Code:
    while (cin >> x) {
    Ah, this. cin is the standard input stream. >> operator means "get from", while << operator means "put to". This code is taking the information from the input stream and storing it in your variable x.

    - Stack Overflow
    Last edited by Stack Overflow; 11-16-2004 at 04:42 PM.
    Segmentation Fault: I am an error in which a running program attempts to access memory not allocated to it and core dumps with a segmentation violation error. This is often caused by improper usage of pointers, attempts to access a non-existent or read-only physical memory address, re-use of memory if freed within the same scope, de-referencing a null pointer, or (in C) inadvertently using a non-pointer variable as a pointer.

  3. #3
    Registered User
    Join Date
    Jun 2004
    Posts
    722
    snap you're new to the forum so please read this thread...
    and wellcome

    about your code, 1st consider what Stack Overflow said.
    So after reading something to x you have a while statement, which is like.
    Code:
    while( expression ){
        //to do here
    }
    While expression evaluates to true, or in other word by C/C++ standards different than 0 (correct me if I'm wrong), the to do code is execited. So while x is different than 0....
    Last edited by xErath; 11-16-2004 at 05:05 PM.

  4. #4
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,090
    Actually, the operator>> returns a reference to the input stream (in this case cin). The streams have an implicit conversion that allows you to check to see if the input succeeded or failed. So if cin succeeded in getting data, and is still in a good state, then the loop will continue. If cin failed to get the data (for example, the user entered a letter instead of a number), then the failbit will be set and the cin stream will convert to false.

    So if the user enters 0, then x will be 0 but the loop will continue because the input was valid and cin will still be in a good state.

  5. #5
    Registered User
    Join Date
    Jun 2004
    Posts
    722
    Upps.. bah! you're right jlou.. I got blinded a bit, but like someelse here at the forums would say, "was beer...", or not

    So.. in**couff** cicle.. good!!

  6. #6
    Registered User snapshooter's Avatar
    Join Date
    Sep 2004
    Posts
    37
    Code:
    ok, i understood.. ;)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Proposal: Code colouring
    By Perspective in forum A Brief History of Cprogramming.com
    Replies: 28
    Last Post: 05-14-2007, 07:23 AM
  2. Values changing without reason?
    By subtled in forum C Programming
    Replies: 2
    Last Post: 04-19-2007, 10:20 AM
  3. Code Explanation
    By dmkanz07 in forum C Programming
    Replies: 1
    Last Post: 03-27-2007, 08:24 PM
  4. Obfuscated Code Contest
    By Stack Overflow in forum Contests Board
    Replies: 51
    Last Post: 01-21-2005, 04:17 PM
  5. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM