Question

Hello, I need help with the following C code. This program asks the user to enter...

Hello, I need help with the following C code. This program asks the user to enter three numbers and outputs the sum on the screen , all im trying to do is have the program reprompt the user for a number if anything other than a number is entered for each of the entries. I wanted to use do while for each of the functions but that did not work

for example:

Enter Number 1: 2
Enter Number 2: Hello
Enter Number 2: 8
Enter Number 3: 10

Please try to make minimal changes to the code, be sure to keep scanf, and printf. NO libraries besides stdio.h.

int main(void) {
    int a,b,c,ans,n;

    do{

            printf("Enter 1st number: ");
            scanf("%d", &a);
            printf("\n");


            printf("Enter second number: ");
            scanf("%d", &b);
            printf("\n");


            printf("Enter 3rd number: ");
            scanf("%d", &c);
            printf("\n");

        ans = a+b+c;

        printf("Answer is %d\n\n", ans);

        printf("Press 1 to start over, or 0 to quit...");
        scanf("%d",&n);
        printf("\n");

}while (n!=0);

   return 0;
}

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

#include<stdio.h>
int main(void) {
int a,b,c,ans,n;

do{

do
{
printf("Enter 1st number: ");
           char term;
           fflush(stdin);
           if(scanf("%d%c", &a, &term) != 2 || term != '\n')
           continue;
           else
           break;

           }while(true);

           do
{
printf("Enter second number: ");
           char term;
           fflush(stdin);
           if(scanf("%d%c", &b, &term) != 2 || term != '\n')
           continue;
           else
           break;

           }while(true);
          
           do
{
printf("Enter 3rd number: ");
           char term;
           fflush(stdin);
           if(scanf("%d%c", &c, &term) != 2 || term != '\n')
           continue;
           else
           break;

           }while(true);

ans = a+b+c;

printf("Answer is %d\n\n", ans);

printf("Press 1 to start over, or 0 to quit...");
scanf("%d",&n);
printf("\n");

}while (n!=0);

return 0;
}

Sample Output:-

Add a comment
Know the answer?
Add Answer to:
Hello, I need help with the following C code. This program asks the user to enter...
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
  • Write a program that asks the user to type an even number or 111 to stop....

    Write a program that asks the user to type an even number or 111 to stop. When the user types an even number, display the message “Great Work”, and then ask for input again. When the user types an odd number, display the message “Try again” and ask the user to enter again. When the user enters the sentinel value of 111 the program ends I've attempted this but it went horribly wrong. we are only suppose to use while,...

  • C programm , ´hello i need your help -Given the C program primes.c with the following main method: int main() { int num, res; char buffer[11]; bool finished = false; while (!finished)...

    C programm , ´hello i need your help -Given the C program primes.c with the following main method: int main() { int num, res; char buffer[11]; bool finished = false; while (!finished) { printf("Enter n > 0 or quit\n"); scanf("%10s", buffer); if (strcmp(buffer, "quit") == 0) { finished = true; } else { // Convert input to number and compute n-th prime num = atoi(buffer); if (num > 0) { res = nth_prime(num); printf("Prime #%d is %d\n", num, res); }...

  • Hello, I have my piece of C programming code and I have a pointer initialized to...

    Hello, I have my piece of C programming code and I have a pointer initialized to point to my array and I need help to display the contents of my array using a while loop or do-while loop. The stop condition should be when the pointer reaches '\0'. The code is below: #include <stdio.h> int main () {    char array[80]; printf("Enter a string: "); scanf("%[^\n]", &array); printf("%s\n", array); char * p;    p = array;         }

  • C++ HELP I need help with this program. I have done and compiled this program in...

    C++ HELP I need help with this program. I have done and compiled this program in a single file called bill.cpp. It works fine. I am using printf and scanf. Instead of printf and scanf use cin and cout to make the program run. after this please split this program in three files 1. bill.h = contains the class program with methods and variables eg of class file class bill { } 2. bill.cpp = contains the functions from class...

  • Write a C Program that inputs an array of integers from the user along-with the length...

    Write a C Program that inputs an array of integers from the user along-with the length of array. The program then prints out the array of integers in reverse. (You do not need to change (re-assign) the elements in the array, only print the array in reverse.) The program uses a function call with array passed as call by reference. Part of the Program has been given here. You need to complete the Program by writing the function. #include<stdio.h> void...

  • In C programming language: This program will output a right triangle based on user specified height...

    In C programming language: This program will output a right triangle based on user specified height triangleHeight and symbol triangleChar. (1) The given program outputs a fixed-height triangle using a * character. Modify the given program to output a right triangle that instead uses the user-specified triangleChar character. (1 pt) (2) Modify the program to use a nested loop to output a right triangle of height triangleHeight. The first line will have one user-specified character, such as % or *....

  • Hello guys ! Could you let me know about this ?? Translate the following C program...

    Hello guys ! Could you let me know about this ?? Translate the following C program to Pep/9 assembly language. #include <stdio.h> int main() { int number; scanf("%d", &number); if (number % 2 == 0) { printf("Even\n") } else { printf("Odd\n"); } return 0; } Thank you !

  • i need flowchart for the following c code #include <stdio.h> int main() {     int n,...

    i need flowchart for the following c code #include <stdio.h> int main() {     int n, i, sum = 0;     printf("Enter an integer: ");     scanf("%d",&n);     i = 1;     while ( i <=n )     {         sum += i;         ++i;     }     printf("Sum = %d",sum);     return 0; }

  • C program help: Write a C program that uses three functions to perform the following tasks:...

    C program help: Write a C program that uses three functions to perform the following tasks: – The first function should read the price of an item sold at a grocery store together with the quantity from the user and return all the values to main(). example: #include void getInput(int *pNum1, int *pNum2); // note: *pNum1 & *pNum2 are pointers to ints int main () {    int numDucks,numCats;    getInput(&numDucks, &numCats); // passing addresses of num1 & num2 to...

  • Using the code below, Complete the C program main.c to stop execution if the user attempts...

    Using the code below, Complete the C program main.c to stop execution if the user attempts a divide by 0. #include <stdio.h> int main() { float a, b; printf("Input two integers to divide the first by the second\n"); scanf("%f%f", &a, &b); printf("%f/%f = %.2f\n", a, b, a/b);    return 0; }

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