   A question
  I was wondering the following:Let's say this is my program:#include <iostream>#include <string>main int(){std::cout << "Please enter your first name: ";std::string name;std::cin >> name;const std::string greeting = "Hello, " + name + "!";std::cout << std::endl; // what does this do??int r = 0;while (r != rows) {std::cout << std::endl; // here I write a row of output}   return 0;}What does the expression before "// what does this do??" do? Just curious.There may be some mistakes in this, you may notify and say them, but please don't concentrate on the errors but on my questions.. And now my real question:Here the while is true (!= bool), what happens now? It will never become false, so the while will keep going and going. What happens? Does the while write a line of output every time it has tested the condition and then runs through the statement, or will it only produce one line of output ?My question is: When the while is true, and will keep being true, so it will keep being repeated, will it keep doing the things in the while body or will it only do it once? I know it will keep running through it, but it's a bit strange to have uncountable lines of output.Thanks![Edited by - Marly on September 13, 2007 3:47:57 AM]
  I dont programme in C++, but std::cout << std::endl; // what does this do??Cout -> Prints to the console (or the standard output device)endl -> inserts an "end of line" character to say that the next output will begin on a new line   ;  Yes, I do know that, but in my book it says that it seperates the input from the output. I don't understand what they mean with that. I would, but they use it some lines further to make a line of output, which makes me a bit confused. They use the same expression for 2 different reasons. Could someone explain to me what they mean with "Seperate the output from the input" ?   ;  They just mean putting a bit of physical space on the screen so that when you next start typing, it won't appear right next to the last output the program sent. (ie. the "Hello" bit.)   ;  Ok, thanks for the answer, I understand that. And my other question, about the while, anyone has an answer to that?   ;Quote:Original post by MarlyMy question is: When the while is true, and will keep being true, so it will keep being repeated, will it keep doing the things in the while body or will it only do it once? I know it will keep running through it, but it's a bit strange to have uncountable lines of output.Hey, yeah it'll keep doing it because the condition will always be true as you said. So you'll have infinite lines of output. (provided you change 'std::cout << std::endl' to 'std::cout << greeting << std::endl' which i think is what you meant).It seems wierd in this particular program, but lots of programs, especially games have an infinite loop like this usually done using while(1) or for(;;) or something similar which will always evaluate true. This gets used for rendering graphics, keyboard input, and basically anything that needs to be checked/done regularly, so like:void main(){  InitAllYourStuff();  //once only  while(1)  {    CheckKeyboardInput();   //check it user pressed a key to do something    DrawGraphics();    //update graphics    AnythingElse();    //do anything else and loop around again  }return 0;};  oh yeah sorry didnt see the second part of your question. The statements inside your loop will keep being executed until you make the "While condition" = false, or explicitly break out of the loop. It will crash your pc eventually if you dont.you always have to build logic into your loops so they dont continue forever"Hey, yeah it'll keep doing it because the condition will always be true as you said. So you'll have infinite lines of output. (provided you change 'std::cout << std::endl' to 'std::cout << greeting << std::endl' which i think is what you meant)."actually writing "Greeting" wont give you much indication either.Rather increment a variable inside the loop and print it out. that will give you an indication that it is executing those commands and looping continuously. Will also give you an indication of how fast its looping. But you have to remember to get out of the loop before the extents of the variable are reached otherwise it will runtime error obviously. You can do a simple check to see if the extents will be reached for the variable type and then break from the loop.   ;Quote:Original post by HumanoidTyphoonyou always have to build logic into your loops so they dont continue foreverAgreed. My above example was more pseudocode than something you'd actually run. There's no condition for when the user wants to exit the program i.e. exit the infinite loop.maybe something like thisvoid main(){  bool UserWantsToExit = false;  InitAllYourStuff();  //once only  while(UserWantsToExit == false)  {    //Returns TRUE if user wants to quit (maybe by pressing ESC), FALSE if not    //and something else is done    UserWantsToExit = DoKeyboardInput();    DrawGraphics();    //update graphics    AnythingElse();    //do anything else and loop around again  }return 0;}Quote:Rather increment a variable inside the loop and print it out. that will give you an indication that it is executing those commands and looping continuously. Will also give you an indication of how fast its looping. But you have to remember to get out of the loop before the extents of the variable are reached otherwise it will runtime error obviously. Good idea, and more meaningful than just showing the same string over again. Although it won't crash, the variable will just overload and start back at 0.Hope we've helped answer your question Marly.   ;  Yea, you guys did. Thank you very much!I rated you both to be very helpful and friendly.   