-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.
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.....
#include <iostream>
usingnamespace 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;
}
#include <iostream>
usingnamespace 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;
elseif(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.
#include <iostream>
usingnamespace 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;
elseif ( 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;
}