| | |
Problems with While and For Loops (I searched this forum before starting thread)
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Oct 2009
Posts: 4
Reputation:
Solved Threads: 0
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 , 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.
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:
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!
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
C++ Syntax (Toggle Plain Text)
if (num%2 == 0)
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.
C++ Syntax (Toggle Plain Text)
cout << “ Celsius Fahrenheit” << endl; cout << “------------------------” << endl; cout << setw(8) << celsius << setw(8) << fahrenheit << endl;
And here's the code I have written so far:
C++ Syntax (Toggle Plain Text)
#include<iostream> using namespace std; const double fahrenheit = 9.0 / 5 * celsius + 32; int main() { int count = 0, i, j; char number; cout << "Enter a non-zero integer (0 to quit): "; cin >> number; while (number != '0') { count = count + 1; cout << "Enter a non-zero integer (0 to quit): "; cin >> number; } cout << "-----------------------------------------" << endl; cout << "Even Count: " << count << endl; cout << "Odd Count: " << count << endl; cout << "-----------------------------------------" << endl; int i, j; cout << "Enter beginning Celsius temperature: "; cin >> i; cout << "Enter ending Celsius temperature: "; cin >> j; for(i = 0; i < 11; i++) { for(j = 1; j <= i; j++) cout << "*"; cout << endl; } return 0; }
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.
-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
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.
•
•
Join Date: Oct 2009
Posts: 4
Reputation:
Solved Threads: 0
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.
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.
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:
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.
C++ Syntax (Toggle Plain Text)
while(number != 0) { //ask for and accept the number from the user if(number%2==0) //increment even counter else //increment odd counter }
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!
URL on facebook!
•
•
Join Date: Oct 2009
Posts: 4
Reputation:
Solved Threads: 0
0
#5 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:
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.C++ Syntax (Toggle Plain Text)
while(number != 0) { //ask for and accept the number from the user if(number%2==0) //increment even counter else //increment odd counter }
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:
C++ Syntax (Toggle Plain Text)
#include<iostream> using namespace std; int main() { int number, even = 0, odd = 0; cout << "Enter even integer: "; cin >> number; while(number != 0) { cout << "Enter even integer: "; cin >> number; if(number%2==0) even++ else odd++ } cout << "-----------------------------------------" << endl; cout << "Even Count: " << even << endl; cout << "Odd Count: " << odd << endl; cout << "-----------------------------------------" << endl; return 0; }
Thanks.
Last edited by jamesbrad288; Oct 24th, 2009 at 5:13 pm.
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!
URL on facebook!
•
•
Join Date: Oct 2009
Posts: 4
Reputation:
Solved Threads: 0
0
#7 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.)
C++ Syntax (Toggle Plain Text)
#include<iostream> using namespace std; const double int main() { int celsius, fahrenheit; cout << "Enter beginning Celsius temperature: "; cin >> i; cout << "Enter ending Celsius temperature: "; cin >> j; fahrenheit = 9.0 / 5 * celsius + 32; for(i = 0; i < 11; i++) { for(j = 1; j <= i; j++) cout << "*"; cout << endl; } cout << “ Celsius Fahrenheit” << endl cout << “------------------------” << endl; cout << setw(8) << celsius << setw(8) << farenheit << endl; return 0; }
Last edited by jamesbrad288; Oct 24th, 2009 at 6:39 pm.
![]() |
Similar Threads
- I click once, my mouse clicks twice (USB Devices and other Peripherals)
- Do we want Java web development starting thread? (JSP)
- Resources to check out before starting a new thread (Getting Started and Choosing a Distro)
- palindrome (C++)
- Classic Windows Taskbar, No audio, MSconfig errors...HELP!! (Viruses, Spyware and other Nasties)
- starting a thread (DaniWeb Community Feedback)
- Hello newbie here & not sure where to post (Community Introductions)
- Helping yourself: What to do before starting a new thread or posting a HiJackThis log (Viruses, Spyware and other Nasties)
Other Threads in the C++ Forum
- Previous Thread: Splitting a string using strtok()
- Next Thread: using 'while'
| Thread Tools | Search this Thread |
Tag cloud for C++
api application array arrays assignment beginner binary bitmap c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete developer display dll dynamiccharacterarray email encryption error file format forms fstream function functions game generator getline givemetehcodez graph iamthwee ifstream image input int java lib list loop looping loops map math matrix memory multidimensional multiple newbie news node number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings struct template templates text tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






