Thread: operator!= and primary-expression error : First time poster

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    2

    operator!= and primary-expression error : First time poster

    Hi, I'm looking for a program that uses parallel one-dimensional arrays so when a user inputs the product ID, they have displayed the quantity and the price.

    Here is my code so far:
    Code:
    #include <iostream>
    
    using std::cout;
    using std::cin;
    using std::endl;
    using std::string;
    using std::transform;
    
    int main()
    {    
        //declare arrays
        string searchId   = " ";
        int ids[5]        = {10, 14, 34, 45, 78};
        int prices[5]     = {125, 600, 250, 350, 225};
        int quantities[5] = {5, 3, 9, 10, 2};
    
    
    //get id to search for, then convert to uppercase
          cout << "Enter ID (X to exit): ";
          getline (cin, searchId);
          transform (searchId.begin(), searchId.end(), searchId.begin(), toupper);
          
          while (searchId != "X")
          {
                //locate position of product ID in the ids array
                int y = 0; //keeps track of array subcripts
                while (y < 5 && ids[y]  != searchId)
                      y += 1;
                //end while
                
                //if ID was found, display price from prices array     
                if (y < 5)
                    cout << "Price for product ID: " << ids[y]
                         << ": $" << prices[y] << endl << endl;
    
                   //then display quantity from the quantities arry
                   cout << "Quantit for product ID: " << ids[y]
                         << ": " << quantities[y] << endl << endl;
    
                //If ID not found, display error message
                else
                    cout << "Invalid product ID" << endl << endl;
                //end if    
    
    
                //get ID to search for, then convert to uppercase
                cout << "Enter ID (X to exit): ";
                getline (cin, searchId);
                transform (searchId.begin(), searchId.end(), searchId.begin(), toupper); 
    
             } //end while
    
        return 0;
    }   //end of main function
    My problem is
    Code:
     while (y < 5 && ids[y]  != searchId)
    is getting a no match for 'operator!+' in 'ids[y]!=searchId'

    My second problem is
    Code:
    else
    is getting an expected primary-expression before "else". I do have a ( at the end of the line above else..

    If anyone can help me, or point me in the right direction, that would be awesome. Ive only been coding for about a month.

    Thanks again

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    ids[y] is of type int. searchID is of type std::string. There is no defined way to compare equality of an int and a std::string. Hence the "no match" error for operator!=

    If you look at your offending if/else code in isolation, you indentation is misleading you. I recreate it here, with your comments removed, and each statement on a single line.
    Code:
                if (y < 5)
                       cout << "Price for product ID: " << ids[y]  << ": $" << prices[y] << endl << endl;
    
                cout << "Quantit for product ID: " << ids[y]  << ": " << quantities[y] << endl << endl;
    
                else
                    cout << "Invalid product ID" << endl << endl;
    The compiler sees your code this way, regardless of how you have indented it. The line I have highlighted in red is not associated with the preceding if(). And, because that line exists, the "else" is also not associated with the preceding if().

    Hence the error about "primary-expression before else".

    You can fix your problem with a couple of curly braces, which I have highlighted in red below.
    Code:
                if (y < 5)
                {
                       cout << "Price for product ID: " << ids[y]  << ": $" << prices[y] << endl << endl;
    
                 cout << "Quantit for product ID: " << ids[y]  << ": " << quantities[y] << endl << endl;
                }
                else
                    cout << "Invalid product ID" << endl << endl;
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  3. #3
    Registered User
    Join Date
    Nov 2011
    Posts
    2

    Thanks!!

    Quote Originally Posted by grumpy View Post
    ids[y] is of type int. searchID is of type std::string. There is no defined way to compare equality of an int and a std::string. Hence the "no match" error for operator!=
    Thanks so much!

    I changed from string to an int and made some small changes to compensate, and it all works...

    Very much appreciated your help!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Error: Expected primary expression before "else"
    By llind212 in forum C++ Programming
    Replies: 1
    Last Post: 03-05-2011, 10:48 PM
  2. primary expression error when calling operation
    By elsparko in forum C++ Programming
    Replies: 2
    Last Post: 05-18-2010, 12:43 PM
  3. Replies: 9
    Last Post: 03-31-2009, 04:23 PM
  4. Replies: 24
    Last Post: 09-06-2006, 06:17 PM