While Loops will not evaluate If's

closed account (S36A7k9E)
My while loop infinitely loops, never really evaluating the code inside it. Is there something I'm doing wrong?

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
  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;
			}
			else if (PurchaseType == "Apples" || PurchaseType == "apples"){
				std::cout << "How many would you like to purchase? ";
				std::cin >> ApplesQuantity;
				std::cout << std::endl;
			}
			else if (PurchaseType == "Pears" || PurchaseType == "pears"){
				std::cout << "How many would you like to purchase? ";
				std::cin >> PearsQuantity;
				std::cout << std::endl;
			}
			else if (PurchaseType == "Oranges" || PurchaseType == "oranges"){
				std::cout << "How many would you like to purhcase? ";
				std::cin >> OrangesQuantity;
				std::cout << std::endl;
			}
			else if (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;
}
Take another look at which operator you are using on Line 83.

EDIT: Is it the comparison operator which would return a boolean value? Or is it something else...
Last edited on
closed account (S36A7k9E)
Sorry, that was initially set as true. I changed it trying to fix this thing....
closed account (S36A7k9E)
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.
closed account (S36A7k9E)
Okay, by using two equals sign as my operator that has fixed my infinite looping, but it still won't wait for the user to answer the std::cin question
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.

Hope all is well at your end :+)
closed account (S36A7k9E)
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!
Topic archived. No new replies allowed.