im writing a c++ code and getting stuck on two parts. The first part, when I go to write the customer order out to the file, it writes everything but the price out right. I'll get a weird number like 5.95828e-039 or nan. When I printout to the console it works fine so not sure what's wrong. Second After I write the order out to the file, I then have to go in and read the file and calculate the sum of all the totals. Where I'm also stuck at. Here what I have so far.
enum strength {light=1, medium=2, dark=3};//enum function for
strength
enum coffeeName {Columbian=1, FrenchRoast=2, ItalianBlend=3,
House=4};
struct cafe
{
int size;
int coffeeName;
int strength;
int sugar;
float price;
bool creamer = true;
bool blend = true;
};
struct cust
{
string fname;
cafe rdata;
int order;
};
void calcPrice(cust c)
{
float price = 0;
if(c.rdata.size == 0)
price = 1.50;
else if (c.rdata.size == 1)
price = 2.10;
else if(c.rdata.size == 2)
price = 2.50;
else if(c.rdata.size == 3)
price = 3.00;
else if (c.rdata.size == 4)
price= 4.25;
if(c.rdata.blend != false)
price = price + 0.25;
if(c.rdata.creamer != false)
price = price + 0.08;
if(c.rdata.sugar == 1)
price = price + 0.13;
else if(c.rdata.sugar == 2)
price = price +(2*0.13);
else if(c.rdata.sugar == 3)
price = price + (3*0.13);
cout<<"Cost = $"<<price<<endl;
}
int main()
{
cust c;
struct cust arr[10]; //order number
char ch1,ch2,input; //char input;
int i,option, answer;
int flag = 0;
do //outer do-while
{
do //inner do-while loop
{
cout<<"Main Menu.\n"<<endl;
cout<<"Order a coffee?(Y/N)\n "<<endl;
cin>>input;
cin.ignore();
if((input == 'y')||(input == 'Y'))
{
cout<<"Can I have a name for the order?"<<endl;
getline(cin,c.fname); //inputting a name from user
cout<<"What coffee would you like?"<<"\nColumbian=1,
FrenchRoast=2, ItalianBlend=3, House=4"<<endl;
cin>>c.rdata.coffeeName;
cout<<"How strong?
(Light=1,Medium=2,Dark=3)"<<endl;
cin>>c.rdata.strength;
cout<<"Size of
Coffee?(S=0,R=1,M=2,L=3,XL=4)"<<endl;
cin>>c.rdata.size;
cout<<"How much sugar?"<<endl;
cin>>c.rdata.sugar;
cout<<"Would you like French roast or Italian
blend(Y/N)"<<endl;
cin>>input;
if(ch1=='Y')
{
c.rdata.blend=true;
}
cout<<"Do you want creamer?(Y/N)"<<endl;
cin>>input;
if(ch2=='Y')
{
c.rdata.creamer=true;
}
cout<<"Here is your order number #:"<<(arr[10].order +
1)<<endl;
//writing data to file
fstream order("Order.dat", ios::out|ios::app);
order<<"\nOrderNumber:"<<
c.order<<"\nCoffeeName:"<<c.rdata.coffeeName<<"\nStrength"<<c.rdata.strength<<
"\nSize: "<<c.rdata.size<<"\nSugarAmount:
"<<c.rdata.sugar<<endl;
order<<"Cream: "<<c.rdata.creamer<<"\nCost:
"<<c.rdata.price<<endl;
order.close();
cout<<"Back to Main menu?"<<endl;
cin>>input;
}
}while(input == 'Y'||input == 'y');
//if user selects No in Main Menu
if(input == 'N'||input == 'n')
{
cout<<"[1]View an order\n"
<<"[2] View Total Profit\n";
cin>>answer;
switch(answer)
{
case 1:
cout<<"Enter a order number";
cin>>option;
for(i=0;i<10;i++){
//display option
if(arr[10].order != option)
{
flag = 1;
cout<<"Here is your order:"<<endl;
cout<<"Name:"<<c.fname<<endl<<"CoffeeName:"<<c.rdata.coffeeName<<"\nRoast:"<<c.rdata.strength<<endl;
cout<<"Size:"<<c.rdata.size<<endl;
cout<<"Sugar:"<<c.rdata.sugar<<endl;
std::cout <<"Wanted French or Italian blend:"<<
std::boolalpha << c.rdata.blend << '\n'; //so it prints
out true or false
std::cout <<"Wanted Creamer:"<< std::boolalpha <<
c.rdata.creamer << '\n';
calcPrice(c); //function to total price
break;
}
/*if(!flag)
{
cout<<"Not an order number"<<endl;
exit(1);
}*/
}
case 2:
/*order.open("order.dat", ios::in);
if(order)
{
//Read an item from the file
getline(order, );
while(order)
{
cout<<price(c)<<endl;
}
}*/
cout<<"Testing total view profit";
break;
}//end of switch
//prompt to reorder
cout<<"\n Want to reorder?"<<endl;
cin>>input;
if((input == 'N')||(input == 'n'))
{
cout<<"Closing program"<<endl;
exit(2);
}
}//end of if statement for no
}//outer do-while loop
while((input=='y')||(input ='Y'));
return 0;
}//end of main
// do comment if any problem arises
// code
#include <iostream>
#include <fstream>
using namespace std;
enum strength
{
light = 1,
medium = 2,
dark = 3
}; //enum function for strength
enum coffeeName
{
Columbian = 1,
FrenchRoast = 2,
ItalianBlend = 3,
House = 4
};
struct cafe
{
int size;
int coffeeName;
int strength;
int sugar;
float price;
bool creamer = true;
bool blend = true;
};
struct cust
{
string fname;
cafe rdata;
int order;
};
void calcPrice(cust &c)
{
float price = 0;
if (c.rdata.size == 0)
price = 1.50;
else if (c.rdata.size == 1)
price = 2.10;
else if (c.rdata.size == 2)
price = 2.50;
else if (c.rdata.size == 3)
price = 3.00;
else if (c.rdata.size == 4)
price = 4.25;
if (c.rdata.blend != false)
price = price + 0.25;
if (c.rdata.creamer != false)
price = price + 0.08;
if (c.rdata.sugar == 1)
price = price + 0.13;
else if (c.rdata.sugar == 2)
price = price + (2 * 0.13);
else if (c.rdata.sugar == 3)
price = price + (3 * 0.13);
c.rdata.price = price;
}
int main()
{
cust c;
struct cust arr[10]; //order number
char ch1, ch2, input; //char input;
int i, option, answer;
int flag = 0;
do //outer do-while
{
do //inner do-while loop
{
cout << "Main Menu.\n"
<< endl;
cout << "Order a coffee?(Y/N)\n " << endl;
cin >> input;
cin.ignore();
if ((input == 'y') || (input == 'Y'))
{
cout << "Can I have a name for the order?" << endl;
getline(cin, c.fname); //inputting a name from user
cout << "What coffee would you like?"
<< "\nColumbian=1, FrenchRoast=2, ItalianBlend=3, House=4" << endl;
cin >> c.rdata.coffeeName;
cout << "How strong? (Light=1,Medium=2,Dark=3)" << endl;
cin >> c.rdata.strength;
cout << "Size of Coffee?(S=0,R=1,M=2,L=3,XL=4)" << endl;
cin >> c.rdata.size;
cout << "How much sugar?" << endl;
cout << "Would you like French roast or Italian blend(Y/N)" << endl;
cin >> input;
if (ch1 == 'Y')
{
c.rdata.blend = true;
}
cout << "Do you want creamer?(Y/N)" << endl;
cin >> input;
if (ch2 == 'Y')
{
c.rdata.creamer = true;
}
cout << "Here is your order number #:" << (arr[10].order + 1) << endl;
//writing data to file
fstream order("Order.dat", ios::out | ios::app);
order << "\nOrderNumber:" << c.order << "\nCoffeeName:" << c.rdata.coffeeName << "\nStrength" << c.rdata.strength << "\nSize: " << c.rdata.size << "\nSugarAmount: " << c.rdata.sugar << endl;
calcPrice(c);
order << "Cream: " << c.rdata.creamer << "\nCost: " << c.rdata.price << endl;
order.close();
cout << "Back to Main menu?" << endl;
cin >> input;
}
} while (input == 'Y' || input == 'y');
//if user selects No in Main Menu
if (input == 'N' || input == 'n')
{
cout << "[1]View an order\n"
<< "[2] View Total Profit\n";
cin >> answer;
switch (answer)
{
case 1:
cout << "Enter a order number";
cin >> option;
for (i = 0; i < 10; i++)
{
//display option
if (arr[10].order != option)
{
flag = 1;
cout << "Here is your order:" << endl;
cout << "Name:" << c.fname << endl
<< "CoffeeName:" << c.rdata.coffeeName << "\nRoast:" << c.rdata.strength << endl;
cout << "Size:" << c.rdata.size << endl;
cout << "Sugar:" << c.rdata.sugar << endl;
std::cout << "Wanted French or Italian blend:" << std::boolalpha << c.rdata.blend << '\n'; //so it prints out true or false
std::cout << "Wanted Creamer:" << std::boolalpha << c.rdata.creamer << '\n';
cout << "Cost = $" << c.rdata.price << endl;
break;
}
}
case 2:
double total_price = 0;
cout << "Testing total view profit";
ifstream order("order.dat", ios::in);
string temp;
double price;
if (order)
{
while (order)
{
// 7 lines which are not required
for (int i = 0; i < 7; i++)
getline(order, temp);
order >> temp >> price;
// add price to total price
total_price += price;
}
}
order.close();
// print total price
cout << "\nTotal profit: " << total_price << endl;
break;
} //end of switch
//prompt to reorder
cout << "\n Want to reorder?" << endl;
cin >> input;
if ((input == 'N') || (input == 'n'))
{
cout << "Closing program" << endl;
exit(2);
}
} //end of if statement for no
} //outer do-while loop
while ((input == 'y') || (input = 'Y'));
return 0;
} //end of main
Order.dat:

Output:

im writing a c++ code and getting stuck on two parts. The first part, when I...
I need help in my C++ code regarding outputting the enums in string chars. I created a switch statement for the enums in order to output words instead of their respective enumerated values, but an error came up regarding a "cout" operator on my "Type of Item" line in my ranged-based for loop near the bottom of the code. I tried casting "i.type" to both "i.GroceryItem::Section::type" and "i.Section::type", but neither worked. Simply put, if a user inputs 1 as their...
C++ getline errors I am getting getline is undefined error messages. I would like the variables to remain as strings. Below is my code. #include <iostream> #include<string.h> using namespace std; int index = 0; // variable to hold how many customers are entered struct Address //Structure for the address. { int street; int city; int state; int zipcode; }; // Customer structure struct Customer { string firstNm, lastNm; Address busAddr, homeAddr; }; // Functions int displayMenu(); Customer getCustomer(); void showCustomer(Customer);...
can you please split this program into .h and .cpp file #include <iostream> #include<string> #include<fstream> #define SIZE 100 using namespace std; //declare struct struct word_block { std::string word; int count; }; int getIndex(word_block arr[], int n, string s); int main(int argc, char **argv) { string filename="input.txt"; //declare array of struct word_block word_block arr[SIZE]; int count = 0; if (argc < 2) { cout << "Usage: " << argv[0] << "...
Im writing a c++ program to translate a sentence to pig latin. But I keep getting an error where it only prints out the "ay" part. Not sure where I'm wrong here. string piglatin(string); string substr(string, int&); int main () //begin main { string input; cout<<"Enter a sentence to convert: "; getline(cin, input); cout<<"Converted to Pig Latin: "<<piglatin(input)<<endl<<endl; //cout << "Search for another value? (Y/N)"; //to convert again system("pause"); return 0; } string piglatin(string input) //piglatin function { int len,...
Having code issues wth my C++ program. My program checks if two binary trees are similar and if they're not they return false. My program is return true with different binary trees. Could use some help thanks #include <iostream> #include <string> using namespace std; //Struct of Nodes struct BinarySearchTree { int data; BinarySearchTree *left; BinarySearchTree *right; }; // Inserting nodes into BST BinarySearchTree* insert( BinarySearchTree* node, int val) { if (node == NULL) { BinarySearchTree *newNode = new BinarySearchTree(); newNode->data...
C++ language I need to update this code with the following: ask the user for how many structures they would like. Once you get the number, allocate an array of pointers. Once they have been created, proceed to loop through and get the data as usual, and display it back to the screen. struct record { int age; string name; }; int check(record r[], int n, string nm) { for (int i = 0; i < n;...
Hello, I have some errors in my C++ code when I try to debug it.
I tried to follow the requirements stated below:
Code:
// Linked.h
#ifndef INTLINKEDQUEUE
#define INTLINKEDQUEUE
#include <iostream>
usingnamespace std;
class IntLinkedQueue
{
private: struct Node {
int data;
Node *next;
};
Node *front; // -> first item
Node *rear; // -> last item
Node *p; // traversal position
Node *pp ; // previous position
int size; // number of elements in the queue
public:
IntLinkedQueue();...
Write a C++ program that uses a structure to store the following inventory information in a file: ⦁ Item description, Quantity on hand, Wholesale cost, Retail cost, and Date added to inventory. ⦁ Use a char array for item description and date. ⦁ The program should have a menu that allows the user to perform the following tasks: i. Add a new record at the end of the file. ii. Display any record in the file. iii. Change any record...
#include <iostream> using namespace std; bool binarySearch(int arr[], int start, int end, int target){ //your code here } void fill(int arr[], int count){ for(int i = 0; i < count; i++){ cout << "Enter number: "; cin >> arr[i]; } } void display(int arr[], int count){ for(int i = 0; i < count; i++){ cout << arr[i] << endl; } } int main() { cout << "How many items: "; int count; cin >> count; int * arr = new...
C++: Need help debugging my code
I am writing this and using some other examples as reference code
while rewriting it with my own understanding of the material but am
having trouble making it finally compile. Below is a picture of the
error messages.
//main.cpp
//Semester Project
//Created by J---on 5/6/2019
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <bits/stdc++.h>
using namespace std;
void instructions(); //displays program details and
instructions
void openFile();
void takeInput(int*);
void switchBoard(int*);
struct price
{...