Open Side Menu Go to the Top
Register
Programming homework and newbie help thread Programming homework and newbie help thread

06-30-2016 , 10:56 PM
Using max/min is one way of getting rid of if statements.
Code:
for (int z = 0; z < MENU_SIZE; z++)
    subTotal += max(0, orderList[z]) * menuList[z].price;
Programming homework and newbie help thread Quote
06-30-2016 , 10:57 PM
Quote:
Originally Posted by LBloom
This is something I struggle with as a beginning programmer. It is difficult to stop relying on the first tools you learned. In the same program as the above code, I have this:
Code:
for (int z = 0; z < MENU_SIZE; z++)
{
    if (orderList[z] > 0)
        subTotal += (orderList[z] * menuList[z].price;
}
The if statement here I now realize isn't even strictly necessary, so should I automatically get rid of it? Is there any value on skipping the math that will just equal 0?
I think you could accomplish your goals with a very simple structure and perhaps a corecursive functions.

http://www.cplusplus.com/articles/D2N36Up4/

Sent from my SM-G900R4 using Tapatalk

Last edited by just_grindin; 06-30-2016 at 11:05 PM.
Programming homework and newbie help thread Quote
06-30-2016 , 11:05 PM
is orderList[z] going to be 0 or greater than zero? (i.e. never negative?) Then you don't need the if statement, because 0*something = 0. I mean, OK, sure, you're doing a tiny pointless piece of math but you get rid of the logic entirely.

Code:
for (int z = 0; z < MENU_SIZE; z++)  subTotal += orderList[z] * menuList[z].price;
Programming homework and newbie help thread Quote
06-30-2016 , 11:16 PM
It has more to do with the fact that zero is neutral element of addition.
IMO neutral elements of operators are underrated/underused in programming.
Programming homework and newbie help thread Quote
06-30-2016 , 11:18 PM
Yes, orderList[z] will always be >= 0.

Thanks again for all the useful replies I get. This forum has been so helpful. That recursion article was interesting and something I'm going to try to incorporate.

There is just so much bad information out there. For the program I've posted some snippets of, my teacher mentioned that there are many solutions available online (it's a modified version of a textbook exercise). I searched for some and at first used some ideas from them, and that's how I ended up with all that unnecessary stuff in the first code snippet I posted today. It's great to have a place where I can consistently get good answers with examples.
Programming homework and newbie help thread Quote
07-01-2016 , 11:06 AM
ya, I cant understand the reason for that if (orderList[z]>0)
Programming homework and newbie help thread Quote
07-01-2016 , 01:58 PM
Quote:
Originally Posted by LBloom
This is something I struggle with as a beginning programmer. It is difficult to stop relying on the first tools you learned. In the same program as the above code, I have this:
Code:
for (int z = 0; z < MENU_SIZE; z++)
{
    if (orderList[z] > 0)
        subTotal += (orderList[z] * menuList[z].price;
}
The if statement here I now realize isn't even strictly necessary, so should I automatically get rid of it? Is there any value on skipping the math that will just equal 0?
Use std::vector instead of the array(s) and yeah there is some value in checking for zero in my view.
Programming homework and newbie help thread Quote
07-01-2016 , 02:17 PM
Quote:
Originally Posted by Victor
ya, I cant understand the reason for that if (orderList[z]>0)
Not necessarily > 0, it could also be 0.

Thanks for the vector tip. I haven't learned about that yet. Going to spend some time in it this weekend.
Programming homework and newbie help thread Quote
07-01-2016 , 04:23 PM
Oh I see what is going on.

As a beginning programme l am much more worried about my program or app outputting the proper stuff or behaving the right way.

And as for code smells, I worry about MVC and SOLID before eliminating for and if statements.

Anyway, in c# you could eliminate both with a LINQ expression. Seems like it's just syntactic sugar so I'm not sure if that counts.
Programming homework and newbie help thread Quote
07-02-2016 , 12:11 PM
Quote:
Originally Posted by LBloom
Just learning about structures in C++ and I want to make sure I'm getting it right. I'm making a program where I have to display a menu and fill orders for a restaurant (so sick of making those). Anyway, I create a structure called menuItem that has a string name and a double price. I then have to read data from a file to get the names and prices. I initially had something that looks like this:

Code:
//// Declares necessary menuItem variables
	//menuItem spamEggs;
	//menuItem eggBaconSpam;
	//menuItem eggBaconSausageSpam;
	//menuItem spam2SausageBacon;
	//menuItem spam4EggBacon;
	//menuItem spam4BaconSausageTomat;
	//menuItem spam4Eggs;
	//menuItem spam5Beans;
	//menuItem spam;
	//menuItem coffee;

	//// Assigns each menu item to an element of menuList
	//menuList[0] = spamEggs;
	//menuList[1] = eggBaconSpam;
	//menuList[2] = eggBaconSausageSpam;
	//menuList[3] = spam2SausageBacon;
	//menuList[4] = spam4EggBacon;
	//menuList[5] = spam4BaconSausageTomat;
	//menuList[6] = spam4Eggs;
	//menuList[7] = spam5Beans;
	//menuList[8] = spam;
	//menuList[9] = coffee;
	

	// Gets first the name and then the price of each menu item.
	for (int z = 0; z < MENU_SIZE; z++)
	{
		getline(dataFile, menuList[z].name, '%');
		dataFile >> menuList[z].price;
		dataFile.ignore(1, '\n');
	}
Basically, the two big sections where I listed a bunch of stuff and assigned values to the list indices (the parts I commented out) are completely unnecessary, right? All I need is the for loop unless I want to refer to the structures by something other than their list index, which I don't.
is all of the data that you commented out available in the file? if so, then ofc you dont need to actually write it in. you can just read it from the file.

so, can you give me a sample from the code?

and when I hear of different values being assigned to different names, that makes me think of HashMap in java or a Dictionary in C#. C++ should have a similar data structure, google tells me it may just be a map.
Programming homework and newbie help thread Quote
07-02-2016 , 12:28 PM
It's an intro programming course, they probably want him to use an array here and not some STL data structure.
Programming homework and newbie help thread Quote
07-02-2016 , 12:58 PM
Here's the whole thing. There are some parts of it that I find kind of odd, such as asking the user if they want to order at the beginning and therefore manipulating the boolean variable, but I have to do that because I have to make my output look identical to a screen shot my teacher posted, which I have done. I've already turned it in so I'm mainly posting the whole thing in case anyone's curious and to get any input about things I could learn on my own to do things better as I'm learning. If posting whole programs like this is taboo let me know and I won't do so in the future.

Code:
#include "stdafx.h"
#include <iostream>
#include <iomanip>
#include <vector>
#include <string>
#include <fstream>

using namespace std;

// Size of any arrays
const int MENU_SIZE = 10;
const double TAX_RATE = .05;

// Structure for each menu item.
struct menuItem
{
	string name;
	double price;
};

// Function prototypes
void getData(ifstream& dataFile, menuItem menuList[]);
void showMenu(menuItem menuList[]);
void printCheck(menuItem menuList[], int orderList[]);

int main()
{
	// Declares two arrays for computations and to store file data.
	menuItem menuList[MENU_SIZE];
	int orderList[MENU_SIZE]={0};
    
	ifstream dataFile;
	int menuChoice;
	char yesNo;
	bool decision;
	
	// Opens necessary CSV file
	dataFile.open("breakfast.csv");

	// Checks to make sure file opened, displays message and ends program
	// if not.
	if (!dataFile)
	{
		cout << "File failed to open. Ending program." << endl;
		return 1;
	}
	
	// Calls functions to get data from csv file and display menu, respectively.
	getData(dataFile, menuList);
	showMenu(menuList);

	cout << endl << "You can make up to 10 single order selections." << endl;
	cout << "Do you want to make a selection? Y/y (yes), N/n (no): ";
	cin >> yesNo;
	cout << endl;

	if (yesNo == 'y' || yesNo == 'Y')
		decision = true;
	
	// Prompts user to order and keeps track of items ordered.
	while (decision)
	{
        cout << "Enter item number: ";
		cin >> menuChoice;

		orderList[menuChoice - 1] += 1;

		// Allows user an exit condition.
		cout << endl << "Select another item? Y/y (yes), N/n (no): ";
	    cin >> yesNo;
		cout << endl;

		if (yesNo == 'n' || yesNo == 'N')
			decision = false;
	}

	// Function call to print final output
	printCheck(menuList, orderList);
	
	// closes file
	dataFile.close();

	return 0;
}


// Function reads data from breakfast.csv into menuItem variables,
// then stores the menuItem variables in a list.
void getData(ifstream& dataFile, menuItem menuList[])
{
	for (int z = 0; z < MENU_SIZE; z++)
	{
		getline(dataFile, menuList[z].name, '%');
		dataFile >> menuList[z].price;
		dataFile.ignore(1, '\n');
	}
}

// Function to display the menu
void showMenu(menuItem list[])
{
    cout << fixed << showpoint << setprecision(2);
	cout << "Welcome to the Viking's Cafe!" << endl;
	cout << "----Today's Menu----" << endl;
		for (int z = 0; z < MENU_SIZE; z++)
	{
		cout << right << setw(2) << z + 1 << ": " << left << setw(50) 
			 << setfill('.') << list[z].name << " $" << list[z].price << endl;
	}
}

// Function that computes total costs and displays final order
void printCheck(menuItem menuList[], int orderList[])
{
	double subTotal = 0.0;
	double taxTotal = 0.0;
	double grandTotal = 0.0;

	cout << "Welcome to the Viking's Cafe!" << endl;

	for (int z = 0; z < MENU_SIZE; z++)
	{
		cout << fixed << showpoint << setprecision(2);
		if (orderList[z] > 0)
		{
			cout << setw(50) << setfill('.') << menuList[z].name << " $"
				 << menuList[z].price * orderList[z] << endl;
		   
			// Keeps track of subtotal
			subTotal += (orderList[z] * menuList[z].price);
		}
    }

	// Computes other necessary totals
	taxTotal = subTotal * TAX_RATE;
	grandTotal = subTotal + taxTotal;

	// Displays user's final order total
	cout << setw(50) << "Tax " << setfill('.') << " $" << taxTotal << endl;
	cout << setw(50) << "Amount Due " << setfill('.') << " $" << grandTotal << endl;
}
Programming homework and newbie help thread Quote
07-02-2016 , 01:14 PM
Quote:
Originally Posted by RustyBrooks
It's an intro programming course, they probably want him to use an array here and not some STL data structure.
ya i dont know much about c++ and the fact that he was able to create his own data structure to handle the data is very worthwhile to a beginner learning to think in the proper terms.
Programming homework and newbie help thread Quote
07-02-2016 , 01:26 PM
ya, that looks good. i cant imagine that you instructor wants you to avoid loops and ifs for that program.
Programming homework and newbie help thread Quote
07-02-2016 , 01:44 PM
Well, most of the issues I post about here are not things I expect to impact my grade. I'm a 30 year old with a worthless liberal arts degree taking classes at a community college. I'm trying to teach myself more on the side to speed the learning process, and that's mainly what I come here for. The classes themselves are easy and grades are not an issue to this point.

Especially since I'm taking online classes this semester I also don't get a ton of teacher feedback, especially in my C++ class.
Programming homework and newbie help thread Quote
07-02-2016 , 04:11 PM
again, I'm not sure how important incorporating functional concepts is. I did a bootcamp and learned object oriented principles and how to create a web app with MVC. We used Java and Spring MVC along with postgres databases and Spring JDBC. I perused this forum and noticed gaming mouse and some others and some others mocking the usage of loops and if statements and I gotta say, I was totally clueless in how to avoid them or even why you would want to. (I'm still pretty clueless on why its a code smell.)

So recently I took an intermediate type C# class and the first thing the instructor went over was LINQ expressions and lambda expressions. a light bulb kinda went off and I realized, hey, this is a way to avoid those things to a large degree. still, I'm not sure if it counts as what the functional guys here advocate.

so, that's something you could look into I suppose. I am not sure if any of that stuff is available in C++. and ofc, I could be way off on it being at all relevant to avoiding those code smells.

also, one of the things that I think was really worthwhile was being able to perform object modeling. once I was able to consistently do that, I found that I really understood OOP and felt confident enough to create just about any program or app.

Last edited by Victor; 07-02-2016 at 04:17 PM.
Programming homework and newbie help thread Quote
07-02-2016 , 04:22 PM
for instance here some things that I had to model

-a deck of cards
-a bank teller. the teller could create checking or savings accounts. could withdraw or deposit for those accounts.
-a program that could create a tournament bracket
Programming homework and newbie help thread Quote
07-02-2016 , 06:11 PM
my fave trick for card decks was learning how to model a deck of cards after a simple, what is it, 1-52 roll?

maybe i'm not understanding what you're meaning
Programming homework and newbie help thread Quote
07-02-2016 , 07:31 PM
Quote:
Originally Posted by Victor
also, one of the things that I think was really worthwhile was being able to perform object modeling. once I was able to consistently do that, I found that I really understood OOP and felt confident enough to create just about any program or app.
Are you talking about making UML diagrams here?
Programming homework and newbie help thread Quote
07-02-2016 , 07:58 PM
Quote:
Originally Posted by RustyBrooks
It's an intro programming course, they probably want him to use an array here and not some STL data structure.
Quote:
Originally Posted by Victor
ya i dont know much about c++ and the fact that he was able to create his own data structure to handle the data is very worthwhile to a beginner learning to think in the proper terms.
He still creates his own data structure using std::vector and the size of the array being a fixed size issue is resolved. Google std::vector and there is a lot of info and it isn't all that hard to use it. This assignment is basically written in C which of course part of C++ but since it actually is a course in C++ and STL is a key component with std::vector being probably the most used STL data structure (container) then it doesn't seem like that difficult of a challenge.

One of the most common defects in C/C++ code is writing outside of fixed array size boundaries. So much so that a great deal of effort has been put into weeding out these issues before code goes into production. Why not learn a better way to do things from the git go instead of learning an inferior way and the having to re-learn. If this was a C language course then ok but it isn't apparently.
Programming homework and newbie help thread Quote
07-02-2016 , 08:10 PM
Quote:
Originally Posted by LBloom
Are you talking about making UML diagrams here?
yup
Programming homework and newbie help thread Quote
07-02-2016 , 09:40 PM
Following this discussion and thinking about my program has helped me realize there is a reason for the "if" I asked about earlier. At least as written, I need it to prevent outputting the names and prices of items that the user did not order.
Programming homework and newbie help thread Quote
07-03-2016 , 09:04 AM
Quote:
Originally Posted by LBloom
Following this discussion and thinking about my program has helped me realize there is a reason for the "if" I asked about earlier. At least as written, I need it to prevent outputting the names and prices of items that the user did not order.
How is your program determining if something is ordered or not? Is it just being fed a list if things that were ordered or?

Sent from my SM-G900R4 using Tapatalk
Programming homework and newbie help thread Quote
07-03-2016 , 04:26 PM
Quote:
Originally Posted by adios
...do things from the git go...
ICWUDT
Programming homework and newbie help thread Quote
07-07-2016 , 12:00 AM
Haven't really researched this question yet, which I plan to do while being insomniac tonight, but how exactly does one use, say, rails in API mode to power a SPA app based off angular or react or whatever?

I know how rails normally separates the V from the MC, but I'm curious how it integrates in the above scenario, particularly from a deployment perspective. Since I know so little about front end frameworks I'm not even sure how they're used. Do they just go in one folder that gets served up via heroku? (assuming deployment on something that's set and forget like heroku in this case)

edit

very outdated, but reading through this, hopefully i'll get some answers.

https://www.angularonrails.com/deplo...cation-heroku/

Last edited by Loki; 07-07-2016 at 12:18 AM.
Programming homework and newbie help thread Quote

      
m