simple code experimentaion : if condition not taken into consideration

B

bpascal123

Hi,

The code below doesn't display "Ending here" when the keyboard input
is '|'...Any reasons?

#include <stdio.h>

int main(void)
{
int a = 0 ;
int b = 0 ;

char sign = '|' ;
int stop = sign ;

printf("\nEnter 2 integers : \n") ;

while ((scanf("%d %d", &a, &b)) != 0)
{
if ((a == stop) || (b == stop))
{
printf("\nEnding here\n") ; // <== it skips this line
break ;
}
printf("\n%d%d\n", a, b) ;
}

return 0 ;
}

Pascal
 
B

bpascal123

Actually, it would quit at anything that is not an integer. There
should be some controls, here but i'm focusing on the if condition
that doesn't display "ending here" on the screen...
 
J

Jens Thoms Toerring

bpascal123 said:
The code below doesn't display "Ending here" when the keyboard input
is '|'...Any reasons?
#include <stdio.h>
int main(void)
{
int a = 0 ;
int b = 0 ;
char sign = '|' ;
int stop = sign ;

You could also just use

int stop = '|';

- under these conditions the '|' character (which is
an integer type) will be converted to a number.
printf("\nEnter 2 integers : \n") ;
while ((scanf("%d %d", &a, &b)) != 0)

This asks scanf() to read two integers, so 'a' or 'b' can
only have the same value as 'stop' if one of those numbers
is actually identical to what the '|' character is encoded
as (if your system is using ASCII that would be 124).

It looks a bit as you would expect that entering '|' would
result in some numerical value derived from '|' getting
stored in 'a' or 'b'. That's not the case. With the format
string you pass it scanf() expects integer numbers, i.e.
something that optionally starts with '-' or '+' and is
immediately followed by one or more digits. Nothing else
will get accepted for '%d'. And if you don't start the
input with something that could be interpreted as an
integer number scanf() will return 0 since it then don't
even starts to read in anything.
{
if ((a == stop) || (b == stop))
{
printf("\nEnding here\n") ; // <== it skips this line

No, unless you entered 124 as one of the numbers (assuming
your system uses ASCII) the if-condition will never be
test true. so that line isn't "skipped" but never reached.
break ;
}
printf("\n%d%d\n", a, b) ;
}
return 0 ;
}
Regards, Jens
 
K

Keith Thompson

bpascal123 said:
The code below doesn't display "Ending here" when the keyboard input
is '|'...Any reasons?

#include <stdio.h>

int main(void)
{
int a = 0 ;
int b = 0 ;

char sign = '|' ;
int stop = sign ;

printf("\nEnter 2 integers : \n") ;

while ((scanf("%d %d", &a, &b)) != 0)
{
if ((a == stop) || (b == stop))
{
printf("\nEnding here\n") ; // <== it skips this line
break ;
}
printf("\n%d%d\n", a, b) ;
}

return 0 ;
}

Why would it?

scanf with a "%d" format reads an integer from stdin, represented as a
sequence of decimal digits preceded by an optional sign. It won't
accept a '|' character.

Try entering 124 (that's 3 characters, '1', '2', and '4') as one of the
inputs and see what happens. (The ASCII encoding for '|' is 124.)
 
J

Joe Pfeiffer

bpascal123 said:
Hi,

The code below doesn't display "Ending here" when the keyboard input
is '|'...Any reasons?

<snip>

I suspect that in the code I snipped you meant %c, not %d.
 
B

bpascal123

Thanks for this, this tells me I have to keep close to the basics of
the language.
The initial code is below. As you can see, it's a cpp code. Maybe I
should post this on the cpp discussion but I have already posted a
discussion about the code here, it makes the thread really look like a
thread. And for most here i guess, dealing with this is a little
boring recreation...

On the cpp code below, if I enter a number out of the positive integer
range, the code code goes into an infinite loop. I'd like to
understand the reason as there are validation controls that should
prevent this infinite loop.

#include "std_lib_facilities.h"

int main()
{
int val1 = 0 ;
int val2 = 0 ;
int stop = 0 ;
string cont = " " ;
bool ok = 1 ;

//cout << "\nValeur int de stop : " << stop ;
//cout << "\nValeur char de stop : " << sign ;

while (cont != "|")
{
cout << "\nEntrez 2 nombres (compris entre 0 et 65,536) : \n" ;
cin >> val1 >> val2 ;

if ( ((val1 >= 0) && (val1 < 65536)) && ((val2 >= 0) && (val2 <
65536)) )
ok = 1 ;
else
ok = 0 ;

while (!ok)
{
cout << "\nSaisie en dehors des limites\n" ;
cout << "\nEntrez 2 nombres (compris entre 0 et 65,536) : \n" ;
cin >> val1 >> val2 ;

if ( ((val1 >= 0) && (val1 < 65536)) && ((val2 >= 0) && (val2 <
65536)) )
ok = 1 ;
else
ok = 0 ;
}

cout << "\nvaleur 1 : " << val1 << "\tvaleur 2 : " << val2 << "\n" ;

cout << "\nAppuyer sur une touche : \n" ;
cin >> cont ;
}

cout << "\nFin\n" ;

cout << "\n\n" ;
system("pause") ;
return 0 ;
}

I don't know if this is again something I have dealt some time ago
that I am missing...

Thanks for your help,
Pascal
 
K

Keith Thompson

bpascal123 said:
Thanks for this, this tells me I have to keep close to the basics of
the language.
The initial code is below. As you can see, it's a cpp code. Maybe I
should post this on the cpp discussion but I have already posted a
discussion about the code here, it makes the thread really look like a
thread. And for most here i guess, dealing with this is a little
boring recreation...

On the cpp code below, if I enter a number out of the positive integer
range, the code code goes into an infinite loop. I'd like to
understand the reason as there are validation controls that should
prevent this infinite loop.

By "cpp", you mean C++, right? Yes, please post to comp.lang.c++, or
perhaps to alt.comp.lang.learn.c-c++. C and C++ are two different
(though closely related) languages.

[snip]
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top