Conditional statement not working

Hello. I am working on a solution that I cannot get an if() condition to work. The condition is true but it skips it as though it were false. Specifically this code

// Return to function getRecipeName if listView is true
if (listView = true)
{
string getRecipeName(string prompt);
} // Else continue

Can someone help please? My function code is below.

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
  	string getDisplayRecipe(string validRecipe)
	{
		// Clear the screen
		system("CLS");

		// Name the inFile and outfile
		ifstream inFile;
		ofstream outFile;

		// Open recipe file
		inFile.open(validRecipe);

		// Initiate variable "character"
		char character;

		// Get the first character from the recipe
		inFile.get(character);

		// Loop until the end of the file
		while (!inFile.eof())
		{
			//Display the character
			cout << character;

			// Get the next character from the recipe
			inFile.get(character);

		} // End while loop

		// Close both inFile & outFile
		inFile.close();
		outFile.close();

		// Check if user entered 'list'
		bool listView = (validRecipe == "list.tsp");
		cout << boolalpha;

		// Return to function getRecipeName if listView is true
		if (listView = true)
		{
			string getRecipeName(string prompt);
		} // Else continue
		
		// Return validRecipe variable
		return validRecipe;
	}
Last edited on
if (listView = true) // = is assignment
should be
if (listView == true) // == is logical comparison

or, even better:
if (listView) // 'cos it's boolean anyway
What @lastchance said is correct, but the assignment of true would always evaluate to true in the if condition. So there's another problem here.

The 2nd problem is that the statement string getRecipeName(string prompt); is actually declaring a function, not calling one. I believe you instead want something like:

validRecipe = getRecipeName(<some prompt string here>);
lastchance thank you.

doug4, thank you as well.

I made the changes and it worked. Thank you both again!
Last edited on
Topic archived. No new replies allowed.