Question

ANSWER ASAP PLEASE Using only the standard input/output function fgetc(), define a C function, called int...

ANSWER ASAP PLEASE

Using only the standard input/output function fgetc(), define a C function, called int readInt(int *value), that reads an integer from the keyboard (stdin), stores it in *value and, returns 1 for success or 0 for fail (in case the user enters nondigit characters). In particular, readInt() reads all characters, one by one, until a blank (or newline) is encountered. The string is then converted to an integer. It is also possible to have the sign charcater (+/-) precedding the number

. Notes: • You need also to define a main() function that should look like the following one:

int main(int argc, char *argv[]){ : :

printf("Enter a first integer> ");

if (readInt(&n1))

printf("You enetered %d\n", n1);

else printf("Wrong input\n");

printf("Enter a secnd integer> ");

if (readInt(&n2))

printf("You enetered %d\n", n2); else printf("Wrong input\n");

printf("the sum of %d and %d is %d\n", n1, n2, n1+n2); exit(0); }

• Do not use any other function from the input/output standard library, and the string-to-integer conversion should be done using ASCII codes and multiplcations by 10.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

solution:

#include <stdio.h>
int readInt(int *value)
{
//if value is a digit using assci 48 is 0 and 57 is 9 return 1
if(*value>=48 && *value<=57)
return 1;
else
return 0;
}
int main()
{
char ch,sign='+';
int p=0;
while(1)
{
fflush(stdin);//fflush to remove buffer
ch=fgetc(stdin);//read from keyboard
if(p==0)//checking for sign + or -
{
//if it is at starting and captures sign
if(ch=='-')
sign='-';
}
//we will read from keyoard untol we get ' ' or '\n'
if(ch==' '||ch=='\n')
break;
int n=ch;//make it as int
if(readInt(&n))//pass int pointer reference
{
p=p*10+(ch-'0');//if it is a number multiply and add the int value of char
}
else
{
printf("Not a digit");//else it is not a digit
}
  
}
//print the number conversion after wards
printf("\nThe number after conversion is %c%d",sign,p);
return 0;
}

output

please give me thumb up

Add a comment
Know the answer?
Add Answer to:
ANSWER ASAP PLEASE Using only the standard input/output function fgetc(), define a C function, called int...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Convert C to C++ I need these 4 C file code convert to C++. Please Convert...

    Convert C to C++ I need these 4 C file code convert to C++. Please Convert it to C++ //////first C file: Wunzip.c #include int main(int argc, char* argv[]) { if(argc ==1){ printf("wunzip: file1 [file2 ...]\n"); return 1; } else{ for(int i =1; i< argc;i++){ int num=-1; int numout=-1; int c; int c1;    FILE* file = fopen(argv[i],"rb"); if(file == NULL){ printf("Cannot Open File\n"); return 1; } else{ while(numout != 0){    numout = fread(&num, sizeof(int), 1, file);    c...

  • /* * This program reads characters from stdin and writes them back to stdout. * Student...

    /* * This program reads characters from stdin and writes them back to stdout. * Student task: change code so that output is rotated output one position * to the left. Thus, if input is "abcd", output should be "bcda". #include <stdio.h> #define BUFFER_SIZE 81 int main(int argc, char **argv) { char string[BUFFER_SIZE]; while(fgets(string, BUFFER_SIZE, stdin) > 0) { int numChars = 0; while(string[numChars] && string[numChars] != '\n') ++numChars; int i; for(i = 0; i < numChars; ++i) putchar(string[i]); putchar('\n');...

  • Please help run all the examples correctly and only the examples without extra things in or...

    Please help run all the examples correctly and only the examples without extra things in or less things in, it should run EXACTLY 100% like the examples. C language ONLY. And please dont reply if you cant run all the examples given. We know the tr command allows you to replace or translate characters in of a stream. It takes in two arguments - a set of characters to be replaced and a set of replacement characters. The full functionality...

  • Convert the C program into a C++ program.Replace all C input/output statements with C++ statements (cin,...

    Convert the C program into a C++ program.Replace all C input/output statements with C++ statements (cin, cout, cin.getline) . Re-make the func function by the following prototype: void func( double, double &); #include int user_interface(); double func(double); void print_table(); int main (int argc, char *argv[]) {    print_table(user_interface());    return 0 ; } int user_interface(int val){    int input = 0;    printf("This function takes in x and returns an output\n");    printf("Enter Maximum Number of X:");    scanf("%d", &input);...

  • Hi everyone, I have a C programming problem, answers are better with explanations. Background Materials: The...

    Hi everyone, I have a C programming problem, answers are better with explanations. Background Materials: The Task: The Answer: The completed C code. Below is the file charIO4C.c: #include <stdio.h> #include <ctype.h> #define QUIT_LETTER 'q' int main(void) { int c, line_length; // Each pass through the outer loop reads a line of input. while (1) { printf("\nEnter a line of text. (To quit, start the line with %c.)\n", QUIT_LETTER); c = fgetc(stdin); if (c == EOF || c == QUIT_LETTER)...

  • In C++ 1.Define a function named "sumCodes" that accepts two input parameters: "s "and "pos". "s"...

    In C++ 1.Define a function named "sumCodes" that accepts two input parameters: "s "and "pos". "s" is an object of the type "string" and "pos" is a boolean value with the default argument of "true". If "pos" is "true", the function returns the summation of the ASCII codes of all the characters located at the even indices of "s" and if "pos" is "false", the function returns the summation of the ASCII codes of all the characters located at the...

  • C++ Define a generic function called CheckOrder() that checks if four items are in ascending, neither,...

    C++ Define a generic function called CheckOrder() that checks if four items are in ascending, neither, or descending order. The function should return -1 if the items are in ascending order, 0 if the items are unordered, and 1 if the items are in descending order. The program reads four items from input and outputs if the items are ordered. The items can be different types, including integers, strings, characters, or doubles. Ex. If the input is: bat hat mat...

  • need this in c programming and you can edit the code below. also give me screenshot...

    need this in c programming and you can edit the code below. also give me screenshot of the output #include <stdio.h> #include <stdlib.h> #include <ctype.h> #include <string.h> #define LINE_SIZE 1024 void my_error(char *s) { fprintf(stderr, "Error: %s\n", s); perror("errno"); exit(-1); } // This funciton prints lines (in a file) that contain string s. // assume all lines has at most (LINE_SIZE - 2) ASCII characters. // // Functions that may be called in this function: // fopen(), fclose(), fgets(), fputs(),...

  • Counting characters, words, and lines based on a text file

    I did the assigment but inside mainWrite a C program (called counting.c) that counts the number of characters, words and lines readfrom standard input (stdin) until EOF is reached. This means counting.c must contain a mainfunction along with other function.- Assume the input is ASCII text of any length.-Every byte read from stdin counts as a character except EOF.- Words are defined as contiguous sequences of letters (a through z, A through Z) and the apostrophe ( ' which has...

  • #include <stdlib.h> #include <stdio.h> #include "main.h" #define MAX_NUM_LENGTH 11 void usage(int argc, char** argv) { if(argc...

    #include <stdlib.h> #include <stdio.h> #include "main.h" #define MAX_NUM_LENGTH 11 void usage(int argc, char** argv) { if(argc < 4) { fprintf(stderr, "usage: %s <input file 1> <input file 2> <output file>\n", argv[0]); exit(EXIT_FAILURE); } } /* This function takes in the two input file names (stored in argv) and determines the number of integers in each file. If the two files both have N integers, return N, otherwise return -1. If one or both of the files do not exist, it...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT