I need to create a program for reading two numbers,printing them,confirming them and then summing them up.
Here is my attempt

#include <stdio.h>
#include <conio.h>

void main()

{

clrscr();

int a,b,s;
char c,d;
s=a+b;

printf("Enter any two numbers ");
scanf("%d %d" , &a, &b);

printf("The numbers you have entered are %d %d. \n\nPress c to continue " , a,b);

scanf("%c" , &d );
if (d==c) printf("\n\nThe sum of the two numbers is %d" ,s);

getch();


}

The compiler however crashes after entering 'c'..please let me know my mistake

Recommended Answers

All 4 Replies

The compiler probably doesn't crash after entering c because the compiler never requests that you enter c. Presumably you are actually running the program to get to the enter c phase. The program doesn't crash either, it does what you have asked and exits normally (or it does for me but then since this program exhibits undefined behaviour it is impossible to say what is happening for you).

You have the following problems

Line 4: Returning void is an extension that not all compilers support, get in the habit of returning int now.

Line 11: Unnecessarily declare c which is never initialised to anything.

Line 12: Attempt to add a and b and store the result in s before you have obtain values for a and b. Reading uninitialised/unassigned automatic variables is undefined behaviour.

Line 15: you don't scan the input return you typed so it is left in the input buffer, we'll come back to this ...

Line 19: Here where you ask for a character, the system looks at the input buffer which contains a line feed and returns that to you, no extra input is required.

Line 20: Access to the initialised variable c is undefined behaviour also this is not how you check if variable d is equal to the character c. You need to use a character constant (you can Google that).

commented: Good effort. +9

Thank you,i have made the following changes and it seems fine upto certain extent

#include <stdio.h>
#include <conio.h>

void main()

{

clrscr();

int a,b,s;
char d,choice;


printf("Enter any two numbers ");
scanf("%d %d" , &a, &b);

s=a+b;

printf("The numbers you have entered are %d %d. \n\nPress c to find the sum " , a,b);

scanf("%c" , &d );

choice=getche();
switch(choice) {
case 'c': printf("\n\nThe sum of the two numbers is %d" ,s); break;

default: printf("\nPress c to continue ");
}

getch();


}

when someone tries to enter any letter other than c,it is supposed to display
'Press c to continue'
It works fine for the first attempt,however when someone tries to enter any other letter for the second time,it simply crashes again.

Since you claim Banfa is wrong, you need to explain how it crashes. We cannot help you fix a crash without knowing the error message explaining the crash.

Much of your problem deals with scanf() . Read this series to understand how to deal with the function when reading numbers and single characters.

I do not think that your program is crashing....
This is what happens when you enter a char other than c.
Since the char is not c, the code goes to the default case, prints "press c to continue" then comes out of the switch scenario and wait for you to enter a char in response to getch()
If you want to give the user another choice to enter the char, you have to put the switch inside a while loop

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.