getch() problem
Apr 14, 2011 at 3:59pm UTC
Look at line 29 with
shop_pick = getch()
For some reason when I input the value 1 is just loops back to the beginning of shop loop rather then continuing on the program. When I use
cin >> shop_loop
though, it works perfectly.
Any Ideas?
Timmah
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 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
#include <iostream>
#include <conio.h>
#include <cstdlib>
#include <string>
#include <windows.h>
#include <sstream> // Used for istringstream
using namespace std;
int main()
{
int shop_pick, amount;
int item_type;
int inventory[11]={0}; // Non initialized arrays contain trash
string item_type_str; // Human input as a string that will be deterimined as char or int
// Boolean loops instead of goto
bool shop_loop = true ; // Loop used when picking which store to enter
bool item_type_loop; // Loop used when picking which piece of merchandise to buy
cout << endl;
while (shop_loop) { //
cout << "1)General Store 5)Bar" ;
cout << "\n2)Blacksmith 6)Joe's House" ;
cout << "\n3)Potions/Magic Shop" ;
cout << "\n4)Weapons" ;
shop_pick = getch();
switch (shop_pick) {
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;
item_type_loop = true ;
while (item_type_loop) {
cout << "\nPress 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_str;
cout << endl;
if (item_type_str.size() == 1 && isalpha(item_type_str[0])) {
item_type = item_type_str[0];
switch (item_type) {
case 'b' :
item_type_loop = false ;
break ;
case 'd' :
item_type_loop = false ;
shop_loop = false ;
break ;
default :
cout << "Incorrect Command!\n" ;
break ;
}
}
else {
istringstream(item_type_str) >> 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 ;
default :
cout << "Incorrect Command!\n" ;
break ;
}
}
}
break ;
}
}
system("PAUSE" );
return EXIT_SUCCESS;
}
Apr 14, 2011 at 4:26pm UTC
getch stands for get character. So when you enter 1, shop_pick becomes '1' which has the integer value of 49. You could either use getch()-'0' ;
or surround your digits with single quotes.
Apr 14, 2011 at 6:27pm UTC
Rather than using getch()
, use the <istringstream>
class exactly as you have on line 66 , and convert it to an integer before you enter the switch statement.
Apr 14, 2011 at 6:33pm UTC
Why in the world would one use a stringstream where direct reading from cin is available?
Apr 14, 2011 at 6:37pm UTC
@hamsterman : it was just a thought... it seemed like a good idea at the time :D
But now that you mention it cin>>
would be a better approach.
Last edited on Apr 14, 2011 at 6:39pm UTC
Topic archived. No new replies allowed.