hi, i am creating a program that reads input from a file into a sudoku square. and my program is supposed to check all rows, columns, grids for any repeat numbers, and if there are repeat numbers, the program is supposed to tell me which number is being repeated in a particular row/column/grid. below is the code i wrote, but there seemed to be some problems. can someone tell me what is wrong? thanks very much.

#include <cstdlib>
#include <iostream>
#include <fstream>
#include <vector>

using namespace std;

char file[] = "sud1.txt";

int collect(int i, int di, int n, int mask)
{
    cout << file << ":" << endl << endl;
    ifstream in("C:\\Documents and Settings\\HP\\Desktop\\sud1.txt");
    vector<int> S;
    int buffer;
    while(in>>buffer)
        S.push_back(buffer);
    if(S.size()==81)
       cout << "input worked" << endl;
    else
       cout << "input error" << endl;
       
    for(; n--;i+=di)
    {
    mask|=1<<S.at(i)-1;
    return mask;
    }
}
int main(int argc, char *argv[])
{
       enum bitflag
       {
       bitflag1 = 0x001,
       bitflag2 = 0x002,
       bitflag3 = 0x004,
       bitflag4 = 0x008,
       bitflag5 = 0x010,
       bitflag6 = 0x020,
       bitflag7 = 0x040,
       bitflag8 = 0x080,
       bitflag9 = 0x100,
       };
       bitflag == (0x001||0x002||0x004||0x008||0x010||0x020||0x040||0x080||0x100);
       
       
      int i;
      for(int i=0;i<81;i+=9)
      {
      if (collect(i, 1, 9, 0) != bitflag)
      cout<<i<<" appears twice in row "<<(i/9)+1<<endl;
      }
      
      int j;
      for(int j=0;j<81;j+=9)
      {
      if (collect(j, 1, 9, 0) != bitflag)
      cout<<j<<" appears twice in row "<<(j/9)+1<<endl;
      }
      
      int grid[9];
      for(int grid=0;grid<9;grid++)
      {
      if (collect(grid, 1, 3, 0) != bitflag)
      cout<<grid<<" appears twice in grid "<<(j/3)+1<<endl;    
      }     
  

          
    system("PAUSE");
    return EXIT_SUCCESS;
}

the input test file is

8         3         2         5         9         1         6         7         4         
 4         9         6         3         8         7         2         5         1         
 5         7         1         2         6         4         9         8         3         
 1         8         5         7         4         6         3         9         2         
 2         6         7         9         5         3         4         1         8         
 9         4         3         8         1         2         7         6         5         
 7         1         4         6         3         8         5         2         9         
 3         2         9         1         7         5         8         4         6         
 6         5         8         4         2         9         1         3         7

the errors given are as follows:
for the line highlighted in green : expected primary expression before '==' token.
for those in red: expected primary expression before ')' token.
if you notice any other problems with my code, please tell me.

thank you very much for your time!

Recommended Answers

All 7 Replies

The line in green causes all your problems. I can't give you a solution (yet), because I haven't got a clue what you expect that line to do :) So enlighten me!

the line in green, it is supposed to be a collection of integers from 1,2,3...,9. and so when i take a particular row/column/grid and compared it with the flag, which is the code in green, it is supposed to match. so if it doesnt match with the flag, an error is supposed to occur and tell me which integer is being repeated.

Member Avatar for iamthwee

Why don't you just do a search on this forum for finding duplicates?

i did. but the main thing i want is to find out what is wrong with my code and what i can do to improve on it.

Member Avatar for iamthwee

i did. but the main thing i want is to find out what is wrong with my code and what i can do to improve on it.

Well for one, your code is horribly obfuscated.

What is the point of bitflag1 = 0x001 ? etc

someone suggested that i do a flag and give each number from 1-9 a flag number, and then to group it, test against the input file. so that is what i came up with. what do you suggest i should do? i have been trying for 5 days and still could not complete the program.

Member Avatar for iamthwee

I suggest you search the forums for finding duplicates in c++.

Use the code you have found to, identify which column/row has duplicate entries and then move on from there.

There are many ways to find duplicates.

One is to sort the values first. That way all the duplicates will all be next to one another.
Or you might be able to use a std::map.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.