Thread: Help with this code please!

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    5

    Help with this code please!

    Basically, I have to build a program that reads a mathematical command (*, /, +, -) and two variables in a text file and performs the command with the two variables and prints the answer to the screen and to another text file.
    i wrote the code and can only see !!!Hello World!!!
    Code:
    #include
    <stdio.h>
    #include
    <stdlib.h>
    int main(void) { File *input; File *output; char initials[2], command[1]; double var1, var2; //Open the input file to read from input = fopen("input.txt", "r"); //Open the output file to write to output = fopen("output.txt", "w");  //Make sure files open if(input && output != NULL) { fscanf(input,'%s %d %d',command, &var1, &var2); if(command == "Q"){exit(0);} else if(command == "+") { printf("%d + %d = (var1+var2)\n"); fprintf(output, "%d + %d = (var1+var2)\n"); } else if(command == "-") {printf("%d - %d = (var1-var2)\n"); fprintf(output, "%d - %d = (var1-var2)\n"); } else if(command == "*") {printf("%d X %d = (var1*var2)\n"); fprintf(output, "%d X %d = (var1*var2)\n"); } else if(command == "/") {printf("%d / %d = (var1/var2)\n"); fprintf(output, "%d / %d = (var1/var2)\n"); } else if(command == H) {printf("Q = Quit\n/ divides\n* multiplies\n+ adds\n- subtracts\nH = Help"); fprintf(output, "Q = Quit\n/ divides\n* multiplies\n+ adds\n- subtracts\nH = Help"); } else {printf("Something went wrong.\n"); fprintf(output, "Something went wrong.\n");} }else {printf("Could not open one of the files.\n"); fprintf(output, "Could not open one of the files.\n");
    Last edited by snicklefritz; 11-06-2011 at 05:40 PM.

  2. #2
    Registered User
    Join Date
    Nov 2011
    Posts
    5
    i've also tried using a switch statement to read the command but it wont recognize /, *, +, -. any help on how to write this would be great thanks.. the instructions are:
    1. Problem Description:

    You are required to design a calculator. This calculator reads commands from data files called CommandsProj1.dat and CommandsProj2.dat, which will be available from the class website.
    Your program should read the commands 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.

    1. Details:

    The first line of the file contains two characters giving the initials of the user (such as GN). The rest of the file consists of a sequence of commands. Each line of the command file contains one command.
    Each command starts with a character and may have zero or more operands. You may assume that the data file has no errors. The number of commands in the file is unknown. But your processing may stop as soon as the Quit command is processed. The commands are: integer add, integer subtract, integer multiply, integer divide, change to uppercase, change to lowercase, separate, print out all digits, print
    out the k-th digit, round real number to desired number of decimal places, divide number into two.

    Calculator Commands:

    + i j [Integer Add] Add integers i and j and print out result

    * i j [Integer Multiply] Multiply integers i and j and print out result

    - i j [Integer Subtract ] Subtract integer j from i and print out result

    / i j [Integer Divide ] Divide integer i by j and print out result of integer division

  3. #3
    Registered User
    Join Date
    Nov 2011
    Posts
    63
    Try
    Code:
    command[0] == 'X'; /* Comparing a character to a character */
    or just use strcmp or strncmp for comparing strings to strings.

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by snicklefritz View Post
    Basically, I have to build a program that reads a mathematical command (*, /, +, -) and two variables in a text file and performs the command with the two variables and prints the answer to the screen and to another text file.
    i wrote the code and can only see !!!Hello World!!!
    Ok, lets start with some basics...

    Post *only* plain text source code between code tags. That mess you posted is ugly as sin and only barely readable...

    Now for the big problem... C is a language with no real string type. Instead it uses character arrays as strings. A list of characters in an array qualifies as a C-String when it is followed by a trailing null.

    All C Library string functions rely upon this format and if it's not followed horrible things can happen. So, for example your ... char initials[2]; ... declaration is wrong. To enter 2 characters you need an array of 3 slots... 2 for the text, 1 for the trailing 0. The same with command... should be 2 not 1... 1 for the command char and 1 for the null.

    Since C has no native string type, obvious stuff such as ... if (string == string) won't work. You cannot manipulate strings across operators (< = > != etc.) you *have* to use the library functions to manipulate text. Thus you have two choices... either use strcmp() to test your operators or use a single ... char command; ... and compare them to their ascii values using ... if (command == 'A')... etc. (Note the single apostrophic quote marks, rather than the standard quotations!)

    If you are going to open files... *Test* the file pointers to make sure the file actually did open and bail out if it didn't. You don't want to go crashing down into the rest of your code causing a cascade of errors if those files don't open properly.

    Your main function appears to have no closing brace and no return value... bad juju that, your compiler should be complaining like crazy about it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 03-10-2010, 11:28 AM
  2. Replies: 14
    Last Post: 04-01-2008, 02:23 AM
  3. producing c/c++ code from flowcharts,pseudo code , algorithims
    By rohit83.ken in forum C++ Programming
    Replies: 3
    Last Post: 02-20-2008, 07:09 AM
  4. Having trouble translating psudeo-code to real-code.
    By Lithorien in forum C++ Programming
    Replies: 13
    Last Post: 10-05-2004, 07:51 PM
  5. Replies: 0
    Last Post: 02-21-2002, 06:05 PM