-2

Previously I've asked a question and got a small answer but now I'm taken to a point in which I'm unable to compile this code successfully. I'm attempting to build a shop type database, for now I can do with simply using an array of strings. But I've been recommended that using an array of strings is bad practise to use in C++ and isn't a good idea to continue with. So for future reference I'd like to this done so I may be later tackle on vectors in my exam.

 #include "string"
 #include "vector"
 #include "iostream"
 using namespace std;
 class shop
 {
    private:
         int i, n, item[20];
         float price[20];
         std::vector<std::string> name;
     public:
         void input();
         void output();
 };
 void shop::input()
 {
     cout << "Enter the number of items: ";
     cin >> n;
     name.clear();
     name.push_back(n);
     name.resize(n);
     for(i = 1; i <= n; i++)
     {
         cout << "Enter the item number of the " << i << " item: ";
         cin >> item[i];
         cout << "Enter the name of the item: ";
         cin >> name[i];
         cout << "Enter the price of the item: ";
         cin >> price[i];
     }
 }
 void shop::output()
 {
     for(i = 1; i <= n; i++)
     {
         cout << "Item Number: " << item[i] << endl;
         cout << "Price: " << price[i] << endl << endl;
         cout << "Name: " << name[i] << endl << endl;
     }
 }
 void main()
 {
     class shop s;
     s.input();
     s.output();
 }

But the error I get is:

1>------ Build started: Project: Project1, Configuration: Debug Win32 ------
1>  Source.cpp
1>c:\users\khale_000\documents\visual studio 2013\projects\project1\project1\source.cpp(20): error C2664: 'void std::vector<std::string,std::allocator<_Ty>>::push_back(const std::basic_string<char,std::char_traits<char>,std::allocator<char>> &)' : cannot convert argument 1 from 'int' to 'std::basic_string<char,std::char_traits<char>,std::allocator<char>> &&'
1>          with
1>          [
1>              _Ty=std::string
1>          ]
1>          Reason: cannot convert from 'int' to 'std::basic_string<char,std::char_traits<char>,std::allocator<char>>'
1>          No constructor could take the source type, or constructor overload resolution was ambiguous
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
Khaleel78
  • 23
  • 1
  • 3
  • 2
    Look at line 20 (which the error gives). It looks like you're pushing back an int into a string vector. Since the error is actually pointing at that line I'd advise you to do your "research" before posting – keyser Apr 19 '14 at 10:08
  • name.push_back(n); n is integer and your vector contains std::strings. you probably want to convert to string first. – AlexTheo Apr 19 '14 at 10:09
  • If you haven't read [any of these](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) yet, I recommend you to start with one of the Introductory section. – Zeta Apr 19 '14 at 10:11

2 Answers2

2

You defined the vector as a vector of objects of type std::string

std::vector<std::string> name;

but tried to push an integer in it.

 cin >> n;
 name.clear();
 name.push_back(n);

Also this code is also invalid

 for(i = 1; i <= n; i++)
 {
     cout << "Enter the item number of the " << i << " item: ";
     cin >> item[i];
     cout << "Enter the name of the item: ";
     cin >> name[i];
     cout << "Enter the price of the item: ";
     cin >> price[i];
 }

because you can write beyond arrays item and price. You do not check whether n is greater than or equal to 20 that is the size of the arrays. In fact the code has no sense.

I would write it the following way

class shop
 {
    private:
         struct article
         {
              int item;
              float price;
              std::string name;
         };
         std::vector<article> articles;
     public:
         void input();
         void output const();
 };

void shop::input()
{
     cout << "Enter the number of items: ";
     int n;
     cin >> n;

     articles.clear();
     articles.reserve( n );

     for ( int i = 1; i < n; i++ )
     {
         article a;
         cout << "Enter the item number of the " << i << " item: ";
         cin >> a.item;
         cout << "Enter the name of the item: ";
         cin >> a.name;
         cout << "Enter the price of the item: ";
         cin >> a.price;
         articles.push_back( a );
     }
 }

Take into account that I am using member function reserve instead of resize as it was in your function.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
1

You're reading an integer n and trying to push it into a vector of strings. The compiler's error is quite clear about it. If you want to convert it into a string use std::to_string. If you cannot use C++11 then use snprintf(buffer, size, "%d", n);.

legends2k
  • 31,634
  • 25
  • 118
  • 222