|
![]() |
Overclock.net - Overclocking.net > Software, Programming and Coding > Coding and Programming | |
C++ array packing...
|
||
![]() |
|
|
LinkBack | Thread Tools |
|
|
#1 (permalink) | |||||||||||
|
Programmer
|
Alright, so here is what i have going...
list[50]; i want to ask the user what array element they want to delete. i am thinking something like this. cin << i; for (i >=0 ; i <=50 ; i++) { list[i +1 ] = list[i] } And thats all i have going... cant think of how to do this. The idea is that i have a static array of 50 elements. Then ask the user what element they want to delete. I am not using any type of "new" or "delete" commands, so what i need to do is just over write that array element that needs to be deleted, by the element to its right... And then stack the array by moving over all the other elements over from the right of the deleted element, to the left... I should know this on. I am having a huge brain fart on this one... HELP!!!!
__________________
My Lego case thread. With PICS!!! ----------------------------------------------------------------------- Video card RMA database thread. I am working on an application that allows users to input their cards issues into a database, to build a knowledge base for what types of cards have a lower fail rate.
|
|||||||||||
|
|
|
|
#2 (permalink) | |||||||||||||
|
Off By 340 Undecillion
|
Close, you have the right idea, but with the way your code is now you would get a couple of syntax errors and move everything to the left, eventually deleting element 50.
Here, this should work: Code:
cin >> i;
for (i; i == 50; ++i) //for starts at i, increases until i == 50
{ //and saying ++i is more efficient than i++ in most cases
list[i] = list[i + 1] //The ith element gets the value from i+1, thus shifting everything to the left.
}
__________________
Congratulations! You have found the secret text! You get a cookie.
|
|||||||||||||
|
|
|
|
#3 (permalink) | |||||||||||
|
Programmer
|
well, i do want to keep the list[50] at 50 elements. So after this loop runs, could i just say something like:
null = list[50]; ??? from my understanding, and the fact that what i am writing this for isn't using pointers, we are passing each array element by its value, and not by reference. With this said, when i move the value of list[50] into list[49], what is inside list[50]?? nothing? null?? the same as list[49] after i copied the value from list[50]??
__________________
My Lego case thread. With PICS!!! ----------------------------------------------------------------------- Video card RMA database thread. I am working on an application that allows users to input their cards issues into a database, to build a knowledge base for what types of cards have a lower fail rate.
|
|||||||||||
|
|
|
|
#4 (permalink) | |||||||||||
|
Programmer
|
Alright, now im lost... I thought i could figure it out once i had the loop for the array issue, now im all mest up...
I have this app running, minus the 'deleteFoodIndex()' and the 'calculateMeal' functions... The deleteFoodIndex just identifies the element that the user wants to delete, and moves over all the elements from its right, over one place, essentially moves over the elements over the element that the user wants to remove... The calculateMeal function needs to do the following: asks the user what muliple foods they want to add together, then take those foods identified by the user, and add up each elements' properties together... sample. add food: Chicken food type: 3 calories: 32 carbohydrates: 52 fat: 45 cholesterol: 69 sodium: 12 protein: 56 Add another food... add food: steak food type: 0 calories: 56 carbohydrates: 48 fat: 17 cholesterol: 48 sodium: 56 protein: 89 So now we have two foods in the array. list[0] is the chicken, and list[1] is the steak... How do i add up each of the calories, carbohydrates,fat, cholesterol, sodium, protein, from each food?? -------------------- Foods placed in meal: chicken, steak combined properties calories: 88 carbohydrates: 100 fat: 62 cholesterol: 117 sodium: 68 protein: 145 The function needs to add up all the user entered foods from the meal function, not from the add function, and add up these things... not sure how to do this, and for this assignment, i cant use pointers!! Damn it!!!
__________________
My Lego case thread. With PICS!!! ----------------------------------------------------------------------- Video card RMA database thread. I am working on an application that allows users to input their cards issues into a database, to build a knowledge base for what types of cards have a lower fail rate.
|
|||||||||||
|
|
|
|
#5 (permalink) | |||||||||||||||
|
Kernel Sanders
|
Quote:
he wants Code:
cin>>i; while(i<49) list[i]=list[++i]; list[i]=NULL; Quote:
|
|||||||||||||||
|
|
|
|
#6 (permalink) | |||||||||||
|
Overclocker
|
// list[0] is chicken
//list[1] is steak //list[2] is added String names[1] = {firstFood, secondFood}; int list[2][6]; for(int i = 0; i<list[0].length(); i++) { list[2][i] = list[i][0]+list[i][1]; } //display
__________________
Last edited by B-80 : 04-25-08 at 03:31 AM. |
|||||||||||
|
|
|
|
|
#7 (permalink) | |||||||||||
|
Programmer
|
Ok, I think I just confused everyone. I'm typing this on my phone, so I can't upload the cpp file.
There is an enum with the food types in it. (meat = 1, fish = 2, pultury = 3, etc...) I have a struct with the foods characteristics in it. (calories, fat, cholesterol, etc...) I also have a list[50] in witch each element has each defined struct in it... So for example: List[0] has the data from the whole struct in it for chicken. List[1] has the whole struct for steak... What I news to figure out is how to get inside each struct through a list[] argument... Add all elements (calories, fat, cholesterol, etx...) of list[0] and list[1] together... But in a way that the user tells the app what list elements they want to add up. Anywhere from 2 up to how ever many enterys there are for foods imputed... I'm grabbing some food now, I'll be back in a few to update this with my code and prototype. I have no idea...
__________________
My Lego case thread. With PICS!!! ----------------------------------------------------------------------- Video card RMA database thread. I am working on an application that allows users to input their cards issues into a database, to build a knowledge base for what types of cards have a lower fail rate.
Last edited by JoBlo69 : 04-25-08 at 07:57 AM. |
|||||||||||
|
|
|
|
#8 (permalink) | |||||||||||
|
Programmer
|
Alright, so I'm back on my PC and I'm posting the code...
Code:
#include <iostream>
#include <cctype>
using namespace std;
const int SIZE = 100;
const int CAPACITY = 100;
enum Category
{
unknown = -1, meat, poultry, seafood, dairy, vegetable,
fruit, grain, sweet, nCategory
};
struct foodType
{
char name[SIZE]; // name of the food
Category category; // what kind of food
float calories; // calories
float carbohydrates; // grams
float fat; // grams
float cholesterol; // grams
float sodium; // grams
float protein; // grams
};
//Prototypes
void showMenu(); //Displays initial database options
void executeCommand (char command, foodType list[],int &size); //case statment that executes command choices for the database options
void addFood(foodType list[], int& size); //adds food choices to the array
void listFood(const foodType list[], int size); //lists food on the screen
void calculateMeal(); // Need to figure out how to calculate each food entry's atributes
char getCommand(); //command choice from database options
void deleteFoodIndex(int i);
int readInteger(char prompt[]);
//-------------------------------------------------------------------
//-------------------------------------------------------------------
//-------------------------------------------------------------------
int main ()
{
foodType foodList[CAPACITY];
char menuOption = '1';
int listSize = 0;
showMenu();
while(menuOption != 'q')
{
menuOption = getCommand();
executeCommand(menuOption, foodList, listSize);
}
cout << "Thank you for using nutricalc 1.0 " << endl;
}
//-------------------------------------------------------------------
//-------------------------------------------------------------------
//-------------------------------------------------------------------
//functions
void showMenu()
{
cout << "Nutricalc 1.0" << endl;
cout << "Implemented by: JoBlo" << endl;
cout << "" << endl;
cout << "" << endl;
cout << "edit food database" << endl;
cout << " Commands: a(dd), l(ist), d(elete) index, m(eal), q(uit)" << endl;
cout << "-----------------------------------------------------------" << endl;
cout << "" << endl;
cout << "> ";
}
char getCommand()
{
char option;
cin.get(option);
cin.ignore(100, 'n');
return option;
}
void executeCommand (char command, foodType list[], int &size)
{
switch (command)
{
case 'a':
case 'A':
addFood(list, size);
break;
case 'l':
case 'L':
listFood(list, size);
break;
case 'd':
case 'D':
//deteteFoodIndex();
case 'm':
case 'M':
//calculateMeal(list, size);
default:
cout<< "Illegeal command" <<endl;
}
}
void addFood(foodType list[], int& size)
{
foodType aFood;
char ch;
cout << "name: > ";
cin.get(aFood.name, SIZE, 'n');
cin.ignore(100, 'n');
aFood.category = static_cast <Category> (readInteger("meat = 0, poultry =1, seafood = 2, dairy = 3, vegetable = 4, fruit = 5, grain = 6, sweet = 7")); //I can get this part to work, but not able to get it to diplay correctly. Ideas??
aFood.calories = readInteger("calories: > ");
aFood.carbohydrates = readInteger("carbohydrates: > ");
aFood.fat = readInteger("fat: > ");
aFood.cholesterol = readInteger("cholesterol: > ");
aFood.sodium = readInteger("sodium: > ");
aFood.protein = readInteger("protein: > ");
cout << "" << endl;
cout << "> ";
list[size++] = aFood;
}
void listFood(const foodType list[], int size)
{
for (int i = 0; i < size; i++)
{
cout << "" << endl;
cout << "index: " << i << endl;
cout << "name: " << list[i].name << endl;
cout << "category: " << list[i].category << endl;
cout << "calories: " << list[i].calories << ".00 grams" << endl;
cout << "carbohydrates: " << list[i].carbohydrates << ".00 grams" << endl;
cout << "fat: " << list[i].fat << ".00 grams" << endl;
cout << "cholesterol: " << list[i].cholesterol << ".00 grams" << endl;
cout << "sodium: " << list[i].sodium << ".00 grams" << endl;
cout << "protein: " << list[i].protein << ".00 grams" << endl;
cout << "" << ".00 grams" << endl;
cout << "> " ;
}
}
int readInteger(char prompt[])
{
int temp;
cout << prompt;
cin >> temp;
while(!cin)
{
//clear the error code of cin
cin.clear();
//remove the garbage on the input stream
cin.ignore(100, 'n');
//ask user to try again
cout << "Illegal integer! Try again: ";
//read in
cin >> temp;
}
//remove the 'n'
cin.ignore(100, 'n');
return temp;
}
void calculateMeal()
{
}
void deleteFoodIndex()
{
int i;
cout << "Please enter food index to delete:" << endl;
cin >> i;
//for (i >= 0 ; i =< 50 ; x++);
{
}
}
__________________
My Lego case thread. With PICS!!! ----------------------------------------------------------------------- Video card RMA database thread. I am working on an application that allows users to input their cards issues into a database, to build a knowledge base for what types of cards have a lower fail rate.
|
|||||||||||
|
|
|
|
#9 (permalink) | |||||||||||
|
Programmer
|
i need help with these two functions:
calculateMeal() deleteFoodIndex() Thanks for the help... Let me know if anyone needs clarification on what these two functions do.
__________________
My Lego case thread. With PICS!!! ----------------------------------------------------------------------- Video card RMA database thread. I am working on an application that allows users to input their cards issues into a database, to build a knowledge base for what types of cards have a lower fail rate.
|
|||||||||||
|
|
|
|
#10 (permalink) | |||||||||
|
Overclocker
|
In all honesty, I think your code is very very messy. Actually, I'm not sure if it will even work at all.
__________________You have: Code:
int main ()
{
foodType foodList[CAPACITY];
...
}
Code:
void deleteFoodIndex(foodtype &list)
{
cin >> i;
for ( ; i<50; i++)
{
if (i<49)
{
*list[i] = *list[i+1];
}
else
{
*list[i] = NULL; //Or whatever you want to use as a blank
}
}
|
|||||||||
|
|
|
![]() |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
|
|