Thread: question about loop tutorial....

  1. #1
    Registered User
    Join Date
    Nov 2003
    Posts
    5

    question about loop tutorial....

    Hey on the tutorial section theres this code on loops.

    Code:
    {                //The loop goes while x<100, and x increases by one every loop
      for(int x=0;x<100;x++) //Keep in mind that the loop condition checks 
      {    //the conditional statement before it loops again.
        //consequently, when x equals 100 the loop breaks    
        cout<<x<<endl;          //Outputting x 
    } 
      return 0;  
    }
    Now i need to write some code that displays a question a set amount of times, i was thinking i could use this, but for some reason i cant seem to work out where the loop content goes. Anyone help me on this?

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    Code:
    for (...)
    {
        // loop content goes here
    }
    gg

  3. #3
    Registered User
    Join Date
    Nov 2003
    Posts
    5

    Thanks

    Hey thanks, now my second question..why when i compile does it just flash up on the screen in a black box then dissapear, im using c++ 5.0, borland. Thanks.

  4. #4
    30 Helens Agree neandrake's Avatar
    Join Date
    Jan 2002
    Posts
    640
    because you aren't running the program from command prompt. your program doesn't wait for input, it just does its thing and quits.

    try going into the command prompt, go to the program directory and execute it
    Environment: OS X, GCC / G++
    Codes: Java, C#, C/C++
    AOL IM: neandrake, Email: neandrake (at) gmail (dot) com

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Re Q2
    It's in the FAQ
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Registered User
    Join Date
    Nov 2003
    Posts
    5

    im back lol

    Code:
    #include <iostream.h>
    int main()
    {
     int marks[4];
     int i;
     while(i<5)
     {
        cout<<"Enter the first mark"<<endl;
        cin>>marks[1];
        i++;
    
      }
    
      cout<<marks[1];
     return 0;
    }
    Ok heres the deal so far, basically i need somehow to make it so that it asks a question, you type the mark, goes into [1], then displays the contents of [1]. Hmmm now im stuck as how to fill the array by looping. bah..please help! Thanks.

  7. #7
    The Defective GRAPE Lurker's Avatar
    Join Date
    Feb 2003
    Posts
    949
    Code:
    #include <iostream>
    using namespace std;
    
    int main() {
     int marks[4];
     int i = 0;
     while(i<5) {
      cout << "Enter the first mark" << endl;
      cin >> marks[i];
      cout << marks[i];
      i++;
     }
     return 0;
    }
    Do not make direct eye contact with me.

  8. #8
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Originally posted by Lurker
    Code:
    #include <iostream>
    using namespace std;
    
    int main() {
     int marks[4];
     int i = 0;
     while(i<5) {
      cout << "Enter the first mark" << endl;
      cin >> marks[i];
      cout << marks[i];
      i++;
     }
     return 0;
    }

    Wrong.

    Array indexes start at 0 and end at size-1. So for the above array, we can access marks[0] thru marks[3]. And since the variable 'i' loops to 4, we end up going off the end of the array (access violation).
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  9. #9
    The Defective GRAPE Lurker's Avatar
    Join Date
    Feb 2003
    Posts
    949
    I hurried through that anyway, spent 3 and a half seconds on it .
    Do not make direct eye contact with me.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Loop question
    By kwood965 in forum C Programming
    Replies: 6
    Last Post: 10-29-2008, 11:12 PM
  2. Help Please, Beginner Question, rewrite loop
    By office888 in forum C Programming
    Replies: 4
    Last Post: 12-11-2006, 10:07 AM
  3. Simple C question: user input to repeat a loop
    By evernaut in forum C Programming
    Replies: 2
    Last Post: 11-18-2006, 09:23 AM
  4. simple for loop function question
    By felicityxiv in forum C Programming
    Replies: 7
    Last Post: 05-06-2006, 11:43 PM
  5. Please don't laugh...SIMPLE loop question!
    By the_lumin8or in forum C++ Programming
    Replies: 5
    Last Post: 03-31-2006, 01:08 PM