I do not think this is possible to use a for( : ), because there is no iterator for the compiler to automatically know how big the data structure that you are using is.
It does no matter too much because you already know how big your structure is if you use your item_count variable. so using a for( ; ; ) loop should work just the same.
#include <iostream>
#include <iterator>
#include <string>
// from stackoverflow
namespace std {
template <typename T> T* begin(std::pair<T*, T*> const& p)
{ return p.first; }
template <typename T> T* end(std::pair<T*, T*> const& p)
{ return p.second; }
}
// your item type (...is not likely to be local to one function)
struct item {
std::string item_name;
double price;
};
int main () {
int item_count{0};
std::cout <<"Enter number of items : ";
std::cin >> item_count;
item *ptr = new item[item_count];
// you need this...
using std::begin;
using std::end;
// the modified ranged-for (notice the reference type)
for ( item& p : std::make_pair(ptr,ptr+item_count) )
{
std::cout << "Enter item name : ";
std::cin >> p.item_name;
std::cout << "Enter Price : ";
std::cin >> p.price;
}
// (make sure you have enough elements before trying to access one...)
if (item_count >= 3)
{
// (notice that you can access it with standard array syntax)
std::cout << "Third item : " << ptr[2].item_name << std::endl;
std::cout << "Third price: " << ptr[2].price << std::endl;
}
return 0;
}