Equal or less problem

Sep 6, 2019 at 6:30pm
"Hello World" :D I am ashamed to admit, but I cannot figure this out, I haven't found any info on the net, so I decided to come here. My problem here is that, it doesn't recognize any number except 1 and 100. When I put something like 4 or 5 or 50 whatever, it doesn't put the line on the screen I have intended. I removed bits of code to spare you the reading time, left the part that doesn't work...

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

using namespace std;

int main()
{
	string username;
	string age;

	cout << "Enter age: ";
	cin >> age;
	if (age <= "100")
	{
		cout << "Still young" << endl;
	}
}
Last edited on Sep 6, 2019 at 6:31pm
Sep 6, 2019 at 6:55pm
The problem is that you're using a string instead of a number. The letter '1' is not less than the number '2' (or any other numeral with the exception of zero). The numerical value of '1' is 0x31 and the numerical value of '2' is 0x32.

You probably meant to use a type int for age not a string.

Sep 6, 2019 at 7:20pm
string comparison works but think about how you would sort strings.

abc
ab
bcd
bcx
bc


would be sorted to
ab
abc
bc
bcd
bcx

and what that means here is that
"23" is < "45" sure
but think about 1, 10, and 100:
1 < 10 < 100, ok so far.
now think about
2, 20, and 100
that sorts to
100 <-- first one sorted lexographically
2
20

so as you can see, most inputs for real people's ages are going to be > 100 in a string compare. 2 is > 100, 30 is > 100, ...


Sep 7, 2019 at 1:20pm
using int on "age" doesn't work for some reason, I tried putting a lower number on age, like 50, and it all work as intended, anyone knows what's up with that?
Sep 7, 2019 at 1:31pm
Hello Deadwisper,

Since you have not posted any new code with your changes no one knows what you have done.

The question is the if statement comparing "age" to a number or a string?

Andy

Edit: typo
Last edited on Sep 7, 2019 at 1:32pm
Sep 7, 2019 at 9:25pm
Hi Andy, I wrote a something smaller to isolate the problem, this is a full code

#include <iostream>
#include <string>

using namespace std;

int main()
{
string username;
string age;
cout << "Enter age: ";
cin >> age;
if ( "50" > age)
{
cout << "Still young" << endl;
}
else
{
cout << "you are old" << endl;
}
return 0;
}

When instead of 50 I put 100, it works only if the age will be 1. But, there is a problem also with 50, if I input anything above 100 and 100 included, it will give output "Still young". I am really at a loss here, I am using Microsoft compiler, if that matters.

Edit: When I use "int age" the code stops working. Here are the errors:
Error (active) E0042 operand types are incompatible ("int" and "const char *")
Error C2446 '<': no conversion from 'const char [3]' to 'int'
Last edited on Sep 7, 2019 at 9:33pm
Sep 7, 2019 at 10:10pm
"50" is a const char[]. age is a string. They can be compared ... but only by dictionary ordering, whereas I presume you wanted numerical ordering.

Make age an int, and strip the quotes from 50. Then you will be comparing two numbers. And insulting the slightly more mature!
Sep 7, 2019 at 10:11pm
When I use "int age" the code stops working.

Did you change the string literal ("50") to an integer literal (50)?

Sep 8, 2019 at 12:12am
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>

int main()
{
   short age { };

   std::cout << "Enter age: ";
   std::cin >> age;

   if (100 >= age)
   {
      std::cout << "Still young\n";
   }
}

Enter age: 25
Still young
Sep 8, 2019 at 12:37am
Jib and Furry Guy explained it all for me. Thanks everyone.

Edit: for those who have similar problem.... Used "int" and removed quotation marks on 100.
"if (100 >= age)" was right thing to go.
Last edited on Sep 8, 2019 at 12:39am
Sep 8, 2019 at 3:31am
Most people would reasonably write an if statement like this:

if (age <= 100)

Nothing inherently wrong with that. It is when you are testing for equality (==) that it can result in very hard to detect errors. Someone meant to write:

if (age == 100) wrote if (age = 100)

That will always be true, and now the value in age has changed.

if (100 = age) will be a compile-time error, so the mistake is caught easily.
Topic archived. No new replies allowed.