PDA

View Full Version : my first c++ program...


MrSlippyFist
03-24-2006, 06:54 PM
isnt working. after i compiled and linked it, the executable only flashes open for a second then closes. im guessing im doing something REALLY stupid because i have absolutely no expertise or experience in this whole scene. someone set me straight, thanks, bye

Myst
03-25-2006, 12:47 AM
It's probably so fast that it just seems like a flash to you, even though actually executes and then closes when it reaches the end.

Try putting something like this at the end:

Sleep(2000); (waits for 2 seconds)

or

char x;
cin >> x; (waits for you to enter a character, then closes)

I haven't written anything in C++, so I'm a bit rusty and could easily have these wrong.

someguy
03-25-2006, 01:59 AM
It's working. It's just a console application automatically closes when its done executing. You should open a command prompt window and then go to the folder where your program is located and type in the name of the program. It will execute it, display the output and won't close the window this way. Start > run > type in "command" hit enter to open command prompt.

NTR
03-25-2006, 02:17 AM
you doubleclick it, right? Yeah, do what Myst said, it works well. For Sleep(int n) tho, you probably need to import cstdlib. The other option is to browse to the file IN THE COMMAND PROMPT (Windows: Start > Run > cmd ) and then run it. you will see the output of the file as long as the buffer remains.

I wrote two programs today in about 10 minutes, one that is made to be ran from a command prompt (unix vt100 terminal, msdos, etc), and one that finds yet another way around the quick closure of the window in windows mode. That other meathod is writing the final output to a file for further review. Here, I'll post it (and the source) for you.

/*Program by Nicholas Byrd
Date: Mar 24 2006*/
#include <iostream>
#include <cstdlib> //for random
#include <fstream> //file io stream
using namespace std;

void swap(int& num1, int& num2)
{
/*Swaps num1 with num2.
int t;
t = num1;
num1 = num2;
num2 = t;
}

int main ()
{
ofstream numfile;
numfile.open ("numbers.txt");
int rangeTotal, rangeMin, rangeMax, rng, n;
cout << "How many numbers do you want?\n:";
cin >> n;
if (n <=0)
{
cout << "\nError: Bad number\n";
exit(1);
}
cout << "What is the lowerbound of the range of numbers you want?\n:";
cin >> rangeMin;
cout << "What is the upperbound of the range of numbers you want?\n:";
cin >> rangeMax;
if (rangeMin == rangeMax)
{
cout << "\nNumber range has to have a length greater than 0!\n";
exit(1);
}//To prevent any weird-ass errors resulting from a weird-ass range
else if (rangeMin > rangeMax)
swap(rangeMin, rangeMax);//No fatal error, we just make it work "correctly"
cout << "\nGenerating your random numbers...\n";
for (n; n > 0; n--, numfile<<endl)
{
rangeTotal = rangeMax - rangeMin + 1;
rng = (rand()%rangeTotal) + rangeMin;
numfile << rng; /*Calculates n random numbers and outputs them to the file "./numbers.txt"*/
}
cout << endl;
numfile.close(); /*Don't forget this. File won't be saved without the close function.*/
return 0;
}
(I added more comments than there were originally)
http://libra.sfsu.edu/~nbyrd/
You can download both programs at the bottom of my sfsu page. The first one is a bit simpler and not far from the source I wrote above (It doesn't use fstream and thus it only uses simple cout statements), but you will notice that it closes right away in the windows enviroment. That's why I wrote the second one. The output is saved to ./numbers.txt. That means that wherever the .exe file is when it is run, there will the output be as well. Thus, you'd be wise to extract the .exe to somewhere like your desktop, where you'll easily be able to find the output. Running it straight from winzip/winrar without extracting it will put the output file in some obscure directory that probably will delete the file before you can ever find it.

hope this helped ya a bit.

MrSlippyFist
03-25-2006, 02:19 AM
alright thanks guys, the command prompt thing worked, havent tried the sleep but im sure thatll work too.

Lividum
03-25-2006, 02:27 AM
Ahh! Holy shit it's C++! I hate this demonic language. So many horrible memories...

NTR
03-25-2006, 02:35 AM
yeah, personally I prefer Java. Higher compatability and... well, can you make a GUI so easily in C++? I think not.

a/k
03-25-2006, 04:18 PM
yeah, personally I prefer Java. Higher compatability and... well, can you make a GUI so easily in C++? I think not.
I took both Java, and C++. Java was alright, but I liked C a little more. Java was a bitch learning though. I hated it. C is pretty good for beginners.

NTR
03-25-2006, 04:29 PM
I somewhat agree. Java requires that you have some knowledge of classes right off the bat, as well as functions. C++... not really anything about functions and nothing about classes. Still, somehow, I learned Java before any other programming language. I don't think it was by luck or my love of computers either. Read the Java book by a guy named Walter Savitch. He wrote this very nice book so that the only prerequisite to it is a highschool level of algebra skills (ie, pretty low).

Oh, btw, nobody use that program I posted if you need real random numbers. I just realized that the seed is the same every time it gets run.