does anyone know where i can find some good c and c++ tutorial because everytime i find one i think is good. i try to compile their code using dev-c++ and there is always an error. but this one bit of code i tried compiled good and than just went away and did not pop up and it was the simplest of programs the hello world one. the code was this:


Code:

#include <iostream>
int main()
{
    cout<<"Hello World\n";
}
http://cplusplus.com or is something I have looked at. I can't rate quality as I don't program C, but the stuff there worked (they recommended dev C++ also).
i have been to that website and when i compile their code i try to rune the exe file and all that happens is the command prompt opens for about one second than goes away. and if i could get dev-c++ to work that would be the site i would go to.
Try this:

#include <iostream>
int main()
{
cout<<"Hello World\n";
int num;
cin >> num;
return 0;

}


EDIT: 0x5, my keyboard tray fell off of my comp desk and it posted before I finished the message.
lafferjm wrote:
i have been to that website and when i compile their code i try to rune the exe file and all that happens is the command prompt opens for about one second than goes away. and if i could get dev-c++ to work that would be the site i would go to.


thats because it is exiting before you have a chance to view the output

The solution is to either add


Code:
char t;
std::cin.get(t);


before the return statement at the end of main(), or to create a command line (start->run->cmd), cd to the directory that has the .exe, and running it that way

As for the errors, I can see why from the code snippet you posted. It must either be "std::cout" or you must add "using namespace std;" to the beginning of the function

This site also has some good (albeit very brief) tutorials: http://www.cprogramming.com/tutorial.html However, your best bet if you are truly serious about learning C or C++ is to actually BUY a begginers book, which goes into far more depth and detail than any web tutorial will.

@Foamy: Your pseudo-pause is fine, except that it requires you to enter something. Using cin.get(char) won't skip over the newlines, so just hitting enter will work.
Kllrnohj wrote:

As for the errors, I can see why from the code snippet you posted. It must either be "std::cout" or you must add "using namespace std;" to the beginning of the function


I've never used std::cout or using namespace std. It hasn't given me any trouble...

EDIT: But that's on a borland compiler.
Then it isn't technically ANSI standard Smile (as cin and cout are in the std namespace)
Yeeeah, lafferjm, your code is working fine. Adding the pause at the end will allow you to see the output before the program terminates and the window closes.
why does C++ look radically different from C?

Or is it just different from TIGCC?
C++ doesn't actually look different. cout >> and cin << are just shortcuts to printf() and scaf().
KermMartian wrote:
C++ doesn't actually look different. cout >> and cin << are just shortcuts to printf() and scaf().


ah Confused
KermMartian wrote:
C++ doesn't actually look different. cout >> and cin << are just shortcuts to printf() and scaf().


well, there is more to it than THAT, but thats the rough idea.

cin, cout, and cerr are streams to c-stdin, c-stdout, and c-stderr. Streams use the << and >> operators to read or write data to them (this includes filestreams, socketstreams etc...). It is much easier to use than snprintf(), and isn't vulnerable to buffer-overflows Smile The type is automatically converted, and formating (indentation, spacing, number of decimals to be shown, etc...) is still available by passing special values to the streams. For example, instead of std::cout << "Hello World\n" it could have been the (slightly) more portable std::cout << "Hello World" << std::endl (as newlines aren't always just \n - for example, in windows newlines are \r\n. It is also \r\n for newlines in HTTP headers/requests Wink )

As for looking different, there are things that are done in TIGCC that aren't normally done in either C or C++ (for example, using #defines for compile options)

Another major difference between C and C++ would be classes, such as this needlessly complex Hello World program


Code:
#include <iostream>
#include <string>

class Hello {
public:
   Hello();
   Hello(std::string newmsg);
   ~Hello();
   void sayhi();
private:
   std::string msg;
};

int main(int argc, char* argv[]) {
   using namespace std;
   
   Hello myHello;
   Hello *otherHello = new Hello("w00t\n");
   otherHello->sayhi();
   myHello.sayhi();

   cout << "Press return to exit." << endl;
   char t;
   cin.get(t);
   return 0;
}

Hello::Hello() {
   msg = "Hello, World!\n";
}

Hello::Hello(std::string newmsg) {
   msg = newmsg;
}

Hello::~Hello() { }

void Hello::sayhi() {
   std::cout << msg;
}
Have you tried objective-C? I read in Wikipedia that objective C and C++ are two different languages that add OOP differently to C. Having no substantial experience with computers, I don't know much about OOP.
No, I haven't tried Obj-C, nor do I have any desire to do so Wink
OOP plus C just sounds sadistic.
Your local library should also have excellent books on C++. Try C++ For Dummies or some other beginner's C++ book. Also, this can be used in C++ to pause:

Code:
system("PAUSE");
Good call re: looking in the library for books. That's always a good source.
i will look in library even though i doubt they will have any because they didnt even have an html. oh and is there a command that will clear the screen.
#include <conio> //windows only

int main()
{
clrscr();
return 0;
}
system("pause") won't ALWAYS work, only if there is a program or command called "pause" for the native system, and it might not work as expected.

You can also use system() to clear the screen (for example, in linux you can do system("clear"); )

@Kerm: GTK is OOP in C, its just feels very hacked together Wink
  
Register to Join the Conversation
Have your own thoughts to add to this or any other topic? Want to ask a question, offer a suggestion, share your own programs and projects, upload a file to the file archives, get help with calculator and computer programming, or simply chat with like-minded coders and tech and calculator enthusiasts via the site-wide AJAX SAX widget? Registration for a free Cemetech account only takes a minute.

» Go to Registration page
Page 1 of 2
» All times are UTC - 5 Hours
 
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

 

Advertisement