I am supposed to code a program that uses 3 parallel arrays. The program should prompt the user to enter in a Product ID. From there it is supposed to search the ids array and then display the corresponding price and quantity remaining. It does not do this.

Instead I am getting error C2784: 'bool std::operator !=(const std::istream_iterator<_Ty,_Elem,_Traits,... &,const std::istream_iterator<_Ty,_Elem,_Traits,... &)' : could not deduce template argument for 'const std::istream_iterator<_Ty,_Elem,_Traits,... &' from 'int'
1> c:\program files\microsoft visual studio 8\vc\include\iterator(277) : see declaration of 'std::operator !='

I believe that it is because I am using the string searchForId and the array is an integer. I need to be able to search for the array and I don't know how to change the coding or even what to put. I have looked at this for 5 hours and it makes me want to give up on C++. I was finally understanding C++ until arrays.

I am only looking for suggestions, not to have my work done for me.

Thanks for any input anyone can offer.

#include <iostream>
#include <string>
#include <algorithm>


using namespace std;


int main()
{
   //declare arrays
   int ids[5] = {10, 14, 34, 45, 78};
   int prices[5] = {125, 600, 250, 350, 225};
   int quantities[5] = {5, 3, 9, 10, 2};
   string searchForId = "";

   // get ID to search for 
   cout << "Enter ID (X to exit): ";
      getline(cin, searchForId);
      
   while (searchForId != "X") {

   // locate position of prices ID in the ids array
   int y = 0; // keeps track of array subscripts
      while (y < 5 && ids[y] != searchForId)
      y = y + 1;
   // end while

   // if ID was found then display the price and quantity from the  prices/quantity array otherwise display error message
   if (y < 5)
   cout << "Price for Product ID " << ids[y]
      << ": $" << prices[y] << endl << endl;
   cout << "Quantities Remaining: " << ids[y] << endl << prices[y]   << endl << quantities[y] << endl << endl;
   else
   cout << "Invalid Product ID" << endl << endl;
   // end if

   // get ID to search for 
   cout << "Enter ID (X to exit): ";
   getline(cin, searchForId);

}
// end while


return 0;
} //end of main function

this line is the problem: while (y < 5 && ids[y] != searchForId) "searchForId" is a std::string and ids[y] is an int. You can't compare apples to pears in c++ :)

You could use stringstreams to convert the string into an integer. Or you could use atoi() or even better strtol() you can research all of these on the nternet, they are all well documented, if you are still stuck then get back to us. (String streams would be the best approach!)

this line is the problem: while (y < 5 && ids[y] != searchForId) "searchForId" is a std::string and ids[y] is an int. You can't compare apples to pears in c++ :)

Think he is already aware of this, but doesn't know what to about chaning that

I believe that it is because I am using the string searchForId and the array is an integer

Chris

I have searched on using stringstreams but I don't know how to implement them into the code. Most of what I found deals with converting one string into an int. I can't locate anything that provides examples on how to do it if you are working with an array.

istringstream iss;
iss.str("56");
int x;
iss >> x;
if(myArray[y] == x)
  cout << "They match! at array position: " << y;

Thanks Freaky_Chris for your input.

I decided that something very simple was going to turn into a make work project. So I took a look at my code and changed it.

Changed code:

#include <iostream>
#include <string>
#include <algorithm>
#include <sstream>

using namespace std;

int main()
{
   //declare arrays
   int ids[5] = {10, 14, 34, 45, 78};
   int prices[5] = {125, 600, 250, 350, 225};
   int quantities[5] = {5, 3, 9, 10, 2};
   int searchForId = 0;

   
   // get ID to search for 
   cout << "Enter ID: ";
      cin >> searchForId;
      
   while (searchForId != 0) {

   // locate position of prices ID in the ids array
   int y = 0; // keeps track of array subscripts
      while (y < 5 && ids[y] != searchForId)
      y = y + 1;
   // end while

   // if ID was found then display the price and quantity from the  prices/quantity array otherwise display error message
	  if (y < 5) 
	  cout << "Price for Product ID " << ids[y]
      << ": $" << prices[y] << endl << endl;
	  cout << "Quantities Remaining for Product ID " <<ids[y] << ": " << quantities[y]   << endl  << endl;

	  else 
		  cout << "Invalid Product ID" << endl << endl; 
   // end if

   // get ID to search for 
   cout << "Enter ID: ";
    cin >> searchForId;
}
// end while


return 0;
} 
//end of main function

Now if I void out the following code, it works:

// else 
		 // cout << "Invalid Product ID" << endl << endl; 
   // end if

Enter ID: 10
Price for Product ID 10: $125

Quantities Remaining for Product ID 10: 5

Enter ID: . . .


What I need it to do is this. When someone enters an incorrect ids[], it is to return the message 'Invalid Product ID' and then the prompt continues to enter in a Product ID.

I set up the else statement to do this, when it is being compiled, Vs 2005 is returning the following error:

\\...error C2181: illegal else without matching if

I have the if statement listed already. Assistance please?

all of the lines to your if statement should be wrapped in {}.

Also using cin>> is a step backwards from getline() you should read the stick "How do I clear the imput buffer?"

Chris

all of the lines to your if statement should be wrapped in {}.

Also using cin>> is a step backwards from getline() you should read the stick "How do I clear the imput buffer?"

Chris

Along with the above you should also try to separate your code with functions.

And then you can use a call to those functions whenever you need to perform the same task .

Thanks for everyone's input. It works. I will be reading more in the stickys regarding "How do I clear the imput buffer?".

This question has already been answered. Start a new discussion instead.