How is this code invalid?

closed account (EwCjE3v7)
I just add and took some stuff away and it dosnt work

This should find numbers in range between the 2numbers that the user types

This works>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>

int main()  
{  
    int v1 = 0, v2 = 0;  
    std::cout << "Enter two integers to find numbers in their range (inclusive): " << std::endl;  
    std::cin >> v1 >> v2;  
    int current =  std::min(v1, v2);
    int max = std::max(v1, v2);
    while (current <= max)  
    {  
        std::cout << current << std::endl;  
        ++ current;  
    }  
    return 0;  
}


This dosn`t work.The only thing I did is add using namespace std; and take away std:: What is the problem?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
using namespace std;

int main()  
{  
    int v1 = 0, v2 = 0;  
    cout << "Enter two integers to find numbers in their range (inclusive): " << endl;  
    cin >> v1 >> v2;  
    int current =  min(v1, v2);
    int max = max(v1, v2);
    while (current <= max)  
    {  
        cout << current << endl;  
        ++ current;  
    }  
    return 0;  
}
Last edited on
int max = max(v1, v2);
It thinks that the second max refers to the variable max.
max is a c++ function
i used max_ variable instead of max
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
using namespace std;

int main()
{
    int v1 = 0, v2 = 0;
    cout << "Enter two integers to find numbers in their range (inclusive): " << endl;
    cin >> v1 >> v2;
    int current =  min(v1, v2);
    int max_ = max(v1, v2);
    while (current <= max_)
    {
        cout << current << endl;
        ++ current;
    }
    return 0;
}



EDIT ::
I recommend that you use code::blocks, because it marks operators and functions in green color, and variables in black, by that way, you can notice easily.
Last edited on
Yemeni Cpluspluser wrote:
max is a c++ operator

max is not an operator. It's a function.
Thank you Peter87
I miss up when using c++ and trying to understand the program in English and in Arabic (my mother language).
post fixed, it's a function.


operators are >> << + - * / &
functions are max , min , and other functions,
Last edited on
closed account (30X1hbRD)
Peter87 is right, but I can't seem to figure out how to change it so it'd work properly is all.
If you really want to use using namespace std; you can still do std::max(v1, v2) or you can change the variable name (what Yemeni said).
closed account (EwCjE3v7)
Thanks guys.

It worked.

Thanks so much. Im new so not really good
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
using std::cin;
using std::cout;
using std::endl;
using std::min;


int main()  
{  
    int v1 = 0, v2 = 0;  
    cout << "Enter two integers to find numbers in their range (inclusive): " << std::endl;  
    cin >> v1 >> v2;  
    int current =  min(v1, v2);
    int max = std::max(v1, v2);
    while (current <= max)  
    {  
        cout << current << endl;  
        ++ current;  
    }  
    return 0;  
}


I think std::max is not as same as max when not using namespace;
std::max in this case is a function
and max is a variable


but when using std::max (Or using namespace std)

max is a function
while you are using max as a variable, which is invalid
Last edited on
closed account (EwCjE3v7)
yep i got you now thanks so much Yemeni and i use Visual studio ultimate 2012..i also have code::block downloaded ages ago.
Last edited on
Topic archived. No new replies allowed.