Need help

Need help with this question:

-Given that n = 5000, write a program that shall prompt the user to enter zero or negative integer value continuously. If the user enters a positive value the program shall prompt the user again. The value entered shall be used to deduct from n. The prompting shall stop when n reaches below 0. The prompting shall also stop when the user enter 2 consecutive 0 values. Implement the program using do..while loops.


What exactly do you need help with? What have you done yourself so far?
Sounds like a homework assignment, have you tried to do this yet?

1
2
3
4
5
Create a variable here equal to 5000;
while variable > 0 prompt user for value
get value from user
if value is negative, deduct it
and so on and so forth.....


I will let you do the actual coding.
Last edited on
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 n = 5000;
	int nega = 0;

	do
	{ 
		cout<<"Please enter a negative value:"<<endl;
		cin>>nega;

		if( nega > 0)
			cout<<"You have entered a positive value, please enter negative value:"<<endl;
			cin>>nega;

		}while(n < 0) ;





	system("pause");
	return 0;
}




it is still not working, any idea?
while(n < 0)

Is that right? I think you want the opposite - that the program should run while n is positive.

edit: and you need to subtract nega from n.
Last edited on
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
#include <iostream>
using namespace std;

int main()
{	
	int n = 5000;
	int nega = 0;

	do
	{ 
		cout<<"Please enter a negative value:"<<endl;
		cin>>nega;
		{
		if( nega < 0 )
		n = n - nega;

		else if(nega > 0)
		cout<<"You have entered an negative value, please enter a negative value:"<<endl;
		}
		
		if(n < 0 )
		break;
	
	


		}while(n > 0);


cout<<"The n is now less than 0:"<<endl;



	system("pause");
	return 0;
}




I've successfully Input the negative number, but somehow even the n is below 0, the program will still continue to prompt the user to enter a value.
Ah, if you subtract a negative number, it's actually doing addition. So you'd want to add the negative number. n = n + nega;

line 17 - can just be else. If the number doesn't fit the condition in the if, then it has to be >=0.

Line 18 - the message should be that they entered a non-negative number?

Line 21/22 - you don't need this since you have the condition on the while for looping.

Line 13/19 - these curly brackets aren't really necessary.
Last edited on
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
#include <iostream>
using namespace std;

int main()
{	
	int n = 5000;
	int nega = 0;
	int zero=0;

	do
	{ 
		cout<<"Please enter a negative value:"<<endl;
		cin>>nega;
		
		if( nega < 0 )
		n = n + nega;

		else if ( nega > 0 )
		cout<<"You have entered an non-negative value, please enter a negative value:"<<endl;
		
		
		if( nega == 0)
		{
			zero++;	
		if( zero == 2)
		break;
		}

		
	


		}while(n >= 0);


cout<<"The n value is now"<<n<<endl;



	system("pause");
	return 0;
}


Thanks :) i manage to do it!
Topic archived. No new replies allowed.