473,379 Members | 1,255 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,379 software developers and data experts.

While loop that displays no negative numbers

How would i write a while loop that displays no negative numbers???
Apr 24 '07 #1
23 6109
Savage
1,764 Expert 1GB
How would i write a while loop that displays no negative numbers???
It's quite simple.All that u need is simple if-else statment inside the loop if exppresion is TRUE don't print anything else print that number.


Savage
Apr 24 '07 #2
ok so like this......



int n;
while (n = 0)
if (n>0)
cin >> n;
else (n < 0)
cout << " Please enter another number";


???? lol
Apr 24 '07 #3
Ganon11
3,652 Expert 2GB
ok so like this......



Expand|Select|Wrap|Line Numbers
  1. int n;
  2. while (n = 0)
  3. if (n>0)
  4. cin >> n;
  5. else (n < 0)
  6. cout << " Please enter another number";

???? lol
Not quite...

Let's look at what your code is doing.

Expand|Select|Wrap|Line Numbers
  1. int n;
  2. while (n = 0)
  3.    if (n>0)
  4.       cin >> n;
  5.    else (n < 0)
  6.       cout << " Please enter another number";
First, you declare an integer variable, n. Cool. Then you have a while loop, with a condition setting n to 0? Maybe you meant n == 0 which would loop while x is 0, but even then your loop will fail because you haven't given a value to n.

Now, inside the loop, you test if n is greater than 0. This would be great if n had a value...but you only cin >> a value if it's greater than 0. Huh? That doesn't make sense! How can you test its value before you get it?

Now, your else statement is incorrectly written. You cannot have else (condition) - you can only say else, or else if (condition). Also, the condition is not necessary - if a number is not greater than 0, then it must be less than 0 (or must it...what if n = 0? Consider this possibility).
Apr 24 '07 #4
Savage
1,764 Expert 1GB
ok so like this......



int n;
while (n = 0)
if (n>0)
cin >> n;
else (n < 0)
cout << " Please enter another number";


???? lol
I do see what u want to do but this is all wrong.Let us go line by line and fix this:

1.while(n=0)

I do see lots of errors here.If i get the program right it's supposed to be a control of input.

a.Couple of errors are here: while(n=0)

solution:instead of using number to identify when loop will break i would rather use identifer,in this case let it be i.Secound error u cant use a single = when testing expression,u must use ==.So here is fisrt part of it:

int i=0;
while(!i)//this is nearly same as while(i==0).

I see more errors in loop construction part:(e.g)

It should be this:

while(!i)
{
//loop body
}

When u have done all this 'repairment' post so that we can move forward.

Savage
Apr 24 '07 #5
hmm ok so let me try this

int n;
cout << "Enter a Number"<<endl;
cin>> n;
while (n)
if (n>0)
elseif
Apr 24 '07 #6
sorry posted that before i saw the last post
Apr 24 '07 #7
Savage
1,764 Expert 1GB
hmm ok so let me try this

int n;
cout << "Enter a Number"<<endl;
cin>> n;
while (n)
if (n>0)
elseif
It's not elseif it's else if(ur expression) and u are missing those{ } compund statments in ur loop

Savage
Apr 24 '07 #8
ok i understand the first part please continue
Apr 24 '07 #9
Savage
1,764 Expert 1GB
ok i understand the first part please continue
Well as we can see it's already continued.Do u have any Q on those new posts?


Savage
Apr 24 '07 #10
How would i write a while loop that displays no negative numbers???

I don't understand your requirement very much...

do you wanna display all the non negative numbers or you just want to display the non negative numbers you received.

if you wanna display all by using a while loop, just do it as follows..

int n = 0;
while(1)
std::cout<<n++<<std::endl;

but if you wanna display the non negative numbers you received, using a if-else statement is good idea.
Apr 24 '07 #11
nah i'm still working on this one lol

so from what you have taught me today:


int i = 0;

cout<< "Please enter a number" << endl;
cin >> i;

while(i == 0)
{
if(i>0)
esle
}
Apr 24 '07 #12
For this part of the program i want to create a while loop that will not display negative numbers.
Apr 24 '07 #13
Savage
1,764 Expert 1GB
No,no u still have number n defined, i is just identifer.

int n,i;

//loop and it's expression

//loop body

if(n>0) i=1;
else i=0;

} //loop compound statement.

Note:this task can be done much more easier if u use do-while.Do u must use while?

Savage
Apr 24 '07 #14
We can you use do-while also...
Can you show me that way also???
Apr 24 '07 #15
Savage
1,764 Expert 1GB
We can you use do-while also...
Can you show me that way also???
Offcourse,it would be like:

Expand|Select|Wrap|Line Numbers
  1. int n;
  2.  
  3. do{
  4.        //cin number
  5.  
  6.  
  7. }while(n<0);//No need for those if-else statments at all
Savage
Apr 24 '07 #16
whenever i try to use your code i get a runtime error saying that n has not been defined
Apr 24 '07 #17
Savage
1,764 Expert 1GB
whenever i try to use your code i get a runtime error saying that n has not been defined
And how did u exactly implemented my code?

PS:Post just do-while loop or while which u have used.

Savage
Apr 24 '07 #18
well when i put the while in it say i, n have not been defined
but with the dowhile it just says n have not been defined
Apr 24 '07 #19


int i = 0;

cout<< "Please enter a number" << endl;
cin >> i;

while(i == 0)
{
if(i>0)
esle
}
ur, it will print nothing...

in your code, the code inside while loop will be executed only when i ==0 .
Apr 24 '07 #20
JosAH
11,448 Expert 8TB
A while loop that doesn't display negative numbers:
Expand|Select|Wrap|Line Numbers
  1. for(;;) cout << 42;
kind regards,

Jos ;-)
Apr 24 '07 #21
Savage
1,764 Expert 1GB
well when i put the while in it say i, n have not been defined
but with the dowhile it just says n have not been defined
But in my code n is defined:

int n;

Post that part here.

Savage
Apr 24 '07 #22
Savage
1,764 Expert 1GB
A while loop that doesn't display negative numbers:
Expand|Select|Wrap|Line Numbers
  1. for(;;) cout << 42;
kind regards,

Jos ;-)
Yes offcourse.
How can i forget this.This is briliant.

He,he.

Good one Jos ;-)

How about this:

Expand|Select|Wrap|Line Numbers
  1. forever(;;) cout <<42;
Savage
Apr 24 '07 #23
Banfa
9,065 Expert Mod 8TB
A while loop that doesn't display negative numbers:
Expand|Select|Wrap|Line Numbers
  1. for(;;) cout << 42;
Strictly speaking that is a for loop not a while loop, when I saw this post I had

Expand|Select|Wrap|Line Numbers
  1. while(1)
  2.     ;
  3.  
in mind. The point these posts Greatness is that your original question (specification) is so loose (poorly specified) that many many constructs will fit what you have said although most of them are unlikely to do anything close to what you want.

When you post your next question please try to be a little more precise.
Apr 24 '07 #24

Sign in to post your reply or Sign up for a free account.

Similar topics

7
by: pj | last post by:
Why does M$ Query Analyzer display all numbers as positive, no matter whether they are truly positive or negative ? I am having to cast each column to varchar to find out if there are any...
5
by: Subrahmanyam Arya | last post by:
Hi Folks , I am trying to solve the problem of reading the numbers correctly from a serial line into an intel pentium processor machine . I am reading 1 byte and 2byte data both positive...
15
by: jaks.maths | last post by:
How to convert negative integer to hexadecimal or octal number? Ex: -568 What is the equivalent hexadecimal and octal number??
6
by: kydavis77 | last post by:
i was wondering if anyone could point me to some good reading about the for and while loops i am trying to write some programs "Exercise 1 Write a program that continually reads in numbers...
29
by: garyusenet | last post by:
I'm trying to investigate the maximum size of different variable types. I'm using INT as my starting variable for exploration. I know that the maximum number that the int variable can take is:...
2
by: itsmee | last post by:
I have a couple of issues with this program: 1. i'm not sure on how to write so that if the users_num is greater than 21 it should ask if the user would want to play again. i tried putting break;...
4
by: danbuttercup | last post by:
Hi everyone I just recently learnt how to do while loops in my java class and I am completely lost. I have to make programs for the following questions but I have no idea were to start. ...
12
by: beatjunkie27 | last post by:
I am working on a class assignment called Pennies for Pay the object of the program is to receive input for number of days worked. This should be > 0 and <= 40. An example output is below and...
5
by: imish06 | last post by:
This is the question: "The user must enter a non-negative number to add to the lists or a negative number to stop the program. For every non-negative number entered, place each even and odd...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.