ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Hello, I'm fairly new to C. I am attempting to write a program to act as a "Math menu." 1st the user would enter A, S, M, D or E. For Add, Subtract, Multiply, Divide or to Exit the program. If the user selects, A, S, M, or D. Then the program would prompt the user for 2 numbers and do the mathmatical equation. I think I have most of it written, I'm just getting a "Memory fault(coredump)" error. I'm not exactly sure what is happening. Although, I just realized the char error. But other than that is there anything else I should be looking out for?Someone please help me out. My code is below...
Code:
#include<stdio.h>
int main()
{
int num1, num2;
float A, S, M, D, E, total;
char choice;
printf("Please enter a letter to perform a mathmatical calculation.\n");
printf("A, S, M, D, or E for Add, Subtract, Multiply, Divide or Exit.\n");
fflush(stdin);
scanf("%C", &choice);
if (choice == 'E')
{
printf("Goodbye thank you for using The Calculator.\n");
return 0;
}
else if (choice == 'A', 'S', 'M', 'D')
{
printf("Please enter the first number to perform your equation.\n");
scanf("%f", num1);
fflush(0);
printf("Please enter the second number of your equation.\n");
scanf("%f", num2);
fflush(0);
}
if (choice == 'A')
{
total = num1 + num2;
printf("%d plus %d equals %d\n", num1, num2, total);
}
else if (choice == 'S')
{
total = num1 - num2;
printf("%d minus %d equals %d\n", num1, num2, total);
}
else if (choice == 'M')
{
total = num1 * num2;
printf("%d multiplied by %d equals %d\n", num1, num2, total);
}
else if (choice == 'D')
{
total = num1 / num2;
printf("%d divided by %d equals %d\n", num1, num2, total);
}
return 0;
}
You did this right for the choice, but not for the ints.
Further you're telling scanf to read a float, but num1 is an integer.
You're also telling printf to print an integer, but you're passing a float (total); might work and might not work.
BTW It's usefull to indicate more or less where the problem occurs.
Question: any reason to have floats A, S, M, D and E? They're not used anywhere.
Last edited by Wim Sturkenboom; 10-30-2005 at 11:01 PM.
#include<stdio.h>
int main()
{
int num1, num2;
float A, S, M, D, E;
int total; /* You are adding two integers? The result fits fine in a total otherwise you may lose significant digits. */
char choice;
printf("Please enter a letter to perform a mathmatical calculation.\n");
printf("A, S, M, D, or E for Add, Subtract, Multiply, Divide or Exit.\n");
fflush(stdin);
scanf("%c", &choice);
if (choice == 'E')
{
printf("Goodbye thank you for using The Calculator.\n");
return 0;
}
else if( ( choice == 'A' ) ||
( choice == 'S' ) ||
( choice == 'M' ) ||
( choice == 'D' ) ) /* You cannot compare to a list, it has to be itemized out if you use an expression. */
{
printf("Please enter the first number to perform your equation.\n");
scanf("%d", &num1);/* You need to match types (%d for int, or %f for float). Moreover, scanf needs addresses, so the ampersand is a requirement. */
fflush(0);
printf("Please enter the second number of your equation.\n");
scanf("%d", &num2);/*See previous comment. */
fflush(0);
}
if (choice == 'A')
{
total = num1 + num2;
printf("%d plus %d equals %d\n", num1, num2, total);
}
else if (choice == 'S')
{
total = num1 - num2;
printf("%d minus %d equals %d\n", num1, num2, total);
}
else if (choice == 'M')
{
total = num1 * num2;
printf("%d multiplied by %d equals %d\n", num1, num2, total);
}
else if (choice == 'D')
{
total = num1 / num2;
printf("%d divided by %d equals %d\n", num1, num2, total);
}
return 0;
}
Alternatively, you can use switch instead of if:
Code:
#include<stdio.h>
int main()
{
int num1, num2;
float A, S, M, D, E;
int total;
char choice;
printf("Please enter a letter to perform a mathmatical calculation.\n");
printf("A, S, M, D, or E for Add, Subtract, Multiply, Divide or Exit.\n");
fflush(stdin);
scanf("%c", &choice);
switch( choice )
{
case 'E':
default:
printf("Goodbye thank you for using The Calculator.\n");
break;
case 'A':
case 'S':
case 'M':
case 'D':
printf("Please enter the first number to perform your equation.\n");
scanf("%d", &num1);
fflush(0);
printf("Please enter the second number of your equation.\n");
scanf("%d", &num2);
fflush(0);
switch( choice )
{
case 'A':
total = num1 + num2;
printf("%d plus %d equals %d\n", num1, num2, total);
break;
case 'S':
total = num1 - num2;
printf("%d minus %d equals %d\n", num1, num2, total);
break;
case 'M':
total = num1 * num2;
printf("%d multiplied by %d equals %d\n", num1, num2, total);
break;
case 'D':
total = num1 / num2;
printf("%d divided by %d equals %d\n", num1, num2, total);
break;
}
}
return 0;
}
Or
Code:
#include<stdio.h>
int main()
{
int num1, num2;
float A, S, M, D, E;
int total;
char choice;
const char *operation;
do
{
printf( "Please enter a letter to perform a mathmatical calculation.\n"
"A, S, M, D, or E for Add, Subtract, Multiply, Divide or Exit.\n" );
fflush( stdin );
scanf( "%c", &choice );
switch( choice )
{
case 'E':
default:
printf( "Goodbye thank you for using The Calculator.\n" );
break;
case 'A':
case 'S':
case 'M':
case 'D':
printf( "Please enter the first number to perform your equation.\n" );
scanf( "%d", &num1 );
fflush( 0 );
printf( "Please enter the second number of your equation.\n" );
scanf( "%d", &num2 );
fflush( 0 );
switch( choice )
{
case 'A': total = num1 + num2, operation = "plus"; break;
case 'S': total = num1 - num2, operation = "minus"; break;
case 'M': total = num1 * num2, operation = "multiplied"; break;
case 'D': total = num1 / num2, operation = "divided"; break;
}
printf("%d %s %d equals %d\n", num1, operation, num2, total);
}
} while( choice != 'E' );
return 0;
}
You still have a danger that your numbers may produce invalid results. Like adding two very big numbers or dividing by zero. Good luck.
Well the A, S, M, D, and E floats are supposed to be 'char' and not float. i changed that. but the problem persists after it asks for the first number. Then it displays the "Memory fault(coredump)" error. I thought it was a problem with 'fflush(stdin)' command, but i guess not.
I've replaced a lot in the code tonight and I'm going to display the new code in a little while. Thank you for your help.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.