P// Lab 2 as created by Matthew Ouille (matthew.ouille@gmail.com)
#include <iostream> //Preprocessor for cin & cout [namespace std]
#include <string> //Preprocessor for the string variable type [namespace std]
int main() {
// Declare all the fruit variables
int BananasQuantity;
int ApplesQuantity;
int PearsQuantity;
int OrangesQuantity;
int PapayasQuantity;
// Store the purchase type in a variable for possible eval
std::string PurchaseType;
// PurchaseIntent is a bool because yes/no, 1/0, true/false are all the same thing
bool PurchaseIntent;
// Assign the fruit variables to 0
BananasQuantity = 0;
ApplesQuantity = 0;
PearsQuantity = 0;
OrangesQuantity = 0;
PapayasQuantity = 0;
PurchaseType == "bananas";
// Set PurchaseIntent's initial value to make the do-while work
// Ask what the customer intends to do
std::cout << "Do you want to order? (yes/no) ";
// Save their response
std::cin >> PurchaseIntent;
std::cout << std::endl;
// Do all the questioning and setting while PurchaseIntent = true
do
{
std::cout << "Your choices are Bananas, Apples, Pears, Oranges, and Papayas." << std::endl;
std::cout << "What would you like to purchase? ";
std::cin >> PurchaseType;
std::cout << std::endl;
// Use a series of if's to set quantities, which will restart the while
if (PurchaseType == "Bananas" || PurchaseType == "bananas"){ // Use the OR (||) to evaluate if the user is lazy or not
std::cout << "How many would you like to purchase? ";
std::cin >> BananasQuantity;
std::cout << std::endl;
}
elseif (PurchaseType == "Apples" || PurchaseType == "apples"){
std::cout << "How many would you like to purchase? ";
std::cin >> ApplesQuantity;
std::cout << std::endl;
}
elseif (PurchaseType == "Pears" || PurchaseType == "pears"){
std::cout << "How many would you like to purchase? ";
std::cin >> PearsQuantity;
std::cout << std::endl;
}
elseif (PurchaseType == "Oranges" || PurchaseType == "oranges"){
std::cout << "How many would you like to purhcase? ";
std::cin >> OrangesQuantity;
std::cout << std::endl;
}
elseif (PurchaseType == "Papayas" || PurchaseType == "papayas"){
std::cout << "How many would you like to purchase? ";
std::cin >> PapayasQuantity;
std::cout << std::endl;
}
// If for some reason all of the above evaluations fail, let the user know their choices again.
else {
std::cout << "Please pick Bananas/Apples/Pears/Oranges/Papayas.";
}
// Set PurchaseIntent's initial value to make the do-while work
// Ask what the customer intends to do
std::cout << "Would you like to order again? (yes/no) ";
// Save their response
std::cin >> PurchaseIntent;
std::cout << std::endl;
//Evaluate again
} while (PurchaseIntent = 1);
return 0;
}
I've been using "yes" but even if I use "true" it does the same thing. This is by far one of the craziest things I've seen of a while loop. I've never seen one that just ignores conditional formatting entirely...
Not really, your 'PurchaseIntent' variable is a bool not a string. try entering '1' or '0' instead and see what happens. You're leaving information on the input stream by assigning characters to the wrong data type.
And look how the variable is initialised on line 31 - is yes/no really the same as true/ false?
I would prefer a while loop instead of a do-while, it avoids testing PurchaseIntent twice. Consider using a switch inside a while loop, instead of the if else's - not that it matters though.
That's insane. You were right. It was that simple. Now I feel pretty stupid. VC++ Intellisense was giving me the impression it would evaluate yes/no, 1/0, and true/false the same way. Thanks man!