-1

I am a beginner student looking for help on a program my professor assigned. The task is to use 2 parallel loops to ask the user 5 different salsas and their cost. Then print out the name, total sold, average, best seller and worst selling. The thing is, I can calculate best and worst selling, but those are ints. I don't understand how I can pull the string instead of int? Any help would be appriciated!#include

#include <string>
#include <cmath>
#include <cstring>
using namespace std;

int main() {
    string name[5];
    int total[5];
    int i, final, counter, high, low;

    for(i=0; i<=4; i++){
        cout << "Salsa name: ";
        cin >> name[i];
        cout << "How many of " << name[i] << " were sold? ";
        cin >> total[i];
        final += total[i];

        if(i < 0) {
            "Sorry, you cannot have negative sales!";
            return 0;
        } else {
            if(counter == 0) {
                low = total[i];
            } else if (total[i] < low) {
                low = total[i];
            } else if (total[i] > high) {
                high = total[i];
            } counter++;
        }
    }

        cout << "Name          Amount Sold\n"
             << "-------------------------\n";
        for(int i = 0; i <= 4; i++){
            cout << name[i] << "        " << total[i] << endl;
        }


    cout << "Total sold: " << final << endl
         << "Most Sold: " << high << endl
         << "Least Sold: " << low;
    return 0;
}

output:

Running /home/ubuntu/workspace/Ch6_Ex3.cpp
Salsa name: salsa1
How many of salsa1 were sold? 10
Salsa name: salsa2
How many of salsa2 were sold? 20
Salsa name: salsa3
How many of salsa3 were sold? 30
Salsa name: salsa4
How many of salsa4 were sold? 40
Salsa name: salsa5
How many of salsa5 were sold? 50
Name          Amount Sold
-------------------------
salsa1        10
salsa2        20
salsa3        30
salsa4        40
salsa5        50
Total sold: 32862
Most Sold: 50
Least Sold: -547659664

Process exited with code: 0
Vincent
  • 99
  • 5

1 Answers1

1

Your code has some hiccups:

  • There is no point in the variable counter because you mean it to have the same value as i, so use i; nor do you initialize it. Comparing counter == 0 is undefined behaviour.
  • Instead of keeping track of the highest/lowest values in total, you should instead keep track of the indexes of those specific values, because the corresponding value in names is the name of each salsa brand...

Take note of the changes below:

#include <iostream>
#include <string>
#include <cmath>
#include <cstring>
using namespace std;

int main() {
    string name[5];
    int total[5];

    int i;
    int final = 0;

    int high, low;
    high = 0, low = 0;

    for(i=0; i<=4; i++){
        cout << "Salsa name: ";
        cin >> name[i];
        cout << "How many of " << name[i] << " were sold? ";
        cin >> total[i];
        final += total[i];

        // if(i < 0) {
        //     "Sorry, you cannot have negative sales!";
        //     return 0;
        // }
        // i is never going to be less than 0.

        if (total[high] < total[i])
            high = i;
        if (total[low] > total[i])
            low = i;
    }

    // ...

    cout << "Most-sold salsa: " << name[high]
         << "\nLeast-sold: " << name[low] << "\n";

    return 0;
}

Other notes:

  • I strongly advise against using namespace std; See why
  • You initially forgot #include <iostream>
Charles
  • 1,384
  • 11
  • 18
  • Ahh, I see! thank you for the help. I wasnt taught the name[high] thing, thank you for teaching this to me! – Vincent Jul 11 '17 at 20:46