Can someone tell me why these two functions return different results?
After running code
#include <string>
#include <stdio.h>
#include <iostream>
using namespace std;
int returnN(string s, int n)
{
int newN = n % s.size();
return newN;
}
int returnN2(string s, int n)
{
int a = s.size();
int newN = n % a;
return newN;
}
int main(void)
{
string text2 = "Hello World!";
cout << returnN(text2, -2) << endl;
string text3 = "Hello World!";
cout << returnN2(text3, -2) << endl;
}
returnN(text2, -2) returns 2
returnN2(text3, -2) returns -2
PS D:\src\cpp> g++ --version
g++.exe (Rev3, Built by MSYS2 project) 13.2.0
Copyright (C) 2023 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
I tried to debug and see why but I still cant figure out why
Can someone tell me why these two functions return different results?
s.size() returns a size_t which is unsigned.
The difference between returnN and returnN2 is in the types of the operands used for the modulo calculation:
returnN you performs a modulo with an unsigned value (s.size()).n is converted to unsigned as well to perform the modulo.size_t minus 1, i.e. a very large positive value.s.size() turns out to be 2.returnN2 you first assign s.size() to int a (which is signed), and then perform the modulo with the signed values.n stays as is (-2), and the result is -2.Your compiler should alert you about the issue.
MSVC for example issues the following warning for initializing a:
warning C4267: 'initializing': conversion from 'size_t' to 'int', possible loss of data
A side note:
It's better to avoid using namespace std; - see What's the problem with "using namespace std;"?.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With