Once I completed the following code, compiled.....this is what displayed: Number of 'mild' jars sold. (That's it). Is there something wrong with my C-prompt? Did I miss something? the code is sappose to:
Prompt the user to enter the number of jars sold for each type. Once this sales data has been entered, the program should produce
a report that displays sales for each salsa type, total sales, and the names of the highest selling and lowest selling products. See code:
//This program lets a maker of chips and salsa keep track of sales
#include <iostream>
#include <string>

using namespace std;
int findMin(int array[])
{
int min = array[0];
int i, index = 0;
for(i = 0; i<5; i++)
{
if(array<min)
{
index = i;
min = array;
}
}
return index;
}
int findMax(int array[])
{
int max = array[0];
int i, index = 0;
for(i = 0; i<5; i++)
{
if(array>max)
{
index = i;
max = array;
}
}
return index;
}
int main()
{
string salsa[] = {"mild", "medium", "sweet", "hot", "zesty"};
int jars_sold[5];
int total_sale = 0;
int index, i;
cout<<"Number of 'mild' jar sold\t: ";
cin>>jars_sold[0];
cout<<"Number of 'medium' jar sold\t: ";
cin>>jars_sold[1];
cout<<"Number of 'sweet' jar sold\t: ";
cin>>jars_sold[2];
cout<<"Number of 'hot' jar sold\t: ";
cin>>jars_sold[3];
cout<<"Number of 'zesty' jar sold\t: ";
cin>>jars_sold[4];

cout<<"\n\nReport\n";
cout<<"------\n";
for(i = 0; i<5; i++)
{
cout<<jars_sold<<" "<<salsa<<" jars sold"<<endl;
total_sale += jars_sold;
}
cout<<"\nTotal jar sold : "<<total_sale<<endl<<endl;
index = findMin(jars_sold);
cout<<"\nLowest selling salsa is (are) : "<<endl;
for(i = 0; i<5; i++)
{
if(jars_sold == jars_sold[index])
cout<<salsa<<endl;
}
index = findMax(jars_sold);
cout<<"\nHighest selling salsa is (are) : "<<endl;
for(i = 0; i<5; i++)
{
if(jars_sold == jars_sold[index])
cout<<salsa<<endl;
}
return 0;
}

What went wrong?

Dani AI

Generated

A few practical follow-ups and hardening tips for , plus acknowledgement of and for the quick troubleshooting.

The symptom you saw is commonly an input/run-time issue; the program blocks waiting for data. To make the utility more robust and friendlier, prompt inside a loop, validate input, avoid magic numbers, and use standard algorithms to find min/max and handle ties. The example below shows a concise, safe pattern you can adapt.

#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <numeric>
#include <limits>

int main() {
    std::vector<std::string> names = {"mild","medium","sweet","hot","zesty"};
    std::vector<int> sold(names.size());
    for (size_t i = 0; i < names.size(); ++i) {
        std::cout << "Enter sales for " << names[i] << ": " << std::flush;
        while (!(std::cin >> sold[i]) || sold[i] < 0) {
            if (std::cin.eof()) return 1;
            std::cin.clear();
            std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
            std::cout << "Invalid entry — enter a non-negative integer: " << std::flush;
        }
    }

    int total = std::accumulate(sold.begin(), sold.end(), 0);
    int minv = *std::min_element(sold.begin(), sold.end());
    int maxv = *std::max_element(sold.begin(), sold.end());

    std::cout << "\nReport\n------\n";
    for (size_t i = 0; i < names.size(); ++i) std::cout << names[i] << ": " << sold[i] << "\n";
    std::cout << "Total: " << total << "\n\nLowest selling:\n";
    for (size_t i = 0; i < names.size(); ++i) if (sold[i] == minv) std::cout << names[i] << "\n";
    std::cout << "Highest selling:\n";
    for (size_t i = 0; i < names.size(); ++i) if (sold[i] == maxv) std::cout << names[i] << "\n";
}

Extra notes: run the program from a terminal so prompts stay visible (double-clicking an executable on some OSes closes the window immediately). Use std::flush or std::endl when you need the prompt shown before input. Prefer std::vector/std::array and names.size() to avoid hard-coded lengths, and always check cin for failures so the program can re-prompt or exit cleanly.

Recommended Answers

All 6 Replies

Try entering a number and pressing enter.

Thanks...That would help wouldn't it.

AnG'

Try entering a number and pressing enter.

Did it work? I would assume it did, all you had to do was enter a number to cin :lol:

your code is fine man.. I enter 5 numbers (one for each type) and the output seems correct to me. Dont know what went wrong on your side?!?...

It did....Thanx a bunch. I guess I'm coding 2hard!

S-L-O-W.....Thanx...I simply forgot!! Appreciate the help!!

Yes it worked. Thanx again!!

AnG'

Please don't post the same thank you three times :rolleyes:

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.