I have...another question.

This is a discussion on I have...another question. within the C++ Programming forums, part of the General Programming Boards category; In my homework, my teacher gave us a piece of code asking, "What would be the output of the following ...

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    13

    I have...another question.

    In my homework, my teacher gave us a piece of code asking,

    "What would be the output of the following piece of code?"

    The code is this:

    Code:
    include <stdio.h>
    main() {
         int x;
    
         x=5;
         while (x < 7);
         {
              printf("value of x is %d\n", x);
              x = x + 1;
         }
         printf("final value of x is %d\n", x);
    }
    But when I run that, NOTHING happens! There is absolutely nothing on the screen. Not even "press any key to continue" or whatever. And "nothing" is not a choice...

    I'm not asking for the answer, I'm just confused as to why nothing happens when apparently my teacher thinks it should. Am I doing something wrong?

  2. #2
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    check the faq: http://faq.cprogramming.com/cgi-bin/...&id=1043284385
    you probably want to run the program from the console/command prompt. also, this is a c program not c++.

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    The code above will run forever without any output. Did your teacher actually say there would be output? If so, double-check that you copied it properly. BTW, it's good practice to specify the return type of main explicitly:
    Code:
    int main(void) {
    You should normally also have "return 0;" at the end of main() in a C program, which this is, though here it doesn't matter since it never gets reached.

  4. #4
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    ah good eye, robatino. didnt see that semi colon!

  5. #5
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,475
    Also you dont need a semi-colon after the while loop header. That reads it as a single statement and not a loop which is technically a logical error
    I'm just trying to be a better person - My Name Is Earl

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    One last thing: this is the C++ forum, and as your code is C, next time post it in the C one.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Alice....
    By Lurker in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 06-20-2005, 02:51 PM
  2. Debugging question
    By o_0 in forum C Programming
    Replies: 9
    Last Post: 10-10-2004, 05:51 PM
  3. Question about pointers #2
    By maxhavoc in forum C++ Programming
    Replies: 28
    Last Post: 06-21-2004, 12:52 PM
  4. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  5. Question, question!
    By oskilian in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 12-24-2001, 12:47 AM

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