The problem professor gave us is:
Write a program which reads two integer values. If the first is less
than the second, print the message "up". If the second is less than
the first, print the message "down" If the numbers are equal, print
the message "equal" If there is an error reading the data, print a
message containing the word "Error" and perform exit( 0 );
And this is what I wrote:
main()
{
int x;
int y;
printf("Enter the first integer: ");
scanf("%d", &x);
printf("Enter the second integer: ");
scanf("%d", &y);
if (x > y)
{
printf("Up");
}
else
{
if (x < y)
{
printf("Down");
}
else
{
if (x = y)
{
printf("Equal") ;
}
else
{
printf("Error") ;
getch();
exit (0);
}
}
}
}
Works fine at first, but whenever I enter invalid characters such as
!, A, %, :, etc. It never asks me for the second integer and skip to
printf("Down");
Can anyone be kind enough to explain to me why that happened? 24 2373
On 2004-09-20, Apotheosis <he************ *@yahoo.com> wrote:
[ code using scanf snipped ]
Works fine at first, but whenever I enter invalid characters such as !, A, %, :, etc. It never asks me for the second integer and skip to printf("Down"); Can anyone be kind enough to explain to me why that happened?
Don't use scanf. Read the answer to question 12.20 in the C-faq
to find out why. http://www.eskimo.com/~scs/C-faq/top.html
-- James
Dude, you're reading the C-faq!
Apotheosis wrote: The problem professor gave us is:
Write a program which reads two integer values. If the first is less than the second, print the message "up". If the second is less than the first, print the message "down" If the numbers are equal, print the message "equal" If there is an error reading the data, print a message containing the word "Error" and perform exit( 0 );
And this is what I wrote:
main() { int x; int y;
printf("Enter the first integer: "); scanf("%d", &x);
printf("Enter the second integer: "); scanf("%d", &y);
.... snip ... Works fine at first, but whenever I enter invalid characters such as !, A, %, :, etc. It never asks me for the second integer and skip to printf("Down"); Can anyone be kind enough to explain to me why that happened?
Basically you have failed to detect the errors resulting from the
scanf calls. Those are both returning a single value, so they
should be of the form:
if (1 != scanf("%d", &x)) erroraction();
else {
....
}
Never use scanf without checking the return value. Also, main
returns an int. Say so. You should be using:
int main(void)
{
....
return 0;
}
--
Chuck F (cb********@yah oo.com) (cb********@wor ldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home .att.net> USE worldnet address!
Apotheosis <he************ *@yahoo.com> scribbled the following: The problem professor gave us is:
Write a program which reads two integer values. If the first is less than the second, print the message "up". If the second is less than the first, print the message "down" If the numbers are equal, print the message "equal" If there is an error reading the data, print a message containing the word "Error" and perform exit( 0 );
And this is what I wrote:
main() { int x; int y;
printf("Enter the first integer: "); scanf("%d", &x);
printf("Enter the second integer: "); scanf("%d", &y);
if (x > y) { printf("Up"); } else { if (x < y) { printf("Down"); } else { if (x = y)
Everyone other failed to spot this. You want == here, not =. = first
assigns y's value to x and then tests if x's value is anything other
than 0. This means that if execution gets this far, any value entered
to y at all, as far as it isn't 0, will print "Equal" regardless of
x's value. Because you already tested for x > y and x < y, this would
otherwise not matter, but it's going to break if you type 0 for both
x and y. In this case your program won't print anything at all.
{ printf("Equal") ; } else { printf("Error") ; getch(); exit (0); } } } }
Works fine at first, but whenever I enter invalid characters such as !, A, %, :, etc. It never asks me for the second integer and skip to printf("Down"); Can anyone be kind enough to explain to me why that happened?
--
/-- Joona Palaste (pa*****@cc.hel sinki.fi) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"I wish someone we knew would die so we could leave them flowers."
- A 6-year-old girl, upon seeing flowers in a cemetery
James Hu wrote: On 2004-09-20, Apotheosis <he************ *@yahoo.com> wrote: [ code using scanf snipped ]
Works fine at first, but whenever I enter invalid characters such as !, A, %, :, etc. It never asks me for the second integer and skip to printf("Down"); Can anyone be kind enough to explain to me why that happened?
Don't use scanf. Read the answer to question 12.20 in the C-faq to find out why.
I disagree.
/* BEGIN new.c */
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <limits.h>
#include <ctype.h>
#define LENGTH 100
#define str(x) # x
#define xstr(x) str(x)
#define VSIZE (sizeof variables / sizeof *variables)
int main(void)
{
int x, y, n, rc;
long temp;
char array[LENGTH + 1], *nptr;
struct V {
int *address;
char *string;
} variables[]= {{&x, "first"}, {&y, "second"}};
for (n = 0; VSIZE > n; ++n) {
printf("Enter the %s integer: ", variables[n].string);
fflush(stdout);
rc = scanf("%" xstr(LENGTH) "[^\n]%*[^\n]", array);
if (!feof(stdin)) {
getchar();
}
if (rc != 1) {
puts("Error");
exit(0);
}
nptr = array;
while (isspace(*nptr) ) {
++nptr;
}
if (*nptr == '+' || *nptr == '-') {
++nptr;
}
if (!isdigit(*nptr )) {
puts("Error_2") ;
exit(0);
}
errno = 0;
temp = strtol(array, NULL, 10);
if (errno != 0 || INT_MIN > temp || temp > INT_MAX) {
puts("Error_3") ;
exit(0);
}
*variables[n].address = (int)temp;
}
if (y > x) {
puts("Up");
} else {
if (x > y) {
puts("Down");
} else {
puts("Equal");
}
}
return 0;
}
/* END new.c */
--
pete
pete <pf*****@mindsp ring.com> writes: rc = scanf("%" xstr(LENGTH) "[^\n]%*[^\n]", array);
For portability reasons a propper #define for the linebreak would be
IMHO fine.
Kind regards,
Nicolas
--
| Nicolas Pavlidis | Elvis Presly: |\ |__ |
| Student of SE & KM | "Into the goto" | \|__| |
| pa****@sbox.tug raz.at | ICQ #320057056 | |
|-------------------University of Technology, Graz----------------|
Apotheosis wrote: The problem professor gave us is:
Write a program which reads two integer values. If the first is less than the second, print the message "up". If the second is less than the first, print the message "down" If the numbers are equal, print the message "equal" If there is an error reading the data, print a message containing the word "Error" and perform exit( 0 );
And this is what I wrote:
main() { int x; int y;
printf("Enter the first integer: "); scanf("%d", &x);
printf("Enter the second integer: "); scanf("%d", &y);
if (x > y) { printf("Up"); } else { if (x < y) { printf("Down"); } else { if (x = y)
here you should replace for x == y { printf("Equal") ; } else { printf("Error") ; getch(); exit (0); } } } } Works fine at first, but whenever I enter invalid characters such as !, A, %, :, etc. It never asks me for the second integer and skip to printf("Down"); Can anyone be kind enough to explain to me why that happened?
--
Felipe Magno de Almeida
UIN: 2113442
email: felipe.almeida@ ic unicamp br, felipe.m.almeid a@gmail com
I am a C, modern C++, MFC, ODBC, Windows Services, MAPI developer
from synergy, and Computer Science student from State
University of Campinas(UNICAM P).
To know more about :
Unicamp: http://www.ic.unicamp.br
Synergy: http://www.synergy.com.br
current work: http://www.mintercept.com
Apotheosis <he************ *@yahoo.com> spoke thus:
Your indentation is suboptimal, to put it mildly. main()
main() returns int. You should say so, and must do so under C99.
else { if (x = y) { printf("Equal") ; } else { printf("Error") ; getch(); exit (0); } } }
You are missing a return statement; you must explicitly return an
integer under C89. Notice that your call to exit() does not always
occur.
}
--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cybers pace.org | don't, I need to know. Flames welcome. he************* @yahoo.com (Apotheosis) writes: The problem professor gave us is:
Write a program which reads two integer values. If the first is less than the second, print the message "up". If the second is less than the first, print the message "down" If the numbers are equal, print the message "equal" If there is an error reading the data, print a message containing the word "Error" and perform exit( 0 );
And this is what I wrote:
main() { int x; int y;
printf("Enter the first integer: "); scanf("%d", &x);
printf("Enter the second integer: "); scanf("%d", &y);
[ rest of code snipped ]
} } Works fine at first, but whenever I enter invalid characters such as !, A, %, :, etc. It never asks me for the second integer and skip to printf("Down"); Can anyone be kind enough to explain to me why that happened?
No, but perhaps if you can answer these questions you might figure it
out:
1) what does scanf() do if the input doesn't match the requested
conversion?
2) does scanf have a return value? If so, what is it, and how might
you use that to help you with this problem?
--
Remove -42 for email
Nicolas Pavlidis wrote: pete <pf*****@mindsp ring.com> writes:
rc = scanf("%" xstr(LENGTH) "[^\n]%*[^\n]", array);
For portability reasons a propper #define for the linebreak would be IMHO fine.
I don't understand what you mean.
Is there something there which is not portable?
What linebreak?
--
pete This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Eric Gibson |
last post by:
I could have sworn this would be a FAQ, but it doesn't appear to be in
there. You must get this all the time, or I'm just totally doing something
wrong.
Math with int's and floats appears to just not work at all, much to my
frustration. :-( I'm trying to write a simple calculation and it just plain
outputs wrong information. Something like this:
function calculateValue(kWh) {
dollar_amount = kWh * .033;
|
by: Joe Peterson |
last post by:
I could not find another example of this via internet searches, so here
it is... I am wondering if this is a python bug or otherwise. The
first example of this happened in a larger program of mine, and the
traceback reports the problem at the start of a "for" loop (making no
sense), but I cannot easily include the full code and instructions here.
So I wrote a small test program containing the offending code, and in
this case the...
|
by: Stuart |
last post by:
Ok
let me explain: I am writing a c# program that calls into
an unmanaged C++ third-party DLL. I have to make a number
of calls and for simplification the protype I am calling
is:
bool fn(int *pResult);
This result is used by other subsequent functions that I
have to call.
Q: Is it possible to call this function from C# declaring
|
by: Matt |
last post by:
I want to know if "int" is a primitive type, or an object?
For example, the following two approaches yield the same result.
> int t1 = int.Parse(TextBox2.Text); //method 1
> int t2 = System.Int32.Parse(TextBox2.Text); //method 2
And people said "int" is a C# alias for "System.Int32". If this is the case,
can we say "int" is an object??
|
by: jacob navia |
last post by:
Hi
Suppose you have somewhere
#define BOOL int
and somewhere else
typedef BOOL int;
| |
by: qingning |
last post by:
Hi,
I've encountered code declaring functions with "const int" as parameter
type, but use "int" when defining the function. This worked fine with
g++, but failed at linking time with Sun CC. The following is an
example.
--cut--
$ cat constf.h
void f(const int);
|
by: hn.ft.pris |
last post by:
I know int *p mean an array of pointers to int, but I meet int
(*p) which is confusing.
What does it mean and how to use it? Thanks for help.
|
by: Lawrence Spector |
last post by:
I ran into a problem using g++. Visual Studio 2005 never complained
about this, but with g++ I ran into this error. I can't figure out if
I've done something wrong or if this is a compiler bug. Here's a very
simple example which should illustrate what I'm doing.
#include <iostream>
template <class T>
class TestBase
{
|
by: Anna |
last post by:
I try to put 8 int bit for example 10100010 into one character of type
char(1 octet) with no hope . Could anyone propose a simple way to do
it? Thank you very much.
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| | |