0

Here is a simple piece of code. actually it is a "fill array" function code.

#include <iostream>
 using namespace std;
int main(){ 
   int size = 10; a[10]; numberUsed;
   cout << "Enter up to " << size << " nonnegative whole numbers.\n"
   << "Mark the end of the list with a negative number.\n";
   int next, index = 0;
   cin >> next;
   while ((next >= 0) && (index < size)){
      a[index] = next;
      index++;
      cin >> next;
   }
  numberUsed = index;
  for(int i = 0 ; i < numberUsed -1 ; i++){
 cout << a[i]  << endl;
  }
}

it works fine when user enters integers. But when i enter double values it should trancate that particular value. and repeats that value for the next entered integers also. so now for input 1 2 3 4 5 6.5 7 8 9 -1 .I get the following output 1 2 3 4 5 6 6 6 6 6 Any help will be appreciated.

6
  • Your program does not compile. Also, why do you need numberUsed? Commented Jun 11, 2013 at 16:32
  • 2
    My guess: It fails to parse the "." because there are no "."s in an integer and leaves it stuck in the stream pipeline. so next is not modified (stays at 6) and is stored over and over Commented Jun 11, 2013 at 16:34
  • The program compiles if you replace the semicolons with commas in line 4. The poster may be using a non-standard compiler that accepts that syntax. Commented Jun 11, 2013 at 16:35
  • 1
    I'm getting 1 2 3 4 5 6 0 0 0 0 when I enter the given code with the given input. I can confirm that @AK4749 is correct in that cin has an error flag. It's weird that my output is different. Commented Jun 11, 2013 at 16:43
  • apologies for semi colons... there are commas in place of those semicolons.. Commented Jun 11, 2013 at 16:48

2 Answers 2

3

You told cin to read an integer, so that's what it's going to do - it will stop as soon as it sees a character that isn't valid for an integer. In this case it's the '.'. Trying to read more integers will just continue to fail, in your case leaving next at its previous value.

If you want to truncate a floating point value, read into a floating point variable and then do the truncation yourself.

double next;
...
a[index] = (int) next;
Sign up to request clarification or add additional context in comments.

5 Comments

oh.. so if i define a variable of int type. cin is actually "conscious" about what the input is given ??? is that what you are saying?
@user128806 yep! that's the beauty of it. Otherwise, you would just use printf() which can be faster in most cases
@MarkRansom +1 I have come to recognize you and your "exactness" around here, and was wondering if you could comment on the 6 vs 0 outcomes in this case. I suspect it is UB to read from a failed stream, but do not have access to the standard. Would you be willing to provide such a clarification? Thanks
@AK4749, thanks for the vote of confidence, but I'm actually more of a pragmatist than a language lawyer. According to en.cppreference.com/w/cpp/io/basic_istream/operator_gtgt the defined behavior changed in C++11 to always return zero, prior to that it was left unchanged.
@MarkRansom perfect, that's all I wanted to hear haha - that explains the discrepancy exactly
2

When reading integer values (and your code is using int next;, anything that isn't an integer will "stop" the input from every reading anything else. Since next is 6 at the point when it hits a '.' in the input stream, it continues to fill the array with that value until it runs out of space.

If you check the status of the input, e.g. if(!cin >> next) ... error handling ...;, you can detect when things have gone wrong. As part of the error handling, you should then "ignore any input until a whitespace" - cin.ignore(1000, ' '); would be a good start. You will also need to reset the error flag on cin to not get an error next time, with cin.clear() will do that.

If you want to input floating point numbers, you need to use float or double type for next (and the array a).

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.