Problems with While and For Loops (I searched this forum before starting thread)

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Oct 2009
Posts: 4
Reputation: jamesbrad288 is an unknown quantity at this point 
Solved Threads: 0
jamesbrad288 jamesbrad288 is offline Offline
Newbie Poster

Problems with While and For Loops (I searched this forum before starting thread)

 
0
  #1
Oct 24th, 2009
I'm having trouble with my programming assignment. It's made up of two parts. Part I is a while loop, and part II is a for loop.

The first part counts how many even numbers and how many odd numbers the user types before typing 0. I think I have most of this part complete, but I don't know how to make it determine the odds from the evens. I know I need to use
  1. if (num%2 == 0)
, but I don't know where it goes, and what else I need to add.

In the second part, the user types in two temperatures. The first temperature is mulitplied by 5 and it loops until the temperature is greater than or equal to the second temperature. I'm pretty sure this is how I set up the rest of the code, but again I'm not sure where it goes.
  1. cout << “ Celsius Fahrenheit” << endl;
  2. cout << “------------------------” << endl;
  3. cout << setw(8) << celsius << setw(8) << fahrenheit << endl;
I have part ii set up how it's essentially supposed to be, but I'm having a hard time putting the pieces together.

And here's the code I have written so far:

  1. #include<iostream>
  2.  
  3. using namespace std;
  4.  
  5.  
  6.  
  7. const double fahrenheit = 9.0 / 5 * celsius + 32;
  8.  
  9.  
  10. int main()
  11.  
  12. {
  13.  
  14. int count = 0, i, j;
  15.  
  16. char number;
  17.  
  18.  
  19.  
  20. cout << "Enter a non-zero integer (0 to quit): ";
  21.  
  22. cin >> number;
  23.  
  24.  
  25.  
  26. while (number != '0')
  27.  
  28. {
  29.  
  30. count = count + 1;
  31.  
  32. cout << "Enter a non-zero integer (0 to quit): ";
  33.  
  34. cin >> number;
  35.  
  36. }
  37.  
  38. cout << "-----------------------------------------" << endl;
  39.  
  40. cout << "Even Count: " << count << endl;
  41.  
  42. cout << "Odd Count: " << count << endl;
  43.  
  44. cout << "-----------------------------------------" << endl;
  45.  
  46.  
  47.  
  48. int i, j;
  49.  
  50.  
  51.  
  52. cout << "Enter beginning Celsius temperature: ";
  53.  
  54. cin >> i;
  55.  
  56. cout << "Enter ending Celsius temperature: ";
  57.  
  58. cin >> j;
  59.  
  60.  
  61.  
  62. for(i = 0; i < 11; i++)
  63.  
  64. {
  65.  
  66. for(j = 1; j <= i; j++)
  67.  
  68. cout << "*";
  69.  
  70. cout << endl;
  71.  
  72. }
  73.  
  74.  
  75.  
  76. return 0;
  77.  
  78. }

Here's an example of the output:
Enter a non-zero integer (0 to quit): 89
Enter a non-zero integer (0 to quit): 55
Enter a non-zero integer (0 to quit): 56
Enter a non-zero integer (0 to quit): 1
Enter a non-zero integer (0 to quit): 2
Enter a non-zero integer (0 to quit): 0
-----------------------------------------
Even Count: 2
Odd Count: 3
-----------------------------------------
Enter beginning Celsius temperature: 25.891
Enter ending Celsius temperature: 58.111
Celsius Fahrenheit
------------------------
25.891 78.604
30.891 87.604
35.891 96.604
40.891 105.604
45.891 114.604
50.891 123.604
55.891 132.604

I hope this isn't confusing. I appreciate any help I may receive, and I want to let it be known that I hope I can return the favour to other users on this forum because I know how frustrating this is.

Thanks in advance!
Last edited by jamesbrad288; Oct 24th, 2009 at 12:45 am.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,585
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1486
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning
 
-7
  #2
Oct 24th, 2009
use the mod % operator to check of the number is odd or even. When (number % 2) == 0 the number is even.

make the data type of number an int, not a char because you can't enter a number greater than one digit in a char. You will also have to change the while loop on line 26 while( number != 0)
Last edited by Ancient Dragon; Oct 24th, 2009 at 12:53 am.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 4
Reputation: jamesbrad288 is an unknown quantity at this point 
Solved Threads: 0
jamesbrad288 jamesbrad288 is offline Offline
Newbie Poster
 
0
  #3
Oct 24th, 2009
I know that I need to add "if (num%2 == 0)", but I don't know where it goes. I don't understand why I need to change "while( number != 0)" because I was pretty sure that's how it's suppose to be. When the number is not equal to 0, it loops. In other words, the program will keep going until I type in zero. It's suppose to do that.

As for part two. Knowing what to put in the for loop would help me tremendously. I know that my program has to multiply the temperature by 5 for each loop, but I'm not sure how to set this up.

Thanks.
Last edited by jamesbrad288; Oct 24th, 2009 at 3:34 pm.
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 466
Reputation: Grn Xtrm will become famous soon enough Grn Xtrm will become famous soon enough 
Solved Threads: 39
Grn Xtrm's Avatar
Grn Xtrm Grn Xtrm is offline Offline
Posting Pro in Training
 
0
  #4
Oct 24th, 2009
In your while loop you will need to include a counter for both odd and even numbers. Also inclyde the if else statement in the while loop. For example:
  1. while(number != 0)
  2. {
  3. //ask for and accept the number from the user
  4. if(number%2==0)
  5. //increment even counter
  6. else
  7. //increment odd counter
  8.  
  9. }
The only thing you need to change in the while loop is to make 0 an integer not a character with quotes. This is what Ancient Dragon was trying to point out to you.
Last edited by Grn Xtrm; Oct 24th, 2009 at 3:50 pm.
Check out my new band URL on facebook. I'm the bass player. :) Become a fan and leave comments if you like.
URL on facebook!
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 4
Reputation: jamesbrad288 is an unknown quantity at this point 
Solved Threads: 0
jamesbrad288 jamesbrad288 is offline Offline
Newbie Poster
 
0
  #5
Oct 24th, 2009
Originally Posted by Grn Xtrm View Post
In your while loop you will need to include a counter for both odd and even numbers. Also inclyde the if else statement in the while loop. For example:
  1. while(number != 0)
  2. {
  3. //ask for and accept the number from the user
  4. if(number%2==0)
  5. //increment even counter
  6. else
  7. //increment odd counter
  8.  
  9. }
The only thing you need to change in the while loop is to make 0 an integer not a character with quotes. This is what Ancient Dragon was trying to point out to you.
My apologies to Ancient Dragon. It was a stupid mistake on my part. In the lab, we used a character to end the loop, and I didn't even notice.

I think I have the part I set up the way it's suppose to be, but the terminal keeps telling me there's a parse error before else (line 27). Here's what I have now:

  1. #include<iostream>
  2.  
  3. using namespace std;
  4.  
  5. int main()
  6.  
  7. {
  8.  
  9. int number, even = 0, odd = 0;
  10.  
  11. cout << "Enter even integer: ";
  12.  
  13. cin >> number;
  14.  
  15. while(number != 0)
  16.  
  17. {
  18.  
  19. cout << "Enter even integer: ";
  20.  
  21. cin >> number;
  22.  
  23. if(number%2==0)
  24.  
  25. even++
  26.  
  27. else
  28.  
  29. odd++
  30.  
  31. }
  32.  
  33. cout << "-----------------------------------------" << endl;
  34.  
  35. cout << "Even Count: " << even << endl;
  36.  
  37. cout << "Odd Count: " << odd << endl;
  38.  
  39. cout << "-----------------------------------------" << endl;
  40.  
  41. return 0;
  42.  
  43. }

Thanks.
Last edited by jamesbrad288; Oct 24th, 2009 at 5:13 pm.
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 466
Reputation: Grn Xtrm will become famous soon enough Grn Xtrm will become famous soon enough 
Solved Threads: 39
Grn Xtrm's Avatar
Grn Xtrm Grn Xtrm is offline Offline
Posting Pro in Training
 
0
  #6
Oct 24th, 2009
You need to put semicolons after the statements in the if/else statements. (i.e. you need semicolons after the increment statements.)
Check out my new band URL on facebook. I'm the bass player. :) Become a fan and leave comments if you like.
URL on facebook!
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 4
Reputation: jamesbrad288 is an unknown quantity at this point 
Solved Threads: 0
jamesbrad288 jamesbrad288 is offline Offline
Newbie Poster
 
0
  #7
Oct 24th, 2009
Originally Posted by Grn Xtrm View Post
You need to put semicolons after the statements in the if/else statements. (i.e. you need semicolons after the increment statements.)
Thanks, I appreciate it. It wasn't working correctly at first because I had odd++ and even++ mixed up, but I figured it out, lol. I'm still having some trouble with part ii though. I'm not sure how to set it up, but I don't think I have much more work to do on it. I got the for loop from an example in my book, and I'm not sure how to connect it with fahrenheit and celsius or if I have set the for loop up correctly. Thanks.
  1. #include<iostream>
  2. using namespace std;
  3. const double
  4.  
  5. int main()
  6. {
  7. int celsius, fahrenheit;
  8.  
  9. cout << "Enter beginning Celsius temperature: ";
  10. cin >> i;
  11. cout << "Enter ending Celsius temperature: ";
  12. cin >> j;
  13.  
  14. fahrenheit = 9.0 / 5 * celsius + 32;
  15.  
  16. for(i = 0; i < 11; i++)
  17.  
  18. {
  19. for(j = 1; j <= i; j++)
  20. cout << "*";
  21. cout << endl;
  22. }
  23.  
  24. cout << “ Celsius Fahrenheit” << endl
  25. cout << “------------------------” << endl;
  26. cout << setw(8) << celsius << setw(8) << farenheit << endl;
  27.  
  28. return 0;
  29.  
  30. }
Last edited by jamesbrad288; Oct 24th, 2009 at 6:39 pm.
Reply With Quote Quick reply to this message  
Reply

Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC