Thread: How to read variablles from txt and make sum,minus,multiply, divide with them?

  1. #1
    Registered User
    Join Date
    Jun 2020
    Posts
    4

    How to read variablles from txt and make sum,minus,multiply, divide with them?

    I need to read read from text file line by line and make four operations. Example:
    If the Input.txt file is like this:
    3+5
    2-1
    4*3
    8/4
    My program should read the veriables, do the operations then create a Output.txt file in same path and write the answers int it.

    This is my code so far and I'm stuck, reading the veriables but I don't know how to do operations and write to output.txt:
    Code:
    #include <stdio.h>
    
    int main(int argc, char* argv[])
    {
    int i;
    FILE* fptr = fopen("input.txt", "r"); 
    
    
    if(fptr == NULL){
    printf("\n Unable to open : %s ");
    return -1;
    }
    
    }
    fclose(fptr);
    return 0;
    }
    Last edited by mkache21; 06-20-2020 at 07:19 AM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    It depends how complicated your expressions are.

    Given your examples, a simple
    Code:
    int n1, n2;
    char op;
    if ( sscanf(output,"%d%c%d",&n1,&op,&n2) == 3 ) {
      if ( op == '+' )
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Jun 2020
    Posts
    4
    Expressions won't be complicated, they will be just like the example. I tried your code but it did nothing. If you tell me how will it make sum and show it, it's realy appreciated.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    It was half an answer (aka a hint) to see how far you could then go by yourself.

    Apparently, not that far.

    Post your effort, not "I tried ... and it didn't work".
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Jun 2020
    Posts
    4
    I did well by your help thank you so much. If I send you my email can you help me. My program working but not properly.

  6. #6
    Registered User
    Join Date
    May 2012
    Posts
    505
    One problem with beginner exercises is that handling text input correctly is quite difficult, because in theory the input file can be anything.

    However it seems that you are guaranteed digit operator digit with no whitespace. So it should be relatively easy to parse from first princples.
    Start by using fgets() to read each line at a time. The print out the lines to give yourself confidence that you are opening and reading the
    file correctly. Then try to break up each line into an integer, a character for the operator, and another integer. Again, print them out
    to satify yourself that the program is working correctly. Then put in the code to do the operation. Finally add the code to print out the
    results to the output file, and you are done.
    I'm the author of MiniBasic: How to write a script interpreter and Basic Algorithms
    Visit my website for lots of associated C programming resources.
    https://github.com/MalcolmMcLean


  7. #7
    Registered User
    Join Date
    Jun 2020
    Posts
    4
    My program is working now but only if I give the input just like example. If I change the order of lines(order of +,-,*,/ operations) or add more lines it does not work properly.

  8. #8
    Registered User
    Join Date
    May 2012
    Posts
    505
    Quote Originally Posted by mkache21 View Post
    My program is working now but only if I give the input just like example. If I change the order of lines(order of +,-,*,/ operations) or add more lines it does not work properly.
    That's no good. It's also surprising. The whole point of input is that the program reads it, and modifies its behaviour accordingly.
    I'm the author of MiniBasic: How to write a script interpreter and Basic Algorithms
    Visit my website for lots of associated C programming resources.
    https://github.com/MalcolmMcLean


  9. #9
    null pointer Structure's Avatar
    Join Date
    May 2019
    Posts
    338
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    char operation, values[2][10]; 
    
    float calculateFormula() {
        float result = 0.0;
        float left = atof(values[0]);
        float right = atof(values[1]);
        if (operation == '+') result = left + right;
        if (operation == '-') result = left - right;
        if (operation == '*') result = left * right;
        if (operation == '/') result = left / right;
        values[0][0] = '\0'; values[1][0] = '\0';
        return result;
    }
    
    void equate(char *calculate) { 
        char *ops = "+/-*";
        int side = 0, count = 0, skip = 0;
        for (int i=0; i < strlen(calculate)+1; i++) {
            skip = 0;
            for (int j=0; j < strlen(ops); j++) {
                if (calculate[i] == ops[j]) {
                    operation = ops[j]; skip = 1;
                    side = 1; count = 0; break;
                }
            } 
            if (calculate[i] == '\n' || calculate[i] == '\0') {
                side = 0; skip = 1; count = 0; 
                printf("%0.2f\n", calculateFormula() );
            }
            if (!skip) values[side][count++] = calculate[i];
        }
    }
    
    int main(int args, char *argv[]) {
    
        equate(" 3+5 \n 2-1 \n 4*3 \n 8/4 ");
      
        return 0;
    }
    Last edited by Structure; 07-01-2020 at 10:26 AM.
    "without goto we would be wtf'd"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How do I divide / multiply two variables?
    By Cofex in forum C Programming
    Replies: 5
    Last Post: 02-27-2013, 11:41 AM
  2. Need help with Multiply and Divide operators
    By Conti in forum C++ Programming
    Replies: 3
    Last Post: 03-03-2012, 04:44 AM
  3. Replies: 15
    Last Post: 01-24-2008, 09:40 PM
  4. Addition using only multiply and divide
    By abachler in forum Contests Board
    Replies: 26
    Last Post: 09-19-2007, 12:40 AM
  5. How to multiply and Divide Huge int numbers
    By Dewayne in forum C++ Programming
    Replies: 3
    Last Post: 10-21-2004, 08:41 PM

Tags for this Thread