-2

In a school assignment we where supposed to write a program that takes in a number and divide it in three parts: 1. Check if the number is positive or negative 2. whole number (magnitude) 3. fractional parts

The requirement is that there should be a own function called separate that has input and output parameter.

For example: if you type in 23.639, the program should sort of out and print out: Sign: + Whole number magnitude: 23 Fractional parts: 0.639

QUESTIONS: 1.The function for sorting out whether the number is positive or negative brings forth the wrong answer when typing in a negative number. It also posts wrong character. I have tried different data types, like int, char and float, but none seem to work. Any tip on how to solve this are greatly appreciated, cause I think I'm blinded by my own errors...

2.The function separating decimals from whole numbers (fractions) won't subtract the whole number away from the decimals, so I am stuck with whole number. Can anyone spot my error here?


* UPDATE *

I managed to solve the questions at hand, and did the terrible n00b error of editing the code I first posted in this question. I have now edited the code once more to hold the original errors as best as I can remember. The right code is posted as an answer below.

Sorry for the rookie error.

/*
Author: Thorbjørn Elvestad
Student ID: *****
E-mail: drommevandrer@gmail.com


This program take in number typed in by the user, and then divide it into three parts.

SIGN: '+' or '-'
Whole number: Show number as a whole number
Fraction: Show fractions 

The program uses function to sort out the number, and print out the result*/

/* Declaring libraries */
#include <stdio.h>
#include <stdlib.h>


/* Declaring functions */
double sorting_sign(char x);
double sorting_whole(double x);
double sorting_fract(double x, int y);



/* Calling main function */
int main()
{
    double num, fractures; /* declaring variables */
    int sign_sorted, part;
    double whole_sorted;

    printf("LET ME TELL YOU SOME INTERESTING STUF ABOUT YOUR NUMBER!\n\n");
    printf("Enter your number: ");
    scanf("%d", &num);

    sign_sorted = sorting_sign(num); /* Calling the function that sorts out if this number is '+' or '-' */
    whole_sorted = sorting_whole(num); /* Calling the function separating whole number from decimals */
    fractures = sorting_fract(num, num); /* Calling the function removing the whole number from the fractures */

    printf("Sign: %c\nWhole: %0.lf\nFraction: %f", sign_sorted, whole_sorted, fractures);

    return 0;
}


/* Function for sorting of if number is '+' or '-' */
double sorting_sign(char x)
{
    int sign;

    /* true if number is less than 0 */
    if(x < 0.0){sign = '-';}

    /* true if number is greater than 0 */
    else if(x > 0.0){sign = '+';}

    return (sign);
}


/* Function for sorting out the whole number */
double sorting_whole (double x)
{
    int whole;

    whole = x;

    return (whole);
}

/* Function for sorting out the fractions */
double sorting_fract(double x)
{
    int whole;
    double fract;
    whole = y;
    fract = x - whole;


    return (fract, whole);
}
  • Is `(fract, whole)` a double? – Barry Michael Doyle Oct 16 '16 at 14:00
  • `scanf("%d", &num);` --> `scanf("%lf", &num);` and similarly `printf("DEBUGGING 1 (in main): Your number is %d ...` should use type `%f`. – Weather Vane Oct 16 '16 at 14:01
  • Yes, ofcourse... that did the trick. Thank you... I had a feeling I had manage to run into a maze of data types somewhere, and messed up the code in my search to fix it all. However, I did think the problem where in the functions, and not my scanf, but that actually made a lot of sense :) – LotharianBC Oct 16 '16 at 14:49
  • 1
    sorry, rookie mistake.... I have edited the faulty code back as good as I remember, and edited the original questions to be more readable and understandable. The answer to the both questions and including fully working program is posted as an fully answer below. – LotharianBC Oct 16 '16 at 18:32

2 Answers2

1

You've declared your sorting_sign function to return a double, when you're returning an int set to the value of a char... sort your types out.

Joseph Young
  • 2,758
  • 12
  • 23
  • Indeed, sort your types out - wrong format types for `scanf` and `printf` too. – Weather Vane Oct 16 '16 at 14:04
  • Thanks... I did mess up with the data types in my search to sort it all out, and it really backfired me into a total mess... Thanks for tip, both of you :)... printf("Enter your number: "); scanf("%lf", &num); and changing that function into int did the trick. – LotharianBC Oct 16 '16 at 14:52
1

SOLVED! For future reference I hereby post the code for the fully working program:

/*
Author: Thorbjørn Elvestad
Student ID: *****
E-mail: drommevandrer@gmail.com


This program take in number typed in by the user, and then divide it into three parts.

SIGN: '+' or '-'
Whole number: Show number as a whole number
Fraction: Show fractions 

The program uses function to sort out the number, and print out the result*/

/* Declaring libraries */
#include <stdio.h>
#include <stdlib.h>


/* Declaring functions */
int sorting_sign(int x);
double sorting_whole(double x);
double sorting_fract(double x);



/* Calling main function */
int main()
{
double num, fractures; /* declaring variables */
int sign_sorted, part;
double whole_sorted;

printf("LET ME TELL YOU SOME INTERESTING STUF ABOUT YOUR NUMBER!\n\n");
printf("Enter your number: ");
scanf("%lf", &num);

sign_sorted = sorting_sign(num); /* Calling the function that sorts out if this number is '+' or '-' */
whole_sorted = sorting_whole(num); /* Calling the function separating whole number from decimals */
fractures = sorting_fract(num); /* Calling the function removing the whole number from the fractures */

printf("Sign: %c\nWhole: %0.lf\nFraction: %f", sign_sorted, whole_sorted, fractures);

return 0;
}


/* Function for sorting of if number is '+' or '-' */
int sorting_sign(int x)
{
int sign;

/* true if number is less than 0 */
if(x < 0.0){sign = '-';}

/* true if number is greater than 0 */
else if(x > 0.0){sign = '+';}

return (sign);
}


/* Function for sorting out the whole number */
double sorting_whole (double x)
{
int whole;

whole = x;

return (whole);
}

/* Function for sorting out the fractions */
double sorting_fract(double x)
{
int whole;
double fract;
whole = (int)x;
fract = x - whole;


return (fract);
}