need help to identify all the key components of a small program in cpp
can you please explain to me what's #include<iostream>, using namespace std,, and int main() do in simple and easy to remember words? thanks :)
you're using the functions cin and cout in your program, how does it know what cin and cout mean? It knows them because you've included the iostream header file which essentially tells what these functions are gonna do. you won't be able to use the input/output functions if you don't #include<iostream>
what if i'm using void keyword? would i still need to type #include<iostream>??? Also i've seen ppl type #include, #include one after the other but won't type <iostream> wat's that fudge about ?
yep, you'd have to type #include<iostream> keyword if you're using void ``` void main(){ } ``` ^this program won't work. ``` int main(){ return 0; } ``` ^but this will work as long as you're using int as the return type it will work without typing #include<iostream> but you'd need #include<iostream> for other important input output functions and much more
so all those keywords that we talked about come under iostream? :O what the.....? :O and so for every input and output program, we'd need to include #include <iostream> using namespace std;
btw is keywords and primitive data types both are the same thing correct?
wdym by the term 'keyword'? are you referring to the data types like int, char, bool or you're referring to the function calls like cin, cout
so my textbook refers keywords to as things like bool, int, double, etc just wanted to make sure it's the same thing as primitive datatypes? same thing
All primitive data types can be used without typing #include<iostream> except for void Also, if you're not typing #include<iostream> then you have to have your return type as int
omg that clears so many things lol thanks
also i need you to please help me understand what's a variable in a function... and please say it in simple words so it stick to my tiny brain. ;))))))))))))
int i, j; char c; bool b; i, j, c, b are variables here. I can store whatever value I wish into them, so their values can change according to my needs, they're variables.
i'm going to print out this thread and stick it on my forehead... really informative and helpful :)))))))))))))))))))))))
``` using namespace std ``` to understand this you need to break it down into two parts because there are two things being used in here- 1)\(\color{purple}{using}\) 2)\(\color{blue}{namespace}\)
listening* :))))
this i didn't know ...very informative
\(\large\color{blue}{namespace}\) Namespace helps you differentiate two things that might have the same name. Kind of how the last name helps to differentiate two people having the same name you can declare different namespaces in your program as I've done below- ``` #include<iostream> using namespace std; namespace first { int x = 1; } namespace second { int x = 2; } //Both namespaces have a variable named x int main(){ //cout << x ----> this would be wrong because the reference to x is ambiguous //how to refer to the x in namespace first? //use first:: cout << "the x in namespace first is: " << first::x << endl; //similarly cout << "the x in namespace second is: " << second::x; } ``` output: ``` the x in namespace first is: 1 the x in namespace second is: 2 ```
ok 2 small questions... first is, if i'm working with a program that calculates the user's pay and under int main() like this... ``` // This program calculates the user's pay. #include <iostream> using namespace std; int main () { //What is the programmer doing down here?? is he declaring the variables? Double hours, rate, pay; // Get the number of hours worked. cout << "How many hours did you work?; cin >> hours; etc
the other question is that i've seen programmer use the #include #include commands but they don't include the #include<iostream> command in their program ???
\(\large\color{purple}{using}\) everything in the iostream header file is in some namespace 'std' but like in the above example you don't do `std::cin >> ` or `std::cout <<` you just directly do `cin >>` and `cout << ` That's because you've typed `using namespace std` in your program whenever you do `using namespace std` you no longer have to deal with putting `std::` before everything which from inside the namespace std ``` #include<iostream> //in this example we won't be typing using namespace std // using namespace std; namespace first { int x = 1; } namespace second { int x = 2; } //Both namespaces have a variable named x //we're using namespace first using namespace first; int main(){ //how to refer to the x in namespace first? std::cout << "the x in namespace first is: " << x << std::endl; //how to refer to the x in namespace second? //you'd have to use second:: std::cout << "the x in namespace second is: " << second::x; } ``` Problem with `using namespace` : you can't type `using namespace first` and `using namespace second` together because then if you do `cout << x` the reference to x would be ambiguous.
\(\color{#0cbb34}{\text{Originally Posted by}}\) @Ballery1 ok 2 small questions... first is, if i'm working with a program that calculates the user's pay and under int main() like this... ``` // This program calculates the user's pay. #include <iostream> using namespace std; int main () { //What is the programmer doing down here?? is he declaring the variables? Double hours, rate, pay; // Get the number of hours worked. cout << "How many hours did you work?; cin >> hours; etc ``` \(\color{#0cbb34}{\text{End of Quote}}\) it should be *double yes, he is declaring some variables of the data type double```
i see now.. what about the second thing.. why do programmers just use #include #include and move on with the rest of the code rather than do #include<iostream>??
you sure it was `#include #include`? because that would throw an error whenever you do `#include` you need to give a filename after it like `#include<iostream>`
``` #include #include int main () etc
i wish i could find an example rn. i'm drowning with papers over here .
\(\color{#0cbb34}{\text{Originally Posted by}}\) @Ballery1 ``` #include #include int main () etc ``` \(\color{#0cbb34}{\text{End of Quote}}\) output: ``` prog.cpp:1:10: error: #include expects "FILENAME" or <FILENAME> prog.cpp:2:9: error: #include expects "FILENAME" or <FILENAME> ```
they probably forgot what to include and left it blank?
sorry i didn't give you the whole thing but that's the gist of it... i'll let you know when i find it.
aight
no it was a proper thing program
omg i forgot about the most important question......oof how do you store an input into the memory?? so for instance i'm working on a account balance and in my pseudocode, i wrote how much credit do they have? then they'd type in their credit. then the program would save that info in the memory and then ask them how much do they wanna take it out of their account and store that info and in the end, the program would just subtract their current balance with the amount they took out and show the remainder on the screen. so how do you store an input into the memory again ?
again, i saw these examples somewhere in the textbook but sadly can't find them rn :/
declare some variables and store the values in them
that's what variables are for, to store values
can you please show a tiny exaple ? :))))))))))))))))))))))))))))))))))))))))))
cuz i'm having trouble with the fact that do i need to declare some digit to store that info or what ??? like how we add numbers and stuf
``` #include<iostream> using namespace std; int main(){ double credit, withdraw; cout << "Enter your current credit: "; cin >> credit; cout << endl; cout<< "Enter the withdraw amount: "; cin >> withdraw; cout << endl; //update the credit credit = credit - withdraw; cout << "Your new credit: " << credit << endl; } ```
you can use the normal +, -, /, * operators to do math operations
so anything under cout and cin is automatically being stored into the memory?
not exactly
so inside those pointy brackets then??? << and >>
cout is only used to output something you use cout whenever you want to display something on the screen cin is used when you want to take in some input from the user whenever you take in input you have to use a variable to store it in
ok so how do i know what's being stored into the memory tho ??? :/ so when i write my own code i'd know that variable a + b = c will be stored into the memory :/
i want the value of c stored into the memory.
just show me one tiny example that's all
you want c to be a+b there a difference between c = a + b and a + b = c the thing on the left-hand side is supposed to represent the place where you store the value of the expression on the right-hand side the correct way will be c = a + b Declare three variables a,b, c every variable represents some location in your memory all the variables are stored in the memory somewhere when you do cout << a it will access the location in memory which has belongs to the variable a and print the value inside it after declaring the variables you need to take input from the user and store it in them cin >> a >> b ^took input from the user and stored it in a and b so the memory locations associated with a and b now have some values which were provided by the user c = a+b it computes a + b and stores that value in the memory location associated with the variable c
ok this makes sense. again, thank you soooooooooo much for you help. really appreciated <3 :))))))))))))))))))))))
np
Join our real-time social learning platform and learn together with your friends!