• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 4167
  • Last Modified:

printing a 2 dimensional array

hi im making a little program that should simulate what items are in a fridge and so on. i have made 3 items that should be in it, and after that you should be able to add more. please remember that this is only a simulation. im getting these errors:
2  In file included from koleskab.cpp
the items i have put into my array are not declared in this scope. honestly i must say that i have no idea what scope it means when it is from the header the error comes from.

7 koleskab.h too many initializers for `char[1]'
i have 3 of these errors which says the same

in function "int main()"
20 koleskab.cpp invalid conversion from `char' to `char (*)[1]'
20 koleskab.cpp   initializing argument 1 of `void udskriv(char (*)[1])'

and just for the record koleskab = refidgerator.

most of it is in english but there are still some danish in it.

and just so you know, this is not an assignment from a book or a class
#include <iostream>
#include "koleskab.h"
 
using namespace std;
 
int main()
{
    int valg = 0;
    
    cout<<"---------MENU:---------"<<endl<<endl;
    
    cout<<"Press 1 to view a list of products."<<endl;
    cout<<"Press 2 to change a product."<<endl;
    cout<<"Press 3 to add new products."<<endl<<endl;
    cin>>valg;
    
    switch(valg)
    {
                case 1:
                     udskriv(items[reakke][kolonne]);
                break;
}
    system("PAUSE");
    return 0;
}
 
void udskriv(char item[reakke][kolonne])
{
     for(char i = 0; i<19;i++)
     {
             for(int n = 0;n<1;n++)
             {
             cout<<item[i][n];
             }
     }
}
 
// and my header file koleskab.h:
const char reakke = 19;
const int kolonne  = 1;
char items[reakke] [kolonne]= {
                   {cheese, 0},
                   {water, 1},
                   {milk, 2}
};
void udskriv(char items[reakke] [kolonne]);
void change(char items[reakke] [kolonne]);
void new_product(char items[reakke] [kolonne]);
 
// i do know that i shouldn't use header files but it makes it easier for me ;)

Open in new window

0
smurf_killer
Asked:
smurf_killer
  • 24
  • 20
5 Solutions
 
Infinity08Commented:
Why did you make reakke a char ?

        const char reakke = 19;

Make it an int like kolonne.


Btw, I think I already told you that it's a bad idea to define variables in a header file !

        char items[reakke] [kolonne]= {
                   {cheese, 0},
                   {water, 1},
                   {milk, 2}
        };

items is a 2D array of chars. 0, 1 and 2 can be interpreted as chars, but I'm not sure that's what you intended.
cheese, water and milk however are not defined ... What are they supposed to be ?

Did you intend something like this :

        char *items[num] = { "cheese", "water", "milk" };
0
 
smurf_killerAuthor Commented:
i do know that its a bad idea to define variables in a header file, but i works better for me that way. that array is supposed to store some things you have in a fridge, example {cheese, 0} should put cheese into the row, and how many there are, in this case 0, into the column. is it possible to make something without pointers, i know it is basic knowledge but i really know nothing about them
0
 
Infinity08Commented:
>> i do know that its a bad idea to define variables in a header file, but i works better for me that way.

There's no reason for it to work better that way. You can simply move it to the .cpp file instead.

Note that you're teaching yourself bad habits ... the longer you keep doing this, the harder it will be to unlearn ;)


>> that array is supposed to store some things you have in a fridge, example {cheese, 0} should put cheese into the row, and how many there are, in this case 0, into the column.

I didn't realize that the value was the amount. How about this :

        typedef struct FridgeItem {
            std::string name;
            int amount;
        } FridgeItem;

        FridgeItem fridge[20] = {
                                                  { "cheese", 0 },
                                                  { "water", 1 },
                                                  { "milk", 2 }
                                              };

using a struct to group related data (in this case the name and amount of an item), and an array of that struct to represent the fridge. In this case, the fridge can only hold 20 different items.


>> is it possible to make something without pointers,

See above ... It uses an std::string


>> i know it is basic knowledge but i really know nothing about them

You can get to that later. If you're interested, here's a nice tutorial about pointers :

        http://www.cplusplus.com/doc/tutorial/pointers.html
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
smurf_killerAuthor Commented:
thanks for the link.

i have tried it but now its saying something:

in function main()
20 koleskab.cpp cannot convert `FridgeItem' to `char (*)[1]' for argument `1' to `void udskriv(char (*)[1])'

koleskab.cpp In function `void udskriv(int*)':
33 koleskab.cpp invalid types `int[int]' for array subscript
0
 
smurf_killerAuthor Commented:
although i am not sure about that typedef struct. is it some kind of template? ive looked briefly at cplusplus.com but weren't able to find anything
0
 
smurf_killerAuthor Commented:
think i found a link
0
 
Infinity08Commented:
>> i have tried it but now its saying something:

Well, of course you'll have to modify the rest of the code to work with this.


>> although i am not sure about that typedef struct.

You didn't learn about structs yet ? They're a way of grouping related data together in one type.

        http://www.cplusplus.com/doc/tutorial/structures.html


What follows is your original code, modified to work with this struct :

/* ---- main.cpp ---- */
 
#include <iostream>
#include "koleskab.h"
 
using namespace std;
 
int main()
{
    int valg = 0;
    
    FridgeItem fridge[19] = {                     // <--- move the fridge in the main
                                {"cheese", 0},
                                {"water", 1},
                                {"milk", 2}
                            };
    int fridgeNum = 3;                            // <--- there are currently 3 items in the fridge
 
 
    cout<<"---------MENU:---------"<<endl<<endl;
    
    cout<<"Press 1 to view a list of products."<<endl;
    cout<<"Press 2 to change a product."<<endl;
    cout<<"Press 3 to add new products."<<endl<<endl;
    cin>>valg;
    
    switch(valg)
    {
                case 1 :
                     udskriv(fridge, fridgeNum);    // <-- pass the fridge array as well as the number of items currently in it
                     break;
                case 2 :
                     // to be implemented
                     break;
                case 3 :
                     // to be implemented
                     break;
                default :
                     // some error message (invalid input)
                     break;
    }
    system("PAUSE");
    return 0;
}
 
 
/* ---- koleskab.cpp ---- */
 
#include "koleskab.h"
#include <iostream>
 
void udskriv(FridgeItem items[], const int num)        // <--- modify the parameter like this
{
     for (int i = 0; i<num; i++)        // <--- use an int as array index
     {
             std::cout << items[i].name << " : " << items[i].amount << std::endl;  // <--- we output the name and amount of each item in the fridge
     }
}
 
 
/* ---- koleskab.h ---- */
 
#ifndef KOLESKAB_H            // <--- add include guards !!!
#define KOLESKAB_H
 
#include <string>
 
typedef struct FridgeItem {     // <--- the struct is declared here
   std::string name;
   int amount;
} FridgeItem;
 
void udskriv(FridgeItem items[], const int num);
void change(FridgeItem items[], int &num);      // <--- needs to be implemented in koleskab.cpp
void new_product(FridgeItem items[], int &num); // <--- needs to be implemented in koleskab.cpp
 
#endif /* KOLESKAB_H */

Open in new window

0
 
Infinity08Commented:
Try creating these three files as I showed them, then compile and run ... See that it works, and then try to understand it. Any bit you don't understand, ask me !! I'll be happy to explain it.
0
 
smurf_killerAuthor Commented:
it works thanks alot, though i do have some questions:

1) i really don't get how it finds koleskab.cpp

2) void udskriv(FridgeItem items[], const int num)  where does it get num from, now that its const. and also

3) std::cout << items[i].name do you have to write std, i mean when i write using namespace std; in main? and also, where does "name" come from?

i don't think there is more, i will have to look at it a little more.
0
 
Infinity08Commented:
>> 1) i really don't get how it finds koleskab.cpp

Every .cpp file in a project is compiled separately by the compiler. The result is an object file (a .o file).
When each .cpp file in the project is compiled, the linker will combine all the resulting object files into an executable which you can run.

A .cpp file corresponds to a compilation unit. The code in an application is split up in compilation units to keep it organized. Code that belongs (closely) together can usually be found in the same compilation unit.


>> 2) void udskriv(FridgeItem items[], const int num)  where does it get num from, now that its const. and also

num is a parameter to the function. When you call the function, you have to specify it, like here :

                     udskriv(fridge, fridgeNum);

where fridgeNum is the current number of items in the fridge. Note that if an item is added, this value needs to be incremented, and if an item is removed, it needs to be decremented.

The reason I declared it const is because the function will not modify the value. It doesn't really matter in this case as it's passed by value, but it doesn't hurt either.


>> 3) std::cout << items[i].name do you have to write std, i mean when i write using namespace std; in main?

No, if you use using namespace std; you don't have to use std::
However, since this is in the koleskab.cpp file, you need to add using namespace std; at the top of the koleskab.cpp file before you can remove the std::

So, the using namespace std; in main.cpp is NOT visible in koleskab.cpp



>> and also, where does "name" come from?

Look at the definition of the FridgeItem struct. It has two fields : name and amount.

items is an array of FridgeItem's, so each element in the array will be a FridgeItem : items[i]. For each of these, there are two fields (name and amount), which we can access using :

        items[i].name     : the name of the i-th item
        items[i].amount  : the amount of the i-th item

Refer to the tutorial on structs I posted earlier.
0
 
smurf_killerAuthor Commented:
aah yes ofcourse, i have read a bit of the tutorial, it all stands clearer now, thanks alot.
0
 
Infinity08Commented:
>> it all stands clearer now, thanks alot.

Don't hesitate to ask, no matter how easy/stupid/insignificant the question might seem.
0
 
smurf_killerAuthor Commented:
is it okay that i leave this question open if i need more help with adding to an array, or should i make new questions for that?
0
 
Infinity08Commented:
You can keep it open for questions related to this program ;)
0
 
smurf_killerAuthor Commented:
>>You can keep it open for questions related to this program ;)

ofcourse, would never use it for another program :D thanks
0
 
smurf_killerAuthor Commented:
now i have made most on it, but i still need the one that adds new things to the array. but im not sure how to do it, cause i should have something that moved everything if it wasn't there. for example number 0 is cheese but there are 0 in the fridge, number 1 is water which there is 1 of, and number 2 is milk where there are 2 in the fridge. but there aren't any cheese so it should be deleted so that water will be on cheese's spot and milk will be on water's spot. i hope you understand what i mean, my code as it looks now:
//main.cpp:
 
#include <iostream>
#include "koleskab.h"
 
using namespace std;
 
int main()
{
        int valg = 0;
    
    FridgeItem fridge[19] = {                     // <--- move the fridge in the main
                                {"cheese", 0},
                                {"water", 1},
                                {"milk", 2}
                            };
    int fridgeNum = 3;                            // <--- there are currently 3 items in the fridge
 
 for(int i=0; i<100;i++
 )
 {
    cout<<"---------MENU:---------"<<endl<<endl;
    
    cout<<"Press 1 to view a list of products available in the fridge."<<endl;
    cout<<"Press 2 to change a product in the fridge."<<endl;
    cout<<"Press 3 to add new products to the fridge."<<endl;
    cout<<"Press 4 to close Fridge Control"<<endl;
    cout<<"Choose: ";
    cin>>valg;
    cout<<endl<<endl;
    switch(valg)
    {
                case 1 :
                     udskriv(fridge, fridgeNum);    // <-- pass the fridge array as well as the number of items currently in it
                     break;
                case 2 :
                     change(fridge, fridgeNum);
                     break;
                case 3 :
                     new_product(fridge, fridgeNum);
                     break;
                case 4 :
                     return 0;
                     break;
                default :
                     cout<<"Please press 1, 2, 3 or 4"<<endl;// some error message (invalid input)
                     break;
    }
    cout<<endl;
}
    system("PAUSE");
    return 0;
}
 
//koleskab.cpp:
 
#include "koleskab.h"
#include <iostream>
 
void udskriv(FridgeItem items[], const int num)        // <--- modify the parameter like this
{
     std::cout<<"Products in the fridge: "<<std::endl;
     for (int i = 0; i<num; i++)        // <--- use an int as array index
     {
             std::cout << items[i].name << " : " << items[i].amount << std::endl;  // <--- we output the name and amount of each item in the fridge
     }
}
 
void change(FridgeItem items[], int &num)
{
     int choice;
     int amount;
     std::cout<<"Enter the number of which item you want to change the status on: "<<std::endl;
     
     for (int i = 0; i<num; i++)
     {
         
         std::cout<<"Number "<<i<<" = "<<items[i].name<<" : "<<items[i].amount<<std::endl;
     }
     std::cin>>choice;
 
std::cout<<"You have chosen "<<items[choice].name<<". There are "<<items[choice].amount<<" in the fridge."<<std::endl;
std::cout<<"Enter the new amount of "<<items[choice].name<<", or the same amount to cancel: ";
std::cin>>amount;
items[choice].amount = amount;
std::cout<<"There are now "<<items [choice].amount<<" "<<items[choice].name<<" ."<<std::endl;
}
 
void new_product (FridgeItem items[], int &num)
{
     std::cout<<"Feature not yet implemented"<<std::endl;
 
}
 
//koleskab.h:
 
#ifndef KOLESKAB_H            // <--- add include guards !!!
#define KOLESKAB_H
 
#include <string>
 
typedef struct FridgeItem {     // <--- the struct is declared here
   std::string name;
   int amount;
} FridgeItem;
 
void udskriv(FridgeItem items[], const int num);
void change(FridgeItem items[], int &num);      // <--- needs to be implemented in koleskab.cpp
void new_product(FridgeItem items[], int &num); // <--- needs to be implemented in koleskab.cpp
 
#endif /* KOLESKAB_H */

Open in new window

0
 
Infinity08Commented:
>> but there aren't any cheese so it should be deleted so that water will be on cheese's spot and milk will be on water's spot. i hope you understand what i mean

Yes, I do. The easiest would be to use an STL vector instead of an array :

        http://www.cplusplus.com/reference/stl/vector/

Is that an option for you ?

If not, here are two options :

1) when removing the cheese item from the array, simple erase the fields, and don't move the other items. The erased fields will be seen as an empty entry, and will be ignored by the rest of the code. Doing it this way will mean certain changes in the rest of the code. It will also mean that the num parameter can be dropped from the function calls.

2) an alternative is to actually move the other items when the cheese item is removed from the array ... ie. shift the other items one place towards the front of the array.
0
 
smurf_killerAuthor Commented:
i must admit, i have no idea how to work with templates or classes. so i am not sure it would be a good idea to jump out in vector coding.

>> 2) an alternative is to actually move the other items when the cheese item is removed from the array ... ie. shift the other items one place towards the front of the array.

that was kinda what my plan was, but the problem is that i have no idea how to do it, maybe some tips or code snippets?
0
 
Infinity08Commented:
>> that was kinda what my plan was, but the problem is that i have no idea how to do it, maybe some tips or code snippets?

It's pretty straightforward (even though it will have bad performance). Visualize it first, and it will make sense.

This is an array with 8 item (dots are empty, letterss are filled positions) :

        x y z . . . . .

The first three are filled. Now, we remove the first item (x) :

        . y z . . . . .

and now need to shift the y and z items to the front. We start by moving the y from second to first position :

        y . z . . . . .

and then the z from third to second position :

        y z . . . . . .

and that's it.

Assume now that we add 3 more items (a, b and c) :

        y z a b c . . .

and then we remove the a item :

        y z . b c . . .

Now we just have to shift the b and c items one position towards the front. We start with b :

        y z b . c . . .

and then c :

        y z b c . . . .

This is the theory. Now, how do you implement it ? The same way as we did it above. When we remove an item at a certain position, then we copy the next item into that (now empty) position, then the item after that, etc.

This can be easily done with a simple loop. Something like this for removing the item at position k in an array of length n :

        for (int i = k; i < n - 1; ++i) {
            // move the item at position k + 1 to position k
        }

Does that make sense to you ?
0
 
smurf_killerAuthor Commented:
yes the theory makes sence, but its more the code that's the problem, but im not sure what to write in the for loop
0
 
Infinity08Commented:
>> but im not sure what to write in the for loop

Nothing special, just copy an item from one position to another :

        items[i] = items[i + 1];
0
 
smurf_killerAuthor Commented:
sry for the late answer, ill look in to it tomorrow
0
 
smurf_killerAuthor Commented:
my code was probably not what you had in mind, but this is how ive tried but now it just sets all of it to 0
0
 
smurf_killerAuthor Commented:
i forgot the code:
void remove_idle(FridgeItem items[], int &num)
{
     
for(int k=0; k<num;k++)
{
         if (items[k].amount = 0)
         {
              
                   for(int i=k; i<num; i++)
                   {
                   items[i] = items[i+1];
                   }
}
}
 
}

Open in new window

0
 
Infinity08Commented:
        if (items[k].amount = 0)

make that :

         if (items[k].amount == 0)

It's a common mistake ;) = for assignment, == for comparison.


Note furthermore that there are two problems with that function :


1) It will iterate for too long if one or more items are removed :

                   for(int k=0; k<num;k++)

    If an item is removed, then num needs to be decremented too !


2) It will shift too many items here :

                   for(int i=k; i<num; i++)

    The upper limit needs to be num-1 as I showed in my code.
0
 
smurf_killerAuthor Commented:
ofcourse, don't know why i forgot ==.

also, i am not 100% sure about  2), but does this solve it?
void remove_idle(FridgeItem items[], int &num)
{
     
for(int k=0; k<num;k++)
{
         if (items[k].amount == 0)
         {
              
                   for(int i=k; i<num; i++)
                   {
                   items[i] = items[i+1];
                   }
         num--;
}
}
 
}

Open in new window

0
 
Infinity08Commented:
>> also, i am not 100% sure about  2), but does this solve it?

No. But if you put the num-- BEFORE the for loop (inside the if), then it would solve it ;)
0
 
smurf_killerAuthor Commented:
thanks you, i hope that i can actually make a function myself that adds something to it without asking you :D but hopefully i'll learn someday
0
 
Infinity08Commented:
>> thanks you

No problem. If you show the function you have now, I'll give it a last look-over if you want ;)


>> i hope that i can actually make a function myself that adds something to it without asking you :D but hopefully i'll learn someday

You're doing fine !! Programming is not the easiest thing to do, and we've all needed help and guidance in the beginning.

The only thing I ask is that you try to understand the code I post, and the explanations I give. And whenever you're not sure about something or you need me to clarify something, don't hesitate to ask.
0
 
smurf_killerAuthor Commented:
okay the thing with doing the add function didn't hold up ;)

for some reason, it doesn't put in my new value, but i can print it out in the for loop, but i don't get it when i choose to see all of them
void new_product (FridgeItem items[], int &num)
{
std::string name;
int amount;
     std::cout<<"Enter name of the product ";
     std::cin>>name;
     std::cout<<std::endl;
     std::cout<<"Enter the amount of "<<name<< " ";
     std::cin>>amount;
     
     for(int i=num; i<=num;i++) //num should tell me the number 2 (cause 1 has been deleted) so that should be free
     {
             items[i].name = name;
             items[i].amount = amount;
             std::cout<<items[i].name<<" "<<items[i].amount;
     }
 
}

Open in new window

0
 
Infinity08Commented:
>>      for(int i=num; i<=num;i++)

Look closely ... what does this do ? How many times will this loop ? Do you need this loop ?

Also, if you add an item, you'll have to increment num of course ...
0
 
smurf_killerAuthor Commented:
>>Also, if you add an item, you'll have to increment num of course ...
tried to do that, got spam and error ;)

but no i don't need it, (doh me) so i took it out, but same thing happens. but the thing i really don't get is that it can print it inside the function, but not when i try it in the function ment to it (udskriv). can't i use num to tell it what place it should be in, it seems as it never gets to be in the array?
void new_product (FridgeItem items[], int &num)
{
std::string name;
int amount;
     std::cout<<"Enter name of the product ";
     std::cin>>name;
     std::cout<<std::endl;
     std::cout<<"Enter the amount of "<<name<< " ";
     std::cin>>amount;
     
             items[num].name = name;
             items[num].amount = amount;
             std::cout<<items[num].name<<" "<<items[num].amount;
    
 
}

Open in new window

0
 
Infinity08Commented:
>> it seems as it never gets to be in the array?

That's because you still haven't incremented num.

Say that num is 2 before you call the function. Then you will call the function, and it will add an item in the array, but it will NOT increment num, so it will still be 2.
Then if you want to show the contents of the array, it will only show the first two items (since num is 2), and NOT the item you just added.

So :

void new_product (FridgeItem items[], int &num) {
     if (num >= 19) return;      // <--- add this check so we don't write past the end of the array !!
 
     std::string name;
     int amount = 0;
 
     std::cout << "Enter name of the product ";
     std::cin >> name;
     std::cout << std::endl;
     std::cout << "Enter the amount of " << name << " ";
     std::cin >> amount;
 
     items[num].name = name;
     items[num].amount = amount;
     std::cout << items[num].name << " " << items[num].amount;
 
     ++num;            // <--- increment num here !!!    
}

Open in new window

0
 
Infinity08Commented:
Note that you also don't need those temporary values (unless you want to do some error checking on them), so this works as well :


void new_product (FridgeItem items[], int &num) {
     if (num >= 19) return;      // <--- add this check so we don't write past the end of the array !!
 
     std::cout << "Enter name of the product ";
     std::cin >> items[num].name;         // <--- write directly to the correct string
     std::cout << std::endl;
     std::cout << "Enter the amount of " << name << " ";
     std::cin >> items[num].amount;       // <--- write directly to the correct int
 
     std::cout << items[num].name << " " << items[num].amount;
 
     ++num;            // <--- increment num here !!!    
}

Open in new window

0
 
smurf_killerAuthor Commented:
thanks, although im not sure why you have to increase num everytime?
0
 
Infinity08Commented:
>> thanks, although im not sure why you have to increase num everytime?

Did you understand my earlier explanation ? Repeated here :


Say that num is 2 before you call the function. Then you will call the function, and it will add an item in the array, but it will NOT increment num, so it will still be 2.
Then if you want to show the contents of the array, it will only show the first two items (since num is 2), and NOT the item you just added.


If not, then let me know, and I'll explain it differently ;)
0
 
smurf_killerAuthor Commented:
ofcourse now i understand it. thanks, much appreciated
0
 
Infinity08Commented:
>> ofcourse now i understand it.

Good :)
Your udskriv function needs the num parameter to know how many items the fridge contains. It will only show the first num items (as it should).


If you want, you can post your complete code, so I can give it a look-over ...
0
 
smurf_killerAuthor Commented:
what would be nice:
//main.cpp
#include <iostream>
#include "koleskab.h"
 
using namespace std;
 
int main()
{
        int valg = 0;
    
    FridgeItem fridge[19] = {                     // <--- move the fridge in the main
                                {"cheese", 0},
                                {"water", 1},
                                {"milk", 2}
                            };
    int fridgeNum = 3;                            // <--- there are currently 3 items in the fridge
 
 for(int i=0; i<100; i++)
 {
 remove_idle(fridge, fridgeNum);
    cout<<"---------MENU:---------"<<endl<<endl;
    
    cout<<"Press 1 to view a list of products available in the fridge."<<endl;
    cout<<"Press 2 to change a product in the fridge."<<endl;
    cout<<"Press 3 to add new products to the fridge."<<endl;
    cout<<"Press 4 to close Fridge Control"<<endl;
    cout<<"Choose: ";
    cin>>valg;
    cout<<endl<<endl;
    switch(valg)
    {
                case 1 :
                     udskriv(fridge, fridgeNum);    // <-- pass the fridge array as well as the number of items currently in it
                     break;
                case 2 :
                     change(fridge, fridgeNum);
                     break;
                case 3 :
                     new_product(fridge, fridgeNum);
                     break;
                case 4 :
                     return 0;
                     break;
                default :
                     cout<<"Please press 1, 2, 3 or 4"<<endl;
                     break;
    }
    cout<<endl;
}
    system("PAUSE");
    return 0;
}
 
 
//koleskap.cpp
 
#include "koleskab.h"
#include <iostream>
 
void udskriv(FridgeItem items[], const int num)        // <--- modify the parameter like this
{
     std::cout<<"Products in the fridge: "<<std::endl;
     for (int i = 0; i<num; i++)        // <--- use an int as array index
     {
             std::cout << items[i].name << " : " << items[i].amount << std::endl;  // <--- we output the name and amount of each item in the fridge
     }
}
 
void change(FridgeItem items[], int &num)
{
     int choice;
     int amount;
     std::cout<<"Enter the number of which item you want to change the status on: "<<std::endl;
     
     for (int i = 0; i<num; i++)
     {
         
         std::cout<<"Number "<<i<<" = "<<items[i].name<<" : "<<items[i].amount<<std::endl;
     }
     std::cin>>choice;
 
std::cout<<"You have chosen "<<items[choice].name<<". There are "<<items[choice].amount<<" in the fridge."<<std::endl;
std::cout<<"Enter the new amount of "<<items[choice].name<<", or the same amount to cancel: ";
std::cin>>amount;
items[choice].amount = amount;
std::cout<<"There are now "<<items [choice].amount<<" "<<items[choice].name<<" ."<<std::endl;
}
 
    void new_product (FridgeItem items[], int &num)
    {
if(num>19)return;
 
         std::cout<<"Enter name of the product ";
         std::cin>>items[num].name;
         std::cout<<std::endl;
         std::cout<<"Enter the amount of "<<items[num].name<< " ";
         std::cin>>items[num].amount;
         
         std::cout<<items[num].name<<" "<<items[num].amount;
        
    num++;
    }
 
 
 
void remove_idle(FridgeItem items[], int &num)
{
     
for(int k=0; k<num;k++)
{
         if (items[k].amount == 0)
         {
              num--;
                   for(int i=k; i<num; i++)
                   {
                   items[i] = items[i+1];
                   }
         
}
}
 
}
 
//koleskab.h
 
#ifndef KOLESKAB_H            // <--- add include guards !!!
#define KOLESKAB_H
 
#include <string>
 
typedef struct FridgeItem {     // <--- the struct is declared here
   std::string name;
   int amount;
} FridgeItem;
 
void udskriv(FridgeItem items[], const int num);// <---  implemented in koleskap.cpp
void change(FridgeItem items[], int &num);      // <---  implemented in koleskab.cpp
void new_product(FridgeItem items[], int &num); // <---  implemented in koleskab.cpp
void remove_idle(FridgeItem items [], int &num);// <---  implemented in koleskap.cpp
 
#endif /* KOLESKAB_H */

Open in new window

0
 
Infinity08Commented:
In your change function you have :

     std::cin>>choice;

What happens if the user enters an invalid value ? ie. a value smaller than 0 or larger than num. Be careful with this !!


The rest looks fine to me.

Take care of properly indenting your code though ... It makes debugging/modifying a lot easier.

        http://en.wikipedia.org/wiki/Indent_style
0
 
smurf_killerAuthor Commented:
yes i know that with the

std::cin>>choice;

as far as i remember there is some code i can use to prevet this. just have to find it.
but im glad that it looks good
0
 
Infinity08Commented:
>> as far as i remember there is some code i can use to prevet this.

Just check whether the value the user entered is between 0 and 19 (not inclusive).
0
 
smurf_killerAuthor Commented:
sorry it has taken a bit long, but went away a couple of days with no internet. anyway ill close this Q as good as i can. and thank you very very much for all the help you have provided. it really helped
0
 
smurf_killerAuthor Commented:
thanks for all the help, i really appreciate it! i have tried to take those comments that helped me the most, and was most related to the question. but thanks alot again
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 24
  • 20
Tackle projects and never again get stuck behind a technical roadblock.
Join Now