naming variables

i have to name some variables, in the exercise they give me the type of variable and i have to name it, this are my answers.

a) int a, b; int a; int b;
b) float x, y, z;
c) char a, b, c;
d) long accountant;
e) string hello;
f) int f (10); int f = 10;
g) float g (2.25); float g = 2.25;
h) char op ('S')  ;       char op = 'S'  ;    

in the exercises a, f ,g , h i found that it could be done in 2 ways so i wrote both ways.

exercise h this is the info i was given:
character variable initialized in S: op

please let me know if i made mistakes and the reason.
thanks in advance


    
Last edited on
It's not clear what you mean. There's no restriction on what name can be used with certain types. Any valid name can be used with any type
In practice, you should name the variables according to their meaning, not their type.

Edit: clean-up.
Last edited on
these were the problems i was given:

give the appropriate declaration for each group of variables

a) integer variable: p, q
b)floating variable: x, y, z
c)character variable: a, b, c
d)long integer variable: accountant
e)character string variable: hello
f) integer variable initialized in 10
g) initialized floating variable initialized in 2.25
h) character variable initialized in S: op

my question is if the syntax of the answers i wrote was right ?
thanks again
what the hell is this exercise? lol what a completely waste of time...

You just need to try to compile, and check if it works...

So far it's seems all right to me.
thanks i also think it's a waste of time and i don't know yet how to compile , i will learn that next week.
Last edited on
O_o
You know that green button that is shaped like a triangle? Press it. Compiled.

Uh, if you haven't gotten to that yet then they probably haven't shown you how to set up main() yet either. If it takes a full week before they show you that then I say fire whoever you're learning from and look up tutorials online. Quicker, funner, easier.

And don't tell me funner ain't a word, cause then I know you're learning from the wrong people :p
Last edited on
haha i don't know that button , actually i don't have yet a compiler, i don't know yet how to set up main()
haha i don't know that button , actually i don't have yet a compiler, i don't know yet how to set up main()


main() is a special function, which will be your program's start. It is required, and apart from it, you can have how many other functions you want, named however you want (of course, there are some rules that are the same as for naming variables.)

There is a site you can use to play around with C++, but it won't replace a true compiler that can compile your source code into real executable exe's.

http://ideone.com/EOuBM
actually i don't have yet a compiler

if your a student with a registered institution and a valid student id number you can get visual studio 2010 from microsoft dreamspark

try this link
https://www.dreamspark.com/Product/Product.aspx?productid=4
one week to explain variable declaration. One week to explain what is a compiler, one week to explain main () function, one week to explain iostream,... final exam: "Hello World!"

if your a student with a registered institution and a valid student id number you can get visual studio 2010 from microsoft dreamspark


visual studio, to learn how to declare variables... not likely! Try devc++, great for starters!

Come on, there are plenty of great non microsoft options...
Last edited on
thanks for all the answers, i prefer programs that are open source , i don't really like micro$oft.
haha i will replace the hello world with game over in that test and yes you are right the course i'm taking is very slow.
starting today i will learn on my own even though i will continue the course just for the certificate i will receive.

i have one last question, what happens if in the main function the return statement is not written ?

thanks again for helping me out :)
If you don't like proprietary software you can use the MinGW compiler (Windows port of the GCC compiler).

You can also use Code::Blocks which is an open source IDE that use MinGW by default. I mention Code::Block here because it is very popular but there are many great open source IDE's out there so use whatever you like.

I don't recommend Dev-C++ because it's very old and comes with an outdated version of MinGW.
Last edited on
what the hell is this exercise? lol what a completely waste of time...

Every book I own on C++ and my degree did that. Gave you a list of variable types to declare to show you understand how to declare them. Even the Teach Yourself series for C++ in all its editions focus on data types for a whole chapter that is supposed to take you a full day to read.

one week to explain variable declaration. One week to explain what is a compiler, one week to explain main () function, one week to explain iostream,... final exam: "Hello World!"

For me, the Intro course, it was:
Week 1 Data types/Variables
Week 2 (day one set up compiler) learn Standard I/O and do Hello World app
Week 3 Functions
Week 4 Arrays
Week 5 Loops/Switch
Week 6 & 7 Just was doing applications and going over some fine points of the previous weeks
Week 8 Final Exam Make a program that made use of everything covered in Weeks 1 -7 and Answer short quiz

Advanced C++ Course was:
Week 1 Class
Week 2 Constructor/Destructor
Week 3 Inheritance
Week 4 Composition
Week 5 -7 focused entirely on Week 3 and 4 concepts
Week 8 Final exam...same as Intro course
i have one last question, what happens if in the main function the return statement is not written ?

... previously ...
actually i don't have yet a compiler, i don't know yet how to set up main()


Right-o. Well since main() is a special function, it can be excepted from having to return -- in which case the compiler silently adds a return 0; (or whatever other value signifies "all OK" for the target system).
Every book I own on C++ and my degree did that. Gave you a list of variable types to declare to show you understand how to declare them. Even the Teach Yourself series for C++ in all its editions focus on data types for a whole chapter that is supposed to take you a full day to read.

Well, one thing is a chapter of a book, another one is A WEEK in my life! Is it really useful to spend a week making a list of all possible combinations of declarations? You will forget and come back to this chapter so many times...

Anyway, I don't really think any book can ever match the tutorial of cplusplus.com! Amazing

If you don't like proprietary software you can use the MinGW compiler (Windows port of the GCC compiler).

You can also use Code::Blocks which is an open source IDE that use MinGW by default. I mention Code::Block here because it is very popular but there are many great open source IDE's out there so use whatever you like.

I don't recommend Dev-C++ because it's very old and comes with an outdated version of MinGW.

I completely agree. Actually, when I have to compile in Windows, I use code::blocks, but it's one of a great list of very capable open source IDE's. I've mentioned Dev-C++ cause it's really simple and clear; it's the easiest way to start compiling right away.

By the way, actually there's a non-oficial revival version of Dev-C++, thanks to "Orwell": http://orwellengine.blogspot.com.es/

:D
Last edited on
i just did my first program :D
got a game over instead of hello world.

@jcaselles about the compiler why dev-C++ is old ? i downloaded it and it's a beta 4.9.9
they quit the project in a beta ?
i just uninstalled and got the one orwell made that is 5.2.0 thanks for the advice even though the version i got doesn't have the MinGW it has TDM-GCC x64

@BHXspecter mine seems to be similar to your course, i'm taking C++ 1 they offer 3 and each one last a month

week 1 basic elements of C++
week 2 basic operations
week 3 conditionals
week 4 repetitions

i will keep taking the course but will also learn on my own.

@catfish thanks for helping me with that answer

thanks to everyone for all the answers :)
Last edited on
Topic archived. No new replies allowed.