J
Jack
Ok, here's the deal...my instructor wants us to do the following
program. Only one problem: what must I use to get this program to work
right? Do I use combinations of if, for and while statements, or just
if statements or what? I have tried nested if and while statements and
CANNOT get the program to execute properly... Perhaps an experienced
programmer(unlike myself) could give me a little insight on how to do
this problem...
ANY help would greatly be appreciated. If there's nething i can help
someone else with...i will...THANK YOU
COMSC 1613 Programming I (C) Programming Assignment
3
In this assignment, you will write a 'C' function that will approximate
the action of the command scanf("%d",&x); namely, to read ASCII
character
digits from the standard input stream and convert/interpret the sequence
into a binary integer.
Consider the following implementation of main.
#include
#define INVALID 0x80000000
long scan_int(void); /* the prototype */
int main(void)
{long num, i;
printf("\nenter an integer: ");
for(i=1; (num = scan_int()) != -999; printf("\n\n\nenter an integer: "))
{ if (num == INVALID)
printf("\n you entered an invalid sequence\n");
else
{
printf("\nthe %ld-th integer read was %ld\n", i, num);
++i;
}
}
return (0);
}
Use this main() as a driver. You write function
long scan_int(void);
The function scan_int will use getchar() to read one character at a time
from the standard input and from the digits read compute "on the fly"
the
integer value they represent. The function will return the integer
computed. If the data entered is not appropriate to be interpreted as
an
integer, the function will return INVALID as defined in the program.
White space will be used to mark the beginning and end of a number.
Do not use arrays/strings and do not use atoi() and do not use scanf()
or
fscanf() or any other library functions other than getchar(). Comment
your code thoroughly.
The following is an example execution of the program you will write:
Script command is started on Fri Aug 19 16:19:47 CDT 2011.
$ a.out
enter an integer: 457
the 1-th integer read was 457
enter an integer: -34
the 2-th integer read was -34
enter an integer: +1
the 3-th integer read was 1
enter an integer:
-967835
the 4-th integer read was -967835
enter an integer: 45h5
you entered an invalid sequence
enter an integer: ---56
you entered an invalid sequence
enter an integer: 45+9
you entered an invalid sequence
enter an integer: hello
you entered an invalid sequence
enter an integer: -
you entered an invalid sequence
enter an integer: +
you entered an invalid sequence
enter an integer: -999
$
program. Only one problem: what must I use to get this program to work
right? Do I use combinations of if, for and while statements, or just
if statements or what? I have tried nested if and while statements and
CANNOT get the program to execute properly... Perhaps an experienced
programmer(unlike myself) could give me a little insight on how to do
this problem...
ANY help would greatly be appreciated. If there's nething i can help
someone else with...i will...THANK YOU
COMSC 1613 Programming I (C) Programming Assignment
3
In this assignment, you will write a 'C' function that will approximate
the action of the command scanf("%d",&x); namely, to read ASCII
character
digits from the standard input stream and convert/interpret the sequence
into a binary integer.
Consider the following implementation of main.
#include
#define INVALID 0x80000000
long scan_int(void); /* the prototype */
int main(void)
{long num, i;
printf("\nenter an integer: ");
for(i=1; (num = scan_int()) != -999; printf("\n\n\nenter an integer: "))
{ if (num == INVALID)
printf("\n you entered an invalid sequence\n");
else
{
printf("\nthe %ld-th integer read was %ld\n", i, num);
++i;
}
}
return (0);
}
Use this main() as a driver. You write function
long scan_int(void);
The function scan_int will use getchar() to read one character at a time
from the standard input and from the digits read compute "on the fly"
the
integer value they represent. The function will return the integer
computed. If the data entered is not appropriate to be interpreted as
an
integer, the function will return INVALID as defined in the program.
White space will be used to mark the beginning and end of a number.
Do not use arrays/strings and do not use atoi() and do not use scanf()
or
fscanf() or any other library functions other than getchar(). Comment
your code thoroughly.
The following is an example execution of the program you will write:
Script command is started on Fri Aug 19 16:19:47 CDT 2011.
$ a.out
enter an integer: 457
the 1-th integer read was 457
enter an integer: -34
the 2-th integer read was -34
enter an integer: +1
the 3-th integer read was 1
enter an integer:
-967835
the 4-th integer read was -967835
enter an integer: 45h5
you entered an invalid sequence
enter an integer: ---56
you entered an invalid sequence
enter an integer: 45+9
you entered an invalid sequence
enter an integer: hello
you entered an invalid sequence
enter an integer: -
you entered an invalid sequence
enter an integer: +
you entered an invalid sequence
enter an integer: -999
$