Looping Help?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • tims
    New Member
    • Feb 2008
    • 4

    Looping Help?

    Hi, I'm working on an easy assignment only I can't get it working properly. All the program needs to do is accept the user-input value and display a message that many times. What I'm having trouble with is the syntax for getting the message to display x amount of times per the user-inputted variable. I'm sure this is an easy solution but I'm going nuts trying to figure it out. Any help is greatly appreciated.


    Oh and I'm working in C++ 2003.
  • gpraghuram
    Recognized Expert Top Contributor
    • Mar 2007
    • 1275

    #2
    Originally posted by tims
    Hi, I'm working on an easy assignment only I can't get it working properly. All the program needs to do is accept the user-input value and display a message that many times. What I'm having trouble with is the syntax for getting the message to display x amount of times per the user-inputted variable. I'm sure this is an easy solution but I'm going nuts trying to figure it out. Any help is greatly appreciated.


    Oh and I'm working in C++ 2003.


    With cin the syntax is the same except you have to use a array variable to get the inputted string.
    Can you show us the code what you have written, then ww will be happy to help you

    raghuram

    Comment

    • tims
      New Member
      • Feb 2008
      • 4

      #3
      Originally posted by gpraghuram
      With cin the syntax is the same except you have to use a array variable to get the inputted string.
      Can you show us the code what you have written, then ww will be happy to help you

      raghuram
      Thanks for the fast reply. I'm at home right now and don't have the full project with me but I think is the closest we got to it working even though its not even close to being right.

      Code:
      #include <iostream>
      
      using namespace std;
      
      int main()
      
      {
      	int counter = 0;
      
      	cout << "Enter the number of hellos.\n" << "    ";
      	cin >> counter;
      
      	while ( counter < 20)
      	{
      		counter++;
      		cout << "Hello.\n";
      	}
      
      	cout << "\nCounter: " << counter << ".\n";
      
      	return 0;
      }
      I'm pretty sure what I need is a statement saying something like 'display hello ((counter) times)' but I can't seem to figure out the c++ version of it.

      Comment

      • gpraghuram
        Recognized Expert Top Contributor
        • Mar 2007
        • 1275

        #4
        Originally posted by tims
        Thanks for the fast reply. I'm at home right now and don't have the full project with me but I think is the closest we got to it working even though its not even close to being right.

        Code:
        #include <iostream>
        
        using namespace std;
        
        int main()
        
        {
        	int counter = 0;
        
        	cout << "Enter the number of hellos.\n" << "    ";
        	cin >> counter;
        
        	while ( counter < 20)
        	{
        		counter++;
        		cout << "Hello.\n";
        	}
        
        	cout << "\nCounter: " << counter << ".\n";
        
        	return 0;
        }
        I'm pretty sure what I need is a statement saying something like 'display hello ((counter) times)' but I can't seem to figure out the c++ version of it.

        R u looking for a function which can print the string the number of times you specify?
        If yes then there is no function of that sort in C++.
        You have to loop to print the required string

        Raghuram

        Comment

        • tims
          New Member
          • Feb 2008
          • 4

          #5
          Originally posted by gpraghuram
          R u looking for a function which can print the string the number of times you specify?
          If yes then there is no function of that sort in C++.
          You have to loop to print the required string

          Raghuram
          What I need it to do is go through the loop and display the message on the screen a certain number of times. For example the command prompt comes up and asks 'How many hellos do you want' and the user types in 3 then the program should display 'Hello' 3 times.

          Comment

          • Simonius
            New Member
            • Feb 2008
            • 47

            #6
            I don't get why you do while(counter<2 0)

            Why don't you just do:
            Code:
            for(i=0;i<HowManyHellos;++i)
                   cout<<"Hello"<<endl;
            Basicly what this coding does is set i (the variable that's used as a counter) to 0,
            do the loop while your counter is lower then the times you want hello to be displayed and then increment the counter.

            The code line just prints Hello followed by a new line.

            You could use while(i<HowMany Hellos)
            But then you'd have to increment i in your while loop.

            Comment

            • tims
              New Member
              • Feb 2008
              • 4

              #7
              Originally posted by Simonius
              I don't get why you do while(counter<2 0)

              Why don't you just do:
              Code:
              for(i=0;i<HowManyHellos;++i)
                     cout<<"Hello"<<endl;
              Basicly what this coding does is set i (the variable that's used as a counter) to 0,
              do the loop while your counter is lower then the times you want hello to be displayed and then increment the counter.

              The code line just prints Hello followed by a new line.

              You could use while(i<HowMany Hellos)
              But then you'd have to increment i in your while loop.
              Thank you very much, that worked great. I had tried the for loop in my program at school but couldn't get the arguments in the parentheses right.

              Comment

              Working...