Converting Char to Int Problem

Pages: 12
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>

using namespace std;

int main(){
  char item_type;
  int s         ;
  int amount    ;
  int inventory[10]={0};

  int int_item_type;


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
switch (item_type) {
            case  '1':
            case  '2':
            case  '3':
            case  '4':
            case  '5':
            case  '6':
            case  '7':
            case  '8':
            case  '9':
            case '10':
              cout << "Enter amount: ";
              cin  >> amount;
              int_item_type = (int)item_type-48;              
              inventory[int_item_type]+=amount;
              cout << "Type " << item_type << " has "<< inventory[int_item_type] << " items in it\n\n";
              break;
int item_type;
same kbw :

int item_type
The type of item_type is not very concern.

But the type of "amount" is not very clear.

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"

The latter is more like what you want.

B2ee
I think item_type should be casted to integer and that will be a good Idea.

Else please post the full code.
What you want is case '0': to case '9':
the value of '1' is 49 (hex 31) and '0' is 48 (hex 30)

'10' is not the value you expected. it is 12592 (hex 3130). So '10' - 48 is 12544 (3100)
I changed int int_item_type to int item_type and got an error: "Conflicting Decloration of int item_type"

Caligulaminus - I know 0 to 9 would work, but I do not want that. I'm going to need cases that are >=10

Coder - I know that the problem is incorrect conversion, but I don't know how to fix it

Any More Ideas?
Make item_type an integer, and compare it to the normal numbers 1 through 10 (not characters).
I'm going to need cases that are >=10
I'd say don't use the vast switch and if(item_type >= 10) instead

Coder - I know that the problem is incorrect conversion, but I don't know how to fix it
Then how do you get the value of item_type? Using 1..10 is certainly better than '1'..'10'
rocketboy - item_type can't be an int because 10 is considered a string, not an int


Sorry coder - what do you mean?
And switches are big if statements basically, so why does it matter?

I tried breaking it up so i did cases 1 thru 9 do this and then after that case 10 do this but it did not work
And switches are big if statements basically, so why does it matter?
legibility


Sorry coder - what do you mean?
Where does the value for item_type come from? Is it like so cin>>item_type;? How?
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.

Does that make sense or are you still confused?
rocketboy - item_type can't be an int because 10 is considered a string, not an int
If you wanted to use a string then make it a string: std::string item_type;

But then you cannot use switch(). You must use 'if' like if("0" == item_type)... Note the different quotation marks
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.

Why are we complicating this?

@ OP: As for your game inventory, I would suggest looking into Structs\Classes instead of trying to manage multiple arrays like that. http://www.cplusplus.com/doc/tutorial/classes/

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.
"10" is a char const *, not an std::string.
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 } ):

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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include <iostream>
#include <conio.h>
#include <cstdlib>
#include <string>
#include <windows.h>

using namespace std;

int main(){
  int item_type;
  int s         ;
  int amount    ;
  //not initialized arrays contain trash
  int inventory[10]={0};

  //for loops that will be used instead goto
  bool loop15 = true;
  bool loop16;

  while(loop15){                                                                 // shop loop

    cout << "\n\n1)General Store          5)Bar";
    cout << "\n2)Blacksmith             6)Joe's House";
    cout << "\n3)Potions/Magic Shop";
    cout << "\n4)Weapons";

    s = getch();               // goes into stores/back to menu of stores to purchase items

  switch (s) {
      case '1':{
        cout << "\n\nWelcome To The General store!" << endl;
        cout << "1) 1 Gallon Water:"                << endl;
        cout << "2) 1 lb Meat:"                     << endl;
        cout << "3) 5 lbs Meat:"                    << endl;
        cout << "4) Assortment of Spices/Herbs:"    << endl;
        cout << "5) Wool Gloves:"                   << endl;
        cout << "6) Wool Coat:"                     << endl;
        cout << "7) Light Leather Boots:"           << endl;
        cout << "8) Pack of 5 Torches:"             << endl;
        cout << "9) Pack of 5 Lanterns:"            << endl;
        cout << "10) 1 Quart of Oil:"       << endl << endl;
        
        loop16 = true;
        while(loop16){  //General store loop
          cout << "Press 1 - 10 and Then Enter To Purachase An Item. Press 'b' To Go Back To The \nShop List. When done, press 'd'";
          cin  >> item_type;

          switch (item_type) {
            case  1:
            case  2:
            case  3:
            case  4:
            case  5:
            case  6:
            case  7:
            case  8:
            case  9:
            case 10:
              cout << "Enter amount: ";
              cin  >> amount;
              inventory[item_type]+=amount;
              cout << "Type " << item_type << " has " << inventory[item_type] << " items in it" << endl;
              break;
Last edited on
Pages: 12