Help with a calculator project

This is a discussion on Help with a calculator project within the C Programming forums, part of the General Programming Boards category; This program is supposed to read commands from a file in sequential order, process them, perform the necessary calculations, and ...

  1. #1
    Registered User
    Join Date
    Feb 2011
    Posts
    3

    Exclamation Help with a calculator project

    This program is supposed to read commands from a file in sequential order, process them, perform the necessary calculations, and then print out the results in a neat, readable manner, both to a file and to the screen. I only have to implement commands: +, -, *, /, H, Q.
    I got it to scan H but not sure how to get Q. Also I'm not sure if it is reading the file or how to write it to another file. What can I do?

    #include <stdio.h>
    #include <stdlib.h>

    Code:
    int main(void) {
    	char H, Q, choice, operate;
    	float i, j;
    	FILE *file1;
    
    	printf("Commands available: +, -, *, or / \n");
    	printf("Press H for help or Q for quit \n");
    		fflush(stdout);
    		scanf("%c",&choice);
    		if (choice = H)
    			printf("Commands available: +, -, *, or / \n");
    		else
    		{
    			if (choice = Q)
    				printf("Program Ended \n");
    		}
    
    	file1= fopen("CommandProj1.dat", "r");
    		if (file1 == NULL)
    			printf("Error opening input file 1 \n");
    		else
    		{
    			fscanf(file1, "%c %f %f", &operate, &i, &j);
    			if (operate == '+')
    				printf("%f\n", i + j);
    			if (operate == '-')
    				printf("%f\n)", i - j);
    			if (operate == '*')
    				printf("%f\n", i * j);
    			if (operate == '/')
    				printf("%f\n", i / j);
    		}
    		fclose (file1);
    
    	return 0;
    }
    Oh and the data file gives these numbers
    GN
    + 34 43
    + -34 43
    + -4 -71
    - 27 15
    - -4 -71
    H
    * 3 -5
    * -11 -12
    / 3 14
    / 14 3
    / 14 -3
    Q
    H
    Last edited by jackellyn; 03-30-2011 at 04:08 PM.

  2. #2
    Registered User TheBigH's Avatar
    Join Date
    May 2010
    Location
    Melbourne, Australia
    Posts
    426
    You want
    Code:
    if( choice == 'H' )
    A single equals sign tells the program to set 'choice' to be whatever 'H' is. To test whether the two are equal you need a double equals.

  3. #3
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Code:
    fscanf(file1, "%c %f %f", &operate, &i, &j);
    if (operate == '+')
       printf("%f\n", i + j);
    else if (operate == '-')
       printf("%f\n)", i - j);
    else if (operate == '*')
       printf("%f\n", i * j);
    else if (operate == '/')
       printf("%f\n", i / j);

  4. #4
    Registered User
    Join Date
    Feb 2011
    Posts
    3
    thanks, but how can I get it to start reading from the file?

  5. #5
    Registered User TheBigH's Avatar
    Join Date
    May 2010
    Location
    Melbourne, Australia
    Posts
    426
    When I compile and run it, it does read from the file. If I make a file containing the contents
    Code:
    + 5.4 7.5
    the program gives me 12.9 like it should. What problem are you getting?

Popular pages Recent additions subscribe to a feed

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21