Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 A Start on C++ Some First Programs/Questions
Index -> Programming, C++ -> C++ Help
Goto page 1, 2, 3, 4, 5  Next
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
[Gandalf]




PostPosted: Sun Jun 26, 2005 4:30 pm   Post subject: A Start on C++ Some First Programs/Questions

Well, I have decided that my lofty goal for the summer is to learn C/C++ and VB.NET. Well, I'm hoping to get at least a basic knowledge of one of them, so I'm starting with C++ (I've already tried C). I will post some of the programs I make and questions I have over here till I give up (which will hopefully not happen). If I can't do C++ then I'll fall back to C, and if not that then I'll fall back to either VB.NET or Ruby.

Well, alright, I think I have an alright understanding of cout and cin, and now I'm going into classes and objects. Then I will go to constructors/deconstructors, multiple objects of the same class, private members and friend functions, inheritance, virtual funtions, and pure virtual functions, then file objects and by then we'll see... If there are any tutorials you suggest on this site or on the internet then please suggest them since I need all the help I can get!

I was looking at inline assembly, and heck - it doesn't seem too hard. Once you get past that barrier then inline machine code doesn't seem all that bad either. But then again, that's just me and my insane first impressions.

Ok, I'll post some of my code when I reinstall my editor/compiler, but first I have a simple newb question.

Can you declare the value of a variable inside a class in C++? I see how it's not neccessary, but I'm just not sure if its possible.

Oh, and if you know of any good reference guides to C/C++ then I would be really happy too Razz .

*EDIT* Oh, and is it wise to use some C libraries while coding in C++? Or should you just use pure C++? Are some C libraries also considered C++ ones?


*NOTE TO SELF*
g++ name.cpp -o name.exe
gcc name.c -o name.exe
gcj name.java -o name.exe?
Sponsor
Sponsor
Sponsor
sponsor
Cinjection




PostPosted: Sun Jun 26, 2005 4:40 pm   Post subject: Re: A Start on C++ Some First Programs/Questions

[Gandalf] wrote:

Can you declare the value of a variable inside a class in C++? I see how it's not neccessary, but I'm just not sure if its possible.

no, but that's what constructers are for.
[Gandalf] wrote:

Oh, and if you know of any good reference guides to C/C++ then I would be really happy too Razz .

www.cprogramming.com
[Gandalf] wrote:

*EDIT* Oh, and is it wise to use some C libraries while coding in C++? Or should you just use pure C++? Are some C libraries also considered C++ ones?

You can, but you can turn C libs into C++ libs by adding a c infornt and getting rid of the .h. So
code:

#include <time.h> //c style
//becomes
#include <ctime> //c++ style

That won't always work but in most cases it will. Need more help , just ask.
[Gandalf]




PostPosted: Sun Jun 26, 2005 4:58 pm   Post subject: (No subject)

Thanks.
What compiler/editor should I use? Embarassed

I had a good one for C, but it doesn't work with C++. Do you know of a good, easy to use editor with a built in compiler? Free, might I add.

*EDIT* Oh man, I forgot so much, even about C. I took a basic "hello world" program just to test my C and it doesn't show hello world when compiled Confused.
c:
include <stdio.h>

int main()
{
   puts("Hello world");
   return 0;
}


It just says return code, execution time, and location of program.
1of42




PostPosted: Sun Jun 26, 2005 5:18 pm   Post subject: (No subject)

http://www.bloodshed.net/devcpp.html

Dev-C++ is a great IDE.
Cinjection




PostPosted: Sun Jun 26, 2005 5:19 pm   Post subject: (No subject)

I use MVC++. But you have to pay to get that one. If you want a good free compiler you can use Dev C++(googleafy)(<--crazy me making new words).
Cinjection




PostPosted: Sun Jun 26, 2005 5:22 pm   Post subject: (No subject)

Oh and here's Hello World in C++:
code:

#include <iostream>
using namespace std;

int main(){

cout<<"Hello World!"<<endl;

fflush(stdin); //flushes the input buffer
cin.get(); //pauses program
return 0;

}
[Gandalf]




PostPosted: Sun Jun 26, 2005 5:45 pm   Post subject: (No subject)

Thanks!

Oh, and for Hello world, couldn't you just use this:

c++:
#include <iostream.h>
int main()
{
  cout<<"Hello World";
  return 0;
}


or wtd's way:
c++:
#include <iostream>

int main()
{
   std::cout << "Hello world" << std::endl;
   return 0;
}


I'm installing it right now.
Cinjection




PostPosted: Sun Jun 26, 2005 5:49 pm   Post subject: (No subject)

Well ANSI standard says not to include the .h. You also need cin.get() to pause the program or else it will instently close. fflush() was thrown in there for good practice.
Sponsor
Sponsor
Sponsor
sponsor
[Gandalf]




PostPosted: Sun Jun 26, 2005 6:08 pm   Post subject: (No subject)

I see, thanks for the explanation.

Tell me what the problem is with this:
c++:
#include <iostream>

// Getting user input

main()
{
    float value;

    cout << "Input a number: ";
    cin >> value;
    cout << "The value is: " << value << "\n";
    return 0;

}

It says that cout and cin have not been declared Sad.

*EDIT* I found the problem, you DO have to have the .h after iostream, at least in my case.
wtd




PostPosted: Sun Jun 26, 2005 6:13 pm   Post subject: (No subject)

Cinjection wrote:
Well ANSI standard says not to include the .h. You also need cin.get() to pause the program or else it will instently close. fflush() was thrown in there for good practice.


"fflush" and "stdin" are C, not C++, and is therefore extremely bad practice. Especially when you mix the two. They are different languages. Treat them as such.

Instead use the std::ostream class' "flush" member function. std::cout is an instance of the std::ostream class.

code:
std::cout.flush();


Flushing a buffer ensures that what is in it is actually output. For instance, on some systems with some compilers, nothing will be output until the end of the line, so if I were to ask for a person's name and get input on the same line, say with:

code:
std::string name;

std::cout << "Enter your name: ";
std::cin >> name;
std::cout << "Hello, " << name << "!" << std::endl;


What I would see on the screen would look like the following, assuming the name I entered was "wtd".

code:
wtd
Enter your name: Hello, wtd!


Flushing the std::cout buffer after prompting for the name ensures this won't happen.

code:
std::string name;

std::cout << "Enter your name: ";
std::cout.flush();
std::cin >> name;
std::cout << "Hello, " << name << "!" << std::endl;
md




PostPosted: Sun Jun 26, 2005 6:33 pm   Post subject: (No subject)

[Gandalf] wrote:
I see, thanks for the explanation.

Tell me what the problem is with this:
c++:
#include <iostream>

// Getting user input

main()
{
    float value;

    cout << "Input a number: ";
    cin >> value;
    cout << "The value is: " << value << "\n";
    return 0;

}

It says that cout and cin have not been declared Sad.

*EDIT* I found the problem, you DO have to have the .h after iostream, at least in my case.


I think if you add the .h the file puts everything into the global namespace; whereas without the .h it's just in "std::". Of course I could be completely wrong Razz

What IDE/compiler did you chose?
wtd




PostPosted: Sun Jun 26, 2005 6:35 pm   Post subject: (No subject)

You should not by any means install Dev-C++. It's a buggy product, and makes the whole process far more complex than it need be.

Instead, install GCC directly via MinGW (MInimalist GNU for Windows).

You will then need to edit your PATH environment variable to have command-line access to the GNU C++ compiler.

http://vlaurie.com/computers2/Articles/environment.htm
[Gandalf]




PostPosted: Sun Jun 26, 2005 6:39 pm   Post subject: (No subject)

Dang, and it was a 50mb install.

Well, for the moment what's wrong with this? It doesn't output the answer.
c++:
#include <iostream.h>

// Getting user input

main()
{
    int x1, y1, x2, y2;  // A value the user inputs
    int slope;

    cout << "Input the first x coordinate: ";
    cin >> x1;
    cout << "Input the first y coordinate: ";
    cin >> y1;
    cout << "Input the second x coordinate: ";
    cin >> x2;
    cout << "Input the second y coordinate: ";
    cin >> y2;
    slope = (y2 - y1) / (x2 - x1);
    cout << "The slope is: " << slope << ".\n";
    cin.get();
}


Oh, and this is the Hello World program Dev-C++ gives:
code:
#include <iostream>

using namespace std;

int main (int argc, char *argv[])
{
  cout << "Hello World!" << endl;
  cout << "Press ENTER to continue..." << endl;
  cin.get();
  return 0;
}


Right now I'm using Dev-C++, but I will probably switch to what wtd said, if I can get it to work.
wtd




PostPosted: Sun Jun 26, 2005 7:07 pm   Post subject: (No subject)

[Gandalf] wrote:
Dang, and it was a 50mb install.


It's a good lesson to learn early.

[Gandalf] wrote:
Well, for the moment what's wrong with this?


Several things. Let's take a look. Smile

c++:
#include <iostream.h>


"iostream.h" is deprecated. Whgile most compilers still do support it, there's no guarantee they will.

Use "iostream" instead.

c++:
// Getting user input


Useless comment.

c++:
main()


"main" should return "int".

c++:
{
    int x1, y1, x2, y2;  // A value the user inputs
    int slope;


You're mixing styles and using extraneous comments. I can gather from the following code that the user inputs values which get stored in those variables.

If you're going to declare more than one variable in one statement, there's no point in putting the declaration of slope in its own statement. You can still have it on a separate line.

However, you also don't need to declare slope here. You can declare it when you initialize it. C++ does not require declarations at the top of a function.

c++:
    cout << "Input the first x coordinate: ";
    cin >> x1;
    cout << "Input the first y coordinate: ";
    cin >> y1;
    cout << "Input the second x coordinate: ";
    cin >> x2;
    cout << "Input the second y coordinate: ";
    cin >> y2;


Nowhere have you specified that cout or cin have been brought into your namespace, from the std namespace where they dwell.

c++:
    std::cout << "Input the first x coordinate: ";
    std::cin >> x1;
    std::cout << "Input the first y coordinate: ";
    std::cin >> y1;
    std::cout << "Input the second x coordinate: ";
    std::cin >> x2;
    std::cout << "Input the second y coordinate: ";
    std::cin >> y2;


Also, that aside, nowhere do you check to make sure reasonable values were stored in the variables.

c++:
    slope = (y2 - y1) / (x2 - x1);


Hey look! We're finally putting a meaningful value in slope.

Instead of simple assignment here, let's declare slope here too.

c++:
    int slope = (y2 - y1) / (x2 - x1);


Now, let's use initialization syntax instead of assignment. Good practice to get into. You can only do this at declaration, though.

c++:
    int slope((y2 - y1) / (x2 - x1));


But there's still a likely problem. In C++, when you divide an int by an int, you get an int. You likely want to be able to get a floating point number, to represent slopes like 1/2.

To do this our slope should have a type of double, and we'll have to cast at least one of the argument to double as well.

c++:
    double slope(static_cast<double>(y2 - y1) / (x2 - x1));


c++:
     cout << "The slope is: " << slope << ".\n";
    cin.get();


Again, specify that cout and cin live in the std namespace.

Also, when you want a newline, do something like the following instead.

c++:
     std::cout << "The slope is: " << slope << "." << std::endl;
    std::cin.get();


And that's all for now. Smile
1of42




PostPosted: Sun Jun 26, 2005 7:12 pm   Post subject: (No subject)

For starters, I would personally ignore wtd. His idea of easy is to have to use the command line to get to the directory containing your files, and to compile everything manually. While less obfuscated, it is in fact not easier. Personally, I think you're better off with an IDE - especially one like Dev-C++, where you can ignore projects and other crap until you're comfortable with the language itself. Just press F9 and you're set.

On to your question:

c++:
#include <iostream.h>

That's bad. Remove the .h to make:
c++:
#include <iostream>

C++ header files do not require the .h at the end.

c++:
main()

Should be:
c++:
int main()

Your program must return a status to the operating system at the end of execution. So add this at the end:
c++:
return 0;

This indicates successful completion

Now you must use your identifiers. Either:

c++:
using namespace std;

or
c++:
using std::cout;
using std::cin;

or qualifying all your identifiers, like so:
c++:

    std::cout << "Input the first x coordinate: ";
    std::cin >> x1;
    std::cout << "Input the first y coordinate: ";
    std::cin >> y1;
    std::cout << "Input the second x coordinate: ";
    std::cin >> x2;
    std::cout << "Input the second y coordinate: ";
    vcin >> y2;
    slope = (y2 - y1) / (x2 - x1);
    std::cout << "The slope is: " << slope << ".\n";
    std::cin.get();


Also, make slope a double so you get decimals:
c++:
double slope;
slope = static_cast<double>(y2 - y1) / (x2 - x1);


Finally, after all that is corrected:

Create a new variables and cin to it:

c++:
int pauseVar;
cin >> pauseVar;


This will pause it.

*edit* Damn, wtd got to it first Wink

Just to note, wtd, that still doesn't solve the problem of the program finishing execution too fast to see, nor does it return a value to the OS, making the program fail compilation. Wink
Display posts from previous:   
   Index -> Programming, C++ -> C++ Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 5  [ 64 Posts ]
Goto page 1, 2, 3, 4, 5  Next
Jump to:   


Style:  
Search: