LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
LinkBack Search this Thread
Old 10-30-2005, 09:29 PM   #1
Gigantor
LQ Newbie
 
Registered: Oct 2005
Posts: 10

Rep: Reputation: 0
Memory fault(coredump) error


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;
 }
 
Old 10-30-2005, 10:59 PM   #2
Wim Sturkenboom
Senior Member
 
Registered: Jan 2005
Location: Roodepoort, South Africa
Distribution: Slackware 10.1/10.2/12, Ubuntu 10.04, Crunchbang Statler
Posts: 3,325

Rep: Reputation: 168Reputation: 168
scanf needs a pointer to the variable
Code:
scanf("%f", &num1);
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.
 
Old 10-30-2005, 11:05 PM   #3
naf
Member
 
Registered: Oct 2005
Location: Chicago, USA
Distribution: Slackware & Fedora
Posts: 66

Rep: Reputation: 15
Comments inserted below:
Code:
#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.
 
Old 10-30-2005, 11:19 PM   #4
Gigantor
LQ Newbie
 
Registered: Oct 2005
Posts: 10

Original Poster
Rep: Reputation: 0
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.
 
Old 10-30-2005, 11:20 PM   #5
Gigantor
LQ Newbie
 
Registered: Oct 2005
Posts: 10

Original Poster
Rep: Reputation: 0
oh thanks naf! I will try that!
 
Old 10-31-2005, 03:11 AM   #6
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Puppy
Posts: 3,048

Rep: Reputation: 95
fflush stdin doesn't do anything.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Memory Fault Error ims_mca Linux - Software 0 03-09-2005 03:17 AM
seg. fault when allocating memory via a pointer inside a struct elmafiacs Programming 4 02-20-2005 07:26 AM
Memory fault russoue Programming 14 11-13-2004 01:01 PM
error: fragmentation fault cscyzl Linux - Software 2 07-28-2004 11:18 AM
Segmentation Fault Error ashwinipahuja Programming 1 03-29-2004 02:09 PM


All times are GMT -5. The time now is 08:58 AM.

Main Menu
 
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: @linuxquestions
Open Source Consulting | Domain Registration