Click here to Skip to main content
15,884,353 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I'm trying to understand while loop, and the difference when I put "!" and "false" as a condition because somehow they produced different result for me.

I'm writing this very simple program to sum an array of numbers, and I want to check if my input is integer or not.

Flowchart :
    1. Ask user how many numbers to sum
    2. Get user input (check if this is integer or not)
    3. (loop) ask user to input each value of number
    4. sum them


What I have tried:

This is my code, I'm using `isdigit` to check whether an input is an integer, and check it using while (isdigit(array_size==false))

C++
#include <iostream>
using std::cout;
using std::cin;
using std::endl;

int array_size;
int sum = 0;

int main(){
    cout << "How many numbers ? " << endl;
    cin >> array_size;

    while (isdigit(array_size==false)){
        cout << "Input is not a number " << endl;
        break;
    } 
        int sum_array[array_size];
        for (int n=0; n < array_size; n++){
            cout << "Number " << n+1 << " is : ";
            cin >> sum_array[n];
            sum+=sum_array[n];
        }
        cout << "Sum of numbers are : " << sum << endl;
    return 0;
}

Here's the result when I tried to input integer and non-integer :
lock@lock ./a.out     
How many numbers ? 3
Number 1 is : 1
Number 2 is : 2
Number 3 is : 3
Sum of numbers are : 6

lock@lock ./a.out 
How many numbers ? a
Sum of numbers are : 0

See that in the second case when I put a non-integer, my line
cout << "Input is not a number " << endl;
is not being executed.

Now if I changed my condition with while (!isdigit(array_size)), here's the result :
lock@lock ./a.out 
How many numbers ? 3
Input is not a number 
Number 1 is : 1
Number 2 is : 2
Number 3 is : 3
Sum of numbers are : 6

lock@lock ./a.out 
How many numbers ? a
Input is not a number 
Sum of numbers are : 0

Apparently the line
cout << "Input is not a number " << endl;
will be executed no mater what the input (even if it's integer). What did I miss here ? How did "!" different from "false".
Posted
Updated 24-Sep-20 20:39pm
v2
Comments
KarstenK 25-Sep-20 5:48am    
the code is confusing. You need to improve the check and the code flow on an error.

You have:
C++
isdigit(array_size==false)


This should be
C++
isdigit(array_size) == false


Note the placement of the brackets.
 
Share this answer
 
Comments
lock&_lock 25-Sep-20 14:39pm    
Thanks, I tried your advice, but the line "cout << "Input is not a number " << endl;" still gets printed even if I put integer as an input
I suspect you want to replace this
C++
while (isdigit(array_size==false)){

with this
C++
while (isdigit(array_size)==false){

But the logic of the loop look rather confuse. May be it is not a loop that you want.
 
Share this answer
 
v2
Comments
lock&_lock 25-Sep-20 14:40pm    
This is similar to Soution 1. I tried this, but the line "cout << "Input is not a number " << endl;" still gets printed even if I put integer as an input
Patrice T 25-Sep-20 14:41pm    
Yes Solution 1 was posted 1 minute before mine.
The argument of isdigit is supposed to be a character, but you are passing it a int variable.
Assuming your code should really deal with numbers (that is integers) and not digits, you might re-write your program this way (note you don't need an array):
C++
#include <iostream>
#include <limits>

using std::cout;
using std::cin;
using std::endl;


int main()
{
  int size;
  int sum = 0;

  cout << "How many numbers ? " << endl;

  for (;;)
  {
    cin >> size;
    if ( cin ) break;
    cin.clear();
    cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
    cout << "Input is not a number " << endl;
  }

  for (int n = 0; n < size; ++n)
  {
    int i;
    cout << "Number " << (n+1) << " is : ";
    cin >> i;
    sum += i ;
  }
  cout << "Sum of numbers is : " << sum << endl;
}
 
Share this answer
 
v2
Comments
lock&_lock 25-Sep-20 14:44pm    
Hi, thanks. Unfortunately, it is instructed for me to use array. Also @Stefan_Lang addressed this issue of argument for "isdigit" in comment section of Solution 3, here's his/her answer :

Actually, the argument type of isdigit is int. See https://www.cplusplus.com/reference/cctype/isdigit/

Your code will work (change it to char) because char gets auto-promoted to int. But declaring array_size as int is correct.
isdigit() takes in ASCII character though the parameter type is integer. Just change the type of array_size from int to char should work.
C++
char array_size;
...
cin >> array_size;
...
while (isdigit(array_size)==false){...

or
C++
char array_size;
...
cin >> array_size;
...
while (!isdigit(array_size)){...

should work.
 
Share this answer
 
v2
Comments
Stefan_Lang 25-Sep-20 6:24am    
Actually, the argument type of isdigit is int. See https://www.cplusplus.com/reference/cctype/isdigit/

Your code will work because char gets auto-promoted to int. But declaring array_size as int is correct.
CPallini 25-Sep-20 19:41pm    
Yes, the argument of isdigit is an int, however the function returns non-zero if the argument is in the range 0x30-0x39 (that is '0'..'9'). It is not useful to check if cin actually got an integer value.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900