brain fart

This is a discussion on brain fart within the C++ Programming forums, part of the General Programming Boards category; here is something simple, and im having a brain fart this after noon, I need to have text lets say ...

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    25

    brain fart

    here is something simple, and im having a brain fart this after noon, I need to have text lets say cout << " hello world"; repeated 5 or more times with out me typing it a bumch of times.. I know.. this is simple.. and i looked in my text book and it doesnt give me any hints.. or maybe im just brain dead today i dont know. chow/

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,318
    A loop?

  3. #3
    Registered User
    Join Date
    Sep 2005
    Posts
    25
    I did a loop like this

    Code:
    int grade = 1;
    
    int main(int argc, char *argv[])
    {
       
       while ( grade != 10 ){
              cout << " hello world " << endl;
              
              }
        
        system("PAUSE");
        return EXIT_SUCCESS;
    but the hello world just repeats it self over and over and over anc over and never ends

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,318
    Then make sure grade changes each time through the loop so that it will eventually be 10.

  5. #5
    Registered User
    Join Date
    Sep 2005
    Posts
    36
    Your loop continues while the variable "grade" does not equal 10. Why would it equal 10? You never even change its value, you have to do that by yourself, it's not automatic.

  6. #6
    Bond sunnypalsingh's Avatar
    Join Date
    Oct 2005
    Posts
    162
    Just try to increment the value of grade with each iteration

  7. #7
    ~Team work is the best!~ wakish's Avatar
    Join Date
    Sep 2005
    Posts
    85
    you may want to have something like:
    Code:
    int main()
    {
    	int grade = 1;
    
       	while( grade != 10 )
       	{
    	   	cout << " hello world " << endl;
    
    	   	grade++;
       	}
    
       	system("pause");
       	return 0;
    }
    or using simply a for loop:
    Code:
    int main()
    {
    
       for( int grade = 1;grade != 10; grade++)
       {
    	   cout << " hello world " << endl;
       }
    
       system("pause");
       return 0;
    }
    But the most important thing is to always see if you are incrementing or decrememnting the variable that you are using as the test condition of your loops!

    In your case you have not included a statement where grade should change or increment inside your while loop. That is why you are getting an infinite loop!

    Regards,
    wakish
    Last edited by wakish; 10-26-2005 at 08:53 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Conditional IF Brain Fart
    By paulogrady in forum C Programming
    Replies: 4
    Last Post: 04-12-2009, 11:07 AM
  2. Re: Girlfriend Post
    By PsychoBrat in forum A Brief History of Cprogramming.com
    Replies: 57
    Last Post: 05-13-2002, 06:11 AM
  3. Brain Fart Problem
    By golfinguy4 in forum C++ Programming
    Replies: 1
    Last Post: 03-15-2002, 10:56 PM
  4. Brain fart
    By Govtcheez in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 08-30-2001, 12:40 PM

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