Jump to content
  • Advertisement
Sign in to follow this  
hothead

what do I need to add to this for loop?

This topic is 5466 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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;
    }

Share this post


Link to post
Share on other sites
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++)

Share this post


Link to post
Share on other sites
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:(.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Homework?

Share this post


Link to post
Share on other sites
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 loop
Copyright (C) 2004 Steven Ashley

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of 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 of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, 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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!