if/else


Write an if/else structure that prints the word complete to the screen if the variable named percent_complete is equal to 100 and prints the phrase not complete to the screen otherwise. I am unsure about the section in underline.

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
#include <iostream>
using namespace std;
int main ()

{
	int percent_completed = 100;
	char Done = 'Y' ;

	cout << "What percent has been completed?"<< endl;
	cin >> percent_completed;
	
	if(percent_completed =100)
     std::cout<<"Complete" << endl;


	if (Done = 'Y');
	{
		percent_completed = 100 ;
	
	}	
	if(percent_completed = 100);
	{cout << "Complete.";}
	else
	{cout << "Not Complete.";}
	return 0;
}

Last edited on
replace line 21:
if(percent_completed = 100);
with
if(percent_completed == 100);


= assigns a value (100 in this case) to percent_complete.
== checks if they are the same and becomes true or false
closed account (LN7oGNh0)
whats the part for DONE??? Isnt it complete and not complete? This is what I would do:

1
2
3
4
5
6
7
8
if (percent_completed == 100) //Notice I use '==' not '='
{ 
   cout << "completed";
}
else 
{
   cout << "not completed";
}

Last edited on
Good point Hazique35.

replace line 16:
if (Done = 'Y');
with
if (Done == 'Y');

and delete lines 12-13.


Edit: On second look, you may want to delete lines 16-20 as well. It over-writes what you are doing in line 10.
Last edited on
Like This?


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
using namespace std;
int main ()

{
	int percent_completed = 100;
	char Done = 'Y' ;

	cout << "What percent has been completed?"<< endl;
	cin >> percent_completed;
	
	

	if(percent_completed =100)
     std::cout<<"Complete" << endl;
	
	
	if(percent_completed == 100);
	{cout << "Complete.";}
	else
	{cout << "Not Complete.";}
	return 0;
}
If you run that program, does it do what you want?

Comment out and/or edit lines until it does.
Like this?

No.
Line 14 is incorrect:
if(percent_completed =100)
There is an assignment statement there which has three effects:

1. it sets the value of variable percent_completed to 100.

2. as a boolean expression, any non-zero value is considered to be true, thus the statement on line 15 will always be executed.

3. Similarly, as we know for certain the value of the variable (it is 100), the if statement on line 18, though correct in itself, will always be true, because of the earlier error.
Topic archived. No new replies allowed.