0

I'm new to this board, and to be honest, I have been beating my head against the wall attempting to complete this last portion of this project. I got everything else working wonderfully. Long story short, I need to apply the associated string values to the variable day. Which string is based off of the data in the array "hotdogs[] and the corresponding value "largest".

"Largest" is the biggest number in the "hotdogs[]" array. My thinking of this is if array element 0 in hotdogs[] is not the largest, move onto the else if, and so on until it finds which element of array hotdogs[] is the largest variable.

The whole thing compiles and runs, but keeps giving me the same answer. That day "7" has the most sold hot dogs. Regardless of where the largest number is entered. Since I need a 10 ranking to post pics, I'll copy past the code.

 for (h=0; h<=6; h++)
{
    cin >> hotdogs[h];
    hdogsales = hdogsales + hotdogs[h];
}
for (int h=0; h<=6; h++)
{
    if(hotdogs[h] > largest)
    largest=hotdogs[h];
}

 {
    if (hotdogs[h] == largest)
    day = "1";
    else if (hotdogs[h] == largest)
    day = "2";
    else if (hotdogs[h] == largest)
    day = "3";
    else if (hotdogs[h] == largest)
    day = "4";
    else if (hotdogs[h] == largest)
    day = "5";
    else if (hotdogs[h] == largest)
    day = "6";
    else if (hotdogs[h] == largest);
    day = "7";
}

I would REALLY appreciate help on this. It is driving me nuts already. I have an inkling that the parameters I'm setting in my if statement is the culprit, but for the life of me, I cannot think what else to use in there.

13
  • Set largest= hotdogs[0]; prior to entering the loop (and optionally start the loop at 1). As-wrtten, largest is in determine entering the loop, and thus evaluating it is undefined behavior. And h is accesing hotdogs[] our of bounds anyway once you finish the for-loop. Commented May 4, 2014 at 0:02
  • @user: A tip. Most experts can read code faster than they can read English. As much as possible, use your code to explain your problem. Of course, you should leave out parts that are not relevant. For example, show your code and then put in a comment like "var seems to be assigned 2 rather than 3 as I expect." Commented May 4, 2014 at 0:05
  • There's a std::max_element function that finds the max element, and the index can be extracted from the result with std::distance. Commented May 4, 2014 at 0:06
  • in my declarations, I have largest = hotdogs[0]. I didn't copy this over because I didn't think that was the problem area. Commented May 4, 2014 at 0:06
  • That sequence of if/else if conditions looks suspicious. The conditions are all the same. Commented May 4, 2014 at 0:07

3 Answers 3

1

Regarding day always being 7, this is probably your problem.

else if (hotdogs[h] == largest);
day = "7";

With this, it's equivalent to this:

else if (hotdogs[h] == largest) {
    ;
}
day = "7";

That's the problem with not using braces; if there's an extra ; it eats up the if/else condition and then the next line is always executed.

You should probably refactor that whole loop down to be set at the same time as the top loop, as well:

for (int h=0; h<=6; h++)
{
    if(hotdogs[h] > largest)
    {
        largest=hotdogs[h];
        day = h;
    }
}
Sign up to request clarification or add additional context in comments.

4 Comments

I see. how could this be rectified though? Make separate if statements, opening and closing braces for each set of conditions?
See the amendment; it's probably easier to set day when you're finding out which is the largest. My example sets the number but you could change that line to whatever format you so desire. For the ; error, just remove that ; and it would theoretically always end up with day being "1" because all conditions are the same. If you don't have braces around the if lines it executes up to the first ;.
Thanks for the input everyone. Leigh gave me the correct answer looked right over, and disregarded. Deleted it, and it works perfectly now. Lesson learned: check, check twice, and check thrice for proper punctuation.
@user3600284 Fantastic. You may want to consider reading the FAQs of how this site works, and up-tick any answers you find helpful. if an answer provides the "best" solution for your needs, you should "accept" it.
0
#include <iostream>
#include <string>
#include <algorithm>
#include <iterator>

using namespace std;

int main()
{
    int hotdogs[7] = { 12, 43, 76, 23, 98, 1, 27 };
    int hdogsales = accumulate(begin(hotdogs), end(hotdogs), 0);


    auto bestDay = std::distance(begin(hotdogs), 
                    std::max_element(begin(hotdogs), end(hotdogs)));

    cout << "total sales for the week: " << hdogsales << endl;
    cout << "best day was day " << to_string(bestDay + 1) << " with " << hotdogs[bestDay] << " sales\n";

    return 0;
}

Comments

0

i think you just should add a loop in line 11, just like this:

for (h = 0; h <= 6; h++)
{
    cin >> hotdogs[h];
    hdogsales = hdogsales + hotdogs[h];
}
for (int h = 0; h <= 6; h++)
{
    if (hotdogs[h] > largest)
        largest = hotdogs[h];
}
for (int h = 0; h <= 6; h++)
{
    if (hotdogs[h] == largest)
        day = to_string(h + 1);
}
cout << day;

EDIT : sorry i missunderstanded the question, here it is i think :)

4 Comments

This code makes about as little sense as the original one. It still has an if statement with the same condition 7 times.
was gonna say the same thing.
@user3600284 i'm sorry missunderstanded the question, now i think it's fine
Thanks for the input everyone. Leigh gave me the correct answer looked right over, and disregarded. Deleted it, and it works perfectly now. Lesson learned: check, check twice, and check thrice for proper punctuation.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.