help with pointers
Nov 20, 2012 at 5:20am UTC
Hi everyone i'm writing a program that reads the name of the restaurant and the number of visitors of the restaurant. the goal of the program is to print the name of the restaurant with the most number of visitors (sum up all the number of visitor). I believe i am close to getting the answer, but i am stuck and i cant figure it out anymore. If any of you are willing to help me, i will appreciate that very much. thank you.
here is the sample data that i am trying to work on
AA 5
BB 2
CC 5
BB 8
CC 11
CC 5
CC 5
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <iomanip>
using namespace std;
void main ()
{
string filename, choice ,readword, *string_pointer;
ifstream infile; int abc;int xyz;
vector<string*> resto; vector<int > likes;
vector<string*> lines;
do
{
cout << " Type in the file name you want to open : " ;
cin >> filename;
infile.open(filename.c_str());
}
while (infile.fail());
string restaurant;
int like;
while (infile >> restaurant >> like )
{
likes.push_back(like);
string_pointer = new string;
*string_pointer = restaurant;
int x = -1;
for (int pos =0; pos <resto.size(); pos++)
{
if (*resto[pos] == *string_pointer)
{
x=pos;
//how to add up the likes with the same restaurant
}
}
if (x == -1)
{
resto.push_back(string_pointer);
likes.push_back(like);
}
cout << "rs : " << resto.size() << endl;
}
cout << resto.size() << endl;
cout << likes.size() << endl;
int abc =0;
for (int x=0; x<10; x++)
{
int max = likes [0];
for (int a=1; a < likes.size(); a++)
{
if ( likes [a] > max )
{
max = likes[a];
abc = a;
}
}
}
cout << setw(10) << *resto[abc] << " has visitor number : " << likes[abc] << " times" << endl;
infile.close();
system ("pause" );
}
Last edited on Nov 20, 2012 at 6:37am UTC
Nov 20, 2012 at 9:02am UTC
how to add up the likes with the same restaurant
It whould be much simplier with a map. But on the othe hand you already found the
pos
on line 43.
Use it:
likes[pos] += like;
Remove line 33
By the way: you really don't need a pointer to the strings (
string*
). Simply use string like a built in variable (
string
)
Nov 20, 2012 at 4:29pm UTC
wow thank you very much! just a simple line solves it all. thanks coder777! i really appreciate your help
Topic archived. No new replies allowed.