Home > Net >  How do I get variables from one function into another?
How do I get variables from one function into another?

Time:04-01

So I am a beginner and I have been trying to create an app in which user enters a four digit number, then the digits of that number are printed and finally out of these digits a largest possible number is created. So if you enter a 1234 lets say, 4321 should be printed out. If you enter 6271 7621 should be printed and so on and so on. I got this to work easily when its all inside the int main() function, however for the life of me I cant get it work as a chain of separate functions, at the end it merely prints out the starting number or some huge number every time.

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>



int vratiBroj(int n) {

    for (int i = 0; i < 3; i  )
            if (n < 1000 || n>9999) {
                printf("Pogresan unos! Imate jos %d pokusaja.Probajte ponovo:\n", 3 - i);
                
                scanf("%d", &n);
                if (i == 2) {
                    printf("Nazalost ponestalo vam je pokusaja da unesete trazeni broj.");

                }

            }
    return n;
}

void vratiCifre(int pocetniBroj, int * ch, int * cs, int *cd, int * cj) {
    //int* ch, * cs, * cd, * cj;
    ch = pocetniBroj / 1000;
    cs = (pocetniBroj % 1000) / 100;
    cd = ((pocetniBroj % 1000) % 100) / 10;
    cj = ((pocetniBroj % 1000) % 100) % 10;

    printf("%d, %d, %d, %d", ch, cs, cd, cj);

    return ch, cs, cd, cj;
}

int formirajNajveci(int c1, int c2, int c3, int c4) {
    int prvi=0,drugi=0,treci=0,cetvrti=0,min=0,max1=0, max2=0, min1=0, min2=0;
    int najvecibroj=0;

    if (c1 > c2) max1 = c1,min1=c2;
    if (c2 > c1) max1 = c2, min1 = c1;

    if (c3 > c4) max2 = c3, min2=c4;
    if (c4 > c3)max2 = c4, min2=c3;

    if (min2 > max1) {
        int temp;
        temp = min2;
        min2 = max1;
        max1 = temp;
    }

    if (min1 > max2) {
        int temp;
        temp = min1;
        min1 = max2;
        max2 = temp;
    }

    if (max1 > max2) prvi = max1,drugi=max2;
    if (max2 > max1) prvi = max2, drugi = max1;

    if (min1 > min2) treci = min1, cetvrti = min2;
    if (min2 > min1) treci = min2, cetvrti = min1;

    najvecibroj = prvi * 1000   drugi * 100   treci * 10   cetvrti;

    return najvecibroj;
}


void domaci(int n) {

    printf("\nNajveci mogu broj je: %d", n);
}

int main() {
    int n;
    int c1=0, c2=0, c3=0, c4=0;
    printf("Unesite broj:\n");
    scanf("%d", &n);
    int a = vratiBroj(n);
    printf("%d\n", a);
    vratiCifre(a, c1, c2, c3, c4);
    formirajNajveci(c1, c2, c3, c4);
    domaci(a);
}

In int main is where my problem lies. The idea is that user enters a number n, which then goes through function vratiBroj which checks if it is indeed a four digit number. The number, if valid, is then supposed to go through the vratiCifre function, where its digits would be taken. The variable pocetniBroj (startingNumber) is supposed to be the number n. The digits are printed, and then I need to transfer the variables of four digits ch,cs,cd,cj into the formirajNajveci function, as c1,c2,c3,c4. Fianlly, after the formirajNajveci fonction forms the biggest number, that number is supposed to be sent into the domaci function and printed out.

Ive tried various stuff but I keep running in circles, tried using some pointer shenaningans, tried making new int variables for funciton results,

CodePudding user response:

You're not reading numbers, you are reading strings. The scanf function is parsing the string and converting it to an integer, and that just makes the problem more difficult. Read the value as a string, sort it as a string, and write it out. There's no need to ever convert it to a number. eg:

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

int cmp(const void *a, const void *b) { return *(char *)a < *(char *)b; }
int
main(void)
{
    char b[6];
    if(
        scanf("%5[0-9]", b) != 1
        || strlen(b) != 4
    ) {
        fprintf(stderr, "Invalid input\n");
        return 1;
    }
    qsort(b, 4, 1, cmp);
    puts(b);
    return 0;
}
  • Related