Thread: C++ problem

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    1

    C++ problem

    Hi
    if anyone can help me to find out what is wrong with my program
    it gives me 3 different errors, which I'm not familier.
    Thanks



    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    #include <cctype>
    using namespace std;
    
    
    int dispmenu(int);                             // Function for menu display
    int checkisbn(string, int);                    // Function that calls verifying functions
    istream& FLUSH(istream& stream);               // Flush function
    int lengthcheck(string);                       // Length check function
    int dashcheck(string);                         // Dash check funtion
    int sumcheck(string);                          // Checksum funtion
    int badstart(string);                          // Dash/Alpha start or end check function
    void disperror(int,string);                    // Display errors function
    
    
    int main()
    {
         int menuinput;                            // Input from menu display
         string ISBN;                              // String used for verifying
         ifstream fin;                             // Fin declared as input file stream
         int linecount;                            // Used as a line counter
         int dashcount;                            // Used as a dash counter
         int i;                                    // Used as place holder in functions
         string filename;
          cout << "This program checks to see if your ISBN Number";
          cout << " is in the correct format." <<endl;
          bool done = false;                       // Initialization  for Do-While
          do
          
    	  // Display menu take input and do user/file input
          {
             int errorcode = 0;                    // Initialization for error reader
             menuinput = dispmenu(menuinput);
             switch(menuinput)                     // Switch input from user
             {
                 case 1:                           // Case 1 for user input
                   cout << endl << "ISBN: ";
                   cin >> ISBN;
                   errorcode = checkisbn(ISBN, errorcode);
                   // Error code returned from verifying functions
                   disperror(errorcode, ISBN);     // Display of error code
                 break;
                 case 2:                           // Case 2 for file verification
                   cout << "Enter the file name: ";
                   cin >> filename;
                   fin.open(filename.c_str());     // File ISBNtest.txt opening
    
                   if (fin.fail())
                   {
                      cerr << endl <<  "Error opening file, so sorry. " << endl;
                   }
                   else
                   {
    
                    linecount = 0;                 // Line count for file read
                    int num;
                  //  fin.ignore(80, '\n');        // Using this would make file not work?
                    fin >> num;
                    for( i=0; i <= num && !fin.eof(); ++i)
                    {
                      getline(fin, ISBN);          // Get from file
    
                      if(ISBN.at(ISBN.length()-1) == '\0')
                       {
                         ISBN.erase((ISBN.length()-1),1);
                       }
    
                      errorcode = checkisbn(ISBN, errorcode);
                                                   // Returned error code
                      disperror(errorcode,ISBN);
                                                   // Display error from verifying functions
                      linecount++;
                    }
                   }
                 break;
                 case 3:                           // Case 3 for  quiting
                 done = true;
                 break;
                 default:
                      cout << endl << "Congratulations Mr. Gelotte you have succeeded in ";
                      cout <<"cracking my code!"<< endl;
                      cout << "Now all i want is your crepe recipe! " << endl;
                 break;
             }                                     // End of input switch
          }while(!done);                           // End do-While
    
    fin.close();
    return 0;
    }
    
    
    
    int dispmenu(int menuinput)                   // Display menu function
    {
    
         cout << endl << "Enter 1 to input your ISBN." <<endl;
         cout << "Enter 2 to check a file of ISBNs." <<endl;
         cout << "Enter 3 to quit the program." <<endl << endl << "Input: ";
         cin >> menuinput;
         cout << "" << endl << endl;
         if (cin.fail())
          {
             cerr << "Invalid entry, please re-enter. " << endl;
             FLUSH (cin);
             cout << "Choice: " << endl;
             cin >> menuinput;
             cout << endl;
          }
    
    
             return menuinput;                    // Return to switch
    
    }                                             // End of menu display function
    
    
    int checkisbn(string ISBN,int errorcode)      // Function for verifying functions
    {
        errorcode = lengthcheck(ISBN);
        if(errorcode != 0)
        {
          return errorcode;
        }
        errorcode = badstart(ISBN);
        if(errorcode != 0)
        {
          return errorcode;
        }
        errorcode = dashcheck(ISBN);
        if (errorcode != 0)
        {
          return errorcode;
        }
        errorcode = sumcheck(ISBN);
        if (errorcode != 0)
        {
          return errorcode;
        }
        return 0;
    }                                             // End of verifying function
    
    
    istream& FLUSH(istream& stream)               // Flush cin function
    {
    	stream.clear();
    	int ch;
    	while ((ch = stream.get()) != '\n' && ch != EOF)
    	{
    	   ;                          	          // Nothing inside loop it's in the condition
    	}
    	stream.clear();
    	return stream;
    }                                             // End of flush function
    
    
    
    int lengthcheck(string ISBN)                  // Length check function
    {
       if((ISBN.length() == 13) || (ISBN.length() == 10))
         {
           return 0;
         }
         else
         {
            return 1;
         }
    }                                             // End of length check function
    
    
    
    int badstart(string ISBN)                     // Start and end check function
    {
      if(!isdigit(ISBN.at(0)))
        {
          return 2;
        }
    
      if(ISBN.at(ISBN.length()-1) == 'x')         // Will take care of later
        {
    
          return 0;
        }
    
      if(ISBN.at(ISBN.length()-1) == 'X')         // Correct 'x' case
        {
          return 0;
        }
    
      if(!isdigit(ISBN.at(ISBN.length()-1)))
        {
          return 2;
        }
        return 0;
    }                                             // End of start and end check function
    
    
    
    int dashcheck(string ISBN)
    {                                             // Dash check function
         int dashcount = 0;
    
         for (int i=0; i < ISBN.length(); ++i)
            {
            if(ISBN.at(i) == '-')
              {
            if(ISBN.at(i + 1) == '-')             // Sequential dash check
              {
                  return 3;
              }
            if(!isdigit(ISBN.at(i)))
              {
            if(ISBN.at(i) == '-')
              {
                 dashcount++;                     // Dash counter for verifying
              }                                   //  correct number of dashes
              }
              }
            }
    
          if(dashcount != 3 && dashcount != 0)    // Min/Max dash check
            {
               return 4;
            }
            else
            {
               return 0;
            }
    }                                             // End of dash check function
    
    
    
    int sumcheck(string ISBN)                     // Check sum function
    {   int value;
        int checksum = 0;
        int finalsum = 0;
        int remove = ISBN.length();               // Had problems using .length in function
        value = (ISBN.at(ISBN.length()-1)-48);    // Set for the value in thatp ostion
    
        for(int i=0; i < remove-1; ++ i)          // If not digit, erase
          {
          if(!isdigit (ISBN.at(i)))
            {
               ISBN.erase(i,1);
               remove -= 1;                       // .length() would not work with decrement
               --i;
            }
          }                                       // End for loop
          if(ISBN.length() !=10)                  // Length should be ten 
    if valid;
            {
               return 5;
            }
    
         for(int i=0; i < ISBN.length()-1; ++i)   // Do arithmetic
            {
               checksum += ((i+1) * (ISBN.at(i)-48));
            }
        finalsum = checksum % 11;
         if(value == 40 )                         // Value == the value in last position
           {
              finalsum = 40;                      // This is for accepting x's
           }
         if(value == 72 )                         // This is for accepting x's
           {
              finalsum = 72;
           }
         if(finalsum == value)
           {
             return 12;
           }
         else
           {
             return 6;
           }
                                                 // End check sum function
    
    
    
    void disperror(int errorcode,string ISBN)     // Error display function
    {
    switch (errorcode)                           // Error case from return
    switch
    {
      case 12:
          if(ISBN.length() == 13)
          {
            cout << "ISBN: " << ISBN << "  Is a valid ISBN." <<endl;
          }                                       // Tried to make pretty in display
          else
          {
            cout << "ISBN: " << ISBN << "     Is a valid ISBN." <<endl;
          }
      break;
      case 1:
          if(ISBN.length() < 13)
          {
             cout << "ISBN: " << ISBN << "   Is invalid, not correct length. "<< endl;
          }
          if(ISBN.length() > 13)                 // Trying to make pretty in display
          {
             cout << "ISBN: " << ISBN << " Is invalid, not correct length. " <<endl;
          }
      break;
      case 2: 
    	cout << "ISBN: " << ISBN << "  Is Invalid, cannot start or end with a dash." << endl;
      break;
      case 3:
        cout << "ISBN: " << ISBN << "  Is invalid, consequtive dashes not allowed." << endl;
      break;
      case 4:
        cout << "ISBN: " << ISBN << "  Is Invalid, wrong number of dashes. " <<endl;
      break;
      case 5:
        cout << "ISBN: " << ISBN << "  Is invalid, bad digit(s). " << endl;
      break;
      case 6:
        cout << "ISBN: " << ISBN << "  Is invalid, bad checksum. " << endl;
      break;
    
      }                                           // End of error code 
    switch
                                                 // End of error displayfunction
    }

    This is what I get as errors:

    Error 5 error C2061: syntax error : identifier 'valid' c:\Documents and Settings\majid\My Documents\Visual Studio 2005\Projects\program4\program4\program4.cpp 250


    Error 7 error C2601: 'disperror' : local function definitions are illegal c:\Documents and Settings\majid\My Documents\Visual Studio 2005\Projects\program4\program4\program4.cpp 281


    Error 8 fatal error C1075: end of file found before the left brace '{' at '.\program4.cpp(234)' was matched c:\Documents and Settings\majid\My Documents\Visual Studio 2005\Projects\program4\program4\program4.cpp 329

  2. #2
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    There's no excuse using an editor the likes of Visual Studio 2005 and not knowing how to interpret and locate the errors in your code.

    Error 5: move to line 250 (it's clearly stated in the error message that line 250 is the source of the error). You will see immediatly how to fix it.

    Error 7: the disperror function is being defined before you closed the braces on the previous function.

    Error 8: Is generated because of error 7
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  3. #3
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    Hmm... and the switch statement inside disperror is a mesh.
    Code:
    switch (errorcode)                           // Error case from return
    switch <--- what is this doing here?
    {
      case 12:
          if(ISBN.length() == 13)
          {
            cout << "ISBN: " << ISBN << "  Is a valid ISBN." <<endl;
          }                                       // Tried to make pretty in display
          else
          {
            cout << "ISBN: " << ISBN << "     Is a valid ISBN." <<endl;
          }
      break;
      case 1:
          if(ISBN.length() < 13)
          {
             cout << "ISBN: " << ISBN << "   Is invalid, not correct length. "<< endl;
          }
          if(ISBN.length() > 13)                 // Trying to make pretty in display
          {
             cout << "ISBN: " << ISBN << " Is invalid, not correct length. " <<endl;
          }
      break;
      case 2:
    	cout << "ISBN: " << ISBN << "  Is Invalid, cannot start or end with a dash." << endl;
      break;
      case 3:
        cout << "ISBN: " << ISBN << "  Is invalid, consequtive dashes not allowed." << endl;
      break;
      case 4:
        cout << "ISBN: " << ISBN << "  Is Invalid, wrong number of dashes. " <<endl;
      break;
      case 5:
        cout << "ISBN: " << ISBN << "  Is invalid, bad digit(s). " << endl;
      break;
      case 6:
        cout << "ISBN: " << ISBN << "  Is invalid, bad checksum. " << endl;
      break;
    
      }                                           // End of error code
    switch <--- It also enjoys being here for some reason. 
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help understanding a problem
    By dnguyen1022 in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2009, 04:21 PM
  2. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  3. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  4. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  5. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM