quick question

I'm completly new to programing. When I'm writting a program do I type this in the command prompt? for example, If I wanted to do one in the tutorial.

1
2
3
4
5
6
7
8
9
10
# include <iostream>
using namespace std;

int main ()

{
cout<< "how do i write in C++???";
return 0;
}


Do I type this in the CMD? Or do I need to buy a program?
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.
Last edited on
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.
Last edited on
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>
using namespace 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>
using namespace std;

int main ()
{
cout<< "how do i write in C++???";
system ("Pause"); //pausing program, waiting to key press
//or cin.get();
return 0;
}
thank you! Now it works. Be prepared for many more questions from me in the near future.
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).

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
// C++ program to figure out how many years it will take for you to be x years old.

#include <iostream>
using namespace 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.
Last edited on
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:
 
   int a = 5; //not necessary, but safer (sometimes) 

The following code should work properly:
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
42
43
44

#include <iostream>
using namespace 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;-)
I still dont get the "If" process. I'll look into it. Thanks for fixing this program =)
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).

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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#include <iostream>
using namespace std;

int main (void)
{
    system ("TITLE C++ Program testing and writting (EXP till lvl Calc.)");
    system ("COLOR A");
    
    unsigned long int 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;
}
    
    
Last edited on
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.
Last edited on
closed account (L1T0RXSz)
I tried to compile your program on my linux computer but it said that "system" was undeclared. By the way what is "system"?
It might be talking about the system ("COLOR") or ("TITLE") cause that information for the layout of the command prompt.
OK i fixed all problems with this code except the options of re starting the program or exiting out. here is the full code.

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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
// experience points calculator

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

int main ()
{
    system ("TITLE Experience Points Calc.");
    system ("COLOR A");
    
    signed long int 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
    signed long int 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;
    
}
    
    
I've maked a few modifications:
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#include <iostream>
#include <string>
using namespace std;

bool UnBlock ()
{
	if (cin.fail())
	{
		cin.clear();
		cin.ignore(100, '\n');
		return true;
	}
	return false;
}

int main ()
{
    system ("TITLE Experience Points Calc.");
    system ("COLOR NeedXP");
    
    signed long int 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
    signed long int 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;
//.....
}
can you please email me about these code changes you made? I'd like to learn how to incorperate them into my programs. [email protected]
Topic archived. No new replies allowed.