0

In the past, I have been programming in c# but now I am learning c++. Why the while loop exit in c++ when I enter a string and in c# if I don't use tryparse it will give an exception. Does the c++ explicitly do something like tryparse in the backend ? Sorry if my question I am using codeblocks as my c++ IDE.

I am reading the book c++ primes and code a simple program that continuously takes integer as input until a string is entered. I wrote the same code in c# but it leads me to an error. So I have to use tryparse method in C#.

int value = 0;

In C++

while( cin >> value );

In C#

while( value  == int.parse(Console.ReadLine());
Riven Callahan
  • 107
  • 1
  • 8

2 Answers2

1

Why the while loop exit in c++ when I enter a string

operator>> performs error handling internally. If it fails to extract an integer, the stream enters a failure state, and the loop is checking the stream's state, so it exits when the stream fails.

in c# if I don't use tryparse it will give an exception.

Yes, because that is the way int.parse() is defined to work.

You can get similar behavior in C++ by enabling exceptions in the stream . That way, if an extraction failure occurs, a std::ios_base::failure exception is thrown.

Does the c++ explicitly do something like tryparse in the backend ?

In a way, yes.

I am reading the book c++ primes and code a simple program that continuously takes integer as input until a string is entered. I wrote the same code in c# but it leads me to an error.

Your C++ and C# codes are not equivalent.

Your C# code reads an entire line as-is, discarding the line break, and then tries to convert the entire line as-is to an int.

Your C++ code discards leading whitespace - including line breaks - until it encounters a non-whitespace character, then it tries to read an int value, and whatever follows after it - including a line break - remains in the stream for subsequent reads.

So I have to use tryparse method in C#.

If you don't want a failed conversion to throw an exception, then yes.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
0

C++ knows to parse for an integer because you defined the variable "value" as type int.

From the C++ reference:

As an object of class istream, characters can be retrieved either as formatted data using the extraction operator (operator>>) or as unformatted data, using member functions such as read.

0xef
  • 23
  • 8
  • I know I can also achieve the same behavior with tryparse but what I want to know how c++ knows that it is not a integer and without throwing an exception he is handling the character. – Riven Callahan Apr 07 '19 at 19:17
  • @RivenCallahan Pardon, I read the question the other way around. C++ knows to parse for an integer because you defined the variable "value" as type int. – 0xef Apr 07 '19 at 19:20
  • means C++ can handle these format exceptions in C# without any type of explicit code ? – Riven Callahan Apr 07 '19 at 19:25
  • @RivenCallahan Yes, the stream extraction operation has exception handling, see http://www.cplusplus.com/reference/istream/istream/operator%3E%3E/ – 0xef Apr 07 '19 at 19:30