• Log in or sign up for an account here: https://www.sythe.org/login

My very first C++ program

evil-oreo

Member
Joined
Dec 3, 2005
Sythe Gold
0
Messages
40
Look below, somehow double posted sorry.

Ill use this space to tell a little about my programming knowledge. lol

Well 14 and a half hours from this second of writing this, I knew abosulutly NOTHING!
Whats this and whats that, why is that and why is this? These questions came up alot.
Thanks to the wonderful eBook of "Beginning C++ Game Programming", Im learning C++ with ease, or so it seems.
Any program I make is written by the author of the book, I just look and re type it from character to character.
 
Well I just started learning c++ 4 hours ago, and following along with a really good book. Just finished chapter 1 and did the tutorial but I looked and typed the code myself so I thought it would be nice to share my first program with you guys.

// Lost Fortune
// A personalized adventure

#include <iostream>
#include <string>
using std::cout;
using std::cin;
using std::endl;
using std::string;

int main()
{
const int GOLD_PIECES = 900;
int adventurers, killed, survivors;
string leader;

//get the information
cout << "Welcome to Lost Fortune!\n\n";
cout << "Please enter the following for your personalized adventure.\n";

cout << "Enter a number: ";
cin >> adventurers;

cout << "Enter a number that is smaller then the first: ";
cin >> killed;

survivors = adventurers - killed;

cout << "Enter a character name: ";
cin >> leader;

// Tell the story
cout << "\nA brave group of " << adventurers << " set out on a quest ";
cout << "-- in search of the lost treasure of the ancient dwarves.";
cout << "The group was led by the legendary rouge, " << leader << ".\n";

cout << "\nAlong the way, a band of maurading ogres ambushed the party. ";
cout << "All fought bravely under the command of " << leader;
cout << ", and the ogres were defeated, but at a cost. ";

cout << "Of the adventurers, " << killed << " were vanquished, ";
cout << "leaving just " << survivors << " in the group.\n";

cout << "\nThe party was about to give up all hope. ";
cout << "But while laying the deceased to rest, ";
cout << "they stumbled upon the buried fortune. ";
cout << "So the adventurers split " << GOLD_PIECES << " gold pieces.";
cout << leader << " held on to the extra " << (GOLD_PIECES % survivors);
cout << " pieces to keep things fair of course.\n";

cout << "Press the ENTER key to exit";// 1 of 2 lines copied
cin.ignore(cin.rdbuf()->in_avail() +1);// 2 of 2 lines copied
return 0;
}
Let me know what you guys think please. And I guess I did copy paste the last 2 lines because it would close once I ran the script.
 
Here is my second one! Decided better to post them here then to make multiple threads.

// Guess My Number
// The classic number guessing game

#include <iostream>
#include <ctime>
#include <cstdlib>

using namespace std;

int main()
{
srand (time(0));
int theNumber = rand() % 100 + 1;
int tries = 0, guess;

cout << "\tWelcome to Guess My Number!\n\n";

do
{
cout << "Enter a guess: ";
cin >> guess;
++tries;

if (guess > theNumber)
cout << " To high!!\n\n";

if (guess < theNumber)
cout << "To low!! \n\n";
}
while (guess != theNumber);

cout << "\nThats it! You got it in " << tries << " guesses!\n";

cout << "Press the ENTER key to exit";
cin.ignore(cin.rdbuf()->in_avail() +1);
return 0;
}
 
Holy shit... I've done C++ from tutorials on the web and never knew that the "cin" command was that easy to use.

Thanks for sharing your program :)
 
ohhh nice welcome to the wonderful world of programming ^>^

and for future reference you can use [ code ] (no spaces) specifically for posting programming code
 
Code:
// Lost Fortune
// A personalized adventure

#include <iostream>
#include <string>
using namespace std;

int main()
{
const int GOLD_PIECES = 900;
int adventurers, killed, survivors;
string leader;

//get the information
cout << "Welcome to Lost Fortune!\n\n";
cout << "Please enter the following for your personalized adventure.\n";

cout << "Enter a number: ";
cin >> adventurers;

cout << "Enter a number that is smaller then the first: ";
cin >> killed;

survivors = adventurers - killed;

cout << "Enter a character name: ";
cin >> leader;

// Tell the story
cout << "\nA brave group of " << adventurers << " set out on a quest ";
cout << "-- in search of the lost treasure of the ancient dwarves.";
cout << "The group was led by the legendary rouge, " << leader << ".\n";

cout << "\nAlong the way, a band of maurading ogres ambushed the party. ";
cout << "All fought bravely under the command of " << leader;
cout << ", and the ogres were defeated, but at a cost. ";

cout << "Of the adventurers, " << killed << " were vanquished, ";
cout << "leaving just " << survivors << " in the group.\n";

cout << "\nThe party was about to give up all hope. ";
cout << "But while laying the deceased to rest, ";
cout << "they stumbled upon the buried fortune. ";
if ( GOLD_PIECES < survivors ) 
{
     cout << "Unfortunately there wasnt enough gold to go around so " << leader << " gave all the gold to charity." << endl; 
}
if ( GOLD_PIECES >= survivors )
{
cout << "So the adventurers split " << GOLD_PIECES << " gold pieces.";
cout << leader << " held on to the extra " << (GOLD_PIECES / survivors);
cout << " pieces to keep things fair of course.\n";
}

system("Pause");
return 0;
}
some modifications for you :D

Code:
// Guess My Number
// The classic number guessing game

#include <iostream>
#include <ctime>
#include <cstdlib>

using namespace std;

int main()
{
srand (time(0));
int theNumber = rand() % 100 + 1;
int tries = 0, guess;

cout << "\tWelcome to Guess My Number!\n\n";

do
{
cout << "Enter a guess: ";
cin >> guess;
++tries;

if (guess > theNumber)
cout << " To high!!\n\n";

if (guess < theNumber)
cout << "To low!! \n\n";
}
while (guess != theNumber);

cout << "\nThats it! You got it in " << tries << " guesses!\n";

system("pause");
return 0;
}

originally with your homemade program halter to view the information the program would just close automatically so i put in
Code:
system("pause");
that tells the program to halt and show information to the user and then have them press a key then it proceeds to execute the next line
 
//so after
system("Pause");
//put something like
cout << "Press a key to continue.\n";
 
Well, the end of chapter 3 in this book and this is what I have worked on.

// Word Jumble
// A classic word jumble game where the player can ask for a hint

#include <iostream>
#include <ctime>
#include <cstdlib>
#include <string>

using namespace std;

int main()
{
enum fields {WORD, HINT, NUM_FIELDS};
const int NUM_WORDS = 5;
const string WORDS[NUM_WORDS][NUM_FIELDS]=
{
{"wall", "Do you feel like your banging your head on something?"},
{"glasses", "These might help you see the answer."},
{"labored", "Going slowly, is it?"},
{"persistant", "Keep going at it!"},
{"jumble", "Its what the game is all about."}
};

srand(time(0));
int choice = (rand()%NUM_WORDS);
string theWord=WORDS[choice][WORD]; //word to guess
string theHint=WORDS[choice][HINT]; //hint for word

string jumble = theWord; //jumbled version of the word
int length = jumble.size();
for (int i=0; i<length; ++i)
{
int index1 = (rand()%length);
int index2 = (rand()%length);
char temp = jumble[index1];
jumble[index1] = jumble[index2];
jumble[index2] = temp;
}

cout << "\t\tWelcome to Word Jumble!!\n\n";
cout << "Unscrabble the letters to make a word.\n";
cout << "Enter 'hint' for a hint.\n";
cout << "Enter 'quit' to quit the game.\n\n";
cout << "The jumble is: " << jumble;

string guess;
cout << "\n\nYour guess: ";
cin >> guess;

while ((guess != theWord) && (guess != "quit"))
{
if (guess == "hint")
cout << theHint;
else
cout << "Sorry, thats not it.";

cout << "\n\nYour guess: ";
cin >> guess;
}

if (guess == theWord);
cout << "\nYou got it, Grats!!\n";

cout << "\nThanks for playing!\n";

system("pause");
return 0;
}
 
on the contrary: if he wants to learn the C++ language he should work on concepts such as pointers, classes, and templates

if you go straight to gui like i did you end up with a lot of gaps about the syntax and find that it's hard to learn from others' code


I actually have no clue what a pointer and a template (in your words) is.
 
for what you use these 3 scripts?

btw they are nice good work;)
 
duelarena.gg
Back
Top