can you guys explain this c++ program line by line?

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
89
90
91
include<iostream.h>
#include<string.h>
void horoscope();
void bulan();
void main()

{
int month;
char horoscope[8]={'W','E','L','C','O','M','E…
char j;
for (j=0;j<8;j++)
{
cout<<horoscope[j];
}


bulan();
cout<<"\nPLEASE ENTER YOUR NUMBER IN THE MONTH OF BIRTH:\t";
cin>>month;
if (month==7)
{
cout<<"YOUR HOROSCOPE IS LEO\n";
}
if (month==12)
{
cout<<"YOUR HOROSCOPE IS CAPRICORN\n";
}


switch (month)
{
case 1:
cout<<"YOUR HOROSCOPE IS AQUARIUS.\n";
break;
case 2:
cout<<"YOUR HOROSCOPE IS PISCES.\n";
break;
case 3:
cout<<"YOUR HOROSCOPE IS ARIES.\n";
break;
case 4:
cout<<"YOUR HOROSCOPE IS TAURUS.\n";
break;
case 5:
cout<<"YOUR HOROSCOPE IS GEMINI.\n";
break;
case 6:
cout<<"YOUR HOROSCOPE IS CANCER.\n";
break;
case 7:
cout<<"WHICH IS SAME WITH THE ADMIN : AKMA NABIHA BINTI AHMAD\n";
break;
case 8:
cout<<"YOUR HOROSCOPE IS VIRGO.\n";
break;
case 9:
cout<<"YOUR HOROSCOPE IS LIBRA.\n";
break;
case 10:
cout<<"YOUR HOROSCOPE IS SCORPIO.\n";
break;
case 11:
cout<<"YOUR HOROSCOPE IS SAGITARRIUS.\n";
break;
case 12:
cout<<"WHICH IS SAME WITH THE ADMIN : AMIRUL FIKRI BIN AZMI.\n";
break;
default:
cout<<"INVALID INPUT";
}
}


void bulan (void)

{
cout<<"\nTHIS HOROSCOPES SYSTEM CAN ONLY BE USED BY DIP2B STUDENTS.\n";
cout<<"\nMENU\n";
cout<<"1.JAN 20-FEB 18\n";
cout<<"2.FEB 19-MAR 20\n";
cout<<"3.MAR 21-APR 19\n";
cout<<"4.APR 20-MAY 20\n";
cout<<"5.MAY 21-JUN 20\n";
cout<<"6.JUN 21-JUL 22\n";
cout<<"7.JUL 23-AUG 22\n";
cout<<"8.AUG 23-SEP 22\n";
cout<<"9.SEP 23-OCT 22\n";
cout<<"10.OCT 23-NOV 21\n";
cout<<"11.NOV 22-DEC 21\n";
cout<<"12.DEC 22-JAN 19\n";
} 
Sure, I can give it my best shot (allthough I don't know whether to be litral here or not, I'll do it both ways because I'm nice)

1
2
include<iostream.h>
#include<string.h> 


should be:
1
2
#include<iostream.h>
#include<string.h> 


which includes two files to your prject, the first file (iostream) is a simple (well not simple but simple for the end-user) I/O file (input/output) which allows the end user to get an input and to print things out. The next file gives acces to a string file, which is much like a char array (you can give it more than one character) but is much more powerfull because there are built-in functions that I won't get into right now, there are too many to mention)

Next you are declaring three functions called

1
2
3
void horoscope();
void bulan();
void main()


which should be

1
2
3
 void horoscope();
void bulan();
int main();


because main should always return an integer value.

Next is month being set up as an integer... Now this line is confusing me

char horoscope[8]={'W','E','L','C','O','M','E…

Because here you are obviously setting up a char array (why when you've got the string library included? the ideal code is:

string horoscope = "Welcome";

But that is not the most confusing part... you also don't close off the last letter which includes every thing else there (including the functions and stuff) into the array, did you mean to do this? I'm guessing not so I'm going to assume that you meant

char horoscope[8]={'W','E','L','C','O','M','E'}

Again this line confuses me because your entering a loop that doesn't need to be there see what it currently does is run through that line of code between the two {} 8 times and outputs each letter of the char array onto the screen.

Another sugegstion: change this from
1
2
3
4
for (j=0;j<8;j++)
{
cout<<horoscope[j];
}


to just

cout << horoscope;

Next your calling the bulan function which then moves into the bulan function, the bulan function just outputs all of those values onto the screen and then returns to the main function.

Now if you have lost track we are at the

cin>> month;

Which will ask the user to input a value into the month variable, after the user has entered their value it will test it against different values (the if test) now the next line:

1
2
3
4
if (month==7)
{
cout<<"YOUR HOROSCOPE IS LEO\n";
}


the code will ask itself "Does the variable month hold the value of 7?" If it does it will run the code inbetween the {} which outputs "Your horoscope is Leo" the same with the next one:

1
2
3
4
if (month==7)
{
cout<<"YOUR HOROSCOPE IS LEO\n";
}


which asks "Does the variable month hold the value of 12?" and if it does it will output "Your horoscope is Leo";

The "\n" gives the program a new line so it will output like this:

1
2
3
4
This is a new line
This is another new line 
Another
Another


"\n" basically presses the Return button for you.

BuThe switch statement I'm not going to go through line by line please find somewhere else that will exxplain that for you...

There will be a few errors here though because your code (wherever you got it from) is a big mess, it should look like this:

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
#include<iostream>
#include<string> // I think you only need the .h if your programming in C 
//but the iostream library is not available in that programming language it is only available in C++
using namespace std;
void bulan();

int main()
{
int month;
string horoscope = "Welcome";

cout << horoscope;

bulan();

cout<<"\nPLEASE ENTER YOUR NUMBER IN THE MONTH OF BIRTH:\t";

cin>>month;

switch (month)
{
case 1:
cout<<"YOUR HOROSCOPE IS AQUARIUS.\n";
break;
case 2:
cout<<"YOUR HOROSCOPE IS PISCES.\n";
break;
case 3:
cout<<"YOUR HOROSCOPE IS ARIES.\n";
break;
case 4:
cout<<"YOUR HOROSCOPE IS TAURUS.\n";
break;
case 5:
cout<<"YOUR HOROSCOPE IS GEMINI.\n";
break;
case 6:
cout<<"YOUR HOROSCOPE IS CANCER.\n";
break;
case 7:
cout<<"WHICH IS SAME WITH THE ADMIN : AKMA NABIHA BINTI AHMAD\n";
break;
case 8:
cout<<"YOUR HOROSCOPE IS VIRGO.\n";
break;
case 9:
cout<<"YOUR HOROSCOPE IS LIBRA.\n";
break;
case 10:
cout<<"YOUR HOROSCOPE IS SCORPIO.\n";
break;
case 11:
cout<<"YOUR HOROSCOPE IS SAGITARRIUS.\n";
break;
case 12:
cout<<"WHICH IS SAME WITH THE ADMIN : AMIRUL FIKRI BIN AZMI.\n";
break;
default:
cout<<"INVALID INPUT";
}
cin >> horoscope; // keeps the window open
return 0;
}


void bulan (void)
{
cout<<"\nTHIS HOROSCOPES SYSTEM CAN ONLY BE USED BY DIP2B STUDENTS.\n";
cout<<"\nMENU\n";
cout<<"1.JAN 20-FEB 18\n";
cout<<"2.FEB 19-MAR 20\n";
cout<<"3.MAR 21-APR 19\n";
cout<<"4.APR 20-MAY 20\n";
cout<<"5.MAY 21-JUN 20\n";
cout<<"6.JUN 21-JUL 22\n";
cout<<"7.JUL 23-AUG 22\n";
cout<<"8.AUG 23-SEP 22\n";
cout<<"9.SEP 23-OCT 22\n";
cout<<"10.OCT 23-NOV 21\n";
cout<<"11.NOV 22-DEC 21\n";
cout<<"12.DEC 22-JAN 19\n";
} 


Next time try and add comments by using the // operator (the compiler ignores everything after the // untill the next line starts

1
2
3
 int main(){ //doesn't read this part of the line
//starts again here
}


P.s. Please look for advice in the tutorial section http://www.cplusplus.com/doc/tutorial/program_structure/ this teaches you how to use the programming langauge

P.s.s. The experts on this forum will probably shout at me for producing this tutorial because it is simplistic and you should have done some research before asking this quesiton, but I did it because I'm nice (and I shouldn't be) but it will probably make some people angry, please study C++ (find some sources of information) before posting this type of subject because not many people will help you if you ask questions like this, your on a good tutorial site right now again please see the tutorial section:

http://www.cplusplus.com/doc/tutorial/program_structure/

which will give a rundown some of the main things you can do with C++

Last edited on
Just a small additional note, be sure to null terminate horoscope if you change the for loop to a cout.
thanks for clarifying. :)
It's all right, I just hope it works for you :)

Btw it would have been a quicker reply but my computer kept crashing so sorry about that haha
Topic archived. No new replies allowed.