Function returning incorrect value

Hi guys,

I'm new to C++ and have come across a problem that has me stumped. It's obviously something I'm doing wrong but for the life of me I can't figure out what.

I have some code to return the width of my monitor in pixels:

 
  int x = GetSystemMetrics(SM_CXSCREEN);


This returns 1366, which is correct for the monitor I'm currently using. However the problem arises when I try to wrap it up in a function as follows:

1
2
3
4
5
  int getScreenWidth() {

	return GetSystemMetrics(SM_CXSCREEN);
  
  }


When calling this function it returns the value 013214B5. I have no idea why?

Can anyone shed some light on what I'm doing wrong?

Thanks.

Edit: I'm using Visual Studio Community 2015 and I should add that I don't get any build errors or warnings. Also, if it matters, creating a function out of GetSystemMetrics(SM_CXSCREEN) in exactly the same manner in Visual Basic (syntactical differences aside of course) returns the correct result, so I'm clearly missing or misunderstanding something specific to C++ here.
Last edited on
Are you calling this function and returning it to an integer value?

int someInt = getScreenWidth();

And you are saying that the value stored in someInt is 013214B5?

I do not understand how an integer value can store this (unless it is hex). If it is hex, it might be a memory address?
Last edited on
Yes I'm returning it to an integer value. I thought it was hex too but if you convert 013214B5 to decimal it equals 20059317, which doesn't make much sense either.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <windows.h>
#include <stdio.h>

using namespace std;

int getScreenWidth();

int main()
{

	cout << getScreenWidth() << endl;
	cin.ignore();

	return 0;
}

int getScreenWidth() {

	return GetSystemMetrics(SM_CXSCREEN);

}


Prints 1920 to the console...

You need to post more of your code so I can see the real problem.
Last edited on
closed account (E0p9LyTq)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
#include <windows.h>

int getScreenWidth()
{
   return GetSystemMetrics(SM_CXSCREEN);
}

int main()
{
   std::cout <<  GetSystemMetrics(SM_CXSCREEN) << "\n";

   std::cout << getScreenWidth() << "\n";
}

gives the following output (my screen resolution is 1920 x 1080):
1920
1920

And yes, that is from VS 2015 Community.
Thanks for the replies guys. I've tried a few different variations on the code to see if I can narrow down what's going wrong. Here's the latest...

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
#include "stdafx.h"
#include <windows.h>
#include <iostream>

using namespace std;

int getScreenWidth() {

	//the screen's width, in pixels
	
	return GetSystemMetrics(SM_CXSCREEN);
	
}

int main()
{

    int x = GetSystemMetrics(SM_CXSCREEN);

    cout << "Width: " << x << endl;

    cout << "Width: " << getScreenWidth << endl;

    cin.ignore();

    return 0;

}


The fist line is correctly printing 1366 but the next line is now printing 012714B5 to the console window.

When I return getScreenWidth() to an integer variable then try to print that I get the value of 013214B5.

Edit: Okay guys I've solved it. Really sorry to waste your time but it was a daft oversight on my part the whole time - I looked again over my code and realised that when I was calling getScreenWidth() I wasn't including the parenthesis at the end!!!! Such a schoolboy error! Sorry again guys but I really appreciate your help in trying to figure it out for me. Thank you.
Last edited on
That makes sense. I was just about to ask why the integer was being printed in hexadecimal format - that suggested it was printing the address of something.
Yeah the hex really threw me as well - I guess that should have been my queue to really read over the code carefully. Ah well, you live you learn eh!
Topic archived. No new replies allowed.