Skip to content
View in the app

A better way to browse. Learn more.

Tip.It Forum

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Help me overcome a problem with my C++ project

Featured Replies

Hi everyone,

 

 

 

My coursework involves creating a sudoku-oriented game in C++

 

 

 

Everything's going swimmingly - I'm using SDL Libraries to generate a sudoku board on the screen, and then using event handling to allow interaction with the user.

 

 

 

However, I'm currently programming the logic which is the last step. I've managed to get the program to evaluate every column/row and make sure that there is only one of each value (1-9) which appears in each column/row. Now I'm trying to evaluate every 3x3 square.

 

 

 

Here's my code:

 

 

 

		//checks every 3x3 square

	for (int x = 0 ; x < 9 ; x+3) {

		for (int y = 0 ; y < 9 ; y+3) {

			//checks for every value 1-9	

ERROR HERE:		for (int number = 1 ; number < 10 ; number++)[/color] {

				//creates variable to store the number of occurences

				int count = 0;

                   //checks each individual square of 3x3

				for (int n = 0 ; n < 3 ; n++) {

					for (int m = 0 ; m < 3 ; m++) {

						//increments count if the number is in that square	

						if (pBitmaps[x+n][y+m] == number) {

							count ++;

						}

					}

				}

				//ensures there is exactly one of each number per 3x3 square

				if (count != 1) {

					threebythree = false;

				}

			}

		}

	}

 

 

 

I've got another statement further down which concludes the program if threebythree is true, however, whenever I run my program, it just freezes up. It didn't do this before I added the above code.

 

 

 

Any ideas what's up?

 

 

 

EDIT: I've identified the flagged line of code to be the issue. When number gets to 10, it automatically resets to 1, and x and y don't increment. Erego I'm stuck in an infinite loop.

 

 

 

Much thanks,

 

 

 

~Ian

newsig4ty.jpg

Retired Tip It Moderator | Zybez Radio DJ - Listen Here

Hey, i added you on msn i think i can help you, but in return i have a question for you

yourmysin.jpeg

Current Goals

80/80 Fletching

60/75 Woodcutting

97/100 Combat

  • Author

SOLVED

 

 

 

Problem's in the FOR loops for x and y... x+3 isn't a valid increment it should be "x+=3"

 

 

 

D'oh!

 

 

 

~Ian

newsig4ty.jpg

Retired Tip It Moderator | Zybez Radio DJ - Listen Here

ahh i wasnt even really looking at that, more along the line that said "error" :P

yourmysin.jpeg

Current Goals

80/80 Fletching

60/75 Woodcutting

97/100 Combat

The code seems very inefficient though. You loop over each square 9 times...

 

 

 

A more efficient way would be to use lists and splice those, but I don't know how easy that is in C++. Another way would be to have a fixed number 9! (which happens to be 3628800) and divide by the number in each field. In the end you should have 1 remaining, and if you don't, something is wrong :) . You would need to take care to catch errors or allow for floating point numbers in the variable though (so not just integers), or the division screws up if it goes bad :)

Create an account or sign in to comment

Important Information

By using this site, you agree to our Terms of Use.

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.