Path: csiph.com!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail From: Keith Thompson Newsgroups: comp.lang.c++ Subject: Re: rational numbers Date: Thu, 23 Sep 2021 16:56:52 -0700 Organization: None to speak of Lines: 106 Message-ID: <87zgs2lubf.fsf@nosuchdomain.example.com> References: Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit Injection-Info: reader02.eternal-september.org; posting-host="cb76b4cf9eef9ed26a2d895456785e0e"; logging-data="15352"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/q6C6CSAkqbhGEa3YNwAKf" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.2 (gnu/linux) Cancel-Lock: sha1:qf/bxnWwSRKIwp3x4yK1aCSNJuQ= sha1:xhoT4SDtYYgjU+7Xf1LBBVyy8SU= Xref: csiph.com comp.lang.c++:81494 Bart writes: > On 23/09/2021 21:48, Ian Collins wrote: >> On 24/09/2021 02:02, Bart wrote: [...] >>> This is a similar example in Basic: >>> >>>     for i=1 to 3 >>>       print "Prompt> ";         rem ";" suppresses the newline >>>       input a,b,c               rem I think this grabs a fresh line >>>       print a,b,c >>>     next i >>> >>> It works better than the C++! And better than C's scanf which everyone >>> tries to avoid using. >> How is that fundamentally different from >>   for( int i = 0; i < 3; ++i ) >>   { >>     std::cout << "Prompt> "; >>     std::cin >> a >> b >> c; >>     std::cout << a << ' ' << b << ' ' << c << '\n'; >>   } >> ? > > Haven't you followed the thread? A lot of things have been pointed > out. But here's a summary of issues with the C++ loop: > > > * It's not line-oriented; the new lines get out of sync with the data > being read Right, it's not designed to be. For example: int a, b, c; std::cin >> a >> b >> c; std::cout << "a=" << a << " b=" << b << " c=" << c << '\n'; The std::cin line reads integer values, skipping white space (which includes newlines) before each. If you just want to skip white space other than newlines, there's probably a way to do that. If you want line-oriented input, read a line at a time using std::getline() and then parse each line. It's not that hard. std::string line; std::getline(std::cin, line); std::stringstream ss(line); ss >> a >> b >> c; > * If fewer than 3 items are present on a line, then it apparently > hangs, with no explanation, waiting for them on the next line Because it's not line-oriented. > * If more than 3 items are present on a line, then those aren't > discarded, but are confusingly used as input for a, b, c for the > next line. So if 400 and 500 are extra items, and the user enters 10 > 20 30 on the next line, if will read '400 500 10', with 20 and 30 > rolled over to the subsequent line Because it's not line-oriented. > * If more than 3 extra items are present, then on the next iteration, > it doesn't wait for user input at all. If will not do so until > everything on that initial line is consumed Because it's not line-oriented. > * Numeric separators within numbers such as "_" and "'" are not > recognised, and cause an error Right. Just how permissive do you think it should be? > * Numbers which are quoted are not recognised, and cause an error Right. 123 is a number; "123", “123”, '123', and «123» are not. > * Separators between numbers other than white space are not > recognised, such as commas, and cause an error Right. > * Floating point numbers (using "." and/or "e") are not recognised; > those characters cause an error Right. You can read into a floating-point object if you want to support floating-point syntax. > * When an out-of-range number is entered, it reads i32.max (etc) for > that number, but also generates an error Yes, and? > * I mentioned error a few times, when that happens, it goes crazy. I > think the internal pointer is not stepped past it, and it just reads > zeros, so it never consumes the rest of the line. So in a loop, it > just prints zeros over and over again without the user entering > anything. It's difficult to respond to that without an example. [...] -- Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.com Working, but not speaking, for Philips void Void(void) { Void(); } /* The recursive call of the void */