13 Replies - 1071 Views - Last Post: 02 March 2016 - 10:05 AM Rate Topic: -----

#1 tlark   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 28-January 16

Help with Counter-Control While Loop

Posted 23 February 2016 - 09:44 AM

I'm new to programming so please play nice.
I've tried running this program however I can't figure out why it won't work. I think I'm missing something but I can't figure out what is missing.
When I run the program all that comes up is: 0 through 10 multiplied by 2 and by 10
I would appreciate any assistance anyone can give me. Thank you in advance.
// Multiply.cpp - This program prints the numbers 0 through 10 along
// with these values multiplied by 2 and by 10.
// Input:  None
// Output: Prints the numbers 0 through 10 along with their values multiplied by 2 and by 10. 


#include <iostream>
#include <string>
using namespace std;

int main()
{
		
   string head1 = "Number: ";
   string head2 = "Multiplied by 2: ";
   string head3 = "Multiplied by 10: ";				
   int numberCounter = 0; 	        // Numbers 0 through 10
   int byTen;		        // Stores the number multiplied by 10
   int byTwo; 		        // Stores the number multiplied by 2
   const int NUM_LOOPS = 10;    // Constant used to control loop

   // This is the work done in the housekeeping() function
   cout << "0 through 10 multiplied by 2 and by 10" << endl;

   // This is the work done in the detailLoop() function
   // Write while loop here		
   while(numberCounter <= 10);
   {
	   cout <<"numberCounter * byTwo" << numberCounter<< endl;
	   cin >> byTwo;
	   cout <<"numberCounter * byTen" << numberCounter<< endl;
	   cin >> byTen;
	   numberCounter++;
   }
   cout << "head1 + numberCounter" << endl;
   cout << "head2 + byTwo" << endl;
   cout << "head3 + byTen" << endl;
   
   // This is the work done in the endOfJob() function
   return 0;
} // End of main() 





Is This A Good Question/Topic? 0
  • +

Replies To: Help with Counter-Control While Loop

#2 modi123_1   User is offline

  • Suitor #2
  • member icon



Reputation: 14097
  • View blog
  • Posts: 56,495
  • Joined: 12-June 08

Re: Help with Counter-Control While Loop

Posted 23 February 2016 - 09:56 AM

35	   cout << "head1 + numberCounter" << endl;
36	   cout << "head2 + byTwo" << endl;
37	   cout << "head3 + byTen" << endl;

Things inside a pair of double quotes is considered a string. As in the exact characters you have there and not any variable you may have. I would suggest removing the double quotes.
Was This Post Helpful? 0
  • +
  • -

#3 #define   User is offline

  • Duke of Err
  • member icon

Reputation: 1853
  • View blog
  • Posts: 6,671
  • Joined: 19-February 09

Re: Help with Counter-Control While Loop

Posted 23 February 2016 - 11:48 AM

On line 27 you have an unlikely placed semicolon.
Was This Post Helpful? 0
  • +
  • -

#4 gallimimus   User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 6
  • Joined: 31-January 16

Re: Help with Counter-Control While Loop

Posted 23 February 2016 - 10:06 PM

First:
// Input:  None

If your program is not getting input, what are the cin statements doing?
cout <<"numberCounter * byTwo" << numberCounter<< endl;
cin >> byTwo;
cout <<"numberCounter * byTen" << numberCounter<< endl;
cin >> byTen;



Second:
const int NUM_LOOPS = 10;    // Constant used to control loop

You declare NUM_LOOPS, but don't use it anywhere. You can certainly use variables to control loops, but here you've used the number 10 so you don't need to. :smile2:/>
while(numberCounter <= 10)

Was This Post Helpful? 0
  • +
  • -

#5 tlark   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 28-January 16

Re: Help with Counter-Control While Loop

Posted 25 February 2016 - 08:46 AM

Thank you all for you assistance.

gallimimus,

I couldn't figure out what to do with const int NUM_LOOPS = 10;

Here is the updated code but now I get a bunch of errors (33, 34, 35)

#include <iostream>
#include <string>
using namespace std;

int main()
{
		
   string head1 = "Number: ";
   string head2 = "Multiplied by 2: ";
   string head3 = "Multiplied by 10: ";				
   int numberCounter = 0; 	        // Numbers 0 through 10
   int byTen;		        // Stores the number multiplied by 10
   int byTwo; 		        // Stores the number multiplied by 2
   const int NUM_LOOPS = 10;    // Constant used to control loop

   // This is the work done in the housekeeping() function
   cout << "0 through 10 multiplied by 2 and by 10" << endl;

   // This is the work done in the detailLoop() function
   // Write while loop here		
   while(numberCounter <= 10)
   {
	   cout <<"numberCounter * byTwo" << numberCounter<< endl;
	   cout <<"numberCounter * byTen" << numberCounter<< endl;
	   numberCounter++;
   }
   cout << head1 + numberCounter << endl;
   cout << head2 + byTwo << endl;
   cout <<head3 + byTen << endl;
   
   // This is the work done in the endOfJob() function
   return 0;
} // End of main()


:code:

This post has been edited by modi123_1: 25 February 2016 - 08:56 AM

Was This Post Helpful? 0
  • +
  • -

#6 tlark   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 28-January 16

Re: Help with Counter-Control While Loop

Posted 25 February 2016 - 08:51 AM

sorry not sure why it didn't show up using proper tags.

It is still not working I am getting more confused.
Was This Post Helpful? 0
  • +
  • -

#7 tlark   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 28-January 16

Re: Help with Counter-Control While Loop

Posted 25 February 2016 - 10:47 AM

I started over on my counter-controlled while loop because I think i completely got myself turned around on it.
Here is the new program. I'm still getting some errors and I'm stuck on what the problem is.
Thank you in advance.
// Multiply.cpp - This program prints the numbers 0 through 10 along
// with these values multiplied by 2 and by 10.
// Input:  None
// Output: Prints the numbers 0 through 10 along with their values multiplied by 2 and by 10. 


#include <iostream>
#include <string>
using namespace std;

int main()
{

	string head1 = "Number: ";
	string head2 = "Multiplied by 2: ";
	string head3 = "Multiplied by 10: ";
	int numberCounter = 0; 	        // Numbers 0 through 10
	int byTen;		        // Stores the number multiplied by 10
	int byTwo; 		        // Stores the number multiplied by 2
	const int NUM_LOOPS = 10;    // Constant used to control loop

								 // This is the work done in the housekeeping() function
	cout << "Enter numbers from 0 through 10 multiply each by 2 and 10." << endl;

	// This is the work done in the detailLoop() function
	while (NUM_LOOPS <= 10)// Write while loop here
	{
		cout << numberCounter;
		numberCounter++;
	}
	cout << endl;

	while (NUM_LOOPS <= 10)
	{
		cout << numberCounter * byTwo;
		cout << numberCounter * byTen;
		cout << numberCounter;
	}
	cout << endl; 
	
	cout << head1 << endl;
	cout << head1 + head2 << endl;
	cout << head1 + head3 << endl;	

// This is the work done in the endOfJob() function
		
	return 0;
} // End of main()

Was This Post Helpful? 0
  • +
  • -

#8 #define   User is offline

  • Duke of Err
  • member icon

Reputation: 1853
  • View blog
  • Posts: 6,671
  • Joined: 19-February 09

Re: Help with Counter-Control While Loop

Posted 25 February 2016 - 11:37 AM

Hi, NUM_LOOPS has the value 10, so you can use it instead of 10 where appropriate.

The first part of the program is to print the numbers 0 to 10. Why not try that first?

The string head1 will be inside the loop as well. It will be printed before each number.
Was This Post Helpful? 1
  • +
  • -

#9 tlark   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 28-January 16

Re: Help with Counter-Control While Loop

Posted 26 February 2016 - 10:55 AM

Here are the changes I made based on the assistance every gave.
Am I at least heading in the right direction?

#include <iostream>
#include <string>
using namespace std;

int main()
{

	string head1 = "Number: ";
	string head2 = "Multiplied by 2: ";
	string head3 = "Multiplied by 10: ";
	int numberCounter = 0; 	        // Numbers 0 through 10
	int byTen;		        // Stores the number multiplied by 10
	int byTwo; 		        // Stores the number multiplied by 2
	const int NUM_LOOPS = 10;    // Constant used to control loop

// This is the work done in the housekeeping() function
	cout << "Enter numbers from 0 through 10 multiply each by 2 and 10." << endl;
	
	cout << head1 << endl;
	

	// This is the work done in the detailLoop() function
	while (NUM_LOOPS <= 10)// Write while loop here
	{
		cout << numberCounter;
		numberCounter++;
	}
	cout << endl;

	while (NUM_LOOPS <= 10)
	{
		cout << numberCounter * byTwo;
		cout << numberCounter * byTen;
		cout << numberCounter;
	}
	cout << endl; 
	
	cout << head1 + head2 << endl;
	cout << head1 + head3 << endl;
	// This is the work done in the endOfJob() function
		
	return 0;
} // End of main()



Was This Post Helpful? 0
  • +
  • -

#10 gallimimus   User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 6
  • Joined: 31-January 16

Re: Help with Counter-Control While Loop

Posted 26 February 2016 - 11:28 AM

while (NUM_LOCKS <= 10)

Because you set NUM_ LOCKS = 10, with this line you’re saying, “while 10 is less than or equal to 10.” Since this statement will always be true, you have created an infinite loop. Your original loop that used the variable numberCounter and added 1 to it each time was correct.

Another problem is with your variables byTen and byTwo. You never assign values to them so they don’t do anything in your program. Try assigning them the values that you want to multiply numberCounter by.

And, like #define said, your strings need to be inside the loop as well so that they’ll print before each number.

Example:
cout << string + number << endl;

Was This Post Helpful? 1
  • +
  • -

#11 tlark   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 28-January 16

Re: Help with Counter-Control While Loop

Posted 27 February 2016 - 02:10 AM

I made some changes but now I've created an infinite loop and I'm not sure how to fix it.

// Multiply.cpp - This program prints the numbers 0 through 10 along
// with these values multiplied by 2 and by 10.
// Input:  None
// Output: Prints the numbers 0 through 10 along with their values multiplied by 2 and by 10. 


#include <iostream>
#include <string>
using namespace std;

int main()
{

	string head1 = "Number: ";
	string head2 = "Multiplied by 2: ";
	string head3 = "Multiplied by 10: ";
	int numberCounter = 0; 	        // Numbers 0 through 10
	int byTen = 10;		        // Stores the number multiplied by 10
	int byTwo = 2; 		        // Stores the number multiplied by 2
	const int NUM_LOOPS = 10;    // Constant used to control loop

// This is the work done in the housekeeping() function
	cout << "Enter numbers from 0 through 10 multiply each by 2 and 10." << endl;
	
	cout << head1 << endl;
	

	// This is the work done in the detailLoop() function
	while (NUM_LOOPS)		// Write while loop here
	{
		cout << numberCounter;
		numberCounter++;
	}
	cout << endl;

	while (NUM_LOOPS)
	{
		cout << numberCounter * 2;
		cout << numberCounter * 10;
		cout << numberCounter;
	}
	cout << endl; 
	
	cout << head1 << endl;
	cout << head1 + head2 << endl;
	cout << head1 + head3 << endl;
	// This is the work done in the endOfJob() function


	return 0;
} // End of main()



Was This Post Helpful? 0
  • +
  • -

#12 tlark   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 28-January 16

Re: Help with Counter-Control While Loop

Posted 27 February 2016 - 09:20 AM

Thank you all for helping me figure this out. Here is the final code and it worked.

#include <iostream>
#include <string>
using namespace std;

int main()
{

	string head1 = "Number: ";
	string head2 = "Multiplied by 2: ";
	string head3 = "Multiplied by 10: ";
	int numberCounter = 0; 	        // Numbers 0 through 10
	int byTen = 10;		        // Stores the number multiplied by 10
	int byTwo = 2; 		        // Stores the number multiplied by 2
	const int NUM_LOOPS = 10;    // Constant used to control loop

// This is the work done in the housekeeping() function
	cout << "Enter numbers from 0 through 10 multiply each by 2 and 10." << endl;
	
	cout << head1 << endl;
	

	// This is the work done in the detailLoop() function
	while (numberCounter <= 10)		// Write while loop here
	{
		cout << numberCounter;
		numberCounter++;
	}
	cout << endl;

	while (numberCounter <= 10)
	{
		cout << numberCounter * 2;
		cout << numberCounter * 10;
		cout << numberCounter;
	}
	cout << endl; 
	
	cout << head1 << endl;
	cout << head1 + head2 << endl;
	cout << head1 + head3 << endl;
	// This is the work done in the endOfJob() function


	return 0;
} // End of main()


Was This Post Helpful? 0
  • +
  • -

#13 #define   User is offline

  • Duke of Err
  • member icon

Reputation: 1853
  • View blog
  • Posts: 6,671
  • Joined: 19-February 09

Re: Help with Counter-Control While Loop

Posted 27 February 2016 - 10:12 AM

Hi, I don't think it is correct on several counts.

Firstly, I thought that this would store the number 10 as well, but the comment says stores the number multiplied by 10, which is not the same.

12	int byTen = 10;		        // Stores the number multiplied by 10



So in the loop, byTen will store the current number times ten.


It seems likely there should only be one loop.

How to use NUM_LOOPS? NUM_LOOPS holds the value 10 (could be changed) so that is used in the program where and when you want to stop looping.

// while the counter value is less than the NUM_LOOPS value (10)
while(numberCounter <= NUM_LOOPS)
{
  cout << head1 << numberCounter << endl;

  // your code
}


Was This Post Helpful? 0
  • +
  • -

#14 tlark   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 28-January 16

Re: Help with Counter-Control While Loop

Posted 02 March 2016 - 10:05 AM

Thanks to all of your assistance I was able to get things on the right track and get it to work.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1