what do I need to add to this for loop?

Started by
12 comments, last by ontheheap 19 years, 6 months ago
I'm trying to make a program that you enter a number and you enter another number that is bigger then it counts till it reaches the other number but it doesn't count to the second number

#include<iostream>

using namespace std;

int main()
{
    int Num,Num2;
    cout<<"enter a number:";
    cin>>Num;
    
    cout<<"enter bigger number:";
    cin>>Num2;
    for(Num<Num2;Num++;)
    return 0;
    }
-----------------------------------Panic and anxiety Disorder HQ
Advertisement
for loops come in three parts
1) Initializatino
2) Test
3) postloop

You forgot the initialization step so it thinks that Num<Num2 is the initialization,

try for(; Num<Num2; Num++)
look up the for statement definition in the language specification. You have made a glaring error.
bah - took me to long to get my response out because I forgot my password!
your for loop is setup wrong.

see this:
http://crasseux.com/books/ctutorial/The-flexibility-of-for.html

Beginner in Game Development?  Read here. And read here.

 

Now it just keeps printing the first number I type in heres the code
#include<iostream>using namespace std;int main(){    int Num,Num2;    cout<<"enter a number:";    cin>>Num;        cout<<"enter bigger number:";    cin>>Num2;    for(Num<Num2;Num+1;)    {    cout<<Num<<endl;    }    return 0;    }

I tried it like you said but it didn't work:(.
-----------------------------------Panic and anxiety Disorder HQ
Homework?
You put the semicolon on the wrong side it should look like this:
for(;Num<Num2;Num+1)
the following code will count from 0 to 99
im sure you can edit it to your needs

/**********************************************************Loop that counts from 0 to 99 using a for loopCopyright (C) 2004  Steven AshleyThis program is free software; you can redistribute it and/ormodify it under the terms of the GNU General Public Licenseas published by the Free Software Foundation; either version 2of the License, or (at your option) any later version.This program is distributed in the hope that it will be useful,but WITHOUT ANY WARRANTY; without even the implied warranty ofMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See theGNU General Public License for more details.You should have received a copy of the GNU General Public Licensealong with this program; if not, write to the Free SoftwareFoundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.**********************************************************/#include<iostream>using namespace std;int main(){    for(int i = 0; i < 100; i++)    {        cout << i << endl;    }    return 0;}


edit: bug fix
Quote:Original post by hothead
Now it just keeps printing the first number I type in heres the code*** Source Snippet Removed ***
I tried it like you said but it didn't work:(.


Try it like this:

#include<iostream>using namespace std;int main(){    int Num,Num2;    cout<<"enter a number:";    cin>>Num;        cout<<"enter bigger number:";    cin>>Num2;    for(int x = Num; x<Num2;x++)    {       cout << x << endl;    }    return 0;}


You need a way to iterate through the loop. This makes x == the smaller number, and each time the loop iterates it prints the value of x and then increases it by 1 until it is 1 less than the larger number. If you want to print the larger number as well, change x<Num2 to x<=Num2

This topic is closed to new replies.

Advertisement