I'm printing a statement from classes using getter functions. The top 1.5 lines of my cout statement are not printing. I have tried flushing the stream, and I also copied and pasted the lines that are not printing outside the if statement, and it prints! I can't figure out what is going on. Here is the function:
// display all books out on loan
void displayBorrowed(vector<LibraryBook>& book)
{
cout << "Books currently checked out: " << endl << endl;
for(unsigned int i = 0; i < book.size(); i++)
{
//cout << "ID: " << book[i].getId_() << " Title: "
//<< book[i].getTitle_() << endl << endl;
if(book[i].getIsLoaned_() == true)
{
std::cout.flush();
cout << "ID: " << book[i].getId_() << " Title: "
<< book[i].getTitle_() << " Author: "
<< book[i].getAuthorFirst_() << " " << book[i].getAuthorLast_()
<< " Year Published: " << book[i].getYearPubl_() << endl
<< "Due Date: " << book[i].getDueMonth_() << "/"
<< book[i].getDueDay_() << "/" << book[i].getDueYear_()
<< " Date Borrowed: " << book[i].getBorrwdMonth_() << "/"
<< book[i].getBorrwdDay_() << "/" << book[i].getBorrwdYear_()
<< endl << "Checked out by: " << book[i].getBorrwFirst_()
<< " " << book[i].getBorrwLast_() << endl << endl;
}
}
}
It displays this:
Books currently checked out:
Author: Brendan Behan Year Published: 1958
Due Date: 8/2/2017 Date Borrowed: 7/21/2017
Checked out by: Cassie Peterson
If the lines in the if statement are copied out of the if statement it displays normally:
ID: 78620 Title: Zhuan Falun
I tried changing the if statement to false to display all the books not loaned, and they all displayed the same except for the very last book (number 50 finally displayed the id # and the title. I am at a complete loss. What is going on?
It should look like this:
ID: 78620 Title: Zhuan Falun Author: Brendan Behan Year Published: 1958
Due Date: 8/2/2017 Date Borrowed: 7/21/2017
Checked out by: Cassie Peterson
(havent formatted display yet)
I just changed it to this where I have every single element that doesn't display in its own cout statement, and NONE of it displays!! What??! (up until author, where it started displaying before, I mean.)
for(unsigned int i = 0; i < book.size(); i++)
{
if(book[i].getIsLoaned_() == true)
{
std::cout.flush();
cout << "ID: " ;
cout << book[i].getId_();
cout << " Title: ";
cout << book[i].getTitle_();
cout << " Author: "
<< book[i].getAuthorFirst_() << " " << book[i].getAuthorLast_()
<< " Year Published: " << book[i].getYearPubl_() << endl
<< "Due Date: " << book[i].getDueMonth_() << "/"
<< book[i].getDueDay_() << "/" << book[i].getDueYear_()
<< " Date Borrowed: " << book[i].getBorrwdMonth_() << "/"
<< book[i].getBorrwdDay_() << "/" << book[i].getBorrwdYear_()
<< endl << "Checked out by: " << book[i].getBorrwFirst_()
<< " " << book[i].getBorrwLast_() << endl << endl;
}
It prints when I put an endl at the end of each element:
if(book[i].getIsLoaned_() == true)
{
std::cout.flush();
cout << "ID: " << endl;
cout << book[i].getId_() << endl;
cout << " Title: " << endl;
cout << book[i].getTitle_() << endl;
cout << " Author: " << endl;
cout << book[i].getAuthorFirst_() << " " << book[i].getAuthorLast_() << endl;
cout << " Year Published: " << book[i].getYearPubl_() << endl;
cout << "Due Date: " << book[i].getDueMonth_() << "/" << endl;
cout << book[i].getDueDay_() << "/" << book[i].getDueYear_() << endl;
cout << " Date Borrowed: " << book[i].getBorrwdMonth_() << "/" << endl;
cout << book[i].getBorrwdDay_() << "/" << book[i].getBorrwdYear_() << endl;
cout << endl << "Checked out by: " << book[i].getBorrwFirst_() << endl;
cout << " " << book[i].getBorrwLast_() << endl << endl;
}
Books currently checked out:
ID:
47492
Title:
Borstal Boy
Author:
Brendan Behan
Year Published: 1958
Due Date: 8/
2/2017
Date Borrowed: 7/
21/2017
Checked out by: Cassie
Peterson
My suggestion:
Print each of the members in their own line.
Find out which member is problematic.
Dig deeper into the contents of the member to understand how it got there and fix it.
if(book[i].getIsLoaned_() == true)
{
std::cout.flush();
std::cout << "ID: " << book[i].getId_() << std::endl
<< "Title: " << book[i].getTitle_() << std::endl
<< "Author First: " << book[i].getAuthorFirst_() << std::endl
<< "Author Last:" << book[i].getAuthorLast_() << std::endl
<< "Year Published: " << book[i].getYearPubl_() << std::endl
<< "Due Date Month: " << book[i].getDueMonth_() << std::endl
<< "Due Date Day: " << book[i].getDueDay_() << std::endl
<< "Due Date Year: " << book[i].getDueYear_() << std::endl
<< "Borrowed Month: " << book[i].getBorrwdMonth_() << std::endl
<< "Borrowed Day: " << book[i].getBorrwdDay_() << std::endl
<< "Borrowed Year: " book[i].getBorrwdYear_() << std::endl
<< "Checked out by first: " << book[i].getBorrwFirst_() << std::endl
<< "Checked out by last: " << book[i].getBorrwLast_() << std::endl
<< std::endl;
}
}
/* Sometimes "half cout statement not printing problem" may occur due to use of dangling or dangerous pointers. In the code below all cout statements that present, after dangling pointer printing statement are not printing anything.
If we remove the dangling pointer's cout statement then everything will be fine.
code:
// dangling pointer is dangerous because it can hold undesired address. */
#include<bits/stdc++.h>
using namespace std;
int main()
{
int a =5;
char b= 'a';
int *p = &a;
char *p1 =&b;
int *p3; // p3 is dangling pointer
cout<<"size of p "<<sizeof(p)<<endl; // o/p is 8, bcoz x64 bit compiler
gives 8byte and x32 gives 4 byte.
cout<<"size of p1 "<<sizeof(p1)<<endl;
cout<<"size of p3 "<<sizeof(p3)<<endl;
cout<<"address of p3 is "<<&p3<<endl;
cout<< "value at p3 is "<<*p3<<endl; //***dangling or dangerous pointer.***
cout<<"hello my name is rahul ";
cout<<a<<endl;
cout<<&a<<endl;
//cout<<*a<<endl; // shows error (invalid type argument)
cout<<p<<endl; // shows base address
cout<<&p<<endl; // shows pointers address
cout<<*p<<endl;
cout<<"this is an end"<<endl;
return 0;
}
Related
I'm currently building a function that has to do with a pharmacist's input and I was wondering if this was the proper way to construct it using else/if statements. Am I lacking in some parts? (I've been getting this one single "expected declaration of the '}'" error that's been driving me crazy and have been trying to fix it for the past hour or so but to no avail.)
I would love to hear your advice on the parts that I might be missing out.
I've also listed the location of the error in the code below.
Thank you so much!
void pharm_page()
{
int choice3;
char pharm_user[30];
char pharm_pass[30];
char ship, ship_choice;
bool value_found = true;
system ("pause");
cout << "\n --------Pharmacist Login-------";
cout << "\n\n Please enter your username: ";
cin >> pharm_user;
cout << "\n Please enter your password:";
cin >> pharm_pass;
if ((strcmp (pharm_user, USER1) == 0) &&
(strcmp (pharm_pass, USER1PASSWORD) == 0))
{
getchar();
system("pause");
cout << "\n";
cout << "Success! You will be redirected to the pharmacist page." << endl;
cout << "\n";
cout << "------------------ Pharmacist Page -----------------\n" << endl
<< "Press 1: Access Patient Records" << endl;
cin >> choice3;
if (choice3 == 1)
{
const int ship_WM = 8.00;
const int ship_EM = 12.00;
int patient_id;
bool value_found = true;
cout << "-------------- Enter patient ID : ";
cin >> patient_id;
cin.ignore();
for (int i = 0; i < SIZE_P; i++)
{
if (patient_id == pat[i].p_id)
{
cout << "Patient Name : " << pat[i].p_name << endl
<< "Patient ID : " << pat[i].p_id << endl
<< "Doctor Name : " << pat[i].p_doctor << endl
<< "Appointment Date : " << pat[i].p_date << endl
<< "Medicine Expiry Date : " << pat[i].expiry << endl
<< "Medicine Name : " << pat[i].med_name << endl
<< "Medicine Quantity : " << pat[i].med_q << endl
<< "Direction of Use : " << pat[i].direct << endl
<< "Patient's Address: " << pat[i].p_add << endl
<< "Continue prescription? " << pat[i].option << endl
<< "\n" << endl;
cout << "\t\t========================================================================================\n\n";
cout << "\t\tPlease take note that fridge items e.g. insulin, medication in liquid form e.g. syrup or \n";
cout << "\t\tlotion and pressurized canisters e.g. inhalers are not eligible for postage. Kindly \n";
cout << "\t\t\t\t\tsource at the nearest hospital/pharmacy.\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\n";
cout << "\t\t========================================================================================\n\n";
cout << "--------- State the method of pick-up for patient's medicine (Press A for 'Shipping' or B for 'Pick-up'): ";
cin >> ship;
if (toupper(ship) == 'A')
{
cout << "You have chosen to ship the medicine. Please enter the shipping zone of the patient to determine shipping charges. (EG: Press C for 'WM' or D for 'EM'): " << endl;
cin >> ship_choice;
if (toupper(ship_choice) == 'C')
{
for (int i = 0; i < 1; i++)
{
ofstream file_label("patient.txt");
if(file_label.is_open())
{
file_label << "Patient Name : " << pat[i].p_name << endl;
file_label << "Patient ID : " << pat[i].p_id << endl;
file_label << "Doctor Name : " << pat[i].p_doctor << endl;
file_label << "Appointment Date : " << pat[i].p_date << endl;
file_label << "Medicine Expiry Date : " << pat[i].expiry << endl;
file_label << "Medicine Name : " << pat[i].med_name << endl;
file_label << "Medicine Quantity : " << pat[i].med_q << endl;
file_label << "Direction of Use : " << pat[i].direct << endl;
file_label << "The medicine will be shipped to the patient's address at " << pat[i].p_add << "." << endl;
file_label << "Shipping charges: RM" << ship_WM << endl;
file_label.close();
cout << "\n" << endl;
break;
}
}
}
else if (toupper(ship_choice) == 'D')
{
for (int i = 0; i < 1; i++)
{
ofstream file_label("patient.txt");
if(file_label.is_open())
{
file_label << "Patient Name : " << pat[i].p_name << endl;
file_label << "Patient ID : " << pat[i].p_id << endl;
file_label << "Doctor Name : " << pat[i].p_doctor << endl;
file_label << "Appointment Date : " << pat[i].p_date << endl;
file_label << "Medicine Expiry Date : " << pat[i].expiry << endl;
file_label << "Medicine Name : " << pat[i].med_name << endl;
file_label << "Medicine Quantity : " << pat[i].med_q << endl;
file_label << "Direction of Use : " << pat[i].direct << endl;
file_label << "The medicine will be shipped to the patient's address at " << pat[i].p_add << "." << endl;
file_label << "Shipping charges: " << ship_EM << endl;
file_label.close();
cout << "\n" << endl;
break;
}
}
}
else
cout << "Unable to open file. Please try again once the program restarts.";
}
}
else if (toupper(ship) == 'B')
{
for (int i = 0; i < 1; i++)
{
ofstream file_label("patient.txt");
if(file_label.is_open())
{
file_label << "Patient Name : " << pat[i].p_name << endl;
file_label << "Patient ID : " << pat[i].p_id << endl;
file_label << "Doctor Name : " << pat[i].p_doctor << endl;
file_label << "Appointment Date : " << pat[i].p_date << endl;
file_label << "Medicine Expiry Date : " << pat[i].expiry << endl;
file_label << "Medicine Name : " << pat[i].med_name << endl;
file_label << "Medicine Quantity : " << pat[i].med_q << endl;
file_label << "Direction of Use : " << pat[i].direct << endl;
file_label << "The medicine will be shipped to the patient's address at " << pat[i].p_add << "." << endl;
file_label << "Shipping charges: None " << endl;
file_label.close();
cout << "\n" << endl;
break;
}
}
}
}
}
else
value_found = false;
}
if(value_found)
cout << "No record of patient in database. Please re-enter current patient's number and try again. ";
else
cout << "Wrong input. Please try again." << endl;
}
} <============================================================== (that one error is located here)
else
{
cout << "\n";
cout << "Your credentials are wrong. Please enter the right credentials once the program has been terminated." << endl;
}
}
}
A good base line is that if you have a function that is long enough for you not to see it in one page, you should generate sub-functions.
Another good base line is that if you can easily name a sub-segment of your code, you should consider making it another function.
If you see that you repeat nearly similar code several times, like your writing lines, it's time to refactor. Avoid repeating the same code all over.
Most important: C++ is an OOP language. Use it as one.
For example, take your lines
cout << "Patient Name : " << pat[i].p_name << endl
<< "Patient ID : " << pat[i].p_id << endl
<< "Doctor Name : " << pat[i].p_doctor << endl
<< "Appointment Date : " << pat[i].p_date << endl
<< "Medicine Expiry Date : " << pat[i].expiry << endl
<< "Medicine Name : " << pat[i].med_name << endl
<< "Medicine Quantity : " << pat[i].med_q << endl
<< "Direction of Use : " << pat[i].direct << endl
<< "Patient's Address: " << pat[i].p_add << endl
<< "Continue prescription? " << pat[i].option << endl
<< "\n" << endl;
I'm not sure what pat stores, but given that it has members, it's at least a structure. Now why does that structure or class not have a method like print() or to_string()? Which would reduce your lines above to
cout << path[i].to_string() << endl;
Do similar things in other parts where you print a lot of things and your code should be far better to read already. Note that a to_string() method suits well as that would also work with your ofstream.
Question, is that what you show us a function or a method? It should be the latter. You should have something like a class PharmacyInterface which has such functionality as methods. Basically every single access you can name should be a method. Something like accessing the data of a single patient should be a method. Entering the password (and storing a boolean that it was successfully entered) should be a method. All those methods should read very shortly and straight-forwarded.
Basically, for anything that you nest, ask yourself if you can easily name it. If so, make it it's own method.
As for your error, use a proper IDE. If you use auto-indentation, the line with the missing { or } will stick out, and maybe even be marked as bad. Also, you can check to which opening brace a closing brace belongs by clicking behind it, a good IDE will then indicate the opening brace.
But in general, if you have troubles finding an incorrect bracing, your code is simply too convoluted, and you should use what I wrote above.
Lastly, I strongly recommend that you put your code on CodeReview, another StackExchange website which concerns itself with coding style rather than fixing bugs like StackOverflow does.
Note that the code you post there will be ripped apart, and that is quite useful for you to learn things.
Edit: Another thing I noticed is that you have a for loop that searches for an ID. This creates an avoidable nesting. Instead, have another method (or function if you want to go procedural) called size_t patient_index_from_id(int id) or similar, which does the search. Saves you from one nesting and can be reused.
I cannot seem to get the "full"
to display the concatenation of First and last.
It compiles but when I run it it will appear blank.
Can you tell me why?
Have tried to figure it our for hours.
Here are my declerations
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
#define tax 0.30
#define parking_deductions 10.00
#define overtime_hours 40
#define max_hours 60
#define max_pay 99
string namestring( string first, string last, string full);
I'm attempting to pass this module to my main
string namestring(string first, string last, string full)
{
//input name
cout << "What is your first name? " << endl;
cout << "first name: " << endl;
cin >> first;
cout << "What is your last name? " << endl;
cout << "last name: " << endl;
cin >> last;
//process name
full = last + " " + first;
return full;
}
By calling it like so
namestring(first, last, full );
Where I expect the full name input by the user to be displayed below
cout << left << fixed << " " << "Reg." << " " << " Ovt." << " Hourly" << " Net" << " " << " Gross" << endl;
cout << left << fixed << setprecision(2) << setw(10) << "Name " << " Hours" << " Hours" << " Rate" << " Pay" << " Taxes" << " Deduct" << " Pay" << endl;
cout << left << fixed << setprecision(2) << setw(10) << "====================" << " " << "=====" << " " << "=====" << " " << "=====" << " " << "======" << " " << "======" << " " << " " << "========" << " " << "=======" << endl;
cout << left << setprecision(2) << setw(20) << full << right << " " << right << setw(4) << hours << right << " " << right << overtime << " " << right << pay << " " << right << net_pay << " " << right << taxs << " " << right << parking_deductions << " " << right << gross_pay << right << endl;
cout << endl;
cout << endl;
I'm assuming the goal here is to get the following 3 strings,
First Name
Last Name
Full Name
To do this, you would need to pass the arguments by reference, not by value:
void namestring(string& first, string& last, string& full)
{
//input name
cout << "What is your first name? " << endl;
cout << "first name: " << endl;
cin >> first;
cout << "What is your last name? " << endl;
cout << "last name: " << endl;
cin >> last;
//process name
full = last + " " + first;;
}
There is also no need to return a string if you pass the "full" string by reference, since the function will fill it for you.
string namestring(string first, string last, string& full)
//input name
{
cout << "What is your first name? " << endl;
cout << "first name: " << endl;
cin >> first;
cout << "What is your last name? " << endl;
cout << "last name: " << endl;
cin >> last;
//process name
full = last + " " + first;
return full;
}
You are passing the full by value. So it's the local copy of the function which is modified. You need it to pass by reference.
If you also want the first and last value you also need to pass it by reference.
Well basically the condition for healthtwo causes the program to stop but not healthone for some reason
complete code: http://en.textsave.org/CmN
if (chance<=rando) {
cout << " " << endl;
cout << "Hit! Dealing " << attackp << " Damage!" << endl;
healthtwo=healthtwo-attackp;
}
else {
cout << " " << endl;
cout << "Miss!" << endl;
}
chance=1+rand()%23;
if (chance<=rando) {
cout << "Comp Used " << comattackname << "!" << " Hit!" << " Dealing " << attackcp << " Damage" << endl;
cout << " " << endl;
healthone=healthone-attackcp;
}
else {
cout << "Comp Used " << comattackname << "!" << " Miss!" << endl;
cout << " " << endl;
}
} while (healthone>=0 || healthtwo>=0);
That should be a logical and (&&). After all, you want to check whether the health of both contestants is greater or equal to zero.
The user inputs a double and string which gets stored into two arrays. Like so :
{
double DoC;
string Nm;
cout << " Please enter the amount charged to your credit card " << endl;
cin >> DoC;
cout << " Please enter where the charge was made " << endl;
cin >> Nm;
getline(cin, Nm);
cca.doCharge(Nm,DoC);
break;
}
Then the double and string are passed to this :
{
if (amount > 0)
{
for (int i = 9; i != 0; i--)
{
last10withdraws[i] = last10withdraws[i-1];
last10charges[i] = last10charges[i-1];
}
last10withdraws[0] = amount;
last10charges[0] = name;
setBalanceW(amount);
}
else
{
cout << " ERROR. Number must be greater then zero. " << endl;
}
return 0;
}
This seems to work very well for storing the data into the arrays. However I then use this function to display the data inside of the arrays :
{
cout << " Account: Creditcard Withdraws " << " " << " Account: Creditcard Deposits " << " " << " Account: Creditcard last 10 charges " << endl;
cout << " " << last10withdraws[0] << " " << last10deposits[0] << " " << last10charges[0] << endl;
cout << " " << last10withdraws[1] << " " << last10deposits[1] << " " << last10charges[1] << endl;
cout << " " << last10withdraws[2] << " " << last10deposits[2] << " " << last10charges[2] << endl;
cout << " " << last10withdraws[3] << " " << last10deposits[3] << " " << last10charges[3] << endl;
cout << " " << last10withdraws[4] << " " << last10deposits[4] << " " << last10charges[4] << endl;
cout << " " << last10withdraws[5] << " " << last10deposits[5] << " " << last10charges[5] << endl;
cout << " " << last10withdraws[6] << " " << last10deposits[6] << " " << last10charges[6] << endl;
cout << " " << last10withdraws[7] << " " << last10deposits[7] << " " << last10charges[7] << endl;
cout << " " << last10withdraws[8] << " " << last10deposits[8] << " " << last10charges[8] << endl;
cout << " " << last10withdraws[9] << " " << last10deposits[9] << " " << last10charges[9] << endl;
cout << endl;
}
Lets say the user has inputted three doubles into the deposit array. When I call the function to display it I get something like this :
60
30
20
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
How can I make it so that the -9.25596e+061's are 0's? I have not been able to find anything really helping me. Also with the array that contains strings, when it is called to display it displays nothing. Why is this ?
Initialize the array as:
int last10withdraws[10] = {0};
Now all elements are zero.
If the user enters three numbers, then first three elements will be non-zero (assuming only non-zero is allowed), and the rest will be zero.
If last10withdraws is a member of a class m(and you're using C++03), then you can use member-initializer list to default-initialize which will initialize all the elements to zero.
class myclass
{
int last10withdraws[10];
public:
myclass() : last10withdraws()
{ //^^^^^^^^^^^^^^^^^^^ member initializer list
}
};
Hope that helps.
You are getting the error because you are not initializing the values. Start by saying int last10withdraws[10] = {0}; That will initialize all values to zero.
My professor instructed us to make a Starbucks like menu where the user can continue to input orders until they are finished. I got the menu display down along with the loop, but I can't get it to add up the orders that were inputted and display a total.
#include <iostream>
using namespace std;
int main()
{
int choice = 1;
cout << endl << "Welcome to Hunterbucks!";
while (choice > 0)
{
cout << endl << "Input -1 when you're finished ordering!";
cout << endl << endl << "Coffee" << " " << "($)";
cout << endl << "1. Regular" << " " << "1.50";
cout << endl << "2. Decaf" << " " << "1.23";
cout << endl << "3. Americano" << " " << "2.25";
cout << endl << "4. Espresso" << " " << "2.25";
cout << endl << "5. Latte" << " " << "2.50";
cout << endl << "6. Cappuccino" << " " << "2.75";
cout << endl << "7. Frappuccino" << " " << "2.75";
cout << endl << "8. Macchiato" << " " << "2.50";
cout << endl << endl << "Snacks" << " " << "($)";
cout << endl << "9. Muffin" << " " << "1.00";
cout << endl << "10. Blueberry Muffin" << " " << "1.25";
cout << endl << "11. Raspberry Muffin" << " " << "1.25";
cout << endl << "12. Scone" << " " << "0.75";
cout << endl << "13. Blueberry Scone" << " " << "1.00";
cout << endl << "14. Croissant" << " " << "0.75";
cout << endl << endl << "What would you like to order? ";
cin >> choice;
if (choice <= 0)
cout << endl << "Thank you for your order.";
else
cout << endl << "What else would you like to order?";
}
cout << endl << "Thank you for choosing Hunterbucks! Come again soon.";
return 0;
}
Any info that can help me? I'm just a beginner and have been trying this for a few hours.
In pseudo-code you want something like this:
float total = 0.0;
while (choice > 0)
{
....
cin >> choice;
if (choice <= 0)
cout << endl << "Thank you for your order.";
else
{
total += costs[choice];
cout << endl << "What else would you like to order?";
}
}
You'll need to define an array names costs that contains the cost of each item. You'll also want to tackle validation of the user input so that you don't erroneously attempt to read outside the range of the costs array.
You're probably looking at something like this:
#include <iostream>
using namespace std;
int main()
{
int choice = 1;
float sum = 0.0;
float arr[] = {
0.00, 1.50, 1.23, 2.25, 2.25, 2.50, 2.75, 2.75, 2.50,
1.00, 1.25, 1.25, 0.75, 1.00, 0.75
};
cout << endl << "Welcome to Hunterbucks!";
while (choice > 0)
{
cout << endl << "Input -1 when you're finished ordering!";
cout << endl << endl << "Coffee" << " " << "($)";
cout << endl << "1. Regular" << " " << "1.50";
cout << endl << "2. Decaf" << " " << "1.23";
cout << endl << "3. Americano" << " " << "2.25";
cout << endl << "4. Espresso" << " " << "2.25";
cout << endl << "5. Latte" << " " << "2.50";
cout << endl << "6. Cappuccino" << " " << "2.75";
cout << endl << "7. Frappuccino" << " " << "2.75";
cout << endl << "8. Macchiato" << " " << "2.50";
cout << endl << endl << "Snacks" << " " << "($)";
cout << endl << "9. Muffin" << " " << "1.00";
cout << endl << "10. Blueberry Muffin" << " " << "1.25";
cout << endl << "11. Raspberry Muffin" << " " << "1.25";
cout << endl << "12. Scone" << " " << "0.75";
cout << endl << "13. Blueberry Scone" << " " << "1.00";
cout << endl << "14. Croissant" << " " << "0.75";
cout << endl << endl << "What would you like to order? ";
cin >> choice;
if (choice <= 0){
cout << endl << "Thank you for your order.";
} else {
cout << endl << "What else would you like to order?";
sum += arr[choice];
}
}
cout << "Total: " << sum << endl;
cout << endl << "Thank you for choosing Hunterbucks! Come again soon.";
return 0;
}
Do note the following:
1) Your menu choices being with '1' thus there is a need to offset your arr at index '0' with the '0.00' value there.
2) The cost added up follows that of your indexed array, thus you would probably want to format your output according to your array, so that next time, all you need to do is to update your array.
Hope it helped. Cheers!
The way you have your code set up warrants a switch statement, like the following:
double total = 0;
switch (choice)
{
case 1:
total += 1.50; // Regular.
break;
case 2:
total += 1.23; // Decaf.
break;
// Etc.
}
cout << endl << "Your total is " << total;
That being said, the easiest way to do this would be to have an array of prices:
double prices[] = {1.50, 1.23, 2.25};
// ...
total += prices[choice - 1]; // No switch statement needed.