Hello, I just recently started learning C ++, so the problem may seem stupid, but I hope that you will help.
The problem is this, I decided for practice to write a simple program that asks for the number of dishes ordered, their names and price, and then displays it with the total amount.
For prices and names of dishes made two vectors, and the output and the calculation of the amount allocated in a separate function, but the compiler swears to call this function and gives the error "no matching function for call to" total ". I tried to do the same with the usual array and it all worked, but I still want to know what was wrong with the vectors.

#include <string> #include <iostream> #include <vector> using namespace std; void line(){ for (int i=0; i<46; i++) { cout<<"*"; } cout<<endl; } int total(int kol_vo, string dish[], int cost[]){ int sum=0; line(); for (int i=0; i<kol_vo; i++) { sum+=cost[i]; cout<<dish[i]<<" : "<<cost[i]<<endl; } line(); return sum; } int main(){ cout<<"Введите количество позиций: "; int kol; cin>>kol; vector<string>dishes(kol); vector<string>cost(kol); for (int i=0; i<kol; i++) { cout<<"Введите название блюда "<<i+1<<": "; cin>>dishes[i]; cout<<"Введите цену блюда "<<i+1<<": "; cin>>cost[i]; } cout<<"Общий счёт: "<<total(kol,dishes, cost);//Вот тут показывает ошибку return 0; } 
  • so you have in the definition of the function of the parameters of the non-vector - Grundy

1 answer 1

Better this way:

 #include <string> #include <iostream> #include <vector> using namespace std; void line(){ for (int i=0; i<46; i++) { cout<<"*"; } cout<<endl; } int total(const vector<string>&dish, const vector<int>&cost) { int kol_vo = dish.size(); int sum=0; line(); for (int i=0; i<kol_vo; i++) { sum+=cost[i]; cout<<dish[i]<<" : "<<cost[i]<<endl; } line(); return sum; } int main() { cout<<"Введите количество позиций: "; int kol; cin>>kol; vector<string>dishes(kol); vector<int> cost(kol); for (int i=0; i<kol; i++) { cout<<"Введите название блюда "<<i+1<<": "; cin>>dishes[i]; cout<<"Введите цену блюда "<<i+1<<": "; cin>>cost[i]; } cout<<"Общий счёт: "<<total(dishes, cost);//Вот тут показывает ошибку return 0; } 

The vector itself remembers the number of entries in it; in addition, sum+=cost[i]; for strings, it simply does not work - strings must be translated into numbers before summing up.

But I would put both the name and the price in one vector - so there is less chance of some kind of mismatch. Here I used the standard pair<> template, but you can write your own structure:

 #include <string> #include <iostream> #include <vector> #include <utility> using namespace std; void line() { for (int i = 0; i < 46; i++) { cout << "*"; } cout << endl; } int total(const vector<pair<string, int>>& dish) { int kol_vo = dish.size(); int sum = 0; line(); for (int i = 0; i < kol_vo; i++) { sum += dish[i].second; cout << dish[i].first << " : " << dish[i].second << endl; } line(); return sum; } int main() { cout << "Введите количество позиций: "; int kol; cin >> kol; vector<pair<string, int>> dishes(kol); for (int i = 0; i < kol; i++) { cout << "Введите название блюда " << i + 1 << ": "; cin >> dishes[i].first; cout << "Введите цену блюда " << i + 1 << ": "; cin >> dishes[i].second; } cout << "Общий счёт: " << total(dishes); //Вот тут показывает ошибку return 0; } 
  • why the link? - Grundy
  • @Grundy I do not understand ... And why not? - Harry
  • Well, why yes? :) In this case, is there any difference? - Grundy
  • @Grundy Actually, copying all the lines - what if there will be a lot of them - somehow not good ... - Harry
  • Understood. Thanks for the help. - Jack