Basic

Feb 13, 2008 at 3:05pm
Hey Folks,

I'm new to c++, what program is the best to write and edit code that isnt to complicated. Any help would be appreciated. Thanks.

Some of the programs wouldn't work on my computer correctly.


Regards,
Knightcastle
Last edited on Feb 13, 2008 at 3:17pm
Feb 13, 2008 at 3:10pm
I'm pretty new to C++ myself, and I've found Dev-C++ to be very simple to use. I'm also using Visual C++ express, and it has a some very useful features, but seems to be slightly more complicated. Both are available for free download. Just search for them on google. Good luck!
Feb 13, 2008 at 3:21pm
Thanks,

Do you know of any scripts that are already written that I can copy and paste just to fool around with?

Regards,
Last edited on Feb 13, 2008 at 3:22pm
Feb 13, 2008 at 3:30pm
Yeah, there are a lot in the help section of Visual C++ express. Also, there is a bunch of example code on this site in the tutorial section. (At the top left of your screen, click on Documentation, C++ Language tutorial.)
Last edited on Feb 13, 2008 at 3:31pm
Feb 13, 2008 at 3:43pm
Thanks,

I Installed Dev-C++ and it's working excellent. I used the "Hello World" Script and it works but it pops up for a second then goes away. Is there a line of code that I can add which will make it last longer on the screen.

Heres the Code I Used:
1
2
3
4
5
6
#include <stdio.h>
int main()
{
    printf("Hello World\n");
    return 15;
}


Also, Would it be hard to make a program just like the Hello World Script which will display the current time and date?

Any help is appreciated.

Regards,
Feb 13, 2008 at 5:11pm
If you want the program to pause so you can see the output, you can to this. At the top of your program, add:

#include <iostream>
That prepares your program for use of the cin, cout, etc.
Then before the return statement, add:

1
2
int variable;
cin>>variable;


This will cause your program to pause while it waits for input from the keyboard. When the program runs, just type any number and press enter to end it. That's probably not the only way, and certainly not the most elegant, but it's easy, so I use it. (Like I said, I'm a real beginner myself.)

As for adding time and date, I'm not sure. I've seen a few other posts that have claimed that it takes a fair amount of know-how to add them to your program. I'm currently experimenting a little with this sample I found in the Visual C++ help index:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
// crt_clock.c
// This example prompts for how long
// the program is to run and then continuously
// displays the elapsed time for that period.
//

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

void sleep( clock_t wait );

int main( void )
{
   long    i = 6000000L;
   clock_t start, finish;
   double  duration;

   // Delay for a specified time.
   printf( "Delay for three seconds\n" );
   sleep( (clock_t)3 * CLOCKS_PER_SEC );
   printf( "Done!\n" );

   // Measure the duration of an event.
   printf( "Time to do %ld empty loops is ", i );
   start = clock();
   while( i-- ) 
      ;
   finish = clock();
   duration = (double)(finish - start) / CLOCKS_PER_SEC;
   printf( "%2.1f seconds\n", duration );
}

// Pauses for a specified number of milliseconds.
void sleep( clock_t wait )
{
   clock_t goal;
   goal = wait + clock();
   while( goal > clock() )
      ;
}


Not exactly the time and date, but I think it's somewhat relevant. I'm trying to use it as a time delay for some text output to the screen. That way for example "Hello World" would gradually be typed one letter at a time rather than just seeming to appear all at once. Anyway, I really suggest you check out the tutorials I referenced above. They'll give you a pretty good grasp of the basics.
Feb 13, 2008 at 5:35pm
When I Added those lines for "Hello World" Script, I Compiled it and two errors came up:

=C:\My Documents\program1.cpp In function `int main()':
=Line: 8 C:\My Documents\program1.cpp `cin' undeclared (first use this function)
=(Each undeclared identifier is reported only once for each function it appears in.)

Heres the code:
1
2
3
4
5
6
7
8
9
#include <iostream>
#include <stdio.h>

int main()
{
    printf("Hello World\n");
int variable;
cin >>variable; return 0;
}


Any idea what the problem is?
Feb 13, 2008 at 5:44pm
My bad. Add the following to the top of the program:

using namespace std;

This sets up the standard namespace required to use the cin and cout functions. Sorry 'bout that.
Feb 13, 2008 at 5:52pm
Thanks,

No Problem, Works excellent now. I'm currently viewing the basics of C++ from the Dev-C++ Help. Starting to understand a lot. Im currently learning symbols and how to insert comments.
Feb 13, 2008 at 8:34pm
if you don't want to add

 
using namespace std;


you can write

 
#include <iostream.h> 


instead of

 
#include <iostream> 


it should works too.
Last edited on Feb 13, 2008 at 8:34pm
Feb 13, 2008 at 9:19pm

i use dev-C++ and all you have to do to pause is right before the
return 0;
type
system("PAUSE");
Last edited on Feb 13, 2008 at 9:20pm
Feb 13, 2008 at 11:36pm
if you don't want to add
 
using namespace std;


you can write

 
#include <iostream.h> 


instead of

 
#include <iostream> 


it should works too.
Feb 15, 2008 at 2:34am
I figured out the "system" command too. Basically it sends a command to the command prompt as though you were giving commands in DOS. So it won't work for people on Linux/Unix/Mac computers.

You can try it yourself by opening up the Command Prompt and typing "pause" or "cls" (the clearscreen command). If you know more dos commands, you can integrate them into your programs with a system call, too. Just don't send them to anybody running anything other than Windows, because they probably won't work quite right. :)
Feb 15, 2008 at 3:14am
To make it pause at the prompt just add this:
1
2
std::cin.ignore ( std::numeric_limits<std::streamsize>::max(), '\n' );
std::cin.get();

That makes it pause until you push enter without all the "PUSH ANY KEY TO CONTINUE" crap. Be sure to #include <limits>.
Topic archived. No new replies allowed.