0

I've got an assignment to do at my university. The assignment is to take a string of some kind, turn it into an integer, do calculation and return the string.

This is what I've done so far, I'm not great at C so any sort of help would be great.

#include <stdio.h>
#include <stdlib.h>
#define _CRT_SECURE_NO_WARNINGS
typedef char* verylong;
verylong multiply_verylong(verylong vl1, verylong vl2);
verylong input_long();
verylong add_verylong(verylong vl1, verylong vl2);


verylong input_long()
{
    verylong string=NULL;
    char arry[1000];
    int input;
    printf("enter your verylong number");
    gets_s("%s",arry);

    for (int i = 0; i < 1000; i++)
    {
        if (arry[i] == '\n')
        {
            input = i - 1;
            string = (verylong)malloc(i);
            break;
        }
        if ((arry[i] < 48) || (arry[i] > 57))
        {
            printf("invalid number");
            return NULL;
        }
    }

    for (int j = 0; j < input; j++)
    {
        string[j] = arry[j];
    }
    string[input + 1] = '\0';

    return string;
}

verylong add_verylong(verylong vl1, verylong vl2)
{   
    verylong input;
    char res;
    int numofvl1, numofvl2, result, i, j;
    numofvl1 = atoi(vl1);
    numofvl2 = atoi(vl2);
    result = numofvl1 + numofvl2;
    input = (int)malloc(result);
    snprintf(input,10, "%d", result);
    return input;
}
verylong multiply_verylong(verylong vl1, verylong vl2) {
    verylong input;
    int numofvl1, numofvl2, result;
    numofvl1 = atoi(vl1);
    numofvl2 = atoi(vl2);
    result = numofvl1 * numofvl2;
    input = (int)malloc(result);
    snprintf(input, 10, "%d", result);
    return input;
}


void main()
{

    verylong a, b, c;
    do {
        printf("enter the first long integer: ");
        a = input_long();
    } while (!a);
    do {
        printf("enter the second long integer: ");
        b = input_long();
    } while (!b);
    c = add_verylong(a, b);
    printf("%s + %s = %s\n", a, b, c);
    free(c);
    c = multiply_verylong(a, b);
    printf("%s * %s = %s\n", a, b, c);
    free(c);
    free(a);
    free(b);

    system("pause");
}


The "verylong input_long()" function is to check if the user have entered a string only from numbers. If he didnt, it should be returning null and printing invalid number. about the second function I'm not sure. I've tried to use atio() but it doesnt seems to be working.

once again, appreciate any sort of help. thank you guys.

EDIT: something goes wrong with my first function input_long(); if anyone has any idea what i should do it would be great. I'm suppose to get a string and see if it is all numbers, if its not. it should print a message and return null

14
  • 2
    Use double quotes for strings, not single quotes. Commented Jan 13, 2020 at 17:41
  • 1
    In add_verylong, result is an integer, but this function is supposed to return verylong, which is not an integer. Commented Jan 13, 2020 at 17:42
  • 1
    Can you be more specific than, "it doesn't seem to be working"? Are you getting errors or warnings? What are some of the details of your debugging efforts? Commented Jan 13, 2020 at 17:42
  • 1
    You can use sprintf() to write an integer value into a string. But there are some more issues in your code, e.g.: a) never use gets() it's dangerous ans deprecated b) if your string type is called verylong do you expect that you can represent the values in an int variable? If not, your calculations would be a little bit more dificult. c) don't cast the result of malloc() (search this site for that), d) don't use magic numbers like 48 and 57, but '0' and '9' Commented Jan 13, 2020 at 17:47
  • 2
    You seem to want to handle strings containing numbers with up to 999 digits? This certainly will fail the way you try to handle them: The int datatype only has a certain number of bits to represent a number, roughly 32, take one for the sign, and you are left with 2^31 possible numbers in an int. That means that the highest number you will be able to keep in an int is a decimal number with less than 31 digits (actually, I guess it will be something around 10 ? digits at most) Commented Jan 13, 2020 at 17:55

1 Answer 1

2

You shouldn't return char * like that, see: How can I return a character array from a function in C?

Your method can't support more than 19 digits (much less right now, you can support 19 digits if you use unsigned long long).

However if you need to support up to 999 digits I would go with a completely different approach: You should than do the calculation yourself, digit by digit, that way there is no limit to the number of digits you support (only limited by the size of the input buffer).

Plus in input_long arry will probably end with \0, not \n ,you should check for both.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.