I just started writing code.

Sep 20, 2019 at 8:23am
Write your question here.
I just started writing code. Not sure where to expect help from everyone.Tks.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
  #include <cstdlib> 
 #include <iostream> 
   
 using namespace std; 
   
 int main(int argc, char *argv[]) 
 { 
     int a; 
     int b; 
     float c; 
     a=8; 
     b=16; 
     c=15.8; 
     cout<<"a="<<a<<"\t"<<"b="<<b<<"\t"<<"c="<c; 
     cout<<"\n" 
         
     return 0; 
 } 
Sep 20, 2019 at 8:29am
Im happy you start writing code. Allow me to show you my coding style. It's easier to find typing errors this way.

1
2
3
4
5
6
7
8
9
10
11
12
13
 #include <iostream>

 using namespace std;

 int main(int argc, char *argv[]) {
     int a = 8;
     int b = 16;
     double c = 15.8;

     cout << "a=" << a << "\t" << "b=" << b << "\t" << "c=" << c << "\n";

     return 0;
 }
Sep 20, 2019 at 12:24pm
Hello dotkickpra,

There are many different styles for writing code and now is a good time to choose one and stay with it. This will show you the different styles: https://en.wikipedia.org/wiki/Indentation_style

Personally I prefer the Allman style as you have used. Remember this point; the compiler does not care about white space, be it spaces, tabs or blank lines. The person, including yourself, reading the code does care about white space be it indentation or blank lines. Make your code as essay to read as possible.

I have made some changes to your code that should help:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
//#include <cstdlib> // <--- Not needed here for this program. And may be included through "iostream" on some compiler header files.
#include <iostream> 

//using namespace std;  // <--- Best not to use.
// A recent post that is worth reading. http://www.cplusplus.com/forum/beginner/258335/


int main() // <--- "argc" and "argv" are not used in the program. You do not need to code for it unless you intend to use it.
{
	int num1; // <--- Could be written as int num1{ 8 }; The same for the other two variables.
	int num2{};  //<--- From C++11 on.
	double floatingPointNum{};

	num1 = 8;
	num2 = 16;
	floatingPointNum = 15.8;

	std::cout << "num1 =" << num1 << "\t" << "num2 =" << num2 << "\t" << "floatingPointNum =" << floatingPointNum << '\n' << std::endl;
	//cout << "\n"

	return 0; // <--- Not required to use, but makes for a good break point when debugging.
}

The comments should explain most of what I did, but if not let me know.

When it comes to including the header files I have found that putting them in alphabetical order helps. Especially when you have missed one. Most often I see "cstdlib" included in a program when you use the "srand" and "rand" functions from C. Including this header file does not hurt the program unless you do not need it then it is just extra code that is never used.

I use VS (Visual Studio) as my IDE and the way the header files are set up "iostream" will eventually include "cstdlib" along with some other "c???" header files. Other compilers and header may not do this.

Now is a good time to read about name space and move away from using using namespace std;. This may be helpful right now, but in the future it WILL work against you when your programs get more complicated. Also now is a good time to learn to qualify what is in the standard name space and grow with it slowly instead of having to do it all at once later. Once you get use to the compiler error messages they will help you.

This may be early, but these links are worth reading:
http://www.cplusplus.com/doc/tutorial/namespaces/
https://www.learncpp.com/ main index.
https://www.learncpp.com/cpp-tutorial/4-3b-namespaces/ from the index it is in section/chapter "S".

I have found the "LearnCpp.com" to be a very good tutorial especially when explaining everything and with the examples they give.

It is a good time to start giving your variables a more proper name and stay away from single letter variable names. As I have read here in the past a variable should be a noun and I add that describes what it is or does. What I have put in the code above is an example of what you could do. In the end it is you choice. The use of a single letter variable name has one exception I can think of: for (int i = 0; I < something; i++). In this case the variable is local to the for loop and the letters "i", "j" and "k" are most often used. On occasion I have use "row" and "col" in a for loop.

When your programs get larger it makes it easier to keep track of your variables and easier to follow.

A "float" may work, but a "double" is the more preferred floating point type.

When you define a variable you have the option/ability to give it a starting value. The comment in the above shows this. Also from C++11 on the use of empty {}s will initialize your variables, so they do not have a garbage value.

When you define a variable int num; this only reserves space on the stack for the variable. Whatever was left in that bit of memory is considered garbage, so if you try to use the variable to soon the compiler may complain or you could be working with in initial number like "-858993460" for an int and "-9.2559631349317831e+64" for a double. Generally this is what my computer comes up with, but I have seen different numbers at times. In the end it is a good to initialize your variables when they are defined. If for no other reason to know that they do not have a garbage value.

Some things that do not need to be initialized: "std::string" and other containers like vectors and lists. These are empty when defined, but you still have the option to give them a value if you need to.

I changed the "cout" statement, mostly adding the "\n" and "std::endl" at the end of the line. Adding the "\n" eliminates the need for the second "cout" statement and I like to use the "std::endl" on the last output line to flush the output buffer and make sure everything has printed. The "std::endl" does come with some overhead. This may not be noticed in a small program, but in a larger program with many "std::endl"s you may notice it taking longer to process everything.

Back to variable names. It is a general rule, although not written, more of a guideline, that regular variable names should start with a lower case letter. Classes and structs would start with a capital letter, and sometimes I add functions to this, my personal choice. And anything defined with "constexpr" or "const" is all capital letters.

For the variable names camel case (floatingPointNum) is what I see most often and works well. There is also (floating_point_num). Either method works. You should pick one and stay with it. Although it is acceptable to start or end your variable names with "_", it is best to avoid doing this as it could conflict with a header file.

Anything yo do not understand let me know.

Hope this helps,

Andy
Topic archived. No new replies allowed.