-3

Here is my exercise:

Write a function that receives from the user two strings that show two real numbers (possibly negative), And produces a string that contains the integer part of most of the numbers represented by the string. For example, Given the string 2356.12 and the string 243.5 the program must create the string 2112.

void addstrings(char *snum1, char *snum2, char *res);

I need to create this function, and two more helper functions

//  a function that receives a string that represents an actual number and returns the above number (that i succeed)
double stod(char *snum);
// A function that receives an integer and produces a string representing the number
void itos(long int num, char *snum);

that are used in the addstrings function.


Here is my attempt so far:

void addstring(char *snum1, char *snum2, char *res) {
    stod(snum1);
    stod(snum2);

    *res = (long int)(*snum1 + *snum2);
}

double stod(char *snum) {
    int res = 0;
    for (int i = 0; snum[i] != '\0'; i++)
        res = res * 10 + snum[i] - '0';

    // return result.
    return res;
}

double itos(long int num, char *snum) {
    int i = 0;
    while (num) {
        snum[i++] = (num % 10) + '0';
        num = num / 10;
    }

    return (double)num;
}

int main() {
    char arr1[SIZE + 1];
    char arr2[SIZE + 1];
    char *res = NULL;
    int temp;

    gets(arr1);
    gets(arr2);

    addstring(arr1, arr2, res);

    printf("%d", addstring);

    return 0;
}

What am I missing in the addstring function?

chqrlie
  • 131,814
  • 10
  • 121
  • 189
Benzz
  • 11
  • 2
  • 2
    Dosen't say anything about ``? So why not `snprintf`? ;) – Some programmer dude May 14 '22 at 13:17
  • 2
    On a more serious note, never ever use `gets`! It's considered so [dangerous](https://stackoverflow.com/questions/1694036/why-is-the-gets-function-so-dangerous-that-it-should-not-be-used) that it have even been removed from the C standard. Use e.g. [`fgets`](https://en.cppreference.com/w/c/io/fgets) instead. – Some programmer dude May 14 '22 at 13:17
  • how you would do this exrcise using snprintf but must include these two functions.? – Benzz May 14 '22 at 13:21
  • Unrelated (for now) advice: you've got to be real careful with floating-point numbers. What appears to be `2.46` can in fact be `2.45999999994236543652` – pmg May 14 '22 at 13:21
  • its not my problem , my problem is to implement itos and addstring functions – Benzz May 14 '22 at 13:22
  • `itos` is easier if you start at the right, with the units digit. Once you have that, just `reverse` the temporary. So the int `2804` get transformed to the "temporary" string `"4082"` and reversed into `"2804"` – pmg May 14 '22 at 13:25
  • how to do that in CODE ? – Benzz May 14 '22 at 13:26
  • Want me to send the solution directly to your teacher? Hint: use `%` (the remainder operator) and `/` to "walk" the digits of the input number. – pmg May 14 '22 at 13:26
  • i made a update please see it , what i missing in addstring function/ – Benzz May 14 '22 at 13:58
  • I have cleaned your question up slightly. Note you have a discrepancy between `addstrings`, plural, in the initial exercise description, and `addstring`, singular, in the code and its description. – Oka May 14 '22 at 14:10
  • @Benzz `stod(snum1)`, `stod(snum2)`: you're returning a value from `stod()`, but you never use that. Apart from that, I don't think `*res = (long int)(*snum1 + *snum2);` is doing what you think it is doing; plus, you're also dereferencing a NULL pointer (`res`) here. – JASLP doesn't support the IES May 14 '22 at 14:14
  • how to fix it ? – Benzz May 14 '22 at 14:15
  • You should start by providing a proper [Minimal, Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example) that compiles without error (preferably treating warnings as errors), or make the focus of the question be about the errors received. Some things to answer: Which headers do you use? If none, where is `printf` defined? What is `SIZE`? What do you expect `printf("%d", addstring);` to do, considering `addstring` is a function? Why do you discard the results of `stod`? No one wants to do your homework, so you must show effort to narrow this to a specific question. – Oka May 14 '22 at 14:49

1 Answers1

0

There are many problems in your question and your code:

  • Write a function that receives from the user two strings that show two real numbers (possibly negative), this part is unclear: do you mean the function should read user input and convert it to double values?

  • And produces a string that contains the integer part of most of the numbers represented by the string This is also unclear. From the next phrase, it appears you should output the integral part of the difference of the double values.

  • double stod(char *snum); could be implemented using sscanf(). Your version only works for positive integral values.

  • void itos(long int num, char *snum); could be implemented simply using sprintf().

  • in addstrings you do not store the return values of stod() and instead you add the values of the first characters of the strings and you store that as the first character of the output.

  • you should never use gets()

  • printf("%d", addstring); is incorrect: you pass the address of function addstring instead of its return value.

Here is a corrected version:

#include <math.h>
#include <stdio.h>

double stod(char *snum) {
    double d;
    if (sscanf(snum, "%lf", &d) == 1)
        return d;
    else
        return 0;
}

void itos(long int num, char *snum) {
    sprintf(snum, "%ld.", num);
}

void addstring(char *snum1, char *snum2, char *res) {
    double d1 = stod(snum1);
    double d2 = stod(snum2);
    long int diff = (long int)fabs(d1 - d2);
    itos(diff, res);
}

int main() {
    char arr1[32];
    char arr2[32];
    char res[32];

    if (scanf("%31s%31s", arr1, arr2) == 2) {
        addstring(arr1, arr2, res);
        printf("%s\n", res);
    }
    return 0;
}
chqrlie
  • 131,814
  • 10
  • 121
  • 189