for and while

Hello guys, I'm a complete beginner in C++ programming. I was wondering if someone could PLEASE explain the difference between 'while' and 'for' and how I could convert a program I've written from using 'while' to using 'for'? The program is a program which checks if a user-inputted number is divisible by all of its numbers. It's written below. Thank you.

#include <iostream>
using namespace std;


int main()
{
int a, n, m;
bool statement;

cout<<"Enter a number: ";
cin>>a;
m=a;
while (m>0){

a = m;

while(a>0)
{
n=a%10;
if (n == 0||n == 10)
break;
if (m%n == 0){
a=a/10;
statement = true;
}
else{
statement = false;
break;
}
}
if (statement == true)
cout << "The number "<<m<<" is divisible by all its numbers."<<endl;
m--;
}
system("pause");
return 0;
}
I'm not sure the logic above is correct. It depends on how zero digits are to be treated. At any rate, the boolean value statement should be set to a specific initial value of either true or false at the start of the outer while loop. Otherwise the behaviour of the program will be dependent on whatever the value of statement was after the end of the previous iteration.

As for the looping,
It might look like this:
1
2
3
4
5
6
    for (int m = a; m > 0 ; m--)
    { 
        for ( a = m; a > 0; a /= 10 )
        {
        }        
    }

Well, a first thought arises. By the time the while loop begins, the only variables used so far are n and k. So we know those will need to remain outside the loop as at present. Similarly the variable m is used after the loop has ended, so that needs to remain outside too.

That leaves two other variables, i and d which belong inside the loop.

First step. Change
 
    while (n>0)
to
 
    for (  ; n>0;  )


Next, just before the end of the loop body, there is a ++i;
That looks very much like the sort of thing that might be put into the for statement.

Now the for statement looks like this:
 
    for (   ; n>0; ++i)


Also, move the declaration/initialisation of i into the same statement:
 
    for (int i = 0; n>0; ++i)


Finally, for completeness, move the declaration of d inside the loop body since it is not used at all outside the loop.
 
        int d = n%10;


That leaves the finished version:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
    int n = 0;
    int k = 0;
    int m = 0;
    
    cout << "Enter a value for n: " << endl;
    cin  >> n;
    cout << "Enter the digit (k) that you want deleted: "<<endl;
    cin  >> k;

    for (int i = 0; n>0; ++i)
    {
        int d = n%10;
        n = n/10;

        if (d==k)
        {
            --i;
            d = 0;
        }

        m += d*pow(10,i);
    }
    
    cout << m << endl;


There, step by step and it should fall into place.
Dude, thank you so much, you're a huge help. That actually clears things up a lot for me in terms of how to use for. I appreciate it!
Actually I missed something. You can put more than one item in each part of the for statement (with care).
You could, if you wished move the n/10 into the for statement too.
 
    for (int i = 0; n>0; ++i, n = n/10)


Notice the comma separator. That's not just punctuation, it has a precise meaning.

Now you need to read about the comma operator as well:
http://www.cplusplus.com/doc/tutorial/operators/

Hey uhm, I'm sorry to bother you again, but I came across another program that I couldn't grasp on how to change from while to for. Could you please help me? This was the assignment "Determine the position of the dot M (x, y) relatively to the circle O (xo, yo, r).
" Below is the source code.

#include <iostream>
#include <cmath>
using namespace std;

int main(){
int x, y, m, a, b, r;
cout<<"Enter the coordinates (Dot) for x and y, respectively."<<endl;
cin>>x;
cin>>y;
cout<<"Enter the coordinates (Circle) for a and b, respectively."<<endl;
cin>>a;
cin>>b;

while((x>0)&&(y>0)&&(a>0)&&(b>0))
{
m=(x*x)+(y*y);
sqrt(m);
r=(a*a)+(b*b);
sqrt(r);
if(m>r)
{
cout<<"The dot is outside the circle."<<endl;
system("pause");
return 0;
}
if(m==r)
{
cout<<"The dot is on the circle."<<endl;
system("pause");
return 0;
}
if(m<r)
{
cout<<"The dot is inside the circle."<<endl;
system("pause");
return 0;
}
}
system("pause");
return 0;
}
Well, after the help so far plus the tutorial pages:
http://www.cplusplus.com/doc/tutorial/control/
I suggest you have a go, and see how you get on.

Though I'm not sure what the aim is, why are you making these changes? I'd have thought it more important to make sure the code works correctly, as a first priority.

I've not analysed that code in detail, but the lines
 
sqrt(m); 
and
 
sqrt(r);
don't do anything useful. It might be better to spend time fixing the problems in the code, wouldn't it?


My assignment is to learn how to convert programs I've already written from while to for, and my teacher is horrible, he's also really senile (And his English isn't very good). I'm practically trying to teach myself lmao. It's a first for me to have an inadequate teacher, and I've been learning at this private college since I moved to Europe 4 months ago. Also I use sqrt(m) to gather the distance from the center to the Dot.. and sqrt(r) gives me the radius of the circle. I'm comparing the radius to the distance from the center to the dot to see if the dot is inside the circle, outside the circle, or on the circle itself.
Last edited on
You missed my point.

Try an example.
1
2
3
4
    int m = 16;
    cout << "m = " << m << '\n';
    sqrt(m);
    cout << "m = " << m << '\n';
m = 16
m = 16


Also, I don't see the need for any loop of any kind in that program.
The flow goes simply:
get input
calculate results
display output


Actually, you're right. There isn't a need for a "while". I'll just use 'if' and that'll work.
Last edited on
closed account (48T7M4Gy)
Try something along these lines - you don't need to use square roots
works perfectly with int's but you need to consider rounding tolerances for a program involving float's or double's in the more general case.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <iostream>

using namespace std;

int main()
{
    int x = 0, y = 0; // Dot point
    int x0 = 0, y0 = 0, r = 0; // Circle centre and radius
    
    int m = 0;
    
    /*
     1. if point(x, y) is on the circle then (x-x0)^2 + (y-y0)^2 = r^2
     2. if point(x, y) is inside the ... ???
     3. if point(x, y) is ... ???
     */
    
    cout << "Enter the coordinates (Dot) for x and y, respectively.\n";
    cin >> x >> y;
    
    cout << "Enter the coordinates (Circle) for a and b, respectively.\n";
    cin >> x0 >> y0 >> r;
    
    m = (x-x0)*(x-x0) + (y-y0)*(y-y0);
    
    if( m > r*r )
        cout << "The dot is outside the circle.\n";
    else if( m ??? )
        cout << "The dot is on the circle.\n";
    else
        cout << "The dot is inside the circle.\n";
    
    return 0;
}
Topic archived. No new replies allowed.