Thread: Program stops working as soon as I input value?..

  1. #1
    Registered User
    Join Date
    Jun 2010
    Posts
    4

    Program stops working as soon as I input value?..

    I'm trying to create a dos-based sudoku solving program. The first step, as you can see in the code here, is to collect user input. The primary responsibility of the function "checkValid" is to collect each row of the puzzle to be solved and check for invalid input - (For example entering more than one of a certain value in a particular row).
    The code here compiles without any errors or warnings. I run the program and, as expected, I am immediately prompted to input the first row of values. However as soon as I finish the first input I get a Windows error: "program has stopped working".
    What is wrong with the checkValid function or the program in general that is causing this error?



    Code:
    #include <iostream>
    #include <string>
    
    
    
    using namespace std;
    
    
    
    
    bool checkValid(char*& chkd) {                  //Checks validity of user input.
     
        cin.getline(chkd, 10);  /*USER WILL BE PROMPTED TO ENTER '#" FOR EMPTY CELLS.*/
        bool j = true;
        string cts = chkd;    //Passed to sizeof to return amount of characters.
        int q = 9;
        if(sizeof(cts) != q)
        {
             cout << "You must enter exactly 9 characters." << endl;
             j = false;
        }    else   
             {
                  for(int nS = 1, a = 0; nS < (q + 1); nS++) 
                  {              
                        for(int nA = 0; nA < q; nA++)
                        {
                             if(chkd[nA] == nS) a++; 
                             if(a > 1)                               //<-Determines if more than one of a  
                             {                                         //certain value is present in a row or column.
                                  j = false;
                                  break;
                             } 
                        }  
                        if(j == false) break;
                  }     
             }  
        return j;                             
    }
    
    
    int main(int argc, char *argv[])
    {   
        //Rows to be implemented.
        char * r1 = "";
        char * r2 = "";
        char * r3 = "";
        char * r4 = "";
        char * r5 = "";
        char * r6 = ""; 
        char * r7 = "";
        char * r8 = "";
        char * r9 = "";
        
        for(int nDict = 1; nDict < 10; nDict++) 
        {
                again:
                bool b = true; 
                switch(nDict)
                {
                              case 1: if(checkValid(r1)) break; else { b = false; r1 = ""; break;}
                              case 2: if(checkValid(r2)) break; else { b = false; r2 = ""; break;}
                              case 3: if(checkValid(r3)) break; else { b = false; r3 = ""; break;}
                              case 4: if(checkValid(r4)) break; else { b = false; r4 = ""; break;}
                              case 5: if(checkValid(r5)) break; else { b = false; r5 = ""; break;}
                              case 6: if(checkValid(r6)) break; else { b = false; r6 = ""; break;}
                              case 7: if(checkValid(r7)) break; else { b = false; r7 = ""; break;}
                              case 8: if(checkValid(r8)) break; else { b = false; r8 = ""; break;}
                              case 9: if(checkValid(r9)) break; else { b = false; r9 = ""; break;}
                }
                if( b == false) goto again;                                                                   
        }                       
    }
    Last edited by darkmagic; 03-08-2011 at 12:37 AM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    It stops working, because you're trying to modify string constants.

    Just delete all your char * variables and replace them all with std::string


    Also, your use of goto is poor to say the least - not least because the loop will do the same thing without the goto.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. program stops before EOF
    By doia in forum C Programming
    Replies: 2
    Last Post: 03-22-2010, 12:10 PM
  2. Program stopping working
    By SterlingM in forum C++ Programming
    Replies: 24
    Last Post: 10-17-2009, 02:38 PM
  3. Program that requests input of an integer number
    By theejuice in forum C Programming
    Replies: 6
    Last Post: 04-30-2008, 02:18 AM
  4. About aes
    By gumit in forum C Programming
    Replies: 13
    Last Post: 10-24-2006, 03:42 PM
  5. Need Delimiter Program helpful hints.
    By Unregistered in forum C Programming
    Replies: 7
    Last Post: 02-16-2002, 06:27 PM