Hello all, for my homework, I am making a sit down pizza restaurant app. This is the code so far - visual-c++

#include <iostream>
#include <string>
string drinkMenu();
string pizzaMenu();
int main()// Main Function to call other Functions
{
using namespace std;
cout << "Hello, welcome to Walter's Pizza Restaurant. \n";
string drink = drinkMenu();
string pizza = pizzaMenu();
string drinkCost = drinkMenu();
string pizzaCost = pizzaMenu();
cout << "So I have a " << drink << ", and a" << pizza;
string totalCosts = drinkCost + pizzaCost;
}
string drinkMenu()//Drink Function
{
using namespace std;
int drink = 0;
double drinkCost = 0.00;
cout << "Here are our drinks, we have Coca-Cola products\n\n"
<< "Coke Enter 1\n"
<< "Sprite Enter 2 \n"
<< "MrPibb Enter 3\n"
<< "FantaOrange Enter 4 \n"
<< "BarqsRootBeer Enter 5\n"
<< "MelloYello Enter 6\n"
<< "MinuteMaidLemonade Enter 7\n"
<< "SeagramsGingerAle Enter 8\n"
<< "Water Enter 9\n"
<< "NoDrink Enter 0";
if (drink == 1)
{
drinkCost = 0.99;
return "Coke";
}
else if (drink == 2)
{
drinkCost = 0.99;
return "Sprite";
}
else if (drink == 3)
{
drinkCost = 0.99;
return "MrPibb";
}
else if (drink == 4)
{
drinkCost = 0.99;
return "FantaOrange";
}
else if (drink == 5)
{
drinkCost = 0.99;
return "BarqsRootBeer";
}
else if (drink == 6)
{
drinkCost = 0.99;
return "MelloYello";
}
else if (drink == 7)
{
drinkCost = 0.99;
return "MinuteMaidLemonade";
}
else if (drink == 8)
{
drinkCost = 0.99;
return "SeagramsGingerAle";
}
else if (drink == 9)
{
drinkCost = 0.00;
return "Water";
}
else
return "NoDrink";
}
string pizzaMenu()//Restaraunt Menu Function
{
using namespace std;
int pizza = 0;
double pizzaCosts = 0.00;
cout << "Hello, welcome to Coleman's Pizza Restaurant!\n\n"
<< "This is our menu. All Pizzas come with Mozzarella. \n\n"
<< "Enter 1 for BigRed: our general meat lovers pizza with Bacon, Pepporonni, Sausage, and Canadian Bacon. 15.99 \n\n"
<< "Enter 2 for Hawaiian: This pie comes with the sweetest natural flavors you could put in a pizza.\n"
<< "Pineapple, Red Peppers, Purple Onions, and Canadian Bacon. 12.99\n\n"
<< "Enter 3 for Suprema: A big pizza for the big topping lovers.\n"
<< "Green Peppers, Onions, Mushrooms, Roasted Tomatoes, Pepporoni, Sausage, and Bacon. 14.99\n\n"
<< "Enter 4 for Veggie: veg out like a herbivore with a veggie loaded pizza.\n"
<< "Red and Green Peppers, Purple Onions, Mushrooms, Spinach Leaves, Roasted Tomatoes. 10.99\n\n"
<< "Enter 5 for BBQ: Everything you like about BBQ, ON A PIZZA ! \n"
<< "Cheddar Cheese, Pulled Pork, Purple Onions, Chopped Pickles, and BBQ sauce. 14.99\n\n"
<< "Enter 6 for Snowman: A pizza as white as snow.\n"
<< "Roasted Chicken, White Onions, White Cheddar, Goat Cheese, and White BBQ sauce. 16.99";
cin >> pizza;
if (pizza == 1)
{
pizzaCosts = 15.99;
return "BigRed";
}
else if (pizza == 2)
{
pizzaCosts = 12.99;
return "Hawaiian";
}
else if (pizza == 3)
{
pizzaCosts = 14.99;
return "Suprema";
}
else if (pizza == 4)
{
pizzaCosts = 10.99;
return "Veggie";
}
else if (pizza == 5)
{
pizzaCosts = 15.99;
return "Smokehouse";
}
else if (pizza == 6)
{
pizzaCosts = 16.99;
return "Snowman";
}
else
{
pizzaCosts = 0.00;
return "NoPizza";
}
}
For the app, I have to use functions. I've talked it over with my professor, but sometimes we just hit a brick wall. The outcome is that you will enter the kind of drink that you want, then you will enter the kind of pizza you want and it will charge you for it. By the way I developed it to where you only enter one drink and one pizza.

Related

My code is stuck in an infinite loop in C++

I am trying to use a while loop to calculate the average of 3 inputted grades, but I can not enter the next grade as the loops keep on going without giving me the chance to enter the next grade.
#include<iostream>
using namespace std;
int main()
{
int grade = 0;
int count = 0;
int total = 0;
cout << "Enter grade: ";
cin >> grade;
while (grade != -1)
{
total = total + grade;
count = count + 1;
cout << "Enter next grade: ";
cin >> grade;
}
int(average) = total / 3;
cout << "Average: " << int(average) << endl;
system("pause");
}
I tested your code with integer and it works fine.
If you only take int as input, the best is to put something to check the input type. Use cin.fail() to check if user input anything other than int.
for example:
while(cin.fail()) {
cout << "Error" << endl;
cin.clear();
cin.ignore(256,'\n');
cout << "Please enter grade:"
std::cin >> grade;
}
which I refer from https://www.codegrepper.com/code-examples/cpp/how+to+check+type+of+input+cin+c%2B%2B
and here as well Checking cin input stream produces an integer

Banking System Program

I've slowly been building up my banking system and need help. First though a quick run through. 1. choose an existing account or new one. 2. after that it will display the options of withdraw, deposit or close account (close account is the least of my worries as of right now). I've been using two different text documents as well. One is "Accounts" where it will scan or add accounts the other is "Money" where it should keep track of money in each account. The question is how can I assign the money to each account?
I've head of struct arrays, however it is confusing me. If that is the best course of action I'm going to need a mini walkthrough by making comments on the coding for me to better understand it.
#include <iostream>
#include <fstream>
#include <iomanip>//used to move text
#include <string>
using namespace std;
int display(char& anwser);
void N_account(char& anwser, string& name);
int Exist(string& name_search, char& anwser, string name_from_file, char&
anwser2, string& money_deposit);
void deposit(string& money_deposit, char& anwser, char& anwser2, int& money_D);
void withdraw(string& withdraw_money, char& anwser, char& anwser2, int& money_D, string& total_money, int& money_W);
int main()
{
int start_money, money_D, money_W;
string name, name_search, name_from_file, money_deposit, withdraw_money, total_money;
char anwser, anwser2;
display(anwser);
if (anwser == '1')
{
N_account(anwser, name);
}
if (anwser == '2')
{
Exist(name_search, anwser, name_from_file, anwser2, money_deposit);
}
if (anwser2 == '1')
{
deposit(money_deposit, anwser, anwser2, money_D);
}
if (anwser2 == '2')
{
withdraw(withdraw_money, anwser, anwser2, money_D, total_money, money_W);
}
}
int display(char& anwser)
{
cout << setw(65) << "=================" << endl;
cout << setw(65) << "Banking Managment" << endl;
cout << setw(65) << "=================" << endl;
cout << setw(60) << "1.New account" << endl;
cout << setw(65) << "2.Existing account" << endl;
cin >> anwser;
return 0;
}
void N_account(char& anwser, string& name)
{
ofstream outfile;
outfile.open("Accounts.txt", std::ofstream::out | std::ofstream::app);
cout << "Enter in first and last name for new account:";
cin.ignore();
getline(cin, name);
outfile << name;
outfile << endl;
cout << "Account added" << endl;
outfile.close();
}
int Exist(string & name_search, char& anwser, string name_from_file, char& anwser2, string & money_deposit)
{
ifstream infile;
infile.open("Accounts.txt");
cout << "Enter in your account:";
cin.ignore();
getline(cin, name_search);
while (getline(infile, name_from_file))
{
if (name_from_file == name_search)
{
cout << "Account found: " << name_search << endl;
cout << "Choose what you would like to do" << endl;
cout << setw(56) << "1.Deposit" << endl;
cout << setw(57) << "2.Withdraw" << endl;
cout << setw(62) << "3.Close account" << endl;
cin >> anwser2;
return 0;
}
}
infile.close();
}
void deposit(string & money_deposit, char& anwser, char& anwser2, int& money_D)
{
ofstream O_file;
O_file.open("Money.txt", std::ofstream::out | std::ofstream::app);
cout << "Enter in how much you would like to deposit: ";
cin.ignore();
getline(cin, money_deposit);
money_D = stoi(money_deposit);//converts string into integer so that I may use basic operators such as less than or subtraction ect.
if (money_D < 0)
{
cout << "Error!" << endl;
system("pause");
exit(1);
}
O_file << money_deposit;
O_file << endl;
O_file.close();
}
void withdraw(string & withdraw_money, char& anwser, char& anwser2, int& money_D, string & total_money, int& money_W)
{
ofstream O_file;
O_file.open("Money.txt", std::ofstream::out | std::ofstream::app);
cout << "Enter in how much you would like to withdraw:";
cin.ignore();
getline(cin, withdraw_money);
money_W = stoi(withdraw_money);//converting string to integer
total_money = money_W - money_D;
O_file << total_money;
O_file << endl;
O_file.close();
}
When I choose the account named "John Doe" with a balance of $45.00 and wish to withdraw $20.00 I want the money for John Doe to decrease to $25.00 and NOT make a new line in the "Money" text document saying $20.00 like it has been doing.
The existing accounts are:
John Doe
Jane Doe
Travis Scott
William Smith
Patrick Michaels
Courtney Desmond
Their bank accounts are:
45.00
98.00
48.00
56.00
120.00`

Do while loop won't break

I am making a Goldilocks game. If the user chooses the wrong answer it would loop back to the beginning of the program. When I try to choose any option it always loops back to the beginning including the correct answer which is 2. I am still new to c++. I do not understand why it is looping to the beginning if the condition is true when 2 is chosen.
#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;
void FirstSet()
{
bool win = false;
string PName;
int choice;
int num1, num2, result;
do
{
system("CLS");
cout << " Please Enter Name \n";
cin >> PName;
cout << PName << " and the Three Bears\n\n ";
cout << PName << " Walks up and sees 3 Doors, 1 Large Door, 1 Medium
Door and 1 Small Door. \n\n\n " << "Which Door do you want to Open?\n "
<< " 1 for The Large Door\n " << " 2 for the Medium Door\n " << " 3
for the small door\n ";
cin >> choice;
if (choice == '1')
{
cout << " The large door is too heavy it will not budge.\n "
<< " Please Try Again\n\n ";
system("pause");
}
else if (choice == '2')
{
win = true;
}
else if (choice == '3') {
cout << " The Door is too small you would get stuck.\n "
<< "Please Try Again\n\n";
}
} while (!win);
}
int main()
{
FirstSet();
system("pause");
return 0;`
The reason none of your comparisons are turning true is because you are reading the input into an int variable. Then you are comparing to ascii character values of 1,2 and 3 which happen to be 49, 50 and 51 respectively. If you modify your if lines to compare directly with integers, it should work:
if (choice == 1)
{
...
}
else if (choice == 2)
{
...
}
else if (choice == 3)
{
...
}
Although, for readability purposes and also to avoid such cases, I recommend using switch case statements in this case.

Why VS 2015 is giving me the exception at the end of the code below?

I am somewhat new to c++, our class only went over debugging briefly. This is probably my 10th do over, I have been over it a week, have done plenty of research over the web and I just don't understand debugging enough to figure out how to fix my code. The program is supposed to real a file like this:
TTFTFTTTFTFTFFTTFTTF
ABC54102 T FTFTFTTTFTTFTTF TF
DEF56278 TTFTFTTTFTFTFFTTFTTF
ABC42366 TTFTFTTTFTFTFFTTF
ABC42586 TTTTFTTT TFTFFFTF
When it reads the file it's supposed to output the student ids, what they answered on each question and the grade for each student. My problem is, I don't know much about debugging and VS keeps throwing an exception at the end of the code I provided. I am just learning how to use dynamic arrays so I know it has something to do with my use of them because I had the program working fine in my other c++ class but I had to change it around to use dynamic arrays for this project.
What's wrong with my program? I have researched the web and reread the chapters in the book over and over and I cannot figure it out.
#include <iostream>
#include <string>
#include <fstream>
#include <stdlib.h>
#include <cstddef>
using namespace std;
// function prototypes
void readFile(ifstream& inFile);
char assignGrade(int score, int numQues);
int main()
{
int numQues = 20;
int numStud = 0;
string *studentIDs;
studentIDs = new string[numStud];
char *correctAnswers;
correctAnswers = new char[numStud];
char *studentAnswers;
studentAnswers = new char[numQues];
ifstream inFile;
cout << "\nRedo Programming Exercise Six of Chapter Eight\nUsing Dynamic Arrays..." << endl;
cout << "\nPlease Enter the Number of Students: ";
cin >> numStud;
cout << endl;
readFile(inFile);
inFile.getline(correctAnswers, '/n'); // read the correct answers first
for (int i = 0; i < numStud; i++) // loop students
{
inFile >> studentIDs[i]; // get the student ID
inFile.get(); // discard the space between the student ID and the answer
for (int j = 0; j < numQues; j++) // loop questions
{
studentAnswers[j] = inFile.get(); // get the student's answers
}// end for
cout << "Student ID: " << studentIDs[i] << endl; // output student id
int score = 0; // declare and initialize score to zero
cout << "Answers: "; // display "Answers: "
for (int j = 0; j < numQues; j++) // loop each question
{
cout << studentAnswers[j]; // output student's answers
if (studentAnswers[j] == correctAnswers[j]) // if student answer equals correct answer
score += 2; // correct answer
else if (studentAnswers[j] != correctAnswers[j] && studentAnswers[j] != ' ')
score -= 1; // incorrect answer but not a blank
else if (studentAnswers[j] == ' ')
score = 0;
delete[] studentAnswers;
}// end for
if (score < 0)
score = 0; // don't allow for negative scores
cout << endl; // new line, housekeeping
char grade = assignGrade(score, numQues); // call assignGrade function
cout << "Grade: " << grade << "\n" << endl; // display grade
}// end for
delete[] studentIDs;
system("pause");
return(0);
}
void readFile(ifstream& inFile)
{
inFile.open("Ch12_Ex2Data.txt"); // use inFile to open Ch8_Ex6Data.txt
if (!inFile) // if the file can't be opened or it is corrupt
{
cout << "There was an error opening the input file...\nPlease check file and try again!\n" << endl; // display error message
system("pause");
exit(1); // exit the program
}
} // end readFile function
char assignGrade(int score, int numQues)
{
double percentScore = static_cast<double>(score) / (numQues * 2); // calculate the score percentage
cout << "Score: " << percentScore * 100 << "%" << endl; // display the score
if (percentScore >= 0.9) // if score is greater than or equal to 90%, return A
return 'A';
else if (percentScore >= 0.8) // if score is greater than or equal to 80%, return B
return 'B';
else if (percentScore >= 0.7) // if score is greater than or equal to 70%, return C
return 'C';
else if (percentScore >= 0.6) // if score is greater than or equal to 60%, return D
return 'D';
else // any score lower thn 60%, return F
return 'F';
} // end assignGrade function
It keeps breaking here in the debugger:
static void __CLRCALL_OR_CDECL assign(_Elem& _Left, const _Elem& _Right)
_NOEXCEPT
{ // assign an element
_Left = _Right;
}
// Redo Programming Exercise Six of Chapter Eight
// Using Dynamic Arrays -- C++ Advanced
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
// function prototype
void readFile(ifstream& inFile, string fileName);
int main()
{
// variables, pointers and dynamic arrays
int numStud = 200; // stores the number of students, max students was 200
int numQues = 20; // stores the number of questions on test
string *studentIDs; // pointer variable for studentIDs dynamic array
char *correctAnswers; // pointer variable for correctAnswers dynamic array
correctAnswers = new char[numQues]; // correctAnswers dynamic char array
char *studentAnswers; // pointer variable for studentAnswers dynamic array
studentAnswers = new char[numQues]; // studentAnswers dynamic char array
char *fileName; // pointer variable for fileName dynamic char array
fileName = new char[25]; // fileName dynamic char array
ifstream inFile; // input stream variable inFile
// display message to explain the program
cout << "\nJames Flowers - Chapter 12 - Programming Exercise 2" << endl;
cout << "\nRedo Programming Exercise Six of Chapter Eight\nUsing Dynamic Arrays..." << endl;
// request file name for fileName dynamic char array
cout << "\nPlease Enter the File Name (Ch12_Ex2Data.txt): ";
cin >> fileName;
cout << endl;
// call readFile function to read the file
readFile(inFile, fileName);
// request number of students for studentIDs dynamic char array and some calculations
cout << "\nHow many students took this test? (4): ";
cin >> numStud;
cout << endl;
studentIDs = new string[numStud]; // create studentIDs dynamic char array
inFile.getline(correctAnswers, '/n'); // read the correct answers first
for (int i = 0; i < numStud; i++) // loop students
{
inFile >> studentIDs[i]; // get the student ID
inFile.get(); // discard the blank space
inFile.getline(studentAnswers, '/n'); // get the student's test answers
cout << "Student ID: " << studentIDs[i] << endl; // output student id
int score = 0; // declare and initialize score to zero
cout << "Answers: "; // display "Answers: "
for (int j = 0; j < numQues; j++) // loop each question
{
cout << studentAnswers[j]; // output student's answers
if (studentAnswers[j] == correctAnswers[j]) // if student answer is correct
score += 2; // add 2 to score
else if (studentAnswers[j] != correctAnswers[j] && studentAnswers[j] != ' ') // incorrect answer but not blank
score -= 1; // subtract 1 from score
else if (studentAnswers[j] == ' ') // if question left blank
score -= 0; // nothing subtracted from score
studentAnswers[j] = ' '; // clear each indice for next student
}// end for
cout << endl; // new line, housekeeping
char grade = 0; // char variable grade initialized to 0
double percentScore = static_cast<double>(score) / (numQues * 2); // calculate the score percentage
cout << "Score: " << percentScore * 100 << "%" << endl; // display the score
if (percentScore >= 0.9) // if score is greater than or equal to 90%, return A
grade = 'A'; // grade = A
else if (percentScore >= 0.8) // if score is greater than or equal to 80%, return B
grade = 'B'; // grade = B
else if (percentScore >= 0.7) // if score is greater than or equal to 70%,
grade = 'C'; // grade = C
else if (percentScore >= 0.6) // if score is greater than or equal to 60%,
grade = 'D'; // grade = D
else // any score lower than 60%,
grade = 'F'; // grade = F
cout << "Grade: " << grade << "\n" << endl; // display grade
}// end for
system("pause"); // pause for readability
return(0);
}// end main
// readFile function reads the file, if not displays error message
void readFile(ifstream& inFile, string fileName)
{
inFile.open(fileName); // use inFile to open input file
if (!inFile.is_open()) // if the file can't be opened or it is corrupt
{
cout << "There was an error opening the input file...\nPlease check file name and try again!\n" << endl; // display error message
system("pause"); // pause for readability
exit(1);
}// end if
} // end readFile function

Why are my grater than and less than if statements not working?

The beginning of my code
#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;
int main()
{
cout << "give two values: ";
int val1 = 0;
int val2 = 0;
int low = 0;
int high = 0;
The section below (up to the break) should differentiate between the highest and
Lowest values for val1 and val2, but Val2 always equals high, and val1
always equals low when I run the program. It seems that the greater than and less that if statements that I have created are not working.
while (cin >> val1 >> val2) {
if (val1 == val2) {
cout << "the values are equal\n";
high = val1;
low = val2 ;
}
if (val1 > val2) {
high = val1;
low = val2;
}
else if (val1 < val2); {
high = val2;
low = val1;
}
break;
}
This is the last part of my code and it gives the user info about the numbers
they picked
cout << "Value one and two added together is: " << val1 + val2 << "\n"
<< "Value one and two subtracted is: " << val1 - val2 << "\n"
<< "value one and two multiplyed is: " << val1*val2 << "\n"
<< "value one and two divided is: " << val1 / val2 << "\n"
<< "The lowest value is:" << low << " and the highest value is:" << high;
return 0;
}

Resources