English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

I need to loop a cin until the use inputs no number. what does the if statement have to be to do this? i tried if input==NULL but with integers, NULL = 0. i don't want to have that because 0 is such a common number. is this possible?

thus, i've written:

bool check;
int input;

while(check)
{
cin >> input;
if(input==????)
check=false
else
list.insertNode(input);
}

2006-10-18 15:46:52 · 3 answers · asked by ifoam 3 in Computers & Internet Programming & Design

what im doing is allowing a user to input as many integers as they want. they choose when to stop. then i will add all the integers to a linkedlist

2006-10-18 16:03:10 · update #1

3 answers

Hi :)

Strings have nothing to do with your question, ignore that previous answer.

I would suggest making your sentinel the number "-1".

You are working with a loop to insert numbers into a list, right?

Here's my suggestion. You don't need to use a boolean value to control the loop, though you can if you want. Instead, you could just simply test the number with a conditional statement (like
!=). Conditional statements return boolean variables anyways - For example:

int input;

cin >> input; // priming read

while (input != -1)
{
list.insertNode(input)
cin>> input; // internal read
} // end while

The first cin reads an integer in, and then decides what to do with it. The second one does the same for the rest of the cases.

Hope that helps :)

Rob

2006-10-18 16:14:17 · answer #1 · answered by Rob 3 · 1 0

What kind of user input are you getting? C string or C++ string? If you're using a C string you can use the isdigit property and cycle through each character until finding a digit -- http://www.cppreference.com/stdstring/isdigit.html

Or if you don't want to use the properties you can just check each individual char and check if it's 0-9. Just do a For loop inside a For loop with a switch statement inside.

2006-10-18 22:57:39 · answer #2 · answered by Robin C 4 · 0 1

did you try...

IF(!input)

2006-10-18 23:23:48 · answer #3 · answered by CHEVICK_1776 4 · 0 0

fedest.com, questions and answers