How to restart main() from the beginning on an error condition?
up vote
1
down vote
favorite
#include <stdio.h>
int main()
{
printf("Hi!nWelcome!nThis is an expression based calculatorndeveloped by Sankasuvra Bhattacharyan");
printf("that performs arithmetic operations onntwo 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...nn");
/* 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...nn");
return main();
}
else {
ans = num1 / num2;
break;
}
default :
printf("nInvalid input! Please try again...nn");
return main();
}
printf("The answer is %gn",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;
}
c
New contributor
|
show 6 more comments
up vote
1
down vote
favorite
#include <stdio.h>
int main()
{
printf("Hi!nWelcome!nThis is an expression based calculatorndeveloped by Sankasuvra Bhattacharyan");
printf("that performs arithmetic operations onntwo 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...nn");
/* 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...nn");
return main();
}
else {
ans = num1 / num2;
break;
}
default :
printf("nInvalid input! Please try again...nn");
return main();
}
printf("The answer is %gn",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;
}
c
New contributor
2
Welcome to stackoverflow.com. Please take some time to read the help pages, especially the sections named "What topics can I ask about here?" and "What types of questions should I avoid asking?". Also please take the tour and read about how to ask good questions. Lastly please read this question checklist.
– Some programmer dude
yesterday
2
As for "repeating" something, use loops.
– Some programmer dude
yesterday
Possible duplicate of calling main() in main() in c
– borrible
yesterday
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
yesterday
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
yesterday
|
show 6 more comments
up vote
1
down vote
favorite
up vote
1
down vote
favorite
#include <stdio.h>
int main()
{
printf("Hi!nWelcome!nThis is an expression based calculatorndeveloped by Sankasuvra Bhattacharyan");
printf("that performs arithmetic operations onntwo 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...nn");
/* 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...nn");
return main();
}
else {
ans = num1 / num2;
break;
}
default :
printf("nInvalid input! Please try again...nn");
return main();
}
printf("The answer is %gn",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;
}
c
New contributor
#include <stdio.h>
int main()
{
printf("Hi!nWelcome!nThis is an expression based calculatorndeveloped by Sankasuvra Bhattacharyan");
printf("that performs arithmetic operations onntwo 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...nn");
/* 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...nn");
return main();
}
else {
ans = num1 / num2;
break;
}
default :
printf("nInvalid input! Please try again...nn");
return main();
}
printf("The answer is %gn",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;
}
c
c
New contributor
New contributor
edited 11 hours ago
HostileFork
24.6k775131
24.6k775131
New contributor
asked yesterday
John Hunter
74
74
New contributor
New contributor
2
Welcome to stackoverflow.com. Please take some time to read the help pages, especially the sections named "What topics can I ask about here?" and "What types of questions should I avoid asking?". Also please take the tour and read about how to ask good questions. Lastly please read this question checklist.
– Some programmer dude
yesterday
2
As for "repeating" something, use loops.
– Some programmer dude
yesterday
Possible duplicate of calling main() in main() in c
– borrible
yesterday
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
yesterday
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
yesterday
|
show 6 more comments
2
Welcome to stackoverflow.com. Please take some time to read the help pages, especially the sections named "What topics can I ask about here?" and "What types of questions should I avoid asking?". Also please take the tour and read about how to ask good questions. Lastly please read this question checklist.
– Some programmer dude
yesterday
2
As for "repeating" something, use loops.
– Some programmer dude
yesterday
Possible duplicate of calling main() in main() in c
– borrible
yesterday
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
yesterday
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
yesterday
2
2
Welcome to stackoverflow.com. Please take some time to read the help pages, especially the sections named "What topics can I ask about here?" and "What types of questions should I avoid asking?". Also please take the tour and read about how to ask good questions. Lastly please read this question checklist.
– Some programmer dude
yesterday
Welcome to stackoverflow.com. Please take some time to read the help pages, especially the sections named "What topics can I ask about here?" and "What types of questions should I avoid asking?". Also please take the tour and read about how to ask good questions. Lastly please read this question checklist.
– Some programmer dude
yesterday
2
2
As for "repeating" something, use loops.
– Some programmer dude
yesterday
As for "repeating" something, use loops.
– Some programmer dude
yesterday
Possible duplicate of calling main() in main() in c
– borrible
yesterday
Possible duplicate of calling main() in main() in c
– borrible
yesterday
3
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
yesterday
@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
yesterday
2
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
yesterday
@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
yesterday
|
show 6 more comments
3 Answers
3
active
oldest
votes
up vote
7
down vote
accepted
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 calculatorndeveloped by Sankasuvra Bhattacharyan");
printf("that performs arithmetic operations onntwo 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...nn");
}
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...nn");
return main();
}
else {
ans = num1 / num2;
break;
}
default :
printf("nInvalid input! Please try again...nn");
return main();
}
printf("The answer is %gn",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...nn");
}
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 calculatorndeveloped by Sankasuvra Bhattacharyan");
printf("that performs arithmetic operations onntwo 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...nn");
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...nn");
return main();
}
else {
ans = num1 / num2;
break;
}
default :
printf("nInvalid input! Please try again...nn");
return main();
}
printf("The answer is %gn",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;
}
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...nn"); }
– John Hunter
yesterday
You have called main elsewhere. Let me edit my answer. The same principle applies
– Kristjan Kica
yesterday
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
yesterday
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
yesterday
Please give an example @paddy... I am really new to programming....
– John Hunter
yesterday
|
show 9 more comments
up vote
0
down vote
@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.
New contributor
Upon running Kristjan's code, it say expected while before }
– John Hunter
yesterday
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...nn"); }
– John Hunter
yesterday
Are u getting the output or is it just compiled?
– bhanu7k
yesterday
You can achieve the same with if statement(remove while inside do-while and try again).
– bhanu7k
yesterday
add a comment |
up vote
0
down vote
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 calculatorndeveloped by
Sankasuvra Bhattacharyan");
printf("that performs arithmetic operations onntwo 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...nn");
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...nn");
return main();
}
else {
ans = num1 / num2;
break;
}
default :
printf("nInvalid input! Please try again...nn");
return main();
}
printf("The answer is %gn",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;
}
}
}
New contributor
add a comment |
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
7
down vote
accepted
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 calculatorndeveloped by Sankasuvra Bhattacharyan");
printf("that performs arithmetic operations onntwo 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...nn");
}
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...nn");
return main();
}
else {
ans = num1 / num2;
break;
}
default :
printf("nInvalid input! Please try again...nn");
return main();
}
printf("The answer is %gn",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...nn");
}
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 calculatorndeveloped by Sankasuvra Bhattacharyan");
printf("that performs arithmetic operations onntwo 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...nn");
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...nn");
return main();
}
else {
ans = num1 / num2;
break;
}
default :
printf("nInvalid input! Please try again...nn");
return main();
}
printf("The answer is %gn",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;
}
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...nn"); }
– John Hunter
yesterday
You have called main elsewhere. Let me edit my answer. The same principle applies
– Kristjan Kica
yesterday
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
yesterday
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
yesterday
Please give an example @paddy... I am really new to programming....
– John Hunter
yesterday
|
show 9 more comments
up vote
7
down vote
accepted
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 calculatorndeveloped by Sankasuvra Bhattacharyan");
printf("that performs arithmetic operations onntwo 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...nn");
}
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...nn");
return main();
}
else {
ans = num1 / num2;
break;
}
default :
printf("nInvalid input! Please try again...nn");
return main();
}
printf("The answer is %gn",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...nn");
}
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 calculatorndeveloped by Sankasuvra Bhattacharyan");
printf("that performs arithmetic operations onntwo 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...nn");
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...nn");
return main();
}
else {
ans = num1 / num2;
break;
}
default :
printf("nInvalid input! Please try again...nn");
return main();
}
printf("The answer is %gn",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;
}
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...nn"); }
– John Hunter
yesterday
You have called main elsewhere. Let me edit my answer. The same principle applies
– Kristjan Kica
yesterday
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
yesterday
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
yesterday
Please give an example @paddy... I am really new to programming....
– John Hunter
yesterday
|
show 9 more comments
up vote
7
down vote
accepted
up vote
7
down vote
accepted
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 calculatorndeveloped by Sankasuvra Bhattacharyan");
printf("that performs arithmetic operations onntwo 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...nn");
}
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...nn");
return main();
}
else {
ans = num1 / num2;
break;
}
default :
printf("nInvalid input! Please try again...nn");
return main();
}
printf("The answer is %gn",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...nn");
}
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 calculatorndeveloped by Sankasuvra Bhattacharyan");
printf("that performs arithmetic operations onntwo 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...nn");
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...nn");
return main();
}
else {
ans = num1 / num2;
break;
}
default :
printf("nInvalid input! Please try again...nn");
return main();
}
printf("The answer is %gn",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;
}
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 calculatorndeveloped by Sankasuvra Bhattacharyan");
printf("that performs arithmetic operations onntwo 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...nn");
}
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...nn");
return main();
}
else {
ans = num1 / num2;
break;
}
default :
printf("nInvalid input! Please try again...nn");
return main();
}
printf("The answer is %gn",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...nn");
}
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 calculatorndeveloped by Sankasuvra Bhattacharyan");
printf("that performs arithmetic operations onntwo 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...nn");
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...nn");
return main();
}
else {
ans = num1 / num2;
break;
}
default :
printf("nInvalid input! Please try again...nn");
return main();
}
printf("The answer is %gn",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;
}
edited 15 hours ago
answered yesterday
Kristjan Kica
2,0311826
2,0311826
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...nn"); }
– John Hunter
yesterday
You have called main elsewhere. Let me edit my answer. The same principle applies
– Kristjan Kica
yesterday
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
yesterday
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
yesterday
Please give an example @paddy... I am really new to programming....
– John Hunter
yesterday
|
show 9 more comments
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...nn"); }
– John Hunter
yesterday
You have called main elsewhere. Let me edit my answer. The same principle applies
– Kristjan Kica
yesterday
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
yesterday
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
yesterday
Please give an example @paddy... I am really new to programming....
– John Hunter
yesterday
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...nn"); }
– John Hunter
yesterday
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...nn"); }
– John Hunter
yesterday
You have called main elsewhere. Let me edit my answer. The same principle applies
– Kristjan Kica
yesterday
You have called main elsewhere. Let me edit my answer. The same principle applies
– Kristjan Kica
yesterday
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
yesterday
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
yesterday
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
yesterday
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
yesterday
Please give an example @paddy... I am really new to programming....
– John Hunter
yesterday
Please give an example @paddy... I am really new to programming....
– John Hunter
yesterday
|
show 9 more comments
up vote
0
down vote
@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.
New contributor
Upon running Kristjan's code, it say expected while before }
– John Hunter
yesterday
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...nn"); }
– John Hunter
yesterday
Are u getting the output or is it just compiled?
– bhanu7k
yesterday
You can achieve the same with if statement(remove while inside do-while and try again).
– bhanu7k
yesterday
add a comment |
up vote
0
down vote
@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.
New contributor
Upon running Kristjan's code, it say expected while before }
– John Hunter
yesterday
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...nn"); }
– John Hunter
yesterday
Are u getting the output or is it just compiled?
– bhanu7k
yesterday
You can achieve the same with if statement(remove while inside do-while and try again).
– bhanu7k
yesterday
add a comment |
up vote
0
down vote
up vote
0
down vote
@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.
New contributor
@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.
New contributor
edited yesterday
New contributor
answered yesterday
bhanu7k
385
385
New contributor
New contributor
Upon running Kristjan's code, it say expected while before }
– John Hunter
yesterday
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...nn"); }
– John Hunter
yesterday
Are u getting the output or is it just compiled?
– bhanu7k
yesterday
You can achieve the same with if statement(remove while inside do-while and try again).
– bhanu7k
yesterday
add a comment |
Upon running Kristjan's code, it say expected while before }
– John Hunter
yesterday
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...nn"); }
– John Hunter
yesterday
Are u getting the output or is it just compiled?
– bhanu7k
yesterday
You can achieve the same with if statement(remove while inside do-while and try again).
– bhanu7k
yesterday
Upon running Kristjan's code, it say expected while before }
– John Hunter
yesterday
Upon running Kristjan's code, it say expected while before }
– John Hunter
yesterday
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...nn"); }
– John Hunter
yesterday
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...nn"); }
– John Hunter
yesterday
Are u getting the output or is it just compiled?
– bhanu7k
yesterday
Are u getting the output or is it just compiled?
– bhanu7k
yesterday
You can achieve the same with if statement(remove while inside do-while and try again).
– bhanu7k
yesterday
You can achieve the same with if statement(remove while inside do-while and try again).
– bhanu7k
yesterday
add a comment |
up vote
0
down vote
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 calculatorndeveloped by
Sankasuvra Bhattacharyan");
printf("that performs arithmetic operations onntwo 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...nn");
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...nn");
return main();
}
else {
ans = num1 / num2;
break;
}
default :
printf("nInvalid input! Please try again...nn");
return main();
}
printf("The answer is %gn",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;
}
}
}
New contributor
add a comment |
up vote
0
down vote
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 calculatorndeveloped by
Sankasuvra Bhattacharyan");
printf("that performs arithmetic operations onntwo 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...nn");
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...nn");
return main();
}
else {
ans = num1 / num2;
break;
}
default :
printf("nInvalid input! Please try again...nn");
return main();
}
printf("The answer is %gn",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;
}
}
}
New contributor
add a comment |
up vote
0
down vote
up vote
0
down vote
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 calculatorndeveloped by
Sankasuvra Bhattacharyan");
printf("that performs arithmetic operations onntwo 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...nn");
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...nn");
return main();
}
else {
ans = num1 / num2;
break;
}
default :
printf("nInvalid input! Please try again...nn");
return main();
}
printf("The answer is %gn",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;
}
}
}
New contributor
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 calculatorndeveloped by
Sankasuvra Bhattacharyan");
printf("that performs arithmetic operations onntwo 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...nn");
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...nn");
return main();
}
else {
ans = num1 / num2;
break;
}
default :
printf("nInvalid input! Please try again...nn");
return main();
}
printf("The answer is %gn",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;
}
}
}
New contributor
New contributor
answered 11 hours ago
John Hunter
74
74
New contributor
New contributor
add a comment |
add a comment |
John Hunter is a new contributor. Be nice, and check out our Code of Conduct.
John Hunter is a new contributor. Be nice, and check out our Code of Conduct.
John Hunter is a new contributor. Be nice, and check out our Code of Conduct.
John Hunter is a new contributor. Be nice, and check out our Code of Conduct.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53372593%2fhow-to-restart-main-from-the-beginning-on-an-error-condition%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
2
Welcome to stackoverflow.com. Please take some time to read the help pages, especially the sections named "What topics can I ask about here?" and "What types of questions should I avoid asking?". Also please take the tour and read about how to ask good questions. Lastly please read this question checklist.
– Some programmer dude
yesterday
2
As for "repeating" something, use loops.
– Some programmer dude
yesterday
Possible duplicate of calling main() in main() in c
– borrible
yesterday
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
yesterday
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
yesterday