Here is my input file:
Plain Egg
1.45
Bacon and Egg
2.45
Muffin
0.99
French Toast
1.99
Fruit Basket
2.49
Cereal
0.69
Coffee
0.50
Tea
0.75
This is my code. I'm having no problem when people order 8 or 9 items, but as soon as they enter 10 or more items, I get really weird results and not the nicely printed check that I would like to see. For instance, let's say someone orders 8 items and they input "12345678" for their order. I get this:
But if the user inputs that they want let's say 15 items and they order "123456781234567", I get THIS:
Even when they order 10 items, I get just a blank check :/ :
What in the world is happening with my program? Does it have to do with me using c-strings? I want to understand what is going on inside my little computer and understand the way my computer thinks. If you can explain me this in a very simple way (I'm really new so I need something very explanatory with definitions of your fancy words) and some way I can understand, because like I said, I'm really new to computer science. Thank you.
// Local Restaurant: The 5,000 Fed
#include <cmath>
#include <cstdlib>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <string>
using namespace std;
struct menuItemType {
string menuItem;
double menuPrice;
};
void getData(menuItemType list[]);
string showMenu(menuItemType list[]);
void printCheck(string y, menuItemType list[]);
int main() {
string y;
menuItemType menuList[8];
cout << "Hello and welcome to the 5,000 fed. Your local Long Beach café, "
"whose name is inspired by Jesus' miraculous manifestation "
"of bread to feed the 5k! His disciples helped him in passing out "
"the bread, and so I am here, your waiter, to help you and assist "
"you with your order. ";
getData(menuList);
y = showMenu(menuList);
printCheck(y, menuList);
return 0;
}
void getData(menuItemType list[]) {
string menupricet[8];
ifstream inFile;
inFile.open("menudata.txt");
for (int i = 0; i < 8; i++) {
getline(inFile, list[i].menuItem);
getline(inFile, menupricet[i]);
}
for (int i = 0; i < 8; i++) {
list[i].menuPrice = atof(menupricet[i].c_str());
}
}
string showMenu(menuItemType list[]) {
int amount;
char number;
string reps;
string response = "y";
string total = "";
cout << fixed << setprecision(2);
cout << endl << endl;
cout << "Here is our wonderful menu. I do recommend the french toast, "
"similar to the bread that Jesus ate."
<< endl
<< endl;
for (int i = 0; i < 8; i++) {
cout << i + 1 << ". " << setw(13) << left << list[i].menuItem << setw(8)
<< right << list[i].menuPrice << endl;
}
cout << endl;
cout << "How to order: Write the number you would like and specify how many "
"you want of that item by typing it in multiple times. For instance "
"an input of \"1135777\" means that you want two plain eggs, one "
"muffin, one fruit basket, and three coffees."
<< endl
<< endl;
cout << "How many items are you ordering total?: ";
cin >> amount;
cout << endl;
cout << "Please input your order, and press enter after your order: ";
for (int i = 0; i < amount; i++) {
cin >> number;
total += number;
}
return total;
}
void printCheck(string y, menuItemType list[]) {
int k = 0;
int temp = 0;
double bill = 0;
int newone = 0;
k = atoi(y.c_str());
while (k != 0) {
newone = newone * 10;
newone = newone + k % 10;
k = k / 10;
}
cout << "Thank you for dining at the 5,000 fed. Here's your check:" << endl
<< endl
<< endl;
cout << "_________________" << endl;
while (newone > 0) {
temp = newone % 10;
newone /= 10;
bill += list[temp - 1].menuPrice;
cout << left << setw(15) << list[temp - 1].menuItem << right << setw(5)
<< "|" << list[temp - 1].menuPrice << endl;
}
cout << fixed << setprecision(2);
cout << left << setw(15) << "Amount Due:" << right << setw(10) << "|" << bill
<< endl
<< endl;
}
EDIT:
So someone asked me to paste the output as text instead of images so here are the three outputs:
They order 8 items such as "12345678":
_________________
Plain Egg |1.45
Bacon and Egg |2.45
Muffin |0.99
French Toast |1.99
Fruit Basket |2.49
Cereal |0.69
Coffee |0.50
Tea |0.75
Amount Due: |11.31
They order 15 items such as "123456781234567":
_________________
French Toast |1.99
\365\277\357\376^ޝ-
YW\300\365\277\357\376all\377
\200\367\277\357\376
\370\277\357\376
\227\370\277\357\376
\331\370\277\357\376%\371\277\357\376\262
|0.00
Coffee |0.50
Muffin |0.99
\362\277\357\376x\362\277\357\376x\362\277\357\376x\362\277\357\376Plain Egg33333 |0.00
Plain Egg |1.45
Cereal |0.69
\365\277\357\376^ޝ-YW\300\365\277\357\376all\377\200\367\277\357\376\370\277\357\376\227\370\277\357\376\331\370\277\357\376%\371\277\357\376\262 |0.00
Cereal |0.69
Amount Due: |6.31
They order 10 items such as "1234567812":
_________________
Amount Due: |0.00
The problem in your code is associated with the size of the type int. Here is the solution, you may declare k and new one as unsigned long int so that they can hold up to 4,294,967,295 which still contains only 10 orders.
As a solution, you can use the string y as it is instead of converting to int. So that you can place as many orders as you wish. To traverse through the orders, you can use substr and length function of the string.
Let me re-write the printCheck function for, if I may
void printCheck(string y, menuItemType list[]) {
double bill = 0;
for(int i = 0; i< y.length(); i++)
{
string a = y.substr(i, 1);
int kk = atoi(a.c_str());
bill += list[kk - 1].menuPrice;
cout << left << setw(15) << list[kk - 1].menuItem << right << setw(5)
<< "|" << list[kk - 1].menuPrice << endl;
}
cout << fixed << setprecision(2);
cout << left << setw(15) << "Amount Due:" << right << setw(10) << "|" << bill << endl;
}
I hop this helps. Regards.
Related
So I want to output a list of the names and randomly generated numbers. I already did everything else it is just that I do not know how to output it the way I want it to.
I want it to output like this:
ID #: Names:
1 bob
23 rob
44 kanye
Here is what I have so far:
cout << "Would you like to view the archived names and IDs? (Y/N)" << endl;
string archiveInput;
cin >> archiveInput;
if(tolower(archiveInput[0]) == 'y')
{
cout << "ID #: Names: " << endl;
output(ids, names);
}
Here are my functions I used.
void output(const vector<int>& ids)
{
for(int i = 0; i < ids.size(); i++)
{
cout << ids[i] << endl;
}
cout << endl;
}
void output(const vector<string>& names)
{
for(int i = 0; i < names.size(); i++)
{
cout << names[i] << endl; //might have to use endl for list format
}
cout << endl;
}
void output(const vector<int>& ids, const vector<string>& names)
{
cout << output(ids) << " " << output(names); //I thought this would work, im new :(
}
Try to using this method
#include <iostream>
#include <iomanip>
using namespace std;
class Student
{
public:
string studentName;
int studentAge;
int studentMarks;
int admissionYear;
Student(string name, int age, int marks, int year)
{
studentName = name;
studentAge = age;
studentMarks = marks;
admissionYear = year;
}
};
int main()
{
Student studentArray[4] = {Student("Alex", 20, 80, 2018), Student("Bob", 21, 82, 2018), Student("Chandler", 23, 85, 2017), Student("Rose", 18, 89, 2019)};
cout
<< left
<< setw(10)
<< "Name"
<< left
<< setw(5)
<< "Age"
<< left
<< setw(8)
<< "Marks"
<< left
<< setw(5)
<< "Year"
<< endl;
for (int i = 0; i < 4; i++)
{
cout
<< left
<< setw(10)
<< studentArray[i].studentName
<< left
<< setw(5)
<< studentArray[i].studentAge
<< left
<< setw(8)
<< studentArray[i].studentMarks
<< left
<< setw(5)
<< studentArray[i].admissionYear
<< endl;
}
return 0;
}
It will print the below output :
Name Age Marks Year
Alex 20 80 2018
Bob 21 82 2018
Chandler 23 85 2017
Rose 18 89 2019
We have set different widths for each column. The first column width is 10, the second column width is 5, the third column width is 8, and the last column width is 5.
The width is important here. If it is less than the size of its content, the content will overflow.
cout << output(ids) << " " << output(names);
First runs the function output overloaded with ids argument, and afterwards runs the version with the names like so:
for(int i = 0; i < ids.size(); i++)
{
cout << ids[i] << endl;
}
cout << endl;
// returns from first function call
cout << " ";
// enters second function call
for(int i = 0; i < names.size(); i++)
{
cout << names[i] << endl; //might have to use endl for list format
}
cout << endl;
Is how the compiler will run it, which is why your output is underneath each other.
This code is what you want inside the overload with the two arguments:
for(int i = 0; i < ids.size(); i++)
{
cout << ids[i] << " " << names[i] << endl;
}
cout << endl;
However your output will still not quite look like how you want. You have to apply other tricks for that.
This is the reason why your current code behave like that. Look at M Khaidar's answer for the correct way to solve your problem.
I need help in a simple way of solving a part of my code that i cant seem to make it work.The point of my "assignment" is that everything must try to be in one class.Now the problem i am having is at a part of my code where it is suppose to "print" the n number of products meaning that it displays what you have inputted in the void get() part,but the problem i cant seem to resolve is it only prints the last name,amount,weight of the product and not everything written.
class Class
{
public:
string name;
int n, amount;
float weight;
void market()
{
cout << "Give the number of products you want to get at Market : " << endl;
cin >> n;
}
void get()
{
for (int i = 0; i < n; i++)
{
cout << "Give product name,amount and weight : " << endl;
cin >> name >> amount >> weight;
}
}
void print()
{
cout << "\nProduct display:\n" << endl;
for (int i = 0; i < n; i++)
{
cout << name << " - " << amount << " , " << weight << " kg" << endl;
cout << "------------------------" << endl;
}
};
};
The main part.
int main()
{
Class market;
market.market();
market.get();
market.print();
}
You're actually overwriting name, amount and weight. You have to make use of something similar to std::vector:
class Class {
public:
std::vector<string> names;
// Same for others
When you get them from std::cin you have to push them into the vector:
names.push_back(name);
amounts.push_back(amount);
weights.push_back(weight);
When printing you loop over the vectors:
for (int i = 0; i < n; i++) {
cout << names[i] << " - " << amounts[i] << " , " << weights[i] << " kg" << endl;
cout << "------------------------" << endl;
}
You must use a std::vector<TYPE> (example 1), or array for holding multiple data in a single variable (example 2).
Example 1
Since you've been coding in C++ programming language, it's highly recommended to use std::vector<> for best results.
Consider the class example (read comments too):
int count = 0; // PRIVATE SECTION
char ask;
std::vector<std::string> name;
std::vector<int> amount;
std::vector<float> weight;
std::string tName; // temp variables
int tAmount;
float tWeight;
public:
void market()
{
cout << "WELCOME!" << endl; // nothing's required with vector
}
void get()
{
do {
cout << "Give product name, amount and weight : " << endl;
cin >> tName >> tAmount >> tWeight; // getting temporary variables
name.push_back(tName); // assigning
amount.push_back(tAmount);
weight.push_back(tWeight);
count++;
cout << "Add more? (Y/n): "; // add more? Go on if yes...
cin >> ask;
} while (ask == 'Y' || ask == 'y');
}
void print()
{
cout << "\nProduct display:\n"
<< endl;
for (int i = 0; i < count; i++)
{
cout << name[i] << " - " << amount[i] << " , " << weight[i] << " kg" << endl;
cout << "------------------------" << endl;
}
}
Sample Output
WELCOME!
Give product name, amount and weight :
ABC 12 55.5
Add more? (Y/n): y
Give product name, amount and weight :
SSD 33 43.2
Add more? (Y/n): n
Product display:
ABC - 12 , 55.5 kg
------------------------
SSD - 33 , 43.2 kg
------------------------
Example 2
You can do the same thing just with few modifications, but having static number isn't considered dynamic. You can use arrays for your program as stated below.
Rather than:
string name;
int n, amount;
float weight;
Consider const int MAX = 1024; and use (class variables must be visible only inner the class and nowhere else):
private: // declare on top of the class ("private:" is by default and redundant)
string name[MAX];
int n, amount[MAX];
float weight[MAX];
Edited & working example class:
void get()
{
for (int i = 0; i < n; i++)
{
cout << "Give product name,amount and weight : " << endl;
cin >> name[i] >> amount[i] >> weight[i];
}
}
void print()
{
cout << "\nProduct display:\n"
<< endl;
for (int i = 0; i < n; i++)
{
cout << name[i] << " - " << amount[i] << " , " << weight[i] << " kg" << endl;
cout << "------------------------" << endl;
}
}
Sample Output
Give the number of products you want to get at Market :
2
Give product name,amount and weight :
ABC 25 102
Give product name,amount and weight :
BDE 22 333
Product display:
ABC - 25 , 102 kg
------------------------
BDE - 22 , 333 kg
------------------------
So i'm making a turn based dice game that's modeled after this game called "underground chinchiro" which was taken from an anime called "Kaiju". I need to set a limit to my program so that it only runs for a set number of rounds,
I'm only a beginner in coding so sorry for anything unusual you see in my code.
#include <iostream>
#include <string>
#include <time.h>
using namespace std;
void roll_3_dice(int &dice1, int &dice2, int &dice3)
{
dice1 = rand() % 6 + 1;
dice2 = rand() % 6 + 1;
dice3 = rand() % 6 + 1;
return;
}
int main()
{
int cash = 90000;
int wager;
int r;
//dealer's die
int dealer1;
int dealer2;
int dealer3;
// your die
int mdice1;
int mdice2;
int mdice3;
for (int i = 0; i < 10; i++)
{
cout << "Wager up boy!"<< endl;
cin >> wager;
while (wager < 100 || wager > 90000)
{
cout << "Minimum wager is 100; Maximum wager is 90000 ";
cin >> wager;
}
cout << "You wagered: " << wager << endl;
cout << "You have " << cash - wager << " remaining" << endl;
cash = cash - wager;
cout << endl;
cout << "Dealer will now roll the dice" << endl;
roll_3_dice(dealer1, dealer2, dealer3);
cout << "Dealer rolled the following: " << endl;
cout << dealer1 << "-" << dealer2 << "-" << dealer3 << endl;
cout << "It's your turn to roll the dice." << endl;
cout << endl;
cout << "Press any key to roll the dice" << endl;
cin >> r;
roll_3_dice(mdice1, mdice2, mdice3);
cout << "You rolled the following: " << endl;
cout << mdice1 << "-" << mdice2 << "-" << mdice3 << endl;
system ("pause");
}
}
Take a look at for loops. For loops will allow you to run your code for a set number iteration.
e.g. iterate over some code 7 times.
int number_of_iterations = 7;
for(int i = 0; i < number_of_iterations; i++) {
// Your code that you would like to iterate over goes here.
}
EDIT:
As has been specified by the OP (in comments below), the issue is appears to be with the program not stopping to receive input from the user through each iteration of the for loop.
This could be for a number of reasons. My best guess would be that the stdin buffer is not clear and that std::cin continues to read in from the buffer. This could be solver by calling cin.clear() before reading in your input.
is time to learn how to use constants...
define one doing
const int max_round = 5;
and the do a while so long round is <= than max_round
Your problem is pretty unclear. Edit your code, find the section where problems occurring and paste that part only.
like:
while(cin>>wager)
{
if(condition fails)
{
break;
}
}
I was reading the chapter on structures in my book, and it got me re-modifying a program I already made, but this time using structures which I have never used before; however, after finishing the program, there's one issue I'm not understanding. The output of the program only displays once. It's in a for loop, and yet even though it asks me to input my information three times, it only outputs the first information.
I'm probably just not understanding how arrays in structures work.
An example of my issue is the following.
I have my output on the following loop
for(int counter = 0; counter <size; counter++)
The size is 3, which would mean I'll get the output printed three times; however the answer I'm getting is the same as if I was asking for the following.
Listofnames[0].F_name
When what I actually want is
Listofnames[0].F_name Listofnames[1].F_name Listofnames[2].F_name
However, I don't want to have to write it three times, I did to test it and it actually worked, but is that the only way to do it? Or did I miss something in my program?
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
struct Names
{
string F_name; //Creating structure called Names.
string L_name;
char Mi;
};
struct Payrate
{
double rate;
double hoursworked; //Creating structure called Payrate.
double gross;
double net;
};
int main()
{
double stateTax = 0, federalTax = 0, unionFees = 0, timeHalf = 1.5; //Initializing variables.
const int size = 2; //Array size.
Payrate employee[size]; //Structure variables
Names Listofnames[size];
for (int counter = 0; counter < size; counter++) //Initializing for loop.
{
cout << "What's your first name?: " << endl;
cin >> Listofnames[counter].F_name;
cout << "What's your last name?: " << endl; //Displaying names, and hours worked, rate.
cin >> Listofnames[counter].L_name;
cout << "What is your middle initial?: " << endl;
cin >> Listofnames[counter].Mi;
cout << "How many hours did you work? Please enter a number between 1-50: " << endl;
cin >> employee[counter].hoursworked;
cout << "What is your hourly rate? Please enter a number between 1-50: " << endl;
cin >> employee[counter].rate;
if (employee[counter].hoursworked < 0 || employee[counter].hoursworked >50) //Initializing conditional statements.
{
cout << "Sorry you entered a erong entry. Pc shutting off " << endl; //Displays what happens is user inputs a number under 0 or over 50.
}
if (employee[counter].rate < 0 || employee[counter].rate > 50) //Initializing conditional statements.
{
cout << "Sorry you entered a erong entry. Pc shutting off " << endl; //Displays what happens is user inputs a number under 0 or over 50.
}
if (employee[counter].hoursworked <= 40) //Initializing conditional statements.
{
employee[counter].gross = employee[counter].hoursworked * employee[counter].rate; //Calculating gross.
}
else if (employee[counter].hoursworked > 40) //Initializing conditional statements.
{
employee[counter].gross = employee[counter].hoursworked * (employee[counter].rate * timeHalf); //Calculating gross.
}
stateTax = employee[counter].gross * 0.06;
federalTax = employee[counter].gross * 0.12; //Calculates all the tax fees, and net.
unionFees = employee[counter].gross * 0.02;
employee[counter].net = employee[counter].gross - (stateTax + federalTax + unionFees);
}
cout << "FirstN " << "MI " << "LastName " << "\t" << "Rate " << "HoursWorked " << "TimeHalf " << "StateTax " << "FederalTax " << "UnionFees " << "Gross " << " " << "Net " << endl; //Displays header of output.
cout << "==================================================================================================================" << endl;
for (int counter = 0; counter <= size; counter++)
{
//Output.
cout << Listofnames[counter].F_name << "\t" << fixed << setprecision(2) << Listofnames[counter].Mi << " " << Listofnames[counter].L_name << "\t" << employee[counter].rate << "\t" << employee[counter].hoursworked << "\t" << setw(7) << timeHalf << "\t" << setw(8) << stateTax << setw(12) << federalTax << "\t" << unionFees << "\t" << employee[counter].gross << "\t" << employee[counter].net << endl;
system("pause");
}
}
P.s If you had to re modify this program again, what would you use to simplify it. Asking so I can keep re-modifying, and learn more advanced stuff. Vectors, pointers? Thanks in advance.
You have an array with 3 indexes but your loop is only going upto 2 indexes. Change your for loop to this.
for (int counter = 0; counter <= size; counter++)
Now, this loop will print the all the indexes.
Instead of using a static value you can also use this.
for (int counter = 0; counter < sizeof(Listofnames)/sizeof(Listofnames[0]); counter++)
sizeof(Listofnames)/sizeof(Listofnames[0]) This will give you the total size of your array.
Ideone Link
I'm having some trouble here. I have a file that looks like this (here's a snippet)
Sophia F 22158
Emma F 20791
Isabella F 18931
Jacob M 18899
Mason M 18856
Ethan M 17547
and I want to put each name, and the name's respective number into seperate vectors. For example, I would have 4 vectors:
1 for women's names and 1 for women's numbers, and the same for men's and men's numbers. (so 4 total)
I have this code, which will go through the file and pull out these elements and put them in vectors.
for (int i = 0; i < numTimes; i++) {
getline (inputFile, inputLine);
ss.str(inputLine); //ss is a string stream
ss >> name >> gender >> popularity;
if (gender == 'M') {
mNames[i] = name;
mFrequency[i] = popularity;
} else if (gender == 'F') {
fNames[i] = name;
fFrequency[i] = popularity;
}
ss.clear();
}
and I use this method to print it out:
cout << counter << " Most Popular Baby Names" << endl << endl;
cout << left << setw(15) << "Girls" ;
cout << right << setw(9) << "Frequency" <<" ";
cout << left << setw(15) << "Boys";
cout << right << setw(9) << "Frequency" << endl;
for (int i = 0; i < counter; i ++) {
cout << left << setw(15) << fNames[i] ;
cout << right << setw(9) << fFreq[i] <<" ";
cout << left << setw(15) << mNames[i];
cout << right << setw(9) << mFreq[i] << endl;
{
but then I get this output:
http://i.stack.imgur.com/2x0ta.png
But I would like for it to be like this:
http://i.stack.imgur.com/OSIX9.png
So I'm thinking I either need to go through before I print and remove all the whitespace/0's in these vectors, or I need to check while I'm printing out. Does anyone have any pointers?
Solved, thanks guys.
I used two separate while loops, one for each set of arrays. In between them I reset the ifstream back to the beginning. This way, it goes through until each vector has 5 non-zero elements.