🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

C++ Workshop - C++ Keywords, Variables, & Constants (Ch. 3)

Started by
65 comments, last by Dbproguy 16 years, 2 months ago
@Furny, Thanks a lot[smile]. I will check out the links and post my doubts later[grin].

@Sr_Guapo, Thanks for the reply[smile]. I haven't used much opertaors and in exams we are asked to do without it. We are only thought loops, loops and more loops[evil] nothing more.

[Edited by - kimi on June 18, 2006 4:59:02 AM]
Advertisement
can i still be in this i just got back from vaction and noticed thats
this think has already been on for 3 weeks
Quote: Original post by londonman
can i still be in this i just got back from vaction and noticed thats
this think has already been on for 3 weeks


Yes, of course. See here for details.
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
Some questions about void and calling functions :

- You explained that using Void main() is incorrect, not standard Cpp and I understand it, I will be using int main() return 0; . Still, using the suggested IDE Microsoft Visual Cpp 2005 Express Edition, void main() works.
Quote:
Some compilers that were written before Cpp got standardized accepted void main(). Outdated tutorials, or tutorials written by people with incompled or outdated knowledge may use void main() even though it is wrong. Same with books.
Does that means Visual 2005 is outdated ?

- Using void for another function will also work. I havent seen Void mentioned anywhere for other functions. Basically, should I never use void ? Is this code correct or should I remove void and use int test() return 0;.

#include <iostream>using namespace std;void test(){	cout<<"Im in Test" <<endl;}int main(){		cout<<"Im in Main" << endl;	test();	return 0;}


- Also, when calling functions, I have noticed that the function you calls has to be written before the one calling. As in ;

#include <iostream>using namespace std;int test(){	cout<<"Im in Test" <<endl;        return 0;}int main(){		cout<<"Im in Main" << endl;	test();	return 0;}


This will compile and execute fine, if I write main() before test() the complier wont know what test() is. With a code as follow :

#include <iostream>using namespace std;int main(){		cout<<"Im in Main" << endl;	test();	return 0;}int test(){	cout<<"Im in Test" <<endl;        return 0;}


Now, when your source code only has two functions it doesnt really matter. If your code had 100 functions... That means you wouldnt be able to go from a function at the beginning of your code to one near the end. Jumping from functions to another freely seems usefull to me.

Thank you for your time.
Quote: Original post by Myotis
Some questions about void and calling functions :

- You explained that using Void main() is incorrect, not standard Cpp and I understand it, I will be using int main() return 0; . Still, using the suggested IDE Microsoft Visual Cpp 2005 Express Edition, void main() works.
Quote:
Some compilers that were written before Cpp got standardized accepted void main(). Outdated tutorials, or tutorials written by people with incompled or outdated knowledge may use void main() even though it is wrong. Same with books.
Does that means Visual 2005 is outdated ?


It's probably supported for legacy reasons: so that programs written before the standard was set will still work.

Quote:

- Using void for another function will also work. I havent seen Void mentioned anywhere for other functions. Basically, should I never use void ? Is this code correct or should I remove void and use int test() return 0;.

*** Source Snippet Removed ***



Void simply means that the function does not return a value. It's perfectly OK to use void for functions other than main. Main should always return an int.

Quote:

- Also, when calling functions, I have noticed that the function you calls has to be written before the one calling. As in ;

*** Source Snippet Removed ***

This will compile and execute fine, if I write main() before test() the complier wont know what test() is. With a code as follow :

*** Source Snippet Removed ***

Now, when your source code only has two functions it doesnt really matter. If your code had 100 functions... That means you wouldnt be able to go from a function at the beginning of your code to one near the end. Jumping from functions to another freely seems usefull to me.


In Cpp, a function has to be declared before you refer to it.
You can do this either by defining the function before it is used, as you did above, or by putting a function prototype at the top of the source file.

E.g.


#include

void test(); // function prototype

int main()
{
std::cout

Hope that helped!
Sorry, that last section came out wrong. It should read:

#include <iostream>void test(); // function prototypeint main(){    std::cout<<"I'm in main!";    test();    std::cout<<"Back in main.";    return 0;}void test(){    std::cout<<"Currently in test()";}

// Demonstration of variables
#include <iostream.h>

int main()
{
unsigned short in Width = 5, Length;
Length = 10;

// create an unsigned short and initialize with result
// of multifplying Width by Length
unsigned short in Area = Width * Length;

cout << "Width:" << Width << "\n";
cout << "Length: " << Length << endl;
cout << "Area: " << Area << endl;
return 0;
}


What am I doing wrong here??? I'm getting compiling errors..

And how do I compile it in vc++ee once I get it typed out?? Sorry I'm a huge nub.. this is my first time with any programming
Quote: Original post by Xzanther
// Demonstration of variables
#include <iostream.h>

int main()
{
unsigned short in Width = 5, Length;
Length = 10;

// create an unsigned short and initialize with result
// of multifplying Width by Length
unsigned short in Area = Width * Length;

cout << "Width:" << Width << "\n";
cout << "Length: " << Length << endl;
cout << "Area: " << Area << endl;
return 0;
}


What am I doing wrong here??? I'm getting compiling errors..

And how do I compile it in vc++ee once I get it typed out?? Sorry I'm a huge nub.. this is my first time with any programming


In future, post the compiler errors. We can help show you how to read them.

From our code it looks like you misstyped int as "in". Twice.
Quote: Original post by Xzanther
What am I doing wrong here??? I'm getting compiling errors..


// Demonstration of variables#include <iostream.h><iostream>int main(){	unsigned short in Width = 5, Length;	Length = 10;	// create an unsigned short and initialize with result	   // of multifplying Width by Length	unsigned short in Area = Width * Length;	std::cout << "Width:" << Width << "\n";	std::cout << "Length: " << Length << endl;	std::cout << "Area: " << Area << std::endl;	return 0;}


Quote: And how do I compile it in vc++ee once I get it typed out?? Sorry I'm a huge nub.. this is my first time with any programming


See here
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
See.. that is the problem.. I'm using 2nd edition of the book because it's free... that is what it is there... :( I don't have the 5th edition.. thx though fruny..

This topic is closed to new replies.

Advertisement