Jump to content

Writing a programm in C, need help

IndisciousWrath

Hello dear Programmers,

so im currently taking the cs50 computer science course online on the pset1 (Smart Water). Please don't correct my code, just answer my question otherwise it would be cheating (so answer by saying for ex "you need to start a new line and then multiply 10 with 12 and NOT something like : "write int blabla = blabla ... (so basically do not tell me what code i need to write but do tell me what i need to do. i know very confusing and i'm sorry, english is not my native language.))

 

This programm is calculating how many bottles of water you use while showering (in minutes).

 

1 #include <cs50.h>
2 #include <stdio.h>

3 int main (void)

4 {
5    printf("How many mintues do you need to shower? :");
 6   float f = GetFloat();
 7   
 8   float b = 192 / 16 = 12 (f*b)
 9   

10 }

 

what i want to do is : in line 8 i am dividing 192/16 and that is 12 so in one minute you use around 12 bottles of water. Now i wanted to multiply 12 with the amount of minutes the user stated above (that's what i tried to do i know my way is wrong tho XD)

Link to comment
Share on other sites

Link to post
Share on other sites

3 minutes ago, Nicholatian said:

#include <cs50.h>
#include <stdio.h>

int main( void )
{
    printf( "How many mintues do you need to shower? :" );
    float f = GetFloat( );
    
    float b = 12 * b;
}

Try that.

dang it, could't edit my post fast enough .... (now it says don't tell me exactly what to write, but how to write it XD) not your fault tho dont wurry

Link to comment
Share on other sites

Link to post
Share on other sites

3 minutes ago, Nicholatian said:

Well, since you already did the math for 192 ÷ 16, you don’t need to write that out; just write the answer, 12. Take b out of parentheses, and assign it to 12 times whatever f is, and do whatever you need to do with b.

 

Keep in mind that you can’t assign a variable more than once in a single statement. So if you needed to change the value of b, you would write something like:


#include <cs50.h>
#include <stdio.h>

int main( void )
{
    printf( "How many mintues do you need to shower? :" );
    float f = GetFloat( );
    
    float b = 12 * f;
    b       = 66; // or whatever you want to do
}

You see?

 

Also, if you need to do something like b = 66 * b, you can do that more simply with b *= 66.

Thank you :D . I did that but how do i print that out now ? (the answer) i tried

 

#include <cs50.h>
#include <stdio.h>

int main (void)

{
    printf("How many mintues do you need to shower? :");
    float f = GetFloat();
    
    float b = 12 * b;
    printf("You use around %f\n", b);
    
}

 

but that ain't working.

Link to comment
Share on other sites

Link to post
Share on other sites

9 minutes ago, Nicholatian said:

Firstly, please use code tags. There should be a button in the editor that looks like a pair of angle brackets; click on that and set the syntax highlighting to C.

 

You need to read the error messages spat out by your compiler. It will tell you that you have a syntax error where string bottles = b is sitting, and it will also tell you that you cannot implicitly cast a float to a string.

 

Considering you’re using C, the string concept doesn’t exist anyway because C isn’t object-oriented. But if it were, you set the bottles variable without ever using it. By the way, strings in C are kept in character arrays/pointers, commonly seen as char*.

 

Luckily for you, printf() handles the conversion of f from a floating-point number to a string. So just pass b to printf() like you were already doing.

uhm yeah about that i already corrected some of thoose things now i got (and btw the libraries you see include string etc.)

#include <cs50.h>
#include <stdio.h>
int main (void)
{
    printf("How many mintues do you need to shower? :");
    float f = GetFloat();
    
    float b = 12.0 * b;
    printf("You use around %f\n", b);
    
}

and my error is :

water.c:8:11: error: unused variable 'f' [-Werror,-Wunused-variable]
    float f = GetFloat();
          ^
water.c:10:22: error: variable 'b' is uninitialized when used within its own initialization [-Werror,-Wuninitialized]
    float b = 12.0 * b;
          ~          ^
2 errors generated.
make: *** [water] Error 1

 

Link to comment
Share on other sites

Link to post
Share on other sites

float b = 12.0 * b;

Is probably a typo and should be "12.0 * f" ? As it is now "f" is never used and "b" itself is used to initialise "b" which is impossible just as your compiler says.

Link to comment
Share on other sites

Link to post
Share on other sites

8 hours ago, Unimportant said:

float b = 12.0 * b;

Is probably a typo and should be "12.0 * f" ? As it is now "f" is never used and "b" itself is used to initialise "b" which is impossible just as your compiler says.

Yup. Hopefully just a typo, and not OP trying to multiply something that doesn't exist yet. 

Here's the correct code, added some comments for OP

#include <cs50.h>
#include <stdio.h>
int main (void)
{
    printf("How many mintues do you need to shower? :");
    float f = GetFloat();	//get user input
    
    float b = 12.0 * f;		//multiply user input by 12.0
    printf("You use around %f\n", b);	//print result to console/terminal
}

 

Edited by SnowCore

"Wait for the 1080ti" - they said.

Link to comment
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

×