checking integer or double, and a little problem

Pages: 12
Hi all, I am trying to create a program which computes the function.I tried to take only the integers from ,for example, 1.4 to 5.9 but it dont works.
Any help will be appreciated.

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
35
36
37
38
39
40
41
#include <iostream>
#include <cstdlib>
#include <cmath>

using namespace std;

double funct (double x,)
{
double sum ;
sum  =x+x;
return sum;
}

int main ()
{
double min = 0, max = 0;
double a,b cn;

cout << "Please enter a.\n";
cin>> a;
cout << "Please enter b.\n";
cin>> b;

for (double x=a;x<=b;x=x+1)
{


if (double floor (x) ==x )
{
cout<<x;

if (cn<min) min=cn;
if (cn>max) max = cn;
}
}


cout << "The mp : " << min <<"i "<< max << ".\n";
system ("PAUSE");
return 0;
} 
Last edited on
.x and y should be only integers


Take a look at your code and see what types you've made x and y.

Also take a look at what values you are incrementing x and y by each time you increment them.

if (floor (x) ==x $$ floor (y)==y)


Is this some weird character set corruption, or does that actually two dollar symbols in there?
Last edited on
yes, at first they were integers, but when i saw the errors i turned them into doubles.the problem is that when i make them integers , in the loop 'a' is double,
Last edited on
Well if the problem is that in the loop 'a' is a double, make 'a' an integer.

If you want the user to input integers, you can force them to. This article shows how:

This article gives some ideas: http://www.cplusplus.com/forum/articles/6046/
yes i think i got the idea but he program should go through all the integer points in the triangle.So in the loop it should increment x 0.1
Last edited on
The program should go through all the integer points in the rectangle.So in the loop it should increment x and y by 0.1


This makes no sense. If you have an integer value of x, and you increase it by 0.1, you no longer have an integer value of x.

Here's an example. Let's say that x =3, and you want the next integer value of x. If you add 0.1, you now have 3.1 which is not an integer.

If you want to go through all the integer points in the rectangle, you need to add 1 to your x and y values, not 0.1
Last edited on
yes thats right
Last edited on
You don't need floor and round. You don't need to use any floating point values. You just need integers. Make a,b,c,d,x,y all integers.

1
2
3
4
5
6
for (int x=a;x<=b;x=x+1)
{
  for(int y=c;y<=d;y=y+1)
  {
   }
}


This code will go through every x and y integer value in the rectangle, starting at (a,c) and finishing at (b,d).
#include <cstdlib>
#include <iostream>
#include <cmath>

using namespace std;

int funct (int x)
{
int result ;
result = x+x+x;
;
return result;
}

int main ()
{
int min = 0, max = 0;
int a,b cn;

cout << "Please enter a.\n";
cin>> a;
cout << "Please enter b.\n";
cin>> b;

for (int x=a;x<=b;x=x+1)
{


if (floor (x) ==x && floor (y)==y)
{
cout<<"The numbers are : x " <<".\n";


if (cn<min) min= cn;
if (cn>max) max = cn;
}
}



system ("PAUSE");
return 0;
}
Last edited on
You set min to zero at the start. If the calculated cn value is never less than this, it will never change.

You set max to zero at the start. If the calculated cn value is never more than this, it will never change.
I fixed that.But The problem with the input of floating-point number still persists
1
2
3
int x;
double a,b;
for( x=ceil(a); x<=b; x++ )


Also the min and max values are always 0
Provide a test case;
int min = 0, max = 0; Instead of that, initialize them with the first result from the function (or std::numeric_limits<int>::min(), std::numeric_limits<int>::max()
#include <cstdlib>
#include <iostream>
#include <cmath>

using namespace std;

int funct (int x)
{
int result ;
result = x+x+x;
;
return result;
}

int main ()
{
int min = 0, max = 0;
int a,b cn;

cout << "Please enter a.\n";
cin>> a;
cout << "Please enter b.\n";
cin>> b;

for (int x=a;x<=b;x=x+1)
{


if (floor (x) ==x && floor (y)==y)
{
cout<<"The numbers are : x " <<".\n";


if (cn<min) min= cn;
if (cn>max) max = cn;
}
}



system ("PAUSE");
return 0;
}
Last edited on
int x=ceil(a)

You are trying to initialise an int value with something that is not an int value.

When 'a' is a double, ceil(a) will return a double. You must turn that double into an int.

Here is a bad way to do it;

int x = int (ceil(a));

A better way is with proper C++ casting, such as static_cast
Last edited on
gives an error : ivalid operands of types 'double' and <unknown type>' to binary.
and it shows it here
if (cn<min) min= cn;
min is an int (actually, I don't think min is anything at all at the moment, as you never seem to create it).

cn is a double.

You cannot assign a double value to an int.

Make cn an int. Given that funct returns an int anyway, cn should never have been a double.


You have missed off the end of that error message.
Last edited on
I made it an int.The same error..
Last edited on
What error? You have not told us the error. You are missing pieces of the error. Tell us what the error is.
ivalid operands of types 'double' and <unknown type>' to binary.
I get the following errors when compiling your code:

badcode.cpp: In function ~int main()~:
badcode.cpp:30: warning: converting to ~int~ from ~double~
badcode.cpp:32: warning: converting to ~int~ from ~double~
badcode.cpp:36: error: ~y~ was not declared in this scope
badcode.cpp:41: error: invalid operands of types ~double~ and ~<unknown type>~ to binary ~operator<~
badcode.cpp:41: error: overloaded function with no contextual type information
badcode.cpp:42: error: invalid operands of types ~double~ and ~<unknown type>~ to binary ~operator>~
badcode.cpp:42: error: overloaded function with no contextual type information
badcode.cpp:47: error: no match for ~operator<<~ in ~std::operator<< [with _Traits = std::char_traits<char>](((std::basic_ostream<char, std::char_traits<char> >&)(& std::cout)), ((const char*)"The minimum value of the function is : ")) << std::min~


If you're not getting all those, I don't know what you're using as a compiler.
Pages: 12