| | |
Multiple Inputs
Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Sep 2009
Posts: 40
Reputation:
Solved Threads: 0
I know this is probably a really easy question, but my teacher failed to mention how to do it, so here goes.
I'm making a simple program that tells a user if a sequence of given numbers are even or odd. Here's what the program should look like:
I've gotten the program made, except my output stops at "12 is even".
My question is, how do I get my program to loop through all of the numbers and stop at 0?
I tried using a while loop,
But, it just looped "12 is even" over and over.
I'm making a simple program that tells a user if a sequence of given numbers are even or odd. Here's what the program should look like:
C++ Syntax (Toggle Plain Text)
Enter a sequence of numbers (0 to quit): 12 4 19 3 0 12 is even 4 is even 19 is odd 3 is odd
I've gotten the program made, except my output stops at "12 is even".
My question is, how do I get my program to loop through all of the numbers and stop at 0?
I tried using a while loop,
C++ Syntax (Toggle Plain Text)
while (x != 0)
0
#2 Nov 1st, 2009
Post the rest of the code. That seems like it should be working to me.
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: Sep 2009
Posts: 40
Reputation:
Solved Threads: 0
0
#3 Nov 1st, 2009
Here's my code.
C++ Syntax (Toggle Plain Text)
int main () { int input; cout <<"Enter a sequence of numbers (0 to quit): "; cin >> x; if (x != 0) { if (x % 2 == 0) { cout << x << " is even" << endl; } else if (x % 2 != 0) { cout << x << " isn't even" << endl; } } }
1
#4 Nov 1st, 2009
Ok your code has a few problems with it. First put this at the top of the code
Next change you variable input to x. This is the variable that will be entered by the user and tested by the if statement.
Next xhange the first if to a while loop and move cin>>x into the while loop.
Then use your current if/else to test the input.
Finally put return 0; before the final right brace }
Your main problem was leaving cin>>x outside of the while loop. You need it in the loop so you can enter many numbers.
C++ Syntax (Toggle Plain Text)
#include <iostream> using namespace std;
Next xhange the first if to a while loop and move cin>>x into the while loop.
Then use your current if/else to test the input.
Finally put return 0; before the final right brace }
Your main problem was leaving cin>>x outside of the while loop. You need it in the loop so you can enter many numbers.
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!
1
#5 Nov 1st, 2009
•
•
•
•
Here's my code.
C++ Syntax (Toggle Plain Text)
int main () { int input; cout <<"Enter a sequence of numbers (0 to quit): "; cin >> x; if (x != 0) { if (x % 2 == 0) { cout << x << " is even" << endl; } else if (x % 2 != 0) { cout << x << " isn't even" << endl; } } }
1) change : int input; TO int x;
2) Wrap your code in a do while loop.
C++ Syntax (Toggle Plain Text)
int x = -1; do { /* get user input and check for even/oddity */ }while(x != 0) //quits if user enters 0
1) Prove that the area of a circle is pi*r^2, where "r" is the radius of the circle. 2) Problem 2[b]solved by : jonsca
![]() |
Other Threads in the C++ Forum
- Previous Thread: calculator in c++ with string as input
- Next Thread: Need help with a simple while loop
Views: 189 | Replies: 5
| Thread Tools | Search this Thread |
Tag cloud for C++
6 add api array arrays beginner binary c++ c/c++ calculator char class classes code compile compiler console conversion convert count data delete desktop directshow dll dynamic encryption error file forms fstream function functions game givemetehcodez google graph homeworkhelper iamthwee ifstream input int integer java lazy lib linkedlist linker linux loop looping loops map math matrix memory microsoft newbie news number output parameter pointer problem program programming project proxy python random read recursion recursive reference return sort stream string strings struct studio system template templates test text tree unix url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






