Question
#include #include #include #include //for using setw() #include //for exit(0) #include using namespace std; int showMenu() { int choice; cout < < Console Pricing -
#include
#include
#include
#include
#include
#include
using namespace std;
int showMenu()
{
int choice;
cout << "Console Pricing - Main Menu ";
cout << "1. Display all console prices ";
cout << "2. Look up the price of a particular console ";
cout << "3. Sort prices in descending order ";
cout << "4. Display the console with the highest price ";
cout << "5. Exit the program ";
cin >> choice;
return choice;
}
void loadArrays(string console[], float price[], int& n)
{
ifstream file;
file.open("prices.txt");
int i = 0;
while (getline(file, console[i]))
{
string temp;
getline(file, temp);
price[i] = stof(temp);
j++;
}
file.close();
}
void showArrays(string console[], float price[], int n)//display
{
cout << "Console" << setw(34) << "Price ";
for (int i = 0; i < n; ++i)
{
cout << console[i] << setw(40 - console[i].length()) << fixed << setprecision(2) << price[i] << " ";
}
// setw put spaces in between
// setprecision(2) output the float number upto 2 decimal points
cout << endl;
}
void lookUpPrice(string console[], float price[], int n)
{
string str;
cout << "Console name?";
cin >> ws; // Discards the input buffer and intial white spaces of string
getline(cin, str);
int index = -1;
for (int i = 0; i < n; ++i)
{
if (console[i] == str)
{
index = i; break; //if the match is found , it will come out of the loop
}
}
if (index == -1)
cout << "Console not found. ";
else
cout << "The current price for " << str << " is $" << setprecision(2) << price[index] << " ";
}
void sortPrices(string console[], float price[], int n)
{
// Bubble sort //you can also use any other sorting algorithm
for (int i = 0; i < n; ++i)
{
for (int j = 0; j < n - i - 1; ++j)
{
if (price[j] < price[j + 1])
{
swap(price[j + 1], price[j]);
swap(console[j + 1], console[j]);
}
}
}
cout << " ";
}
void highestPrice(string console[], float price[], int n)
{
int mx = -1;
int index;
for (int i = 0; i < n; ++i)
{
if (mx < price[i])
{
mx = price[i]; index = i; // for finding the index highest price
}
}
cout << "The " << console[index] << " has the highest price of $" << setprecision(2) << price[index] << " ";
}
int main()
{
int n = 0;
ifstream fi; //for reading input
fi.open("prices.txt");
string temp;
while (!fi.eof()) {// for getting the number of lines present in the file
getline(fi, temp); ++n;
}
n /= 2; // this will give the size of an array required to hold all the values
string console[n];
float price[n];
loadArrays(console, price, n);
while (1) // this will run until the user enters 5
{
int choice = showMenu();
switch (choice)
{
case 1: showArrays(console, price, n); break;
case 2: lookUpPrice(console, price, n); break;
case 3: sortPrices(console, price, n); break;
case 4: highestPrice(console, price, n); break;
case 5: exit(0); break;
}
}
}
Why isn't this code working? I can't figure it out. It keeps showing a red line under the while (getline(file, console[i])) , price[i] = stof(temp) , getline(cin, str) , getline(fi, temp); ++n . string console[n] , and float price[n] . I included the whole code so you could cut and paste it in the program. I have been using Visual Studio 2019. Here is the instructions for the code.
Fix this program to price video game consoles.Use parallel arrays to input, print, find the most valuable, and sort pricing data.
Console Current Price
Nintendo Entertainment System 57
Sega Genesis 34
Microsoft Xbox 360 58
Sony PlayStation 4 261
Atari 2600 26
Nintendo Game Cube 52
Requirements
1.Read the input file prices.txt into parallel arrays. Fix the program to allow for any number of lines in the input file (up to a certain maximum).The console names have spaces in them, so be sure to use the getline() function.
2.Use a menu-driven program with these choices:
a.Display all console prices
b.Look up the price of a particular console
c.Sort prices in descending order
d.Display the console with the highest price
e.Exit the program
3.Create these functions according to the prototypes:
a.void loadArrays(string[], float[], int &);
b.void showArrays(string[], float[], int);
c.void lookUpPrice(string[], float[], int);
d.void sortPrices(string[], float[], int);
e.void highestPrice(string[], float[], int);
f.int showMenu();// display a menu and return the user's choice
4.Use a loop to allow the user to keep making menu selections until the exit option is chosen.
5.Use the data types which accurately represent the data, and show to two decimal places whenever displaying dollars and cents.
6.Functions must pass parameters and return values as needed, using only local variables.Global variables are not allowed.
7.make any other functions or variables as you see fit.
8.Output must be labeled and easy to read.
9.Use comments to document your program as you see fit.
10.Submit your source code and a screen shot of your program output
Hints for using getline()
The getline() function does two things: reads data from the file (or cin), and returns a Boolean indicating success.When you try to read past the end of file, getline() returns false.Use this as the condition for a while loop.
Mixing the >> operator with getline() is problematic, so use getline() once to read the console name, and again to read the price.Since getline() requires reading into a string, define a temporary string variable for reading the price, then use the stof() function to convert the string to a floating-point type.
For example:
while (getline(file, console[i]))// read in console name
{
string temp;
getline(file, temp);//read price into a temporary string variable
price[i] = stof(temp);//convert string to floating-point
i++;//increment counter
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started