Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c++ > #6615 > unrolled thread
| Started by | papoupapa <papoupapoug@gmail.com> |
|---|---|
| First post | 2011-06-11 03:53 -0700 |
| Last post | 2011-06-12 16:25 -0700 |
| Articles | 5 — 4 participants |
Back to article view | Back to comp.lang.c++
cin and scanf which is best for simple input papoupapa <papoupapoug@gmail.com> - 2011-06-11 03:53 -0700
Re: cin and scanf which is best for simple input "osmium" <r124c4u102@comcast.net> - 2011-06-11 12:06 -0500
Re: cin and scanf which is best for simple input "Sprechen Sie C++" <spamabuse@hotmail.com> - 2011-06-11 10:50 -0700
Re: cin and scanf which is best for simple input Kai-Uwe Bux <jkherciueh@gmx.net> - 2011-06-11 20:29 +0200
Re: cin and scanf which is best for simple input papoupapa <papoupapoug@gmail.com> - 2011-06-12 16:25 -0700
| From | papoupapa <papoupapoug@gmail.com> |
|---|---|
| Date | 2011-06-11 03:53 -0700 |
| Subject | cin and scanf which is best for simple input |
| Message-ID | <a0468c57-e5e2-4076-a27b-008a19b533d8@g28g2000yqa.googlegroups.com> |
Hi,
Can someone tell from the code below why scanf solves the issue of a
number out of
the range and cin seems to read twice a great number such as
100000000000 or go into a wild loop if a character or some text is
entered.
#include "std_lib_facilities.h"
int main()
{
int val1 = 0 ;
int val2 = 0 ;
int nb_try ;
bool ok = 0 ;
cout << "\nThis code reads 2 numbers and takes these 2 numbers into
simple
processes : \n" ;
nb_try = 3 ;
do
{
cout << "\nPlease enter 1st number (between 0 and 65,535) : \n" ;
//scanf("%d", &val1) ;
cin >> val1 ;
if ((val1 >= 0) && (val1 < 65536))
ok = 1 ;
else
{
ok = 0 ;
nb_try-- ;
}
if (!ok)
{
cout << "\nEntry outside 0-65535 range\n" ;
cout << nb_try << " left\n\n" ;
}
} while ((!ok) && (nb_try >= 0)) ;
nb_try = 3 ;
do
{
cout << "\nPlease enter 2nd number between 0 and 65,535) : \n" ;
//scanf("%d", &val2) ;
cin >> val2 ;
if (((val1 >= 0) && (val1 < 65536)) && ((val2 >= 0) && (val2 <
65536)))
ok = 1 ;
else
{
ok = 0 ;
nb_try-- ;
}
if (!ok)
{
cout << "\nEntry outside 0-65535 range\n" ;
cout << nb_try << " left\n\n" ;
}
} while ((!ok) && (nb_try >= 0)) ;
cout << "\n\n" ;
system("pause") ;
return 0 ;
}
[toc] | [next] | [standalone]
| From | "osmium" <r124c4u102@comcast.net> |
|---|---|
| Date | 2011-06-11 12:06 -0500 |
| Message-ID | <95hlk9Fpu4U1@mid.individual.net> |
| In reply to | #6615 |
"papoupapa" wrote::
<snippage>
> Can someone tell from the code below why scanf solves the issue of a
> number out of
>
> the range and cin seems to read twice a great number such as
> 100000000000 or go into a wild loop if a character or some text is
> entered.
>
> #include "std_lib_facilities.h"
>
> int main()
> {
> int val1 = 0 ;
> int val2 = 0 ;
> int nb_try ;
> bool ok = 0 ;
>
> cout << "\nThis code reads 2 numbers and takes these 2 numbers into
> simple
I suggest you simplify things. You seem to think cin does something wrong.
Write a *current* conforming C++ program with cin and only cin which
exhibits the problem. Don't complicate things with a second program using
scanf. In general, the use of scanf is discouraged. WRT conforming:
o your include file is not standard.
o your program will not compile. A simple cout will no longer be accepted.
My wild guess is that your use of "solve" is misguided.
[toc] | [prev] | [next] | [standalone]
| From | "Sprechen Sie C++" <spamabuse@hotmail.com> |
|---|---|
| Date | 2011-06-11 10:50 -0700 |
| Message-ID | <2WNIp.2812$8G4.2633@newsfe17.iad> |
| In reply to | #6623 |
Might be a better idea to use #include <iostream>; std::cout << mydata << std::endl; easier to work with and 100x more maintainable
[toc] | [prev] | [next] | [standalone]
| From | Kai-Uwe Bux <jkherciueh@gmx.net> |
|---|---|
| Date | 2011-06-11 20:29 +0200 |
| Message-ID | <it0c5i$nm3$1@hoshi.visyn.net> |
| In reply to | #6615 |
papoupapa wrote:
> Hi,
>
> Can someone tell from the code below why scanf solves the issue of a
> number out of
>
> the range and cin seems to read twice a great number such as
> 100000000000 or go into a wild loop if a character or some text is
> entered.
>
> #include "std_lib_facilities.h"
>
> int main()
> {
> int val1 = 0 ;
> int val2 = 0 ;
> int nb_try ;
> bool ok = 0 ;
>
> cout << "\nThis code reads 2 numbers and takes these 2 numbers into
> simple
>
> processes : \n" ;
>
> nb_try = 3 ;
> do
> {
> cout << "\nPlease enter 1st number (between 0 and 65,535) : \n" ;
> //scanf("%d", &val1) ;
> cin >> val1 ;
>
> if ((val1 >= 0) && (val1 < 65536))
You test whether val1 is in the required range. However, you have no test
for whether the reading from cin succeeded. You could try:
if ( ( cin >> val1 ) && ( val1 >= 0 ) && ( val1 < 65536 ) ) {
...
}
[snip]
Best,
Kai-Uwe Bux
[toc] | [prev] | [next] | [standalone]
| From | papoupapa <papoupapoug@gmail.com> |
|---|---|
| Date | 2011-06-12 16:25 -0700 |
| Message-ID | <4408011f-1291-4d82-b74c-57f42d589252@e14g2000yqa.googlegroups.com> |
| In reply to | #6626 |
thanks
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.c++
csiph-web