Administrator
Administrator
Posts: 3307
Joined: Thu Dec 24, 2009 2:06 am
Allegiance:: Space Rome
Location: ON, Canada
User avatar
Administrator
Administrator
Re: C++ GUI Frameworks

Post by Scott »

Thanks for the help. I'm just starting on C++ and am getting stuck quite often. I'll probably end up join a dedicated forum or mailbag soon. If you don't mind, I do have one more question...

In the following code:

Code: Select all

#include <iostream>

using namespace std;

int Divide(unsigned short int x, unsigned short int y);

int main()
{
    unsigned short int x, y, divide;
    
    cout << "Please enter a number...\n";
    cin >> x;
    cout << "Please enter another number...\n";
    cin >> y;
    
    divide = Divide(x,y);
    
    if (divide != -1)
    cout << x << " / " << y << " = " << divide << endl;
    else
    cout << "Error, cannot divide by 0";
  
    char response;
    cin >> response;
    return 0;
}

int Divide(unsigned short int x, unsigned short int y)
{
    if (y == 0)
    return -1;
    else
    return (x/y);
}
I get the error: 18 [Warning] comparison is always true due to limited range of data type. I understand what this means but no matter what I do with the if statements it always ends being always false or always true.
Image
Jedi Master
Jedi Master
Posts: 336
Joined: Sun Aug 09, 2009 9:16 am
Allegiance:: Jedi
User avatar
Jedi Master
Jedi Master
Re: C++ GUI Frameworks

Post by Matthew »

Your function returns a signed integer which is right but the division variable is an unsigned short integer. This means it can't hold a negative value.

Change this:

Code: Select all

unsigned short int x, y, divide;
To this:

Code: Select all

unsigned short int x, y;
int divide;
That will make the divide variable a signed 4 byte (32 bit) integer. You can't use a signed short integer if you will be using unsigned short integers for the arguments because the answer could potentially be above the range of the signed short datatype. A 4 byte integer should be used in this case.

Datatypes can get confusing. It's best to familiarise yourself with the data ranges of different data types.
Administrator
Administrator
Posts: 3307
Joined: Thu Dec 24, 2009 2:06 am
Allegiance:: Space Rome
Location: ON, Canada
User avatar
Administrator
Administrator
Re: C++ GUI Frameworks

Post by Scott »

Didn't notice I had different integer types. Thanks again :D
Image
Jedi Master
Jedi Master
Posts: 336
Joined: Sun Aug 09, 2009 9:16 am
Allegiance:: Jedi
User avatar
Jedi Master
Jedi Master
Re: C++ GUI Frameworks

Post by Matthew »

You could have the result as an unsigned short integer and use a flag to represent division by 0, keeping it all in a structure. That may be unnecessary but I think the solution is more elegant and sophisticated that way. I'm going to look into C++ programming again because I want to try out that idea.

I haven't touched C++ for almost a year now. I was halfway through a project in making a menu GUI library for the PSP. I gave up when I replaced the project with a python project.

It took me a while because I didn't know "Y" was different from 'Y'. The later being a char which is what I wanted but here it is.

Code: Select all

#include <iostream>

using namespace std;

struct divide_answer{
	unsigned short int val;
	bool nan;
};
divide_answer divide(unsigned short int x, unsigned short int y){
	divide_answer ans;
	if (y == 0){
		ans.nan = true;
	}else{
		ans.nan = false;
		ans.val = x/y;
	}
	return ans;
}
int main(){
	unsigned short int x, y;
	char leave_ans;
	while(true){
		divide_answer resultant;
		cout << "Please enter number 1: ";
		cin >> x;
		cout << "Please enter number 2: ";
		cin >> y;
		resultant = divide(x,y);
		if(resultant.nan){
			cout << "Error: Cannot divide by 0";
		}else{
			cout << x << " / " << y << " = " << resultant.val;
		}
		cout << "\n";
		while(true){
			cout << "Do you wish to leave? (Y/N): ";
			cin >> leave_ans;
			if(leave_ans == 'Y'){
				exit(0);
			}else if(leave_ans == 'N'){
				break;
			}
		}
	}
}
To show you how much easier python is at the same thing:

Code: Select all

def divide(x,y):
	if y == 0:
		return False
	else:
		return x/y
while 1:
	num1 = raw_input("Please enter number 1: ")
	num2 = raw_input("Please enter number 2: ")
	ans = divide(int(num1),int(num2))
	if ans:
		print num1 + " / " + num2 + " = " + str(ans)
	else:
		print "Error: Division by zero not possible."
	while 1:
		ans = raw_input("Do you wish to leave? (Y/N): ")
		if ans == "Y":
			raise (SystemExit)
		elif ans == "N":
			break
It does pretty much exactly the same thing.
Administrator
Administrator
Posts: 3307
Joined: Thu Dec 24, 2009 2:06 am
Allegiance:: Space Rome
Location: ON, Canada
User avatar
Administrator
Administrator
Re: C++ GUI Frameworks

Post by Scott »

I considered learning Python after I had learned some Ruby but decided to try C++ instead. It is a very difficult language. One specific thing I'm confused about is the practical function of function recursion. I don't see how this could help when there are easier ways to get most of the tasks that recursion would be used for, done. Anyway, thanks again for the help.
Image
Jedi Master
Jedi Master
Posts: 336
Joined: Sun Aug 09, 2009 9:16 am
Allegiance:: Jedi
User avatar
Jedi Master
Jedi Master
Re: C++ GUI Frameworks

Post by Matthew »

Recursion is very useful. Take this factorial function in python as a good simple example:

Code: Select all

def factorial(n):
	if n == 0:
		return 1
	elif n < 0:
		return n * factorial(abs(n) - 1)
	else:
		return n * factorial(n - 1)
>>> factorial(0)
1
>>> factorial(1)
1
>>> factorial(2)
2
>>> factorial(3)
6
>>> factorial(4)
24
>>> factorial(5)
120
>>> factorial(-1)
-1
>>> factorial(-2)
-2
>>> factorial(-3)
-6
>>> factorial(-4)
-24
>>> factorial(-5)
-120
>>>
Administrator
Administrator
Posts: 3307
Joined: Thu Dec 24, 2009 2:06 am
Allegiance:: Space Rome
Location: ON, Canada
User avatar
Administrator
Administrator
Re: C++ GUI Frameworks

Post by Scott »

Well I know of uses (Fibonacci number, powers, etc) but I can't see when I would use it. Although I said the same about arrays when I first started Ruby.
Image
Jedi Master
Jedi Master
Posts: 336
Joined: Sun Aug 09, 2009 9:16 am
Allegiance:: Jedi
User avatar
Jedi Master
Jedi Master
Re: C++ GUI Frameworks

Post by Matthew »

In my game I've made a recursive function for several things. One of the things is the function which processes a bullet line. It calls itself again for ricochets. Another one was to recursively update the offset values for my Surface objects when I redraw their parent at another position.

One thing I should do all the time now but didn't used to, is set constants for magic numbers.

For my game I am very likely going to rewrite parts in C++. You can write python modules in C++, so it's very easy to extend python with C++. Well I'm hoping anyway.
Administrator
Administrator
Posts: 3307
Joined: Thu Dec 24, 2009 2:06 am
Allegiance:: Space Rome
Location: ON, Canada
User avatar
Administrator
Administrator
Re: C++ GUI Frameworks

Post by Scott »

Well I'm just in the beginning stages of C++ and am trying to use simpler techniques. Right now I'm trying to make Tic-Tac-Toe which is harder than I expected...
Image
Jedi Master
Jedi Master
Posts: 336
Joined: Sun Aug 09, 2009 9:16 am
Allegiance:: Jedi
User avatar
Jedi Master
Jedi Master
Re: C++ GUI Frameworks

Post by Matthew »

I tried to make battleships in VB at college. That was command line based but I was trying to make a nice interface and I never finished it. I do get to the stage of places the ships in a grid. The grid was made out of lots of dots and the ships were represented by letters.

Will you use a command based idea or an interactive grid?
Administrator
Administrator
Posts: 3307
Joined: Thu Dec 24, 2009 2:06 am
Allegiance:: Space Rome
Location: ON, Canada
User avatar
Administrator
Administrator
Re: C++ GUI Frameworks

Post by Scott »

I'm using a command line interface. The controls are basically the number pad. 1 is the bottom left, 5 is the middle, etc. I'm storing all of the info in arrays and want to eventually have a computer opponent, but I need to learn more about random numbers in C++ first.
Image
Jedi Master
Jedi Master
Posts: 336
Joined: Sun Aug 09, 2009 9:16 am
Allegiance:: Jedi
User avatar
Jedi Master
Jedi Master
Re: C++ GUI Frameworks

Post by Matthew »

C++ uses a simple rand() function but it gives you an integer. To convert it to a floating point number with the range (n) 0 < n < 1 you'll need to divide the result by RAND_MAX which is the maximum result. Of-course you'll need to convert to float .

If you want a random integer you don't need to convert to a floating pint number first, you can simply times the result by your range and then divide by RAND_MAX.
Administrator
Administrator
Posts: 3307
Joined: Thu Dec 24, 2009 2:06 am
Allegiance:: Space Rome
Location: ON, Canada
User avatar
Administrator
Administrator
Re: C++ GUI Frameworks

Post by Scott »

Do you know how to make small noises relatively simply? By scanning some source I found \a which makes a beep for every \a. It's neat to have for some programs (like the cola machine program I made when practicing with switches).
Image
Jedi Master
Jedi Master
Posts: 336
Joined: Sun Aug 09, 2009 9:16 am
Allegiance:: Jedi
User avatar
Jedi Master
Jedi Master
Re: C++ GUI Frameworks

Post by Matthew »

I'm sure a simple library can't be too difficult to find.
Administrator
Administrator
Posts: 3307
Joined: Thu Dec 24, 2009 2:06 am
Allegiance:: Space Rome
Location: ON, Canada
User avatar
Administrator
Administrator
Re: C++ GUI Frameworks

Post by Scott »

That would be an option but I'd like to stay close to the STL for now.
Image
Post Reply