This is source code that is saved as a text file, usually with the extension .cpp. Because it is a text file, you can write code using any text editor, like Notepad.
To compile the source code into a program, you need a compiler. There's many options available, both free and commercial. The most basic ones simply compile source code. Others are known as IDE and have a graphical interface that allows you to edit, compile, and debug code using the same program.
I would advise getting dev cpp, its free and a really nice complier to use. I know its complicated when you first start out, but everything will soon become clear :D
ok thank you. I downloaded dev cpp. But howcome when I test it out and use one of the codes listed on this site like the hello world code or the first math code the Command prompt pops up for a split second then closes. Their is no effect when I click to run the program.
It's because you most probably just write some text to Console, and then terminate the program (maybe code above?):
1 2 3 4 5 6 7 8 9
# include <iostream>
usingnamespace std;
int main ()
{
cout<< "how do i write in C++???"; //writing to console
return 0; //terminating program -> closing console
}
before you close the program, you should call something that 'pause' it, and wait to a key press:
1 2 3 4 5 6 7 8 9 10
# include <iostream>
usingnamespace std;
int main ()
{
cout<< "how do i write in C++???";
system ("Pause"); //pausing program, waiting to key press
//or cin.get();
return 0;
}
ok, after working on SEVERAL programs i've created, i've ran into problems with this one. The purpose is to find out how many years and months till you are a certain age. (I will add days when I get a more extensive knowledge of the c++ language).
// C++ program to figure out how many years it will take for you to be x years old.
#include <iostream>
usingnamespace std;
int main ()
{
int x; // Current Age
int a; // Age requested
int xx; // Month of users birth
int aa; // Month it is
int t; // Total of users birth month and what month it is now
int b; // Number of months till birthday
t = 12 - aa; // T may need some revising. Can't figure out a formula that will work.
b = // What should b equal?
cout << "Currently, how old are you: ";
cin >> x;
cout << "What is the number of month you were born in: ";
cin >> xx;
cout << "What age do you want to be: ";
cin >> a;
cout << "What is the number of the month it is now: ";
cin >> aa;
cout << "You will be " << a << " in " << a - x << " years and " << b <<" months."".\n";
system ("pause");
return 0;
}
The problem is I can't figure out a formula that will work. I had another formula but whenver i did the survey the months would always equal some 10 digit number.
Problem is, you are trying to use value in variable, that is not initialized (it has, say, random value:-)) so you first must read it from user, and then using it. Remember that - it can be very dangerous using uninitialized variables, and good custom is initializing it immidialtely after declaration:
#include <iostream>
usingnamespace std;
int main ()
{
int x; // Current Age
int a; // Age requested
int xx; // Month of users birth
int aa; // Month it is
//int t; // Total of users birth month and what month it is now
int b; // Number of months till birthday
int y; // Number of years remaining
//we cannot using value in aa, because it wasn't yet initialized
//t = 12 - aa; // T may need some revising. Can't figure out a formula that will work.
//b = // What should b equal?
cout << "Currently, how old are you: ";
cin >> x;
cout << "What age do you want to be: ";
cin >> a;
cout << "What is the number of month you were born in: ";
cin >> xx;
cout << "What is the number of the month it is now: ";
cin >> aa;
b = xx - aa; //months remaining
if (b < 0)
b += 12; //equal to b = 12 + b; ( + because b < 0)
y = a - x - 1; //years remaining
//if aa == xx
if (!b) //equal to if (b == 0)
++y; //y = y + 1
//total number of months
//t = b + y*12;
cout << "You will be " << a << " in " << y << " year(s) and " << b <<" month(s).\n";
system ("pause");
return 0;
}
I can't find simplier solution, so if you just don't hear about 'if', it selecting code into two ways, based on the trueness of condition. You sure soon get it;-)
ok, I created a MUCH more advanced program but running into a similar problem. I just do not understand the "if" tool.
Here is the code... towards then end of it if you select the option to find out how many mobs needed till nxt lvl it throws in some random numbers. (This might be easier to understand if you are a gamer).
#include <iostream>
usingnamespace std;
int main (void)
{
system ("TITLE C++ Program testing and writting (EXP till lvl Calc.)");
system ("COLOR A");
unsignedlongint x, xx; // These are the current and required level of EXP
int a; // This is the number of EXP till next level
int b; // This is the number of EXP per kill
int c; // This is the current level
int d; // This is the next level
int zz; // This is the total number of mobs that need to be killed for a level
int tt; // Name of the prey
char cChar; // Gives user Yes or no options
char cDoagain; // Gives user option to re-do calculator
do
{
system ("CLS");
cout << "Welcome to the Experience Points Calculator!" << endl;
cout << "Please answer the following questions accuratly for the best results. " << endl << endl;
cout << "Please enter your current level: ";
cin >> c;
d = c + 1;
cout << "Please enter your current Experince Points level: ";
cin >> x;
cout << "Please enter the ending amount of Experience Points needed to level up: ";
cin >> xx;
a = xx - x; // "a" is the total number of experience points required to level
cout << "You need " << a << " Experience Points to reach level " << d << ". " << endl << endl;
cout << "Would you like to know how many enemys you need to kill to reach your next lvl? (Y or N)"
<< endl;
cin >> cChar; // Option for user to decide if they want to know how many mobs they need to kill
switch (cChar)
{
case'Y' :
cout << "What is the name of your prey?";
cin >> tt;
cout << "Exactly, how much exp do you get per " << tt << " : ";
cin >> b;
zz = a / b; // Formula for how many mobs need to be killed till next level
cout << "You need to kill about " << zz << " " << tt << "(s) until you recieve your next level. " << endl << endl;
break;
case'y' :
cout << "What is the name of your prey? ";
cin >> tt;
cout << "Exactly, how much exp do you get per " << tt << " : ";
cin >> b;
zz = a / b; // Formula for how many mobs need to be killed till next level
cout << "You need to kill about " << zz << " " << tt << "(s) until you recieve your next level. " << endl << endl;
break;
case'N' :
cout << "Good luck!"
<< endl << endl;
break;
case'n' :
cout << "Good luck!"
<< endl << endl;
}break;
cout << "Would you like to start again? (Y or N) " << endl;
cin >> cDoagain;
}while (cDoagain == 'Y' || cDoagain == 'y');
system ("Pause");
return 0;
}
My problem is very similar to before. The basic information i entered in is...
Current lvl: 50
Current EXP: 50
Next lvl EXP: 60
Mob: Rats
EXP per Mob: 2
The correct number of rats needed to be killed for lvl 51 should be 5 rats
After I type in the name of the mob I'm killing (rats)
and press enter it immediatly fills in the next 4 lines with random numbers in the "zz and tt" spots.
Also, how would i add the "No" option towards then end of the program? So that the program would close out if selected.
// experience points calculator
#include <iostream>
#include <string>
usingnamespace std;
int main ()
{
system ("TITLE Experience Points Calc.");
system ("COLOR A");
signedlongint x, xx; // These are the current and required level of EXP
int a; // This is the number of EXP till next level
int b; // This is the number of EXP per kill
int c; // This is the current level
int d; // This is the next level
signedlongint zz; // This is the total number of mobs that need to be killed for a level
// Name of the prey will be listed at mystr
char cChar; // Gives user Yes or no options
char cDoagain; // Gives user option to re-do calculator
char Y, y;
char N, n;
do
{
system ("CLS");
cout << "Welcome to the Experience Points Calculator!" << endl;
cout << "Please answer the following questions accuratly for the best results. " << endl << endl;
cout << "Please enter your current level: ";
cin >> c;
d = c + 1;
cout << "Please enter your current Experince Points level: ";
cin >> x;
cout << "Please enter the ending amount of Experience Points needed to level up: ";
cin >> xx;
a = xx - x; // "a" is the total number of experience points required to level
cout << "You need " << a << " Experience Points to reach level " << d << ". " << endl << endl;
cout << "Would you like to know how many enemys you need to kill to reach your next lvl? (Y or N)"
<< endl;
cin >> cChar; // Option for user to decide if they want to know how many mobs they need to kill
switch (cChar)
{
case'Y' :
{ string mystr;
cout << "What is the name of your prey?";
cin >> mystr;
cout << "Exactly, how much exp do you get per " << mystr << " : ";
cin >> b;
zz = a / b; // Formula for how many mobs need to be killed till next level
cout << "You need to kill about " << zz << " " << mystr << "(s) until you recieve your next level. " << endl << endl;
}break;
case'y' :
{ string mystr;
cout << "What is the name of your prey? ";
cin >> mystr;
cout << "Exactly, how much exp do you get per " << mystr << " : ";
cin >> b;
zz = a / b; // Formula for how many mobs need to be killed till next level
cout << "You need to kill about " << zz << " " << mystr << "(s) until you recieve your next level. " << endl << endl;
}break;
case'N' :
cout << "Good luck!"
<< endl << endl;
break;
case'n' :
cout << "Good luck!"
<< endl << endl;
break;
} // Everything works good until here, how do i make these next lines work properly? They are self explanitory.
cout << "Would you like to start again? (Y or N) " << endl;
if (cin >> (Y, y))cDoagain;
else cin >> N,n;
cout << "Good luck" << endl << endl;
}
while (system ("Pause"));
return 0;
}
#include <iostream>
#include <string>
usingnamespace std;
bool UnBlock ()
{
if (cin.fail())
{
cin.clear();
cin.ignore(100, '\n');
returntrue;
}
returnfalse;
}
int main ()
{
system ("TITLE Experience Points Calc.");
system ("COLOR NeedXP");
signedlongint CurXP, NextXP; // These are the current and required level of EXP
int NeedXP; // This is the number of EXP till next level
int XPPerPrey; // This is the number of EXP per kill
int CurLevel; // This is the current level
int NextLevel; // This is the next level
signedlongint Mobs; // This is the total number of mobs that need to be killed for NeedXP level
char cDoagain; // Gives user option to re-do calculator
char YesNo;
do
{
system ("CLS");
cout << "Welcome to the Experience Points Calculator!\n"
<< "Please answer the following questions accuratly for the best results.\n\n"
<< "Please enter your current level: ";
cin >> CurLevel;
if (UnBlock())
CurLevel = 0;
NextLevel = CurLevel + 1;
cout << "Please enter your current Experince Points level: ";
cin >> CurXP;
if (UnBlock())
CurXP = 0;
cout << "Please enter the ending amount of Experience Points needed to level up: ";
cin >> NextXP;
if (UnBlock())
NextXP = 0;
NeedXP = NextXP - CurXP; // "NeedXP" is the total number of experience points required to level
cout << "You need " << NeedXP << " Experience Points to reach level " << NextLevel << ".\n\n";
cout << "Would you like to know how many enemys you need to kill to reach your next lvl? (Y or N)\n";
string PreyName;
cin >> YesNo;
switch (YesNo)
{
case'Y' :
case'y' :
cout << "What is the name of your prey? ";
cin >> PreyName;
cout << "Exactly, how much exp do you get per " << PreyName << " : ";
cin >> XPPerPrey;
Mobs = NeedXP / XPPerPrey;
cout << "You need to kill about " << Mobs << " " << PreyName << "(s) until you recieve your next level.\n\n";
break;
case'N' :
case'n' :
cout << "Good luck!\n\n";
break;
default:
cout << "Sorry sir, I don't understand.\n";
break;
}
cin.ignore(100, '\n'); //ignore bad input
cout << "Would you like to start again? (Y or N)\n";
cin >> cDoagain;
}
while ((cDoagain != 'n')&&(cDoagain != 'N'));
cout << "Good bye sir!\n";
return 0;
}
It's an good idea call variables according to their purpose. Using five variables called a, b, c, d, e.... makes your code unsynoptical.
When using 'switch', you can select more options to using the same code like this:
1 2 3 4 5 6 7 8 9
switch (CHAR)
{
case'a':
case'b':
case'c':
cout << "CHAR is a, b, or c";
break;
//.....
}