Jump to content

Welcome to Geeks to Go - Register now for FREE

Geeks To Go is a helpful hub, where thousands of volunteer geeks quickly serve friendly answers and support. Check out the forums and get free advice from the experts. Register now to gain access to all of our features, it's FREE and only takes one minute. Once registered and logged in, you will be able to create topics, post replies to existing threads, give reputation to your fellow members, get your own private messenger, post status updates, manage your profile and so much more.

Create Account How it Works
Photo

c++ question


  • Please log in to reply

#1
ihatetheworld

ihatetheworld

    New Member

  • Member
  • Pip
  • 6 posts
im stuck again how would i got about showing the highest amount of sales i need to display the winners name and price, but the price keeps displaying what ever was the last amount entered and i dont know how to display name any help would be good thank you.


// displays highest amounts and the name of the highest.
#include <iostream>
#include <iomanip>
using namespace std;

int main()
{
const int airlines = 1 ; //< problem here i dont get
int count;
double sales[airlines], // To hold sales amounts
highest; // To hold the highest sales


// Get the sales data.
cout << "Enter the sales for this week.\n";
for (count = 0; count < airlines; count++)
{
cout << "northeast:";
cin >> sales[count];
cout << "southeast:";
cin >> sales[count];
cout << "northwest:";
cin >> sales[count];
cout << "southwest:";
cin >> sales [count];

}





// Find the highest sales amount.
highest = sales[0];
for (count = 1; count < airlines; count++)
{
if (sales[count] > highest)
highest = sales[count];
}



// Display the results.
cout << fixed << showpoint << setprecision(2);


cout << "The highest sales amount is $" << highest << endl;


return 0;
}
  • 0

Advertisements


#2
gust0208

gust0208

    Member

  • Member
  • PipPipPip
  • 311 posts

for (count = 0; count < airlines; count++)
{
cout << "northeast:";
cin >> sales[count];
cout << "southeast:";
cin >> sales[count];
cout << "northwest:";
cin >> sales[count];
cout << "southwest:";
cin >> sales [count];
}


Hi, the problem seems to lie in your user input loop. With the way your loop is set up, every value you enter is being saved in the same sales array space. Each time through the loop, the loop variable "count" does not change, so each value is over-writing the previous value. You need to increment the "count" variable after each cin statement or add on an appropriate value to shift your location in the sales array. One way to accomplish this is below, but there are many different approaches:

int array_offset = 0;
for (count = 0; count < airlines; count++)
{
cout << "northeast:";
cin >> sales[count + array_offset];
array_offset++;

cout << "southeast:";
cin >> sales[count + array_offset];
array_offset++;

cout << "northwest:";
cin >> sales[count + array_offset];
array_offset++;

cout << "southwest:";
cin >> sales [count + array_offset];
array_offset++;
}

This way your loop preserves the essence of being based on the number of airlines, but will save each inputted value into a unique slot in the sales array.

Hope that helps!

Cheers,
Tom
  • 0

#3
gust0208

gust0208

    Member

  • Member
  • PipPipPip
  • 311 posts

const int airlines = 1 ; //< problem here i dont get
double sales[airlines], // To hold sales amounts


The way above is coded, you will get an error trying to enter values into the sales array since it will only have two open slots. You declare array variables by name followed by the number of slots in []'s. For each airline, you are going to require 4 array slots, so I would use the following declaration for my array:

double sales[airlines * 4];

Thus, you will always have enough array slots to hold the values and is based on the number of airlines. The "airlines" const appears to suggest how the instructor wants you to design your loops and think about your program. At the top level, you are dealing with airlines, and all other variables and loops should be based (reference back) to that top level variable or concept.

Cheers,
Tom
  • 0

#4
ihatetheworld

ihatetheworld

    New Member

  • Topic Starter
  • Member
  • Pip
  • 6 posts
First off thanks for the help.

But its till displaying the wrong airline instead of the last one it is dispalying the first one not the highest income. I am still working on how to display the name of the highest income to. ive been stareing at this for like 4 hours now trying diff things thanks for the help.
  • 0

#5
gust0208

gust0208

    Member

  • Member
  • PipPipPip
  • 311 posts

highest = sales[0];
for (count = 1; count < airlines; count++)
{
if (sales[count] > highest)
highest = sales[count];
}


For your question on why the only the first value is displaying, you need to think through the for loop as you have it set up. Right now, with the const "airlines" set to 1, the for loop will NEVER run. The loop condition is that count < airlines and you have initialized count to 1. So highest gets set to sales[0] and the for loop will never run so you will output your first value every time. You need to change your if statements and the for loop condition. An example is:

highest = 0;
for (count = 0; count < airlines; count++)
{
for (int array_var = 0; array_var < 4; array_var++) {
if (sales[array_var + count*4] > highest)
highest = sales[array_var + count*4]
}
cout << "Highest for airline is " << highest;
highest = 0;
}

This will output the highest input for each airline. If you want to output the highest overall value among all airlines, you can easily change the second for loop.

Cheers,
Tom
  • 0






Similar Topics

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

As Featured On:

Microsoft Yahoo BBC MSN PC Magazine Washington Post HP