This program choose any bin of same number of parts from 10 bins. When the program chose one, it ask if we want to either add or remove parts from that specific bin. How can I get a element from the structure array.
//Structure
struct Inventory
{
char description[35];
int num;
};
//Function Prototypes.
void choiceMenu(Inventory[], int);
void AddParts(Inventory[], int);
void RemoveParts(Inventory[]);
int main()
{
char election;
int choice;
const int Number_Bins = 10;
Inventory parts[Number_Bins] = {
{"Valve", 10},
{"Bearing", 5},
{"Bushing", 15},
{"Coupling", 21},
{"Flange", 7},
{"Gear", 5},
{"Gear Housing", 5},
{"Vacuum Gripper", 25},
{"Cable", 18},
{"Rod", 12}
};
Is there other way to do it without putting the elements of the arrays from 0 to 9. like trying to do it with a accumulator. how can I take a specific element from the array.
void choiceMenu(Inventory bin[], int z)
{
cout << " Inventoy Bins\n";
cout << " = = = = = = = = \n";
cout << " *Choose the part of your preference.\n";
cout << " 1. Valve" << bin[0].num << endl;
cout << " 2. Bearing. Currently Number of Bearing = " << bin[1].num << endl;
cout << " 3. Bushing. Currently Number of Bushing = " << bin[2].num << endl;
cout << " 4. Coupling. Currently Number of Coupling = " << bin[3].num << endl;
cout << " 5. Flange. Currently Number of Flange = " << bin[4].num << endl;
cout << " 6. Gear. Currently Number of Gear = " << bin[5].num << endl;
cout << " 7. Gear_Housing" << bin[6].num << endl;
cout << " 8. Vacuum_Gripper" << bin[7].num << endl;
cout << " 9. Cable. Currently Number of Cable = " << bin[8].num << endl;
cout << " 10. Rod. Currently Number of Rod = " << bin[9].num << endl;
cout << " 11. Choose 11 to quit the Program" << endl;
}
I'm thinking you're asking how to pass one of the array elements to a function that adds or removes parts. You can do that by using a reference variable (http://www.cprogramming.com/tutorial/references.html).
For example, in your case, this function would remove a part:
void RemovePart(Inventory& part)
{
if (part.num > 0)
part.num -= 1;
cout << part.description << " now has " << part.num << " parts." << endl;
}
Then you can call that function with an array element. For example, this removes a Bearing:
RemovePart(bin[1]);
Related
its a text based monopoly game where i need the dice to select the number from the array like on a board.
I have the number generator, what i need to do though is when the value comes up it pluses it on the array to get the matching number so for example if the players rolls a 6, the 6 + array 0 = array value 6 which will be a name of a street but it means the player knows which place on the made up board they are on. here is the coding i am using to try and do so but i keep on getting 006ff65 what ever. i how can i get it for showing just the number as the names will be added later.
{
int main()
{
int number = 12;
int rnum = (rand() % number) + 1;
int house = 1;
int moneyscore = 10000;
double values[] = {
1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40 };
char name[50];
cout << "Who are you, Dog, Car, Hat or Bus" << endl;
cin.getline(name, 50);
cout << "Welcome to Our Game " << name << " You have " << moneyscore << " .PLease Roll dice to get started" << endl;
cout << "\n----------------------Press any Enter to roll dice----------------------" << endl;
system("cls");
int choiceOne_Path;
cout << "# You roll a " << rnum << endl;
rnum = values[rnum];
cout << "# you have " << moneyscore << endl;
cout << "# You move to grid "<< values << endl;
cout << "\t >> Enter '1' Buy Property" << endl;
cout << "\t >> Enter '2' Recieve Rent" << endl;
cout << "\t >> Enter '3' End turn" << endl;
retry:
cout << "\nEnter your choice: ";
cin >> choiceOne_Path;
if (choiceOne_Path == 1)
{
cout << "\n Buy Property " << endl;
cout << " " << name << " has " << moneyscore << endl;
cout << " " << house <<" House has been placed by " << name <<" who spent 2,500" << endl;
moneyscore -= 2500;
cout << " " << name << " now has " << moneyscore << endl;
cout << "\n Roll again" << endl;
cout << "# You roll a " << rnum << endl;
}
else if (choiceOne_Path == 2)
{
cout << "\n You recieved 2500 from rent" << endl;
moneyscore += 2500;
cout << " " << name << "\n now has" << moneyscore << endl;
cout << "\n(Player will gain money form house, will need to find a way in order to make the
console remember what score == to postion)" << endl;
cout << "Ends turn" << endl;
}
else if (choiceOne_Path == 3)
{
cout << "\n Roll again" << endl;
cout << "# You roll a " << rnum << endl;
}
else
{
cout << "You are doing it wrong, player! Press either '1' or '2', nothing else!" << endl;
goto retry;
}
cout << "\n----------------------Press any key to continue----------------------" << endl;
_getch();
}
}
As far as I know, you should use srand (time(NULL)); between every call to rand() to correctly return a new random number from every call.
"srand" initialize the random number generator using a seed. In this case seed is time, which should be different on every call.
Pretty basic. You either made a few typos or need to learn how arrays work (and program flow, and subroutines, but perhaps that is for later lessons.)
First you are assigning the result of the array lookup back into your random number: rnum = values[rnum]; which is not a big deal except you use that variable later and it no longer contains what you may think it does. It actually contains the value you are looking for!
Second the variable values is a pointer to the head of your array so you are outputting the address of the values array with this line: cout << "# You move to grid "<< values << endl; there is no array look up happening at all here. It is strange you missed that because you did reference the array contents properly when you replaced the random number value earlier.
I understand how Arrays work despite their dimensions, but I can't make it come together in code without this basic information. As you can tell I tried multiple ways starting with line 13, but they all failed.
#include <iostream>
#include <math.h>
#include <string>
using namespace std;
int main()
{
int MasterArray [3][3][2]; // Declaration of the Array
int one = 0, two = 0, three = 0; // Declaration of Variables
cout << "Would you like to edit class 1 or 2?" << endl ; //Ask for input
cin >> one; // store input in the variable named one
one -=1; // Since c++ is 0 based input - 1 = one
MasterArray[3][3][2] = {{one, two, three}, {one, two, three}, {one, two}};
// Above line attempt to store variable one in array
// Rinse and repeat this process for lower lines, but storing is all that matters.
cout << "Which student account would you like to access 1, 2, or 3?";
cin >> two;
two -= 1;
MasterArray[3][3][2] = [one][two][three];
cout << "Which grade would you like to edit 1, 2, or 3?";
cin >> three;
three -= 1;
MasterArray[3][3][2] = [one][two][three];
cout << MasterArray[one][two][three];
return 0;
}
Multi-dimensional arrays in c++ are a bit more complicated and require extra work so using a multidimensional vector would probably be better for you.
vector<vector<vector<int> > > yourVector;
for(int i = 0; i<3; i++)
for(int j = 0; j < 3; j++)
for(int k = 0; k <3; k++)
yourVector[i][j][k] = i + j +k
cout << yourVector[1][2][3]; //prints 6
I figured out my answer last night. This is the updated code if anyone is interested. Thanks for trying to help everyone.
#include <iostream>
#include <math.h>
#include <iomanip>
using namespace std;
int main()
{
int one = 0, two = 0, three = 0, x = 0;
int MasterArray [3][3][2] = {three, two, one};
float A1 = 0, A2 = 0, A3 = 0, A4 = 0, A5 = 0, A6 = 0;
for (int i = 0;i <18; i++)
{
cout << "Enter a class number 1 or 2 : ";
cin >> one;
one -= 1;
cout << "Enter a student number 1, 2, or 3: ";
cin >> two;
two -= 1;
cout << "Enter the test number 1, 2, 3 : ";
cin >> three;
three -= 1;
cout << "Enter a grade for test number " << three << ": ";
cin >> x;
MasterArray[three][two][one] = x;
cout <<"Class number: " << one + 1 << " Student number: " << two + 1 << " Test Number: " << three + 1 << " Grade: " << MasterArray[three][two][one] << endl << "\n";
}
//Math
cout.precision(2), cout << fixed;
A1 = MasterArray[0][0][0]*MasterArray[0][0][1]*MasterArray[0][0][2]/3;
A2 = MasterArray[0][1][0]*MasterArray[0][1][1]*MasterArray[0][1][2]/3;
A3 = MasterArray[0][2][0]*MasterArray[0][2][1]*MasterArray[0][2][2]/3;
A4 = MasterArray[1][0][0]*MasterArray[1][0][1]*MasterArray[1][0][2]/3;
A5 = MasterArray[1][1][0]*MasterArray[1][1][1]*MasterArray[1][1][2]/3;
A6 = MasterArray[1][2][0]*MasterArray[1][2][1]*MasterArray[1][2][2]/3;
cout << "Class" << setw(10) << "Student" << endl;
cout << "Number" << setw(8) << "Number" << setw(8) <<"Grade 1" << setw(8) <<"Grade 2" << setw(8) <<"Grade 3" << setw(8) << "Average" << endl;
// class 1
cout << "1" << setw(8) << "1" << setw(8) << MasterArray[0][0][0] << setw(8) << MasterArray[0][0][1] << setw(8) << MasterArray[0][0][2] << setw(12) << A1 << endl;
cout << setw(9) << "2" << setw(8) << MasterArray[0][1][0] << setw(8) << MasterArray[0][1][1] << setw(8) << MasterArray[0][1][2] << setw(12) << A2 << endl;
cout << setw(9) << "3" << setw(8) << MasterArray[0][2][0] << setw(8) << MasterArray[0][2][1] << setw(8) << MasterArray[0][2][2] << setw(12) << A3 << endl;
cout << "--------------------------------------------------------------------------------";
// class 2
cout << "2" << setw(8) << "1" << setw(8) << MasterArray[1][0][0] << setw(8) << MasterArray[1][0][1] << setw(8) << MasterArray[1][0][2] << setw(12) << A4 << endl;
cout << setw(9) << "2" << setw(8) << MasterArray[1][1][0] << setw(8) << MasterArray[1][1][1] << setw(8) << MasterArray[1][1][2] << setw(12) << A5 << endl;
cout << setw(9) << "3" << setw(8) << MasterArray[1][2][0] << setw(8) << MasterArray[1][2][1] << setw(8) << MasterArray[1][2][2] << setw(12) << A6 << endl;
return 0;
}
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I was doing an assignment for my subject here and in the assignment, every student is required to create a simple C++ program. The problem here is, when I do not assign a default to a variable, a compile error will occur. However, when I assign a default value (in this case, value = 0), the value will always be 0.
My question is, is there any way to solve this problem while not encountering the compile error?
If there was a similar question asked, could you please also, include the link to the solved question as well? Thank you very much!
[Edit #1: To those who wonder why the code is "messy", I only used simple commands and did not use object oriented components, if/else statements etc. The project created was meant to be composed of "simple codes".]
[Edit #2: This is the output display that I snapped:
http://i.imgur.com/4csk1Rz.png
The Total Discounted fee part should be displaying numbers instead of default value 0]
(Microsoft Visual Studio Pro 2013; C++)
Expected output display:
http://i.imgur.com/jOJvymV.png
#include "stdafx.h"
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
int main()
{
/* Declare variables */
int qtyVehicleCar, qtyVehicleTaxi, qtyVehicleTruck; // User input
double feeOriginalCar, feeTotalOriginalCar, rateDiscountCar, feeTotalDiscountedCar, feeTotalAfterDiscountedCar; // variables for vehicle Car
double feeOriginalTaxi, feeTotalOriginalTaxi, rateDiscountTaxi, feeTotalDiscountedTaxi, feeTotalAfterDiscountedTaxi; // variables for vehicle Taxi
double feeOriginalTruck, feeTotalOriginalTruck, rateDiscountTruck, feeTotalDiscountedTruck, feeTotalAfterDiscountedTruck; // variables for vehicle Truck
double feeRepTotalOriginalFee, feeRepTotalDiscountedFee, feeRepTotalAfterDiscounted; // Generate final output
cout << "=====================================\n";
cout << "=====================================\n";
cout << "====== Malaysia Highway Company =====\n";
cout << "=====================================\n";
cout << "=====================================\n";
cout << endl;
cout << "Enter quantity of the vehicles on 1st January 2014: " << endl;
cout << "Car\t\t:\t";
cin >> qtyVehicleCar;
cout << "Taxi\t\t:\t";
cin >> qtyVehicleTaxi;
cout << "Truck\t\t:\t";
cin >> qtyVehicleTruck;
/* Lay out the Report table */
cout << endl << endl << endl;
cout << "Report\n";
cout << "--------\n";
cout << endl;
/* ============================== */
/* Declaration prior to process */
/* ============================== */
/* The problematic part
feeTotalDiscountedCar = 0;
feeTotalDiscountedTaxi = 0;
feeTotalDiscountedTaxi = 0;
*/
// Prices
feeOriginalCar = 2.00;
feeOriginalTaxi = 1.00;
feeOriginalTruck = 3.50;
feeTotalOriginalCar = feeOriginalCar * qtyVehicleCar;
feeTotalOriginalTaxi = feeOriginalTaxi * qtyVehicleTaxi;
feeTotalOriginalTruck = feeOriginalTruck * qtyVehicleTruck;
// In percentage
rateDiscountCar = 2;
rateDiscountTaxi = 10;
rateDiscountTruck = 15;
feeTotalAfterDiscountedCar = (rateDiscountCar/100) * feeTotalOriginalCar;
feeTotalAfterDiscountedTaxi = (rateDiscountTaxi/100) * feeTotalOriginalTaxi;
feeTotalAfterDiscountedTruck = (rateDiscountTruck/100) * feeTotalOriginalTruck;
// Final total - to be displayed
feeTotalAfterDiscountedCar = feeTotalOriginalCar - feeTotalAfterDiscountedCar;
feeTotalAfterDiscountedTaxi = feeTotalOriginalTaxi - feeTotalAfterDiscountedTaxi;
feeTotalOriginalTruck = feeTotalOriginalTruck - feeTotalAfterDiscountedTruck;
feeRepTotalOriginalFee = feeTotalOriginalCar + feeTotalOriginalTaxi + feeTotalOriginalTruck;
feeRepTotalDiscountedFee = feeTotalDiscountedCar + feeTotalAfterDiscountedTaxi + feeTotalAfterDiscountedTruck;
feeRepTotalAfterDiscounted = feeRepTotalOriginalFee - feeRepTotalDiscountedFee;
/* ================= */
/* Declaration END */
/* ================= */
/* Gives the following variables a default value, they will be modified later by the program */
feeTotalAfterDiscountedTruck = 0;
feeTotalDiscountedTruck = 0;
/* Processes */
cout << "Item\t\t\t\t" << "Car\t" << "Taxi\t" << "Truck\t" << endl;
cout << "------------------------------------------------------\n";
cout << "Original fee\t\t\t" << "RM " << feeOriginalCar << "\t" << "RM " << feeOriginalTaxi << "\t" << "RM " << feeOriginalTruck << endl;
cout << "Quantity\t\t\t" << qtyVehicleCar << "\t" << qtyVehicleTaxi << "\t" << qtyVehicleTruck << endl;
cout << "Total original fee\t\t" << "RM " << feeOriginalCar << "\t" << "RM " << feeOriginalTaxi << "\t" << "RM " << feeOriginalTruck << endl;
cout << "Discount rate\t\t\t" << rateDiscountCar << "%\t" << rateDiscountTaxi << "%\t" << rateDiscountTruck << "%\t" << endl;
cout << "Total discounted fee\t\t" << "RM " << feeTotalDiscountedCar << "\t" << "RM " << feeTotalDiscountedTaxi << "\t" << "RM " << feeTotalDiscountedTruck << endl;
cout << "Total after discounted fee\t" << "RM " << feeTotalAfterDiscountedCar << "\t" << "RM " << feeTotalAfterDiscountedTaxi << "\t" << "RM " << feeTotalAfterDiscountedTruck << endl;
cout << endl;
/* Displays output */
cout << "Total original fee\t\t:\t" << "RM " << feeRepTotalOriginalFee << endl;
cout << "Total discounted fee\t\t:\t" << "RM " << feeRepTotalDiscountedFee << endl;
cout << "Total after discounted fee\t:\t" << "RM " << feeRepTotalAfterDiscounted << endl;
cout << endl << endl;
/* Displays end line */
cout << "------------------- End of Program -------------------" << endl;
system("PAUSE");
return 0;
}
An uninitialized variables warning can mean either (1) you forgot to initialize something or (2) you thought you initialized it but didn't. Looks to me that in this case it's the second problem. Your variable names are mixed up.
For example,
feeTotalAfterDiscountedCar = (rateDiscountCar/100) * feeTotalOriginalCar;
feeTotalAfterDiscountedTaxi = (rateDiscountTaxi/100) * feeTotalOriginalTaxi;
feeTotalAfterDiscountedTruck = (rateDiscountTruck/100) * feeTotalOriginalTruck;
These are supposed to be the amounts of the discounts, no? So why are you storing them into feeTotalAfterDiscountedSomething rather than feeTotalDiscountedSomething?
Similarly,
feeTotalAfterDiscountedCar = feeTotalOriginalCar - feeTotalAfterDiscountedCar;
feeTotalAfterDiscountedTaxi = feeTotalOriginalTaxi - feeTotalAfterDiscountedTaxi;
feeTotalOriginalTruck = feeTotalOriginalTruck - feeTotalAfterDiscountedTruck;
Why is the third one storing the result into feeTotalOriginalTruck instead of feeTotalAfterDiscountedTruck? (Note also that if you fix the first problem above, the rhs of the subtraction will also need to change).
Also,
feeRepTotalDiscountedFee = feeTotalDiscountedCar + feeTotalAfterDiscountedTaxi + feeTotalAfterDiscountedTruck;
Similar problem. These should, I believe, all be feeTotalDiscountedSomething rather than feeTotalAfterDiscountedSomething.
The compiler rightfully complains that feeTotalDiscountedCar and feeTotalDiscountedTaxi are uninitialized. You need to initialize them somewhere. Maybe you want the user to enter them somewhere like this:
cout << "Discount Fee Total Taxi:";
cin >> feeTotalDiscountedTaxi;
cout << "Discount Fee Total Car:";
cin >> feeTotalDiscountedCar;
why are you setting them to zero just before printing out?
feeTotalAfterDiscountedTruck = 0;
feeTotalDiscountedTruck = 0;
It will show the error because you are trying to use the variable with out initializing it.
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.
I'm doing some self study on C++ and have just being doing some chapter on arrays, loops etc. There are a bunch of exercises and the one I'm referencing is quite simple. Initialise two matrices of two rows and three columns.
Output the contents of the matrices (formatted as specified), then perform an addition which is held in a third matrice. Output the third array with the addition done. The code I have works but I'm thinking there's a better way to do the output rather than address each matrice element? I'm thinking of another loop given this is the chapter preceding the exercise, or is this way acceptable?
#include <iostream>
#include <string>
using namespace std;
int main()
{
int amatrix[2][3]=
{
{-5, 2, 8},
{1, 0, 0},
};
int bmatrix[2][3]=
{
{1, 0, 2},
{0, 3, -6},
};
int cmatrix[2][3]=
{
{0, 0, 0},
{0, 0, 0},
};
//add generated matrices
for (int i = 0; i <= 1; i++)
{
for (int j =0; j <= 2; j++)
{
cmatrix[i][j]=amatrix[i][j]+bmatrix[i][j];
}
}
//output to screen - NEED ADVICE FROM HERE
cout << "A= " << endl;
cout << amatrix[0][0] << ", " << amatrix[0][1] << ", " << amatrix[0][2] << endl;
cout << amatrix[1][0] << ", " << amatrix[1][1] << ", " << amatrix[1][2] << endl << endl;
cout << "B= " << endl;
cout << bmatrix[0][0] << ", " << bmatrix[0][1] << ", " << bmatrix[0][2] << endl;
cout << bmatrix[1][0] << ", " << bmatrix[1][1] << ", " << bmatrix[1][2] << endl << endl;
cout << "C= " << endl;
cout << cmatrix[0][0] << ", " << cmatrix[0][1] << ", " << cmatrix[0][2] << endl;
cout << cmatrix[1][0] << ", " << cmatrix[1][1] << ", " << cmatrix[1][2] << endl << endl;
}
cout << amatrix[i][j] in a for loop