473,394 Members | 1,774 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

how to initialize multiple loops

hi everyone I am a newbie to C++. I have an assignment that has multiple loops. I have to use a for, do, while loops.
I have to ask user to input numbers based on (test data 75, 62, 13,-35,55,0. My program should loop processing input from the user until user indicates user wants to quit by entering 0 for an integer.
If the number is a multiple of 5, print all multiples of 5 from 5 through number entered. Use a For loop to generate numbers
If number entered is even, print all numbers from 2 through number entered. Use a While loop to generate numbers
If the number entered is a odd number, print all odd numbers from 1 through number entered.
only print one set of numbers. For example if 20 is entered print 5 10 15 20 etc but do not print 2 4 6....20
If the remainder of the number divided by 5 is 0, the number is a multiple of 5, If the remainder of the number divided by 2 is 0, the number is even

Here is what I have so far. But when I tried using the while loop the number continuously ran?

thank you for any and all suggestions given




#include <iostream>
using namespace std;
int main()
{

int number;



cout << " Enter a positive integer or a 0 to quit program?" << endl;
cin >> number;


if (number % 5 == 0)


for (number = 5; number <= 75; number += 5)
{

cout << number << endl;

}

cout << endl;

cout << " Enter a positive integer or a 0 to quit program?" << endl;
cin >> number;

if (number % 2 == 0 )

for (number = 2; number <= 82; number += 2)
{
cout << number << endl;

}
cout << endl;


cout << " Enter a positive integer or a 0 to quit program?" << endl;
cin >> number;

if(number % 1 == 0)

do
{
cout << number << endl;
number += 1;
}

while (number % 1 == 0);



return 0;
}
Nov 20 '14 #1

✓ answered by weaknessforcats

Your do-while uses while (number > % 1). The > % will never compile and in any case a number modulo 1 is always zero. Why? Because any number divided by 1 has a zero remainder. Stop coding that.

The problem says to use a while loop to generate the numbers. So this loop would generate 10 numbers:

Expand|Select|Wrap|Line Numbers
  1. int ctr = 0;
  2. int number = 0;
  3. while (ctr <10)
  4. {
  5.    number = rand();
  6.    ++ctr;
  7. }
  8.  
All you have to do now is insert code to check if the number is a multiple of 5 or is even.

So this won't work:

Expand|Select|Wrap|Line Numbers
  1. if ((number %5) == 0)
  2. {
  3.      --number is a multiple of 5
  4. }
  5. if ((number % 2) == 0)
  6. {
  7.      -- number is even (a multiple of 2) --
  8. }
because a number could be a multiple of 5 and even at the same time. To separate these cases use the "else" of the if statement:

Expand|Select|Wrap|Line Numbers
  1. if ((number %5) == 0)
  2. {
  3.      --number is a multiple of 5
  4. }
  5. else if ((number % 2) == 0)
  6. {
  7.      (a multiple of 2) --
  8. }
Place this code in the while loop between the call to rand() and the increment of the loop counter ++ctr.

6 1563
weaknessforcats
9,208 Expert Mod 8TB
n % 1 is always 0. Therefore, your while loop becomes 0 == 0, which is always true and the loop runs forever.

POst again if you are still stuck.
Nov 20 '14 #2
Hi weaknessforcats!
Thank you so much for replying. So do I use
Expand|Select|Wrap|Line Numbers
  1. do
  2. {
  3. cout << number << endl;
  4. number += 1;
  5. }
  6.  
  7. while (number > 0);
  8.  


also I am using Microsoft Visual Studio 2010 Professional, not quite sure that matters, but just in case it does.

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. using namespace std;
  3. int main()
  4. {
  5.  
  6. int number;
  7.  
  8.  
  9.  
  10. cout << " Enter a positive integer or a 0 to quit program?" << endl;
  11. cin >> number;
  12.  
  13.  
  14. if (number % 5 == 0)
  15.  
  16.  
  17. for (number = 5; number <= 75; number += 5)
  18. {
  19.  
  20. cout << number << endl;
  21.  
  22. }
  23.  
  24. cout << endl;
  25.  
  26. cout << " Enter a positive integer or a 0 to quit program?" << endl;
  27. cin >> number;
  28.  
  29. if (number % 2 == 0 )
  30.  
  31. for (number = 2; number <= 82; number += 2)
  32. {
  33. cout << number << endl;
  34.  
  35. }
  36. cout << endl;
  37.  
  38.  
  39. cout << " Enter a positive integer or a 0 to quit program?" << endl;
  40. cin >> number;
  41.  
  42. if(number % 1 == 0)
  43.  
  44. do
  45. {
  46. cout << number << endl;
  47. number += 1;
  48. }
  49.  
  50. while (number > % 1 );
  51.  
  52.  
  53.  
  54. return 0;
  55.  
  56.  
  57.  
  58.  
I'm just not clear on the while or do while statements. I mean the correct way to state them? The first For statement executes fine. The second for statement should have been a while statement. the do while is just running on forever. What have I done wrong? Thank you for your assistance and clarity
Nov 21 '14 #3
weaknessforcats
9,208 Expert Mod 8TB
Your do-while uses while (number > % 1). The > % will never compile and in any case a number modulo 1 is always zero. Why? Because any number divided by 1 has a zero remainder. Stop coding that.

The problem says to use a while loop to generate the numbers. So this loop would generate 10 numbers:

Expand|Select|Wrap|Line Numbers
  1. int ctr = 0;
  2. int number = 0;
  3. while (ctr <10)
  4. {
  5.    number = rand();
  6.    ++ctr;
  7. }
  8.  
All you have to do now is insert code to check if the number is a multiple of 5 or is even.

So this won't work:

Expand|Select|Wrap|Line Numbers
  1. if ((number %5) == 0)
  2. {
  3.      --number is a multiple of 5
  4. }
  5. if ((number % 2) == 0)
  6. {
  7.      -- number is even (a multiple of 2) --
  8. }
because a number could be a multiple of 5 and even at the same time. To separate these cases use the "else" of the if statement:

Expand|Select|Wrap|Line Numbers
  1. if ((number %5) == 0)
  2. {
  3.      --number is a multiple of 5
  4. }
  5. else if ((number % 2) == 0)
  6. {
  7.      (a multiple of 2) --
  8. }
Place this code in the while loop between the call to rand() and the increment of the loop counter ++ctr.
Nov 24 '14 #4
thank you soo much weaknessforcats! That helped me tremendously. I appreciate you taking time out of your day to be of assistance to me.
I really need to read more on while and do while statements and execution any suggestions? Thank you so much
Nov 24 '14 #5
weaknessforcats
9,208 Expert Mod 8TB
We have all been where you are now. Learning this just takes time and multiple exposures.

Here's what I did:

1) Pick any C++ book you think you can read.
2) Read the book and work all the examples to see you get the same answer as the book
3)Work all the problems in the book
4) Never read the book again

Repeat steps 1-4 using a different book.

I was on book #6 when all the AHA! lights went on.

Good luck.
Nov 24 '14 #6
Will do! Thank you so much for your patience and much needed suggestion.
Nov 25 '14 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Tank | last post by:
I have had this post up here when i was trying to figure out how to make leading zeros and have been able to fudge that to work. I am now have trouble getting the loop that makes the folders to...
6
by: John Salerno | last post by:
This is from Programming in the Key of C#: "You can initialize multiple variables in the for loop: for (int i = 0, j = 0; ... But it's an either/or situation. If you declare one or more...
6
by: Fabian Vilers | last post by:
Hi all, I'm my script I've three loops processing a very huge data file. IE & Firefox show a message box after some time saying my script could be infinite looping and give me a chance to stop...
3
by: monomaniac21 | last post by:
hi all i have a script that retrieves rows from a single table, rows are related to eachother and are retrieved by doing a series of while loops within while loops. bcos each row contains a text...
5
by: sigloiv | last post by:
I've very much a beginner to VB.NET, and I'm running Visual Basic 2005 Express. Anyways, all I'd like to do is run three concurrent Do loops at the same time. Is this at all possible?
11
by: O.B. | last post by:
Does C# support anything like PHP's break command that optionally accepts a parameter specifying how many loops to break out of?
5
by: TP | last post by:
Hi everybody, Several means to escape a nested loop are given here: http://stackoverflow.com/questions/189645/how-to-break-out-of-multiple-loops-in-python According to this page, the best...
2
by: dechen | last post by:
I have an input in the form (as given below) in a text file. I have to loop through the contents in between two '$-$-$-$' characters. This is done by splitting at '$-$-$-$'. To get the Output as...
3
by: omar999 | last post by:
im trying to figure out a way to query sql table several times within the same asp statement. as opposed to opening a new asp statement each time to query sql table again & again etc so this is...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.