Hello,
I am new to C++ programming but not to programming in general. I have coded in VB, Java for a number of years. But C++ is a bit trickier than I expected. I have searched and looked over several threads on here pertaining to tokenizing and char to int, but I am my whits end with this code.
I have the following issues with the code below:
I am trying to input a line of text from a command prompt like so:
2 23
I tokenized the input into an array, but for some reason the second index in the array only includes 2, not 23. Finally, I cannot figure out for the life of me how to convert a char to integer. I know that if you perform mathematical computations on a char value, it will return the ascii value not the integer value that was entered in. In C++ I cannot figure out how to convert char to int!!
Here is the code that I am having trouble with:
void Store::inputProducts()
{
char productInfo[80];
int productQTY;
bool isStop = false;
prod1Count = 0;
prod2Count = 0;
prod3Count = 0;
prod4Count = 0;
prod5Count = 0;
do
{
char productNumber;
char *tokenPtr;
cout << "Press 1 for Product 1, 2 for Product 2, 3 for Product 3, 4 for Product 4, 5 for Product 5 or Q to Quit:";
//cin >> productNumber >> productQTY;
//cin.get(productNumber, productQTY);
cin.getline( productInfo, 80, '\n');
tokenPtr = strtok(productInfo, " ,.-");
productNumber = tokenPtr[0];
productQTY = tokenPtr[2];
//int n = atoi(productQTY);
cout << " Product QTY is: " << productQTY << endl;
cout << " Product # is: " << productNumber << endl;
int productQuantity;
//productQuantity = static_cast< int >(productQTY);
string s(1, productQTY);
productQuantity = atoi(s.c_str());
cout << " Product Quantity is " << productQuantity << endl;
switch ( productNumber )
{
case '1':
prod1Count = prod1Count + productQTY;
cout << "Prod 1: " << prod1Count;
cout << "Prod QTY: " << productQTY;
break;
case '2':
prod2Count = prod2Count + productQTY;
cout << prod2Count;
break;
case '3':
prod3Count = prod3Count + productQTY;
cout << prod3Count;
break;
case '4':
prod4Count = prod4Count + productQTY;
cout << prod4Count;
break;
case '5':
prod5Count = prod5Count + productQTY;
cout << prod5Count;
break;
case 'Q':
case 'q':
isStop = true;
break;
default:
cout << "Incorrect Product # was entered. Please enter a new product # (1 through 5 only)." << endl;
break;
} // end switch
//cout << "Press 1 for Product 1, 2 for Product 2, 3 for Product 3, 4 for Product 4, 5 for Product 5 or Q to Quit:";
//cin >> productNumber >> productQTY;
} while ( isStop == false );// end while
this->displayResults();
}
What suggestions could anyone give me? Any would be appreciated! :)
Thank you,
Paul