case 1-9 work fine, but 10 screws up. I know it's because of incorrect char to int conversion, but I don't know how to fix it. Help?
Thanks!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
#include <iostream>
#include <conio.h>
#include <cstdlib>
#include <string>
#include <windows.h>
usingnamespace std;
int main(){
char item_type;
int s ;
int amount ;
int inventory[10]={0};
int int_item_type;
If its type of amout is "int", when you enter "3" for example, the "3" will be assinged to "amount".
If its type of amout is "char", when you entr "3", the "51" is actually assinged to "amount"
yes, sorry that I did not post that, I thought I did.
Before the switch statement is
cin >> item_type;
Then you pick 1 thru 10, which is assigned to the item type. In words, it asks you which item you want, and then the amount of that item. Then, using an array, the value stored at the item type location in the array is equal to the amount.
I feel like strings wouldn't work for what I'm doing - I'm making a game inventory. I was planning on storing the amount of item_type 1 in inventory[1], and storing the amount of item_type 2 in inventroy[2] etc. If I use strings, would the amount of item_type 10 still be able to be accessed at a specific location in inventory[] easily?
Also, why can't I use switch and what's up with your if statement. Which is it 0 equaling something, and why are the quotes reversed?
Use an int, as I said. Input item_type as an int. And compare it to the number 10, not the string "10". I don't know what you mean by "10 is considered a string". It's not, the type of 10 is int. And then you will be able to use it as an array index as you have said.
"10" = std::string. Will NOT work with a switch statement.
'10' = Multi-character constant. Throws a compiler warning but still works. (Mingw\GCC)
10 = Integer. The right way to use a switch statement which takes chars and casts them into their integer equaivalent values for evaluation anyway.
The really cool part is that you can run your switch(inventory.intType) check on the integer component of the object, and return the name of that object, AND still have the quantity associated with that object. Just sitting here thinking about it I'm getting carried away with all of the potential posibilities.
Unless I'm missing something, than no, it does NOT work. I changed item_type to int and made all the cases look like "case 1, case 2, etc" and 1 thru 9 work but 10 does not. Try it yourself, here's the whole code (only the first half of my entire actual programm is here so you have to put in some end } ):