c++ question, i put the txt attachment picture for this question... please include comments on calculations and varibles
#include<iostream>
#include<string>
#include<fstream>
#include<stdlib.h>
#include<iomanip>
using namespace std;
#define MAX_ITEMS 20
struct snackType {
string name;
string code;
double price;
int remaining;
};
string format(string str){
for (int i = 0; i<str.length(); i++){
if (i == 0){
str[i]
= toupper(str[i]);
}
else {
str[i]
= tolower(str[i]);
}
}
return str;
}
int readItems(ifstream &fin, snackType data[]){
int count = 0;
string s1,s2;
int a1;
double pr;
while(!fin.eof()){
fin >> s1 >> s2
>> a1 >> pr;
data[count].name =
format(s1);
data[count].code =
format(s2);
data[count].remaining =
a1;
data[count].price = pr;
count++;
}
return count;
}
void outputItem(string n, string c, double pr){
cout << setw(20) << left <<
n;
cout << setw(20) << left <<
c;
cout << setw(20) << fixed <<
setprecision(2) << pr << endl;
}
void bubblesort(int n, snackType data[]){
string s1,s2;
int a1;
double pr;
for (int i = 0; i<n-1; i++){
for (int j = 0; j<n-1;
j++){
if
(data[i].remaining > data[i+1].remaining){
s1 = data[i].name;
s2 = data[i].code;
a1 = data[i].remaining;
pr = data[i].price;
data[i].name = data[i+1].name;
data[i].code = data[i+1].code;
data[i].remaining = data[i+1].remaining;
data[i].price = data[i+1].price;
data[i+1].name = s1;
data[i+1].code = s2;
data[i+1].remaining = a1;
data[i+1].price = pr;
}
}
}
}
int main(){
snackType snacks[MAX_ITEMS];
cout << "Enter file name :";
string name;
cin >> name;
ifstream fin(name.c_str());
if (!fin){
cout << "Error opening
file\n";
return 0;
}
int n = readItems(fin,snacks);
while(true){
cout << setw(20) << left
<< "Item Name";
cout << setw(20) << left
<< "Item Code";
cout << setw(20) <<
"Price" << endl;
bubblesort(n,snacks);
for (int i = 0; i<n; i++){
if
(snacks[i].remaining > 0)
outputItem(snacks[i].name, snacks[i].code, snacks[i].price);
}
cout << "Enter Item
Code:";
string code;
cin >> code;
code = format(code);
int found = 0;
for (int i = 0; i<n; i++){
if (code ==
snacks[i].code){
if (snacks[i].remaining > 0){
found = 1;
snacks[i].remaining--;
}
}
}
if (found == 1)
cout <<
"Good Purchase\n";
else
cout <<
"item not found\n";
cout <<
"Continue(Y/N):";
string ch;
cin >> ch;
if (ch[0] == 'N')
break;
}
return 0;
}
c++ question, i put the txt attachment picture for this question... please include comments on calculations...
Please use C Programming language (Not C++) 7.6 LAB*: Warm up: Online shopping cart (Part 1) (1) Create three files to submit: • Item ToPurchase.h - Struct definition and related function declarations • Item ToPurchase.c-Related function definitions • main.c-main function Build the ItemToPurchase struct with the following specifications: • Data members (3 pts) • char itemName • int itemPrice • int itemQuantity • Related functions • MakeltemBlank0 (2 pts) Has a pointer to an item To Purchase parameter. Sets item's...
URGENT. Need help editing some C code. I have done most of the code it just needs the following added: takes an input file name from the command line; opens that file if possible; declares a C struct with three variables; these will have data types that correspond to the data types read from the file (i.e. string, int, float); declares an array of C structs (i.e. the same struct type declared in point c); reads a record from the...
C Language program. Please follow the instructions given. Must use functions, pointers, and File I/O. Thank you. Use the given function to create the program shown in the sample run. Your program should find the name of the file that is always given after the word filename (see the command line in the sample run), open the file and print to screen the contents. The type of info held in the file is noted by the word after the filename:...
How would I change the following C code to implement the following functions: CODE: #include <stdio.h> #include <stdlib.h> int main(int argc, char * argv[]) { if (argc != 2) printf("Invalid input!\n"); else { FILE * f = fopen (argv[1], "r"); if (f != NULL) { printf("File opened successfully.\n"); char line[256]; while (fgets(line, sizeof(line), f)) { printf("%s", line); } fclose(f); } else { printf("File cannot be opened!"); } } return 0; } QUESTION: Add a function that uses fscanf like this:...
GIVEN CODE. PLEASE FILL IN THE BLANK. // Include Block #include <fcntl.h> #include <unistd.h> // Preprocessor declarations #define BUF_SIZE 10 /* global constant buffer size: Generally this would be much larger, but I want to show you that it works in a loop until the entire file is read.*/ // main function int main(int argc, char *argv[]) { // Variable declarations int fd; // file descripter char buffer[BUF_SIZE]; // string for...
After pillaging for a few weeks with our new cargo bay upgrade, we decide to branch out into a new sector of space to explore and hopefully find new targets. We travel to the next star system over, another low-security sector. After exploring the new star system for a few hours, we are hailed by a strange vessel. He sends us a message stating that he is a traveling merchant looking to purchase goods, and asks us if we would...
*Urgent* I've tried getting help with this a couple times, but nobody has answered my questions, and it's been several days. #################################################################################### This needs to be Python that works in a Zybooks programming window. 1. Build an ItemToPurchase class with the following specifications: Attributes (3 pts) item_name (string) item_price (float) item_quantity (int) Default constructor (1 pt) Initializes item's name = "none", item's price = 0, item's quantity = 0 Method print_item_cost() Example of print_item_cost() output: Bottled Water 10 @ $1...
For my assignment I have to write a program that creates a restaurant billing system. I think that I got it correct but it says that it is incorrect, so I was wondering if someone would be able to look over my code. It says that I need tax of $0.20, and the amount due to be $4.10, which is what I have in my output.https://imgur.com/a/jgglmvWMy issue is that I have the correct outcome when I run my program but...
JAVA Write an application to test the HuffmanTree class. Your application will need to read a text file and build a frequency table for the characters occurring in that file. Once that table is built create a Huffman code tree and then a string consisting of 0 and 1 characters that represents the code string for that file. Read that string back in and re-create the contents of the original file. Prompt the user for file name. Read the file,...
The following is a sample inventory in C++, i want to ask the user to input a item number for removing from inventory. //CPP #include <iostream> #include <fstream> #include <cstdlib> #include <iomanip> #define MAX 1000 using namespace std; //Function to Add a new inventory item to the data into the array in memory void addItem(string desc[],string idNum[], float prices[], int qty[],int &num) { cout<<"Enter the names:"; cin>>desc[num]; cout<<"Enter the item number:"; cin>>idNum[num]; cout<<"Enter the price of item:"; cin>>prices[num]; cout<<"Enter the...