Computer Science
Ballery1:

need help to identify all the key components of a small program in cpp

Ballery1:

Ballery1:

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 :)

imqwerty:

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>

Ballery1:

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 ?

imqwerty:

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

Ballery1:

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;

Ballery1:

btw is keywords and primitive data types both are the same thing correct?

imqwerty:

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

Ballery1:

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

imqwerty:

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

Ballery1:

omg that clears so many things lol thanks

Ballery1:

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. ;))))))))))))

imqwerty:

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.

Ballery1:

i'm going to print out this thread and stick it on my forehead... really informative and helpful :)))))))))))))))))))))))

imqwerty:

 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}$$

Ballery1:

listening* :))))

Ballery1:

this i didn't know ...very informative

imqwerty:

$$\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 

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

Ballery1:

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 ???

imqwerty:

$$\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.

imqwerty:

$$\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

Ballery1:

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>??

imqwerty:

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>

Ballery1:

 #include #include int main () etc

Ballery1:

i wish i could find an example rn. i'm drowning with papers over here .

imqwerty:

$$\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> 

imqwerty:

they probably forgot what to include and left it blank?

Ballery1:

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.

imqwerty:

aight

Ballery1:

no it was a proper thing program

Ballery1:

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 ?

Ballery1:

again, i saw these examples somewhere in the textbook but sadly can't find them rn :/

imqwerty:

declare some variables and store the values in them

imqwerty:

that's what variables are for, to store values

Ballery1:

can you please show a tiny exaple ? :))))))))))))))))))))))))))))))))))))))))))

Ballery1:

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

imqwerty:

 #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; } `

imqwerty:

you can use the normal +, -, /, * operators to do math operations

Ballery1:

so anything under cout and cin is automatically being stored into the memory?

imqwerty:

not exactly

Ballery1:

so inside those pointy brackets then??? << and >>

imqwerty:

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

Ballery1:

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 :/

Ballery1:

i want the value of c stored into the memory.

Ballery1:

just show me one tiny example that's all

imqwerty:

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

Ballery1:

ok this makes sense. again, thank you soooooooooo much for you help. really appreciated <3 :))))))))))))))))))))))

imqwerty:

np