Support Forums
For loop problem - Printable Version

+- Support Forums (https://www.supportforums.net)
+-- Forum: Categories (https://www.supportforums.net/forumdisplay.php?fid=87)
+--- Forum: Coding Support Forums (https://www.supportforums.net/forumdisplay.php?fid=18)
+---- Forum: Programming with C++ (https://www.supportforums.net/forumdisplay.php?fid=20)
+---- Thread: For loop problem (/showthread.php?tid=3646)



For loop problem - GeneralOJB - 12-15-2009

Hey

Why doesn't this work:
PHP Code:
#include <iostream.h>
int main() {
   
int x 0;
   
int i;
   for (
1<= 100i++)  {
     
+= i;
   }
   
cout << << endl;
   return 
0;


For 'x', I should be getting the same as 'i' (1 going down to 100) but I get 5050.

Does anyone know what the problem is?

P.S - I'm learning from a book, so any mistakes are Jeff Cogswell's fault.


RE: For loop problem - Gaijin - 12-15-2009

Code:
x += i;

Simply means x = x + i

That means your adding the "i" number to the "x" number.
Try removing the +


RE: For loop problem - GeneralOJB - 12-15-2009

This time I get '100'

Instead of

1
2
3
4

ect.


RE: For loop problem - Gaijin - 12-15-2009

Yes that is because you're declaring the x variable every time new.
To do what you want you'll need an array.

Code:
char[] x;
//then in the loop
x[i] = i;



RE: For loop problem - GeneralOJB - 12-15-2009

(12-15-2009, 02:38 PM)Master of The Universe Wrote: Yes that is because you're declaring the x variable every time new.
To do what you want you'll need an array.

Code:
char[] x;
//then in the loop
x[i] = i;

I'm getting an error on the 'char[] x;', it says it expected primary expression before char.

Where exactley do I put this?

And sorry im pretty new to C++.


RE: For loop problem - Brainless Control - 12-15-2009

Is that book outdated? cause new compilers also accept <iostream> as an header.
<iostream.h> is old school


RE: For loop problem - GeneralOJB - 12-15-2009

Yeah, i'm using <iostream>, the original post was copied from the old book. But I do have a new compiler. It's just the book that's old.

2003. LOL


RE: For loop problem - Brainless Control - 12-15-2009

2003...lol you might want to change the book you're reading to something up to date Smile


RE: For loop problem - Gaijin - 12-15-2009

(12-15-2009, 02:48 PM)GeneralOJB Wrote: I'm getting an error on the 'char[] x;', it says it expected primary expression before char.
Where exactley do I put this?
And sorry im pretty new to C++.

I'm the one who need to apologize.
I was with my tought in a bit different language! Big Grin

Here Take a look at this code.

Code:
#include <iostream>

using namespace std;

int main(int argc, char argv[]) {
    int i = 0;
    
    int x[101];
    
    cout << "i:" << endl;
    
    for(i; i <= 100; i++) {
        cout << "i = " << i << endl;
        x[i] = i;      
    }
    
    cout << "x:" << endl;
    cin.get();
    
    for(i = 0; i <= 100; i++) {
          cout << "x[" << i << "] = " << x[i] << endl;      
    }
    
    cin.get();
    return 1;
}



RE: For loop problem - GeneralOJB - 12-17-2009

Love you <33