### I need for loop help

I am finishing up some homework on for loops. I am stuck on a couple of the questions. I need to make this for loop output a number given for x - 0.5. x is given as an input by the user. Here is what I have for this:
 ``123456`` ``````double x; cout << "Enter a value for 'x': " << endl; cin >> x; for (double x; x<=0.5; x++) cout << x - 0.5 << endl;``````

Right now it does not output anything after the user inputs x.

My next question is how would I go about making a for loop that prompts the user for 10 integers and then it outputs the number of even and odd numbers? I think I could do this with a while loop easier but the assignment requires me to do it with a for loop.

Any help is much appreciated.
Instead of using double x within the for loop, use a different name, ex: double a.

When you have it recall x, it will just erase the input from earlier in the code. This most likely causes nothing to be outputted cause x is most likely a random big number greater than 0.5.

The other thing x++ would increment by 1 and I'm not sure if you are purposely doing this or not. I most likely think you want to increment by 0.1. ex: x += 0.1.

So overall, your for loop should look like: for(double a = x; a <= 0.5; a += 0.5).

Or even yet, you could try this: for(x; x <= 0.5; x += 0.5).

I hope any of this helps.
Even if I have my code set up like this:

 ``1234567`` `````` double x; cout << "Enter a value for 'x': " << endl; cin >> x; for(double n; n<=0.5; x+=0.5) cout << x - 0.5 << endl; }``````

It does still not output anything
You got to have n = x at the beginning:
double n = x;

And at the end you would want to increment n:
n+= 0.5;

And within the for loop you would want n for output:
cout << n - 0.5 << endl;
The reason you need double n = something; is because it is unspecified what a POD stack variable will hold when not initialized. It could contain 0, or "garbage" data that was previous in memory.

 ``12345678`` `````` double x; std::cout << "Enter a value for 'x': " << std::endl; std::cin >> x; for(double n(0.); n<=0.5; x+=0.5) { // You forgot the { I suppose; or is the ending } part of another control structure? std::cout << (x - 0.5) << std::endl; }``````
 ``1234567`` ``````double x; cout << "Enter a value for 'x': " << endl; cin >> x; for(double n = x; n>=0.5; x+=0.5) cout << x - 0.5 << endl; }``````

I have this now and it still does not output anything.. any ideas?
any ideas??
I am not really sure what u want but is this your point?

 ``12345678`` ``````double x; cout << "Enter a value for 'x': " << endl; cin >> x; for (x; x>=0.5; x=x-0.5) cout << x - 0.5 << endl; ``````
For your second problem is this what you want?
 ``12345678910111213141516171819`` ``````#include using namespace std; int main() { int a[10],odd=0,even=0; for(int i=0;i<10;i++) cin>>a[i]; /*after you input your numbers*/ for(int i=0;i<10;i++) { if(a[i]%2==1)odd++; else even++; } cout<

or you can check everyone individually for every cin check the %2== ....
Last edited on
Topic archived. No new replies allowed.