CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    Oct 2009
    Posts
    19

    Prining a * from a user input integer Loop Help

    How can I make it continually loop? It only asks for it one time? Is it possible to stop it after 5 times? Do I need a control condition/while loop?
    or
    is it possible to have the user input 5 integers in the beginning and have the output show at the end?


    Code:
    #include <iostream>
    using namespace std;
    
    int main()
    {
        int input = 0;
    
        cout << "Enter an integer: ";
        cin >> input;
        
        for(int num=0; num < input; num++)
    {
           cout<<"*";
    }
    	   
    }

  2. #2
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Prining a * from a user input integer Loop Help

    Am I understanding correctly that you wish to execute the cout/cin pair as well as the following for loop multiple times?

    Simple wrap another for loop around whatever you want to repeat.

  3. #3
    Join Date
    Nov 2006
    Location
    Australia
    Posts
    1,569

    Re: Prining a * from a user input integer Loop Help

    Quote Originally Posted by ashley19 View Post
    How can I make it continually loop? It only asks for it one time? Is it possible to stop it after 5 times? Do I need a control condition/while loop?
    or
    is it possible to have the user input 5 integers in the beginning and have the output show at the end?


    Code:
    #include <iostream>
    #include <vector>
    using namespace std;
    
    int main()
    {
        std::vector<int> inputs;
    
        for(int i = 0; i < 5; ++i)
       {
            cout << "Enter an integer: ";
            cin >> inputs[i];
       }
            
        for(int i = 0; i < 5; ++i)
        {
              cout<< inputs[i];
        }
    	   
    }
    Like that?
    Good judgment is gained from experience. Experience is gained from bad judgment.
    Cosy Little Game | SDL | GM script | VLD | Syntax Hlt | Can you help me with my homework assignment?

  4. #4
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Prining a * from a user input integer Loop Help

    That isn't entirely correct.

  5. #5
    Join Date
    Oct 2009
    Posts
    19

    Re: Prining a * from a user input integer Loop Help

    I added a while loop, with counter. But I'm not sure why it's an infinite loop. can someone modify this so I can learn what I'm doing wrong? I only want this loop to run 5 times.


    Code:
    #include <iostream>
    using namespace std;
    
    int main()
    {
        int input = 0;
        int counter = 0;
    
        cout << "Enter an integer (between 1-30): ";
        cin >> input;
    
    	while(counter != 5)	
    	{    
    
        for(int num=0; num < input; num++)
    {
           cout<<"*";
    }	
    	}   
    }

  6. #6
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Prining a * from a user input integer Loop Help

    Well, you never modify counter within the loop (or anywhere else). Of course it's going to be infinite.

  7. #7
    Join Date
    Oct 2009
    Posts
    19

    Re: Prining a * from a user input integer Loop Help

    got it thanks!!!
    <3Lindley
    Last edited by ashley19; October 14th, 2009 at 12:09 AM.

  8. #8
    Join Date
    Oct 2009
    Posts
    19

    Re: Prining a * from a user input integer Loop Help

    got it thanks!!!
    <3Lindley

  9. #9
    Join Date
    Nov 2006
    Location
    Australia
    Posts
    1,569

    Re: Prining a * from a user input integer Loop Help

    Quote Originally Posted by Lindley View Post
    That isn't entirely correct.
    Care to elaborate or just going to leave it at that?
    Good judgment is gained from experience. Experience is gained from bad judgment.
    Cosy Little Game | SDL | GM script | VLD | Syntax Hlt | Can you help me with my homework assignment?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured