Hello all this is my first post here. I just started my first semester at Penn State for Software Engineering and I have a class on C++ right now. (Don't worry this isn't homework haha.)
Well my mom constantly has me do some math conversions for her business. She makes makes her products and uses metric weights to do so. She needs me all the time to convert them into a certain number of Ounces. So I wanted to make her a simple program to has her input the ingredients weights and then input the final weight she wants them converted to and display the results. The thing I have a problem with is each recipe has a different number of ingredients. So how would I have it prompt for a variable number of variables (sorry haha). And how would I store them?
Im assuming I'd use a loop statement with a break command or something, but im not to sure. Now in my programming class we haven't gotten to far, so for storing the ingredients i'm at a loss too because the number in each calculation always changes. Im assuming I could use arrays after reading my book some, but I'm not to sure how.
I know all the calculations so I don't necessarily need help with that.
Any input would be greatly appreciated. Thanks so much =)
You can do something like this to get the input into a vector:
You will need #include <vector>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
std::vector <int> ingredients_vect; //new vector
int usr_input, ingredient_weight;
std::cout << "Enter how many ingredients to convert: ";
std::cin >> usr_input;
for (int i = 0; i < usr_input; i++)
{
std::cout << "Enter ingredient " << i + 1 << "'s weight." << std::endl;
std::cin >> ingredient_weight;
ingredients_vect.push_back(ingredient_weight); //adds that input to the vector - push_back()
}
for (unsignedint y = 0; y < ingredients_vect.size(); y++)
{
std::cout << ingredients_vect[y] << std::endl; //prints out [y] in the vector - like an array
}
After that you can create some functions and prompt for which conversion user wants and then apply that function to the integers inside the vector.
(i.e - convertOunces(int x) { return x * OUNCE_FORMULA })
Thank you so much. This is exactly what I was looking for =) The conversions usually only need done in 1, 2, 6, 8, or 16 oz. Would a switch statement be the best bet for this?
To simplify input enter weights in grams
As firedraco suggested use two standard vectors to store the paired data. (ingredient and weight)
Consider the following:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
int wt=0;//weight of ingredients in gms.
string ingred=" ";
cout<<"enter the ingredient and weight (gms)";
cin>>ingred>>wt;
//1 oz = 1000/(16*2.2) = 28.4gm
wt/=28.4//converts wt to ozs
//Now load two vectors
vector<string>name;//ingredient
vector <int> mass;//wt in ozs
while(cin>>name && name !="exit")
{
name.push_back(ingred);
while (cin>>wt)
mass push_back(wt);
}
Now print out the data pairs using nested for loops.
Not too elegant and obviously capable of improvement but may do the job
Thanks you guys have been very helpful. Vectors are turning out to be very useful =)
buffbill - with that code would the user just keep inputting ingredients and weights until they type exit then it continues? and thanks a lot =) I just needed something that works, no need for anything fancy =).
Yes that`s right............
But if you think about it there is no need for vectors or arrays. Just type in the ingredient followed by a space and then the weight in grammes and press enter. The program will perform and print the conversion on the same line.QED
1 2 3 4 5 6 7 8 9 10
int wt=0;//weight of ingredients in gms.
string ingred=" ";
cout<<"enter the ingredient and weight (gms)";
cin>>ingred>>wt;//separated by a space
//1 oz = 1000/(16*2.2) = 28.4gm // i think!
wt/=28.4//converts gms to ozs
while(cin>>name && name !="exit")
{
cout<<" "<<wt<<endl;
}
Man am I pleased I saw this before someone like helios demolished me!
Except it does not print the ounces on the same line......sorry.