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 Previous  1, 2, 3, 4, 5  Next
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
md




PostPosted: Thu Jul 07, 2005 9:37 am   Post subject: (No subject)

One of the most annoying bugs I've ever run across was because some bright light as MS decided that instead of making teh GetObject function a real function (it's part of the GDI) they'd make it a marco to the function apropriate to if you were using a wide characters or not. So when I wrote my own GetObject function as part of one of my classes which used something in windows.h, which pulled in the GDI (bah!) it broke because of the stupid redefinition of some moron. So yeah... preprocessor macros == bad.
Sponsor
Sponsor
Sponsor
sponsor
[Gandalf]




PostPosted: Fri Jul 08, 2005 10:26 pm   Post subject: (No subject)

Ok, for now I'll hold of on them.

I'm confused as to why some books start out with just a simple int main(); function, and why some do something weird (to me) like:
code:
int main(int argc, char *argv[])


Also, I read that you can simple put the function above the main() one, and you don't have to declare it, like so:
c++:
#include <iostream>

void greet()
{
   std::cout << "Hello world" << std::endl;
}

int main()
{
   greet();
   return 0;
}

I haven't been able to try compiling it (lack of access) but if it does work then which method is better?

*edit*
Oh, and I remember wtd mentioning that arrays are bad and one reason was because you can't tell the upper bounds (I think that was it). Can you just have a dynamic array and check that variable for the size?
wtd




PostPosted: Fri Jul 08, 2005 10:48 pm   Post subject: (No subject)

[Gandalf] wrote:
Ok, for now I'll hold of on them.

I'm confused as to why some books start out with just a simple int main(); function, and why some do something weird (to me) like:
code:
int main(int argc, char *argv[])


You can pass arguments to a program when you start it. You've most likely already seen this. You run "g++ my_program.cpp" to compile your program. In this case "my_program.cpp" is an argument to the program.

How do you get at these arguments? Well, they get passed as arguments to the main function, as an array of character arrays (C "strings"). Since C and C++ arrays don't keep track of their size, it also passes in the size of the array as "argc".

[Gandalf] wrote:
Also, I read that you can simple put the function above the main() one, and you don't have to declare it, like so:
c++:
#include <iostream>

void greet()
{
   std::cout << "Hello world" << std::endl;
}

int main()
{
   greet();
   return 0;
}

I haven't been able to try compiling it (lack of access) but if it does work then which method is better?


It will work, and for programs of this size, it's largely a question of style. You see, the compiler needs two pieces of information about a function before another function can call it. It needs to know what type of data the function takes as arguments, and it needs to know what type of data the function returns.

With this information it can check to make sure types are being used appropriately. Using a forward declaration along the lines of:

c++:
void greet();

int main()
{
   greet();
}


Gives the compiler that information.

[Gandalf] wrote:
*edit*
Oh, and I remember wtd mentioning that arrays are bad and one reason was because you can't tell the upper bounds (I think that was it). Can you just have a dynamic array and check that variable for the size?


The best solution is to use the std::vector class. The std::vector class is a templated one. That means there's no actual executable code, but rather a "pattern" of sorts. Based on the template parameters, the compiler then generates the appropriate code at compile-time.

This means there doesn't have to be a std::int_vector class and a std::double_vector class, and a std::string_vector class. Smile

So, using a std::vector...

c++:
#include <vector>

int main()
{
   std::vector<int> grades;
   grades.push_back(42);
   grades.push_back(78);
   grades.push_back(89);

   std::cout << "There are " << grades.size() << " grades." << std::endl;

   return 0;
}
[Gandalf]




PostPosted: Sun Jul 10, 2005 4:27 pm   Post subject: (No subject)

Thanks again, always a help! Smile

So, is there a way to add in lots of 'grades' without having to write the grades.push_back every time? Other than a for loop?
wtd




PostPosted: Sun Jul 10, 2005 4:41 pm   Post subject: (No subject)

[Gandalf] wrote:
Thanks again, always a help! Smile

So, is there a way to add in lots of 'grades' without having to write the grades.push_back every time? Other than a for loop?


Not really, but as you say, with a loop it isn't much trouble.

c++:
#include <vector>
#include <iostream>

int main()
{
   std::vector<int> grades(10); // pre-allocate space for ten ints
   
   for (int i(0); i < 10; ++i)
   {
      int temp;
     
      while (true)
      {
         std::cout << "Enter a grade: ";
         std::cout.flush(); // make sure line buffering doesn't screw up the output
     
         std::cin >> temp;

         if (!std::cin.fail()) break;
         std::cout << "Please enter a valid grade." << std::endl;
      }
   
      grades.push_back(temp);
   }

   return 0;
}
wtd




PostPosted: Sun Jul 10, 2005 4:57 pm   Post subject: (No subject)

Of course, if you don't like creating the temp variable, you could always create your own class which wraps a std::istream. Smile

c++:
#include <iostream>
#include <pair>
#include <vector>

class my_istream
{
   private:
      std::istream& in;
   public:
      explicit my_istream(std::istream& in_stream);
 
      template <typename _t> _t get_value();
      template <typename _t> std::pair<_t, bool> get_value_and_status();

      bool fail() const;
};

explicit my_stream::my_istream(std::istream& in_stream)
: in(in_stream)
{ }

template <typename _t>
_t my_istream::get_value()
{
   _t temp;
   in >> temp;
   return temp;
}

template <typename _t>
std::pair<_t, bool> my_istream::get_value_and_status()
{
   return std::pair<_t, bool>(get_value(), fail());
}

bool my_istream::fail() const
{
   return in.fail();
}


untested
[Gandalf]




PostPosted: Sun Jul 10, 2005 5:07 pm   Post subject: (No subject)

Wow Shocked . Considering the first was at the limit of my knowledge, the seconds seems a bit complicated. Maybe I'll look back in a little while and understand Smile.
wtd




PostPosted: Sun Jul 10, 2005 8:16 pm   Post subject: (No subject)

Well, if it works, you could create a my_istream variable which wraps std::cin.

c++:
my_istream in(std::cin);


Then get an int from std::cin like so:

c++:
int main()
{
   my_istream in(std::cin);
   std::vector<int> grades(10);

   std::cout << "Input grade: ";
   std::cout.flush();

   grades.push_back(in.get_value<int>());

   return 0;
}
Sponsor
Sponsor
Sponsor
sponsor
jamonathin




PostPosted: Sun Jul 10, 2005 8:45 pm   Post subject: (No subject)

I read through this entire post just now, and you remind me of, well me when I first started this Gandalf. This is about where I left off. I got distracted by the games being made in Turing, and I spent all of my programming time on making Games and whatnot, simply because I didn't have the know-how in C++.

It's good that you're using other resources to teach yourself. One website the I used a lot was this. This may/may not help you, but It helped me a lot. It mostly helped me because After you read the chapter and learned from it, it gave you sample progarms to try, and full solutions. Also, it's a lot quicker than posting on CompSci, and I felt that I became a nussiance.

Not to knock on wtd. I mean, I don't know anyone else on CompSci that responds to any question faster than him. And very Detailed mite i add. He's without a doubt one of the most important mods here. Smile

But yeah, check out that site. You'll soon/may already be finishing it, but yeah. Good-Luck on it. Stick to it man, I'm comin back to it . . . later. . Wink
wtd




PostPosted: Sun Jul 10, 2005 9:01 pm   Post subject: (No subject)

jamonathin wrote:
It's good that you're using other resources to teach yourself. One website the I used a lot was this. This may/may not help you, but It helped me a lot.


Unfortunately that resource is quite outdated.

c++:
#include <iostream.h>


c++:
int number;
char character;
       
for (number = 32 ; number <= 126 ; number = number + 1) {
   character = number;
   cout << "The character '" << character;
   cout << "' is represented as the number ";
   cout << number << " in the computer.\n";
}


As for this, it shows that the author doesn't know what you don't have to declare variables ahead of time in C++. It also demonstrates automatic conversion of integral data types, rather than the more sane explicit cast.

c++:
for (int number(32); number <= 126; number += 1)
{
   cout << "The character '" << static_cast<char>(number)
        << "' is represented as the number "
        << number << " in the computer." << endl;
}
wtd




PostPosted: Sun Jul 10, 2005 9:04 pm   Post subject: (No subject)

Most horrifically that tutorial uses char arrays instead of proper strings. This is inexcusable.
jamonathin




PostPosted: Sun Jul 10, 2005 9:11 pm   Post subject: (No subject)

wtd wrote:
This is inexcusable.


Snooty What was I thinking, lol. Ok ok, here's a better site. I mainly used taht site for programs. As in, "Ok, make <this> program", and so on . .
[Gandalf]




PostPosted: Sun Jul 10, 2005 9:18 pm   Post subject: (No subject)

Well... Thanks for the explanation wtd, and the site jamonathin, although it might be out of date, I may be able to learn from it a bit, and it was an effort Smile.

All of that is just because it's old C++, right? Before they made the standard? That's why all the books and everythign I am learning from, I made sure it was all newer than 2002. They probably don't use typecasts right away because they want the person to understand the basics before going into more details.

Yep, I know what you mean jamonathin, I want to learn C++, but I also want to use graphics and the sort. One step at a time though. Still, I might make a game in Turing in between...

Oh, and wtd, do you know how to use colours in the standard console output? It's probably not that important, I know, but it would make the output more legible in larger amounts. Thanks for all the help.

*edit* Laughing yes, this site is the most helpful one out there. Especially with wtd here to help you out with your C++ needs.
wtd




PostPosted: Sun Jul 10, 2005 9:27 pm   Post subject: (No subject)

[Gandalf] wrote:
Well... Thanks for the explanation wtd, and the site jamonathin, although it might be out of date, I may be able to learn from it a bit, and it was an effort Smile.


You'll have to unlearn most of what you learn from it.

[Gandalf] wrote:
All of that is just because it's old C++, right?


Some of it, probably, but there's also the fact that a lot of C programmers like to pretend that they know C++, just because of the syntactic similarities.


[Gandalf] wrote:
Oh, and wtd, do you know how to use colours in the standard console output? It's probably not that important, I know, but it would make the output more legible in larger amounts. Thanks for all the help.


Certainly no portable way. On Windows you could use the Win32 API to achieve this.

One post on the subject found by Google.
[Gandalf]




PostPosted: Sun Jul 10, 2005 9:47 pm   Post subject: (No subject)

Too bad, at least it's possible...

Also, while looking at the source code for one of my favourite games, I saw that it uses printf(); even though it's coded in C++. Some of the books I am learning from use it, and other C-like functions as well. I think they might be a part of the 'standardized' library too. Why?
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 4 of 5  [ 64 Posts ]
Goto page Previous  1, 2, 3, 4, 5  Next
Jump to:   


Style:  
Search: