Jump to content

I'm a really noob programming, I mean that I don't have any idea on what I'm doing but somehow I decided to write a little program just for fun.

Since my C's knowledge is just awful because the last time I used something even close was months before, I have no idea why this thingy thing doesn't work.

 

 

//MRU

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

int main () {
    //MRU x=p+v(t-y)
    double x,p,v,t,y,q,w,e,r,u;
    
    printf ("                                       MRU\nIntroduzca los datos\nPresione '0' si no conoce el valor\n\n");
    printf ("Distancia final:\n");
    scanf ("%d.", &x);
    printf ("Distancia inicial:\n");
    scanf ("%d.", &p);
    printf ("Velocidad:\n");
    scanf ("%d.", &v);
    printf ("Tiempo final:\n");
    scanf ("%d.", &t);
    printf ("Tiempo inicial:\n");
    scanf ("%d.", &y);
    
    if (x==0) {
        q==p+v*(t-y);
        printf ("La distancia final es %f.",q);
    }
    if (p==0) {
        w==x-v*(t-y);
        printf ("La distancia inicial es %f.",w);
    }
    if (v==0) {
        e==(x-p)/(t-y);
        printf ("La velocidad es %f.",e);
    }
    if (t==0) {
        r==(x-p-v*y)/v;
        printf ("El tiempo final es %f.",r);
    }
    if (y==0) {
        u==(p+v*t-x)/v;
        printf ("El tiempo inicial es %f.",u);
    }
    
    return 0;
}

 

It is on Spanish but feel free to edit it and do whatever you want with the code since it is garbage

 

Thanks

 

Link to comment
https://linustechtips.com/topic/599268-unknown-problemsolved/
Share on other sites

Link to post
Share on other sites

in scanf %d is for integers

you need to use %lf for doubles

NEW PC build: Blank Heaven   minimalist white and black PC     Old S340 build log "White Heaven"        The "LIGHTCANON" flashlight build log        Project AntiRoll (prototype)        Custom speaker project

Spoiler

Ryzen 3950X | AMD Vega Frontier Edition | ASUS X570 Pro WS | Corsair Vengeance LPX 64GB | NZXT H500 | Seasonic Prime Fanless TX-700 | Custom loop | Coolermaster SK630 White | Logitech MX Master 2S | Samsung 980 Pro 1TB + 970 Pro 512GB | Samsung 58" 4k TV | Scarlett 2i4 | 2x AT2020

 

Link to comment
https://linustechtips.com/topic/599268-unknown-problemsolved/#findComment-7779056
Share on other sites

Link to post
Share on other sites

1 minute ago, Enderman said:

in scanf %d is for integers

you need to use %lf for doubles

OK, thanks but It continues giving me 0 as a result (I you traduce the printfs you will understand what I want to do [I'll fix the "Introduce '0' if you don't have the value" thing])

 

Link to comment
https://linustechtips.com/topic/599268-unknown-problemsolved/#findComment-7779074
Share on other sites

Link to post
Share on other sites

3 minutes ago, bySLiTz said:

OK, thanks but It continues giving me 0 as a result (I you traduce the printfs you will understand what I want to do [I'll fix the "Introduce '0' if you don't have the value" thing])

 

if you remove one of the statements from an if does it still give you 0?

try printing %f instead of %f. and see if that makes a difference

NEW PC build: Blank Heaven   minimalist white and black PC     Old S340 build log "White Heaven"        The "LIGHTCANON" flashlight build log        Project AntiRoll (prototype)        Custom speaker project

Spoiler

Ryzen 3950X | AMD Vega Frontier Edition | ASUS X570 Pro WS | Corsair Vengeance LPX 64GB | NZXT H500 | Seasonic Prime Fanless TX-700 | Custom loop | Coolermaster SK630 White | Logitech MX Master 2S | Samsung 980 Pro 1TB + 970 Pro 512GB | Samsung 58" 4k TV | Scarlett 2i4 | 2x AT2020

 

Link to comment
https://linustechtips.com/topic/599268-unknown-problemsolved/#findComment-7779097
Share on other sites

Link to post
Share on other sites

2 minutes ago, Enderman said:

if you remove one of the statements from an if does it still give you 0?

try printing %f instead of %f. and see if that makes a difference

Same

What I want to do is a program that ask you for the values of x, p, v, t and y that follows the next equation x=p+v(t-y). If you don't have one of these, you just type 0 (I'll do this another way, don't worry) and the program will give you the value of the variable you don't have the value of.

Link to comment
https://linustechtips.com/topic/599268-unknown-problemsolved/#findComment-7779115
Share on other sites

Link to post
Share on other sites

4 minutes ago, bySLiTz said:

Same

What I want to do is a program that ask you for the values of x, p, v, t and y that follows the next equation x=p+v(t-y). If you don't have one of these, you just type 0 (I'll do this another way, don't worry) and the program will give you the value of the variable you don't have the value of.

yeah i know what you want the program to do

i found what the issue is though

 

when youre doing 

q==p+v*(t-y);

on all your equations youre using == instead of =

== just compares the two and says true or false

= is what you need to use to set p equal to the answer :)

NEW PC build: Blank Heaven   minimalist white and black PC     Old S340 build log "White Heaven"        The "LIGHTCANON" flashlight build log        Project AntiRoll (prototype)        Custom speaker project

Spoiler

Ryzen 3950X | AMD Vega Frontier Edition | ASUS X570 Pro WS | Corsair Vengeance LPX 64GB | NZXT H500 | Seasonic Prime Fanless TX-700 | Custom loop | Coolermaster SK630 White | Logitech MX Master 2S | Samsung 980 Pro 1TB + 970 Pro 512GB | Samsung 58" 4k TV | Scarlett 2i4 | 2x AT2020

 

Link to comment
https://linustechtips.com/topic/599268-unknown-problemsolved/#findComment-7779134
Share on other sites

Link to post
Share on other sites

4 minutes ago, Enderman said:

yeah i know what you want the program to do

i found what the issue is though

 

when youre doing 


q==p+v*(t-y);

on all your equations youre using == instead of =

== just compares the two and says true or false

= is what you need to use to set p equal to the answer :)

Holy thanks, I didn't realised that I had used == and not =.

Thanks I really aprecciate it.

Now that we fixed it. Do you know how can I pass a scanf and then see if the variable have data or not to execute the if or not instead of using 0s since they can appear normally in the equation. Maybe what I want the way I'm doing it, is impossible but maybe you can come out with an idea to do it.

Thanks

Link to comment
https://linustechtips.com/topic/599268-unknown-problemsolved/#findComment-7779169
Share on other sites

Link to post
Share on other sites

4 minutes ago, bySLiTz said:

Holy thanks, I didn't realised that I had used == and not =.

Thanks I really aprecciate it.

Now that we fixed it. Do you know how can I pass a scanf and then see if the variable have data or not to execute the if or not instead of using 0s since they can appear normally in the equation. Maybe what I want the way I'm doing it, is impossible but maybe you can come out with an idea to do it.

Thanks

hm
usually for stuff like this -1 is used to indicate "no value"

 

not sure if that would work for you though, maybe you want to calculate with negative values too

in which case you would need to put in something other than a number, and idk what happens if you give a character to scanf expecting a double

there must be some way of detecting that, but ive only been doing C for a few weeks so idk yet

NEW PC build: Blank Heaven   minimalist white and black PC     Old S340 build log "White Heaven"        The "LIGHTCANON" flashlight build log        Project AntiRoll (prototype)        Custom speaker project

Spoiler

Ryzen 3950X | AMD Vega Frontier Edition | ASUS X570 Pro WS | Corsair Vengeance LPX 64GB | NZXT H500 | Seasonic Prime Fanless TX-700 | Custom loop | Coolermaster SK630 White | Logitech MX Master 2S | Samsung 980 Pro 1TB + 970 Pro 512GB | Samsung 58" 4k TV | Scarlett 2i4 | 2x AT2020

 

Link to comment
https://linustechtips.com/topic/599268-unknown-problemsolved/#findComment-7779195
Share on other sites

Link to post
Share on other sites

1 minute ago, Enderman said:

hm
usually for stuff like this -1 is used to indicate "no value"

 

not sure if that would work for you though, maybe you want to calculate with negative values too

in which case you would need to put in something other than a number, and idk what happens if you give a character to scanf expecting a double

there must be some way of detecting that, but ive only been doing C for a few weeks so idk yet

What languages do you use?

Link to comment
https://linustechtips.com/topic/599268-unknown-problemsolved/#findComment-7779201
Share on other sites

Link to post
Share on other sites

1 minute ago, bySLiTz said:

What languages do you use?

C++ python and just started C

NEW PC build: Blank Heaven   minimalist white and black PC     Old S340 build log "White Heaven"        The "LIGHTCANON" flashlight build log        Project AntiRoll (prototype)        Custom speaker project

Spoiler

Ryzen 3950X | AMD Vega Frontier Edition | ASUS X570 Pro WS | Corsair Vengeance LPX 64GB | NZXT H500 | Seasonic Prime Fanless TX-700 | Custom loop | Coolermaster SK630 White | Logitech MX Master 2S | Samsung 980 Pro 1TB + 970 Pro 512GB | Samsung 58" 4k TV | Scarlett 2i4 | 2x AT2020

 

Link to comment
https://linustechtips.com/topic/599268-unknown-problemsolved/#findComment-7779203
Share on other sites

Link to post
Share on other sites

1 minute ago, bySLiTz said:

Could you try to do the scanf's thing on C++ or even Python to see if an idea appears to me?

scanf is only C

python is raw_input() and C++ is cin>>

so yeah not really :/

 

but i have found this

http://stackoverflow.com/questions/1681873/how-to-make-sure-input-is-a-double-in-the-c-programming-language

i think you can use this to check if the input was a double or not

so you could input some letter instead of a number and make it know that that is what you want to calculate

 

but thats a lot harder than just detecting -1 or not

NEW PC build: Blank Heaven   minimalist white and black PC     Old S340 build log "White Heaven"        The "LIGHTCANON" flashlight build log        Project AntiRoll (prototype)        Custom speaker project

Spoiler

Ryzen 3950X | AMD Vega Frontier Edition | ASUS X570 Pro WS | Corsair Vengeance LPX 64GB | NZXT H500 | Seasonic Prime Fanless TX-700 | Custom loop | Coolermaster SK630 White | Logitech MX Master 2S | Samsung 980 Pro 1TB + 970 Pro 512GB | Samsung 58" 4k TV | Scarlett 2i4 | 2x AT2020

 

Link to comment
https://linustechtips.com/topic/599268-unknown-problemsolved/#findComment-7779240
Share on other sites

Link to post
Share on other sites

1 minute ago, Enderman said:

scanf is only C

python is raw_input() and C++ is cin>>

so yeah not really :/

 

but i have found this

http://stackoverflow.com/questions/1681873/how-to-make-sure-input-is-a-double-in-the-c-programming-language

i think you can use this to check if the input was a double or not

so you could input some letter instead of a number and make it know that that is what you want to calculate

 

but thats a lot harder than just detecting -1 or not

Yeah, I didn't mean with scanf, just the way the language gets data from the user

Link to comment
https://linustechtips.com/topic/599268-unknown-problemsolved/#findComment-7779256
Share on other sites

Link to post
Share on other sites

2 minutes ago, bySLiTz said:

Yeah, I didn't mean with scanf, just the way the language gets data from the user

well C++ and python are different cause raw input and cin can take anything

but in C scanf needs the % thingy to specify what the input is

NEW PC build: Blank Heaven   minimalist white and black PC     Old S340 build log "White Heaven"        The "LIGHTCANON" flashlight build log        Project AntiRoll (prototype)        Custom speaker project

Spoiler

Ryzen 3950X | AMD Vega Frontier Edition | ASUS X570 Pro WS | Corsair Vengeance LPX 64GB | NZXT H500 | Seasonic Prime Fanless TX-700 | Custom loop | Coolermaster SK630 White | Logitech MX Master 2S | Samsung 980 Pro 1TB + 970 Pro 512GB | Samsung 58" 4k TV | Scarlett 2i4 | 2x AT2020

 

Link to comment
https://linustechtips.com/topic/599268-unknown-problemsolved/#findComment-7779267
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×