[Boards: 3 / a / aco / adv / an / asp / b / bant / biz / c / can / cgl / ck / cm / co / cock / d / diy / e / fa / fap / fit / fitlit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mlpol / mo / mtv / mu / n / news / o / out / outsoc / p / po / pol / qa / qst / r / r9k / s / s4s / sci / soc / sp / spa / t / tg / toy / trash / trv / tv / u / v / vg / vint / vip / vp / vr / w / wg / wsg / wsr / x / y ] [Search | Free Show | Home]

/NTC/ New to coding

This is a blue board which means that it's for everybody (Safe For Work content only). If you see any adult content, please report it.

Thread replies: 17
Thread images: 1

File: Gnome_child_chathead.png (4KB, 74x96px) Image search: [Google]
Gnome_child_chathead.png
4KB, 74x96px
I am relatively new to coding. I was assigned reading in my textbook for an intro to coding class and I copied down this code into visual basic and it ran fluently. Can anyone explain to me why the word "double" appears in line 7 of the code?

//This program calculates the user's pay
#include <iostream>
using namespace std;

int main()
{
double hours, rate, pay;

//get the number of hours worked
cout << "How many hours did you work?";
cin >> hours;

//Get the hourly pay rate
cout << "How much do you get paid per hour?";
cin >> rate;

//Get pay
pay = hours * rate;

//Show pay
cout << "You have been paid:";
cout << pay << endl;
cout << "Thank you for your work" << endl;
return 0;
}

>TL;DR - what does "double" mean in the 7th line of this basic code?
>>
shameless self bump.
>>
>>56497197
That isn't Visual Basic. That is C++.

The `double` line is declaring 3 variables named "hours", "rate" and "pay", and `double` is the type of the variables. The type `double` is a floating point number, which means it's a number which isn't an integer and has more precision, to (approximately) store numbers with decimal places like `1.5`.
>>
>>56497268
so having "double" in the line allows the data range when hours and rate are input and when pay is output to be much larger? including decimals.

and yeah, i know it is C++, though the program I am "Coding" in is visual basic 2012
>>
>>56497268
sorry, I am using Microsoft Visual Studios.
>>
Firstly, this violates the rules, but whatever
Secondly, this isn't visual basic
Thirdly, you can litteraly google that
And lastly, simply, it's just a number that can have a decimal point
>>
A double is a 64 bit complementary signed IEEE 754 standard floating point number.
>>
>>56497357
I apologize, I was not aware this violated a rule. And thank you for the clarification. I did google it but I just wanted to make sure what I read was what I thought it was. It's a lot to take in at once, but I appreciate you guys assisting me. Thank you
>>
>>56497333
If you want to use a variable in C++, you have to declare it first, which involves giving it a type.

All you do to declare a variable is type
variable_type variable_name;
, for example
int number_of_days;
or
double rate;
.

In C++ there is also a shortcut for declaring multiple variables at once, by doing
variable_type variable1, variable2, variable3;
, which is what is happening here with
double hours, rate, pay;


Using a `double` does actually allow for a larger range than using an `int`, but the integer types available in C++ can typically have a very large range. `double` is used here simply for more accuracy, since an integer is a number with no decimal places. Though you probably will find that there are a couple of unusual problems you can run into using floating point numbers for accurate calculations involving money.
>>
>>56497515
That helped a lot, thank you.
>>
>>56497393
Not any of these anons, but a piece of advice: 1. Do not "using namespace std;" in global scope (see namespace polution) 2. Computers can't calc fractions and decimals correctly. For a computer (1/3)3 would be 0.(9), not 1. Overall adding fractions like (4/9) will produce wrong results, so you should avoid decimals at all if possible. Sometimes, it's just better to use a long int and just show it as a decimal to the user. Like: In memory - 1089(long int), to the user - 10.89.
>>
>>56497333
>the program I am "Coding" in is visual basic 2012

oh boy
>>
Gonna explain from the beginning so apologies if you know some of this already

In C++, variables have to be explicitly created (declared) before you use them. They also have a specific type that you assign upon declaration. You can declare a variable with the syntax
type varname
. You can also declare multiple variables of the same type in one go with
type varname1, varname2, varname3, etc
.

Line 7 in that programming is declaring three variables "hours", "rate", and "pay" of the type "double". These variables are used later on in lines such as

cin >> hours; //assigning user input to the hours variable
cout << pay //printing the pay variable's value to the screen


A double is a type of number in C++. In layman's terms, it's just a number that can have a really precise decimal value (e.g. 1.23456789). Other number types include "int" (an integer with no decimal) and "float" (another type of decimal that takes less space to store, but cannot be as precise)
>>
>>56497627
Thank you, this was helpful as well
>>
Can i ask questions too?
I tried many programming languages (as a hobby), and my favourite are java, c# and c++. I want to learn a language very well, and if possible use it for some small projects. What would you recommend? I already tried gtk#, javafx and qt and for what i need, they're both good.

Also, are rust and go actually worth learning or are they useless. Rust was fun to use with cargo and atom
>>
>>56497197
Hi gentooman. If you like RuneScape, try making a bot with DreamBot or TopBot API (the latter is easier, but not that active).

Look at some tutorials on the forums, thats a really good way to kickstart your programming.
>>
Also worth noting that you'll want to put << flush; at the end of your cout statements where there is no << endl;
Thread posts: 17
Thread images: 1


[Boards: 3 / a / aco / adv / an / asp / b / bant / biz / c / can / cgl / ck / cm / co / cock / d / diy / e / fa / fap / fit / fitlit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mlpol / mo / mtv / mu / n / news / o / out / outsoc / p / po / pol / qa / qst / r / r9k / s / s4s / sci / soc / sp / spa / t / tg / toy / trash / trv / tv / u / v / vg / vint / vip / vp / vr / w / wg / wsg / wsr / x / y] [Search | Top | Home]

I'm aware that Imgur.com will stop allowing adult images since 15th of May. I'm taking actions to backup as much data as possible.
Read more on this topic here - https://archived.moe/talk/thread/1694/


If you need a post removed click on it's [Report] button and follow the instruction.
DMCA Content Takedown via dmca.com
All images are hosted on imgur.com.
If you like this website please support us by donating with Bitcoins at 16mKtbZiwW52BLkibtCr8jUg2KVUMTxVQ5
All trademarks and copyrights on this page are owned by their respective parties.
Images uploaded are the responsibility of the Poster. Comments are owned by the Poster.
This is a 4chan archive - all of the content originated from that site.
This means that RandomArchive shows their content, archived.
If you need information for a Poster - contact them.