Condition at while not working

Hello everyone. Could someone explain me, what's wrong with this code? It doesn't do the condition, which I wrote in the while. Even if i write negative number, it still makes the while loop.
Thanks for the help, guys.

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
 #include <iostream>
using namespace std;
int main()
{
     int x,
         y,
         i = 0;
         
cout << "Write the positive number ";
cin >> x;
if (x>0)
{
 y = x;
 while ( (x>0) || (x != (y*y)))
 {
  cin >> x;
  i++;
 };
 cout << "Minimal number: " << y <<endl;
 cout << "Maximal number: " << x << endl;
 cout << "Total amount of numbers: " << i << endl;
}
else
cout <<"Next time write the positive number." << endl;
    return 0;
}
The while loop is going through because the condition x != y*y is always being met. So, even if the user enters a negative number the loop will go through.

EDIT: Sorry, the condition is not ALWAYS being met, but until x=y^2 is entered the user is free to enter negative numbers.

IMO you could do something like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
while( x != y*y)
   {
      cin >> x;
      if( x < 0 )
         {
            cout << "Write positive numbers only...\n";
         }
      else
         {
            i++;
         }
   }
...
Last edited on
What are you trying to accomplish?

Because of this condition x != (y*y) and the line y = x; on the previous line, the only way the while loop won't execute is if x == 0 or x == 1.

EDIT: Given that x would have to be 0 or 1, going back to the first condition of the while you can rule out x == 1. So the only value for x that would not enter the while loop would be 0. Beyond that, the condition of the initial 'if' rules out x being zero. There's no way a user can input anything to avoid the while loop.
Last edited on
Thanks guys, now I found the main problem.
#include <stdio.h>

main()
{
int x,
y = 0,
i;
printf("Write the sequence of numbers: ");
scanf("%d",x);
for (i=1; i < EOF;i++)

{
if(x>y)
{
if (x == y*y)
break;
else
y = x;
}
else
if (y == x*x)
break;
else
scanf("%d",x);

}
printf("Minimal number is: %d\n", x);
printf("Maximal number is: %d\n", y);
printf("The number of numbers: %d\n", i);

getch();
}
Last edited on
this loop doesn't work properly. Any ideas?

Check to see that your toaster is plugged in.

Seriously, what do you want out of this code and what is it doing currently? Be specific about your problem.
I'm very sorry about this.
To be certain:
1)The person should enter the sequence of numbers till the minimal number would be two times lower that the maximal number.

So, first of all, the person enter the number. First 'if' statement in the loop checks whether the entered number is two times bigger than the previous maximal number. If it's not - make the new number -maximal.
Else statement checks whether the new entered number is two times lower the maximal number and if not asks to write the x again.

Anyone?
Topic archived. No new replies allowed.