#include <stdio.h>
int main()
{
printf("Hi!\nWelcome!\nThis is an expression based calculator\ndeveloped by Sankasuvra Bhattacharya\n");
printf("that performs arithmetic operations on\ntwo numbers.\n");
float num1;
float num2;
float ans = 0.0;
char symbol;
char ask;
printf("Please type the expression you want to calculate: ");
if(scanf("%f%1s%f",&num1,&symbol,&num2) != 3)
{
printf("\nInvalid input! Please try again...\n\n");
/* want to restart main() again here */
}
else {
switch(symbol) {
case '+' : ans = num1 + num2;
break;
case '-' : ans = num1 - num2;
break;
case '*' :
case 'x' :
ans = num1 * num2;
break;
case '/' :
if(num2 == 0) {
printf("Division by zero is not possible!\nPlease try again...\n\n");
return main();
}
else {
ans = num1 / num2;
break;
}
default :
printf("\nInvalid input! Please try again...\n\n");
return main();
}
printf("The answer is %g\n",ans);
printf("\nTo use the calculator again, type 'Y'. ");
printf("To exit, type any other character...\n");
scanf("%s",&ask);
if (ask == 'y' || ask == 'Y') {
printf("\n");
main();
}
else {
printf("Thank you for using the program. Please give full marks.");
}
}
return 0;
}

- 32,904
- 11
- 98
- 167

- 23
- 5
-
2Welcome to stackoverflow.com. Please take some time to read [the help pages](http://stackoverflow.com/help), especially the sections named ["What topics can I ask about here?"](http://stackoverflow.com/help/on-topic) and ["What types of questions should I avoid asking?"](http://stackoverflow.com/help/dont-ask). Also please [take the tour](http://stackoverflow.com/tour) and [read about how to ask good questions](http://stackoverflow.com/help/how-to-ask). Lastly please read [this question checklist](https://codeblog.jonskeet.uk/2012/11/24/stack-overflow-question-checklist/). – Some programmer dude Nov 19 '18 at 10:27
-
2As for "repeating" something, use loops. – Some programmer dude Nov 19 '18 at 10:28
-
Possible duplicate of [calling main() in main() in c](https://stackoverflow.com/questions/4238179/calling-main-in-main-in-c) – borrible Nov 19 '18 at 10:31
-
@borrible Maybe it isn't. The intent is clearly different. The other post asks about recursion, where the OP of this question has something else in mind (and possibly isn't aware of that functionality) – kyriakosSt Nov 19 '18 at 10:33
-
3@John Hunter. Are you aware of loops in programming? Also, are you aware of recursion? Because it seems you are trying to use recursion for something that a simple loop would be more than enough – kyriakosSt Nov 19 '18 at 10:35
-
@kyriakosSt I am really new to programming. Besides, it's for one of my friends, who doesn't own a computer. And so I am stuck with his assignment. – John Hunter Nov 19 '18 at 10:42
-
@kyriakosSt please help.. I would like to repeat the program if this statement is true.... if(scanf("%f%1s%f",&num1,&symbol,&num2) != 3) { printf("\nInvalid input! Please try again...\n\n"); } – John Hunter Nov 19 '18 at 10:43
-
@JohnHunter please don't put code in comments, as you can see it's unreadable. What's wrong with the answer below? – Jabberwocky Nov 19 '18 at 10:48
-
@jabberwocky I'm sorry.. I am really new... Let me explain... I would like the program to repeat if the input from the user is invalid....as in case of the first if statement... – John Hunter Nov 19 '18 at 10:57
-
2@JohnHunter I understand what you need to do, but I need to know if you are aware of loops in the first place in order to explain. Kristjan Kica 's answer however seems pretty good – kyriakosSt Nov 19 '18 at 10:59
-
Yes, please explain... I am aware of loops... For, while, do.... And I have tried editing my code just as Kristjan answered. But all I am getting in output is "Invalid input. Please try again.." endlessly – John Hunter Nov 19 '18 at 11:06
3 Answers
To answer your question.
I would not recommend calling main.
You could create another function that has all you code.
Inside main, you call that function.
You can call a function inside that function (called recursion)
However, a simple loop could do the job.
do{
printf("Hi!\nWelcome!\nThis is an expression based calculator\ndeveloped by Sankasuvra Bhattacharya\n");
printf("that performs arithmetic operations on\ntwo numbers.\n");
float num1;
float num2;
float ans = 0.0;
char symbol;
char ask;
printf("Please type the expression you want to calculate: ");
if(scanf("%f%1s%f",&num1,&symbol,&num2) != 3)
{
printf("\nInvalid input! Please try again...\n\n");
}
else {
switch(symbol) {
case '+' : ans = num1 + num2;
break;
case '-' : ans = num1 - num2;
break;
case '*' :
case 'x' :
ans = num1 * num2;
break;
case '/' :
if (num2 == 0) {
printf("Division by zero is not possible!\nPlease try again...\n\n");
return main();
}
else {
ans = num1 / num2;
break;
}
default :
printf("\nInvalid input! Please try again...\n\n");
return main();
}
printf("The answer is %g\n",ans);
printf("\nTo use the calculator again, type 'Y'. ");
printf("To exit, type any other character...\n");
scanf("%s",&ask);
printf("\n");
}while(ask == 'y' || ask == 'Y') ;
printf("Thank you for using the program. Please give full marks.");
}
Edit: To answer the comment to this question what you want to do is:
while(scanf("%f%1s%f",&num1,&symbol,&num2) != 3)
{
printf("\nInvalid input! Please try again...\n\n");
}
And remove the else
EDIT2: Full code. Note that the expression cannot be more than 99 characters.
#include <stdio.h>
int main()
{
float num1;
float num2;
float ans = 0.0;
char symbol;
char ask;
char string[100];
do{
printf("Hi!\nWelcome!\nThis is an expression based calculator\ndeveloped by Sankasuvra Bhattacharya\n");
printf("that performs arithmetic operations on\ntwo numbers.\n");
printf("Please type the expression you want to calculate: ");
while(1){
fgets (string , 100 ,stdin);
if(sscanf( string, "%f%1s%f",&num1,&symbol,&num2)!=3)
printf("\nInvalid input! Please try again...\n\n");
else
break;
}
switch(symbol) {
case '+' : ans = num1 + num2;
break;
case '-' : ans = num1 - num2;
break;
case '*' :
case 'x' :
ans = num1 * num2;
break;
case '/' :
if (num2 == 0) {
printf("Division by zero is not possible!\nPlease try again...\n\n");
return main();
}
else {
ans = num1 / num2;
break;
}
default :
printf("\nInvalid input! Please try again...\n\n");
return main();
}
printf("The answer is %g\n",ans);
printf("\nTo use the calculator again, type 'Y'. ");
printf("To exit, type any other character...\n");
scanf("%s",&ask);
printf("\n");
}while(ask == 'y' || ask == 'Y') ;
printf("Thank you for using the program. Please give full marks.");
return 0;
}

- 4,034
- 1
- 20
- 40
-
Hi, please note that I was referring to this line... if(scanf("%f%1s%f",&num1,&symbol,&num2) != 3) { printf("\nInvalid input! Please try again...\n\n"); } – John Hunter Nov 19 '18 at 10:37
-
You have called main elsewhere. Let me edit my answer. The same principle applies – kkica Nov 19 '18 at 10:42
-
Showing invalid input infinite no. times... Something like this... Invalid input. Please try again.... Invalid input. Please try again.... Invalid input. Please try again.... Invalid input. Please try again.... Invalid input. Please try again.... Invalid input. Please try again.... – John Hunter Nov 19 '18 at 10:50
-
If scanf can't find matching input for the expected format, it will return. With this loop you no longer know where the input is, and will probably fail indefinitely. This pattern is generally a bad idea. Consider using line based input with fgets and then sscanf. – paddy Nov 19 '18 at 11:36
-
-
@JohnHunter Yes, I forgot the closing bracket. What is your problem now? How are you giving the input? Do you separate by space? Because if you are, then the program would get the first space as the %1s and it would search for a float number where you want the %1s. If you want to be able to give the inputs separated by space, you need to change to scanf("%f %1s %f",&num1,&symbol,&num2) – kkica Nov 19 '18 at 15:06
-
@KristjanKica Lets just return to the main issue... All I want is my program to call main() just once...not endlessly.... if the return value of scanf is not equal to 3. As in this line... if (scanf("%f%1s%f",&num1,&symbol,&num2) != 3) { printf("\nInvalid input! Please try again...\n\n"); main(); } Here, all I want is to call main() just once(1) and not endlessly.... If you run the code, you will know what I mean.... – John Hunter Nov 19 '18 at 15:25
-
-
-
@kristjanKica Why is it that if I type some character, say 'abcd', in place num1 or num2, and hit enter,it prints invalid input. Please try again endlessly. Also, how can I fix it... I want it to print Invalid input. Please try again once, then ask for the expression I want to calculate... – John Hunter Nov 19 '18 at 18:30
-
That would be for the exact reason I recommended that you shouldn't use scanf in a loop like this. Read a single line of input with `fgets`, then parse it with `sscanf`. If that is invalid, you can discard it and read another line. There are countless examples on Stack Overflow of people reading lines of text with `fgets`, or you can find an example [here](https://en.cppreference.com/w/c/io/fgets) – paddy Nov 19 '18 at 19:20
@Kristjan Kica answer is good. I think you are using spaces in your input like 1 + 2.
According to manual page of scanf
All conversions are introduced by the % (percent sign) character. The format string may also contain other characters. White space
(such as blanks, tabs, or newlines) in the format string match any amount of white space, including none, in the input. Everything else
matches only itself. Scanning stops when an input character does not match such a format character. Scanning also stops when an input
conversion cannot be made.
Remove the spaces and try again.
Example: 1+2 should work by the changes mentioned in kristijan answer.
Edit: Replace the line in @Kristjan Kica answer
while(ask == 'y' || ask == 'Y') ;
with
}while(ask == 'y' || ask == 'Y') ;
Edit 2: Last closing } should be your main functions closing brace.

- 65
- 9
-
-
Yup it's working... But how can I do the same thing in this line... if(scanf("%f%1s%f",&num1,&symbol,&num2) != 3) { printf("\nInvalid input! Please try again...\n\n"); } – John Hunter Nov 19 '18 at 13:23
-
-
You can achieve the same with if statement(remove while inside do-while and try again). – bhanu7k Nov 19 '18 at 13:31
Finally solved it. All thanks to Kristjan Kica...
#include <stdio.h>
int main(void)
{
float num1;
float num2;
float ans;
char symbol;
char ask;
char string[100];
fflush(stdin);
printf("Hi!\nWelcome!\nThis is an expression based calculator\ndeveloped by
Sankasuvra Bhattacharya\n");
printf("that performs arithmetic operations on\ntwo numbers.\n");
printf("Please type the expression you want to calculate: ");
fgets (string , 100 ,stdin);
if(sscanf( string, "%f%1s%f",&num1,&symbol,&num2)!=3)
{
printf("\nInvalid input! Please try again...\n\n");
main();
}
else
{
switch(symbol) {
case '+' : ans = num1 + num2;
break;
case '-' : ans = num1 - num2;
break;
case '*' :
case 'x' :
ans = num1 * num2;
break;
case '/' :
if (num2 == 0) {
printf("Division by zero is not possible!\nPlease try again...\n\n");
return main();
}
else {
ans = num1 / num2;
break;
}
default :
printf("\nInvalid input! Please try again...\n\n");
return main();
}
printf("The answer is %g\n",ans);
printf("\nTo use the calculator again, type 'Y'. ");
printf("To exit, type any other character...\n");
scanf("%s",&ask);
printf("\n");
if (ask == 'y' || ask == 'Y')
{
main();
}
else {
return 0;
}
}
}

- 23
- 5