C++ Programming Code Examples
C++ > File Manipulation Code Examples
Astrology - This code is used to determine certain information
/* Astrology - This code is used to determine certain information
by giving the necessary data */
#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <DOS.h>
int menu();
int year, month, day;
void main()
{
start:
clrscr();
printf("
Enter your year of birth(1970-2005):");
scanf("%d",&year);
if (year<1970||year>2005)
goto start;
skip:
printf("
Enter Your Month of birth(1-12):");
scanf("%d",&month);
if (month==1)
{
back:
printf("
Enter Day (1-31):");
scanf("%d",&day);
if (day<=19)
{
clrscr();
printf("%d-%d-%d",month,day,year);
menu();
printf("
You are born on the month of JANUARY.");
printf("
Your BirthStone is GARNET.");
printf("
You are a CAPRICORN.");
}
else if (day>19&&day<=31)
{
clrscr();
printf("%d%d%d",month,day,year);
menu();
printf("
You are born on the month of JANUARY.");
printf("
Your BirthStone is GARNET.");
printf("
You are an AQUARIUS.");
}
else
{printf("
Enter Correct Day!!!!!!");
goto back;
}
}
else if (month==2)
{
back1:
printf("
Enter Day (1-29):");
scanf("%d",&day);
if (day<=18)
{
clrscr();
printf("%d-%d-%d",month,day,year);
menu();
printf("
You are born on the month of FEBRUARY.");
printf("
Your BirthStone is AMETHYST.");
printf("
You are an AQUARIUS.");
}
else if (day>18&&day<=29)
{
clrscr();
printf("%d-%d-%d",month,day,year);
menu();
printf("
You are born on the month of FEBRUARY.");
printf("
Your BirthStone is AMETHYST.");
printf("
You are a PISCES.");
}
else
{printf("
Enter Correct Day!!!!!!");
goto back1;}
}
else if (month==3)
{
back2:
printf("
Enter Day (1-31):");
scanf("%d",&day);
if (day<=20)
{
clrscr();
printf("%d-%d-%d",month,day,year);
menu();
printf("
You are born on the month of MARCH.");
printf("
Your BirthStone is AQUAMARINE or BLOODSTONE.");
printf("
You are a PISCES.");
}
else if (day>20&&day<=31)
{
clrscr();
printf("%d-%d-%d",month,day,year);
menu();
printf("
You are born on the month of MARCH.");
printf("
Your BirthStone is AQUAMARINE or BLOODSTONE.");
printf("
You are an ARIES.");
}
else
{printf("
Enter Correct Day!!!!!!");
goto back2;}
}
else if (month==4)
{
back3:
printf("
Enter Day (1-30):");
scanf("%d",&day);
if (day<=19)
{
clrscr();
printf("%d-%d-%d",month,day,year);
menu();
printf("
You are born on the month of APRIL.");
printf("
Your BirthStone is DIAMOND.");
printf("
You are an ARIES.");
}
else if (day>19&&day<=30)
{
clrscr();
printf("%d-%d-%d",month,day,year);
menu();
printf("
You are born on the month of APRIL.");
printf("
Your BirthStone is DIAMOND.");
printf("
You are a TAURUS.");
}
else
{printf("
Enter Correct Day!!!!!!");
goto back3;}
}
else if (month==5)
{
back4:
printf("
Enter Day (1-31):");
scanf("%d",&day);
if (day<=20)
{
clrscr();
printf("%d-%d-%d",month,day,year);
menu();
printf("
You are born on the month of MAY.");
printf("
Your BirthStone is EMERALD.");
printf("
You are a TAURUS.");
}
else if (day>20&&day<=31)
{
clrscr();
printf("%d-%d-%d",month,day,year);
menu();
printf("
You are born on the month of MAY.");
printf("
Your BirthStone is EMERALD.");
printf("
You are a GEMINI.");
}
else
{printf("
Enter Correct Day!!!!!!");
goto back4;}
}
else if (month==6)
{
back5:
printf("
Enter Day (1-30):");
scanf("%d",&day);
if (day<=20)
{
clrscr();
printf("%d-%d-%d",month,day,year);
menu();
printf("
You are born on the month of JUNE.");
printf("
Your BirthStone is PEARL, ALEXANDRITE or MOONSTONE.");
printf("
You are a GEMINI.");
}
else if (day>20&&day<=30)
{
clrscr();
printf("%d-%d-%d",month,day,year);
menu();
printf("
You are born on the month of JUNE.");
printf("
Your BirthStone is PEARL, ALEXANDRITE or MOONSTONE.");
printf("
You are a CANCER.");
}
else
{printf("
Enter Correct Day!!!!!!");
goto back5;}
}
else if (month==7)
{
back6:
printf("
Enter Day (1-31):");
scanf("%d",&day);
if (day<=22)
{
clrscr();
printf("%d-%d-%d",month,day,year);
menu();
printf("
You are born on the month of JULY.");
printf("
Your BirthStone is RUBY.");
printf("
You are a CANCER.");
}
else if (day>22&&day<=30)
{
clrscr();
printf("%d-%d-%d",month,day,year);
menu();
printf("
You are born on the month of JULY.");
printf("
Your BirthStone is RUBY.");
printf("
You are a LEO.");
}
else
{printf("
Enter Correct Day!!!!!!");
goto back6;}
}
else if (month==8)
{
back7:
printf("
Enter Day (1-31):");
scanf("%d",&day);
if (day<=22)
{
clrscr();
printf("%d-%d-%d",month,day,year);
menu();
printf("
You are born on the month of AUGUST.");
printf("
Your BirthStone is PERIDOT or SARDONYX.");
printf("
You are a LEO.");
}
else if (day>22&&day<=31)
{
clrscr();
printf("%d-%d%-d",month,day,year);
menu();
printf("
You are born on the month of AUGUST.");
printf("
Your BirthStone is PERIDOT or SARDONYX.");
printf("
You are a VIRGO.");
}
else
{printf("
Enter Correct Day!!!!!!");
goto back7;}
}
else if (month==9)
{
back8:
printf("
Enter Day (1-30):");
scanf("%d",&day);
if (day<=22)
{
clrscr();
printf("%d-%d-%d",month,day,year);
menu();
printf("
You are born on the month of SEPTEMBER.");
printf("
Your BirthStone is SAPPHIRE.");
printf("
You are a VIRGO.");
}
else if (day>22&&day<=30)
{
clrscr();
printf("%d-%d-%d",month,day,year);
menu();
printf("
You are born on the month of SEPTEMBER.");
printf("
Your BirthStone is SAPPHIRE.");
printf("
You are a LIBRA.");
}
else
{printf("
Enter Correct Day!!!!!!");
goto back8;}
}
else if (month==10)
{
back9:
printf("
Enter Day (1-31):");
scanf("%d",&day);
if (day<=22)
{
clrscr();
printf("%d-%d-%d",month,day,year);
menu();
printf("
You are born on the month of OCTOBER.");
printf("
Your BirthStone is OPAL or TOURMALINE.");
printf("
You are a LIBRA.");
}
else if (day>22&&day<=31)
{
clrscr();
printf("%d-%d-%d",month,day,year);
menu();
printf("
You are born on the month of OCTOBER.");
printf("
Your BirthStone is OPAL or TOURMALINE.");
printf("
You are a SCORPIO.");
}
else
{printf("
Enter Correct Day!!!!!!");
goto back9;}
}
else if (month==11)
{
back10:
printf("
Enter Day (1-30):");
scanf("%d",&day);
if (day<=21)
{
clrscr();
printf("%d-%d-%d",month,day,year);
menu();
printf("
You are born on the month of NOVEMBER.");
printf("
Your BirthStone is TOPAZ.");
printf("
You are a SCORPIO.");
}
else if (day>21&&day<=30)
{
clrscr();
printf("%d-%d-%d",month,day,year);
menu();
printf("
You are born on the month of NOVEMBER.");
printf("
Your BirthStone is TOPAZ.");
printf("
You are a SAGITARIUS.");
}
else
{printf("
Enter Correct Day!!!!!!");
goto back10;}
}
else if (month==12)
{
back11:
printf("
Enter Day (1-31):");
scanf("%d",&day);
if (day<=21)
{
clrscr();
printf("%d-%d-%d",month,day,year);
menu();
printf("
You are born on the month of DECEMBER.");
printf("
Your BirthStone is TURQUOISE OR ZIRCON");
printf("
You are a SAGITARIUS.");
}
else if (day>21&&day<=31)
{
clrscr();
printf("%d-%d-%d",month,day,year);
menu();
printf("
You are born on the month of DECEMBER.");
printf("
Your BirthStone is TURQUOISE OR ZIRCON.");
printf("
You are a CAPRICORN.");
}
else
{printf("
Enter Correct Day!!!!!!");
goto back11;}
}
else
{printf("
Enter Correct No.!!!!!!");
goto skip;}
getch();
}
int menu()
{
if (year==1992||year==1980||year==2004)
printf("
You are born on the Year of the MONKEY.");
else if (year==1993||year==1981||year==2005)
printf("
You are born on the Year of the ROOSTER.");
else if (year==1994||year==1982||year==1970)
printf("
You are born on the Year of the DOG");
else if (year==1995||year==1971||year==1983)
printf("
You are born on the Year of the BOAR.");
else if (year==1996||year==1984||year==1972)
printf("
You are born on the Year of the RAT.");
else if (year==1997||year==1985||year==1973)
printf("
You are born on the Year of the OX.");
else if (year==1998||year==1986||year==1974)
printf("
You are born on the Year of the TIGER.");
else if (year==1975||year==1999||year==1987)
printf("
You are born on the Year of the RABBIT.");
else if (year==1976||year==2000||year==1988)
printf("
You are born on the Year of the DRAGON.");
else if (year==1977||year==2001||year==1989)
printf("
You are born on the Year of the SNAKE.");
else if (year==2002||year==1978||year==1990)
printf("
You are born on the Year of the HORSE.");
else if (year==2003||year==1979||year==1991)
printf("
You are born on the Year of the RAM.");
getch();
}
A program shall contain a global function named main, which is the designated start of the program in hosted environment. main() function is the entry point of any C++ program. It is the point at which execution of program is started. When a C++ program is executed, the execution control goes directly to the main() function. Every C++ program have a main() function.
The if...else statement executes two different codes depending upon whether the test expression is true or false. Sometimes, a choice has to be made from more than 2 possibilities. The if...else ladder allows you to check between multiple test expressions and execute different statements. In C/C++ if-else-if ladder helps user decide from among multiple options. The C/C++ if statements are executed from the top down. As soon as one of the conditions controlling the if is true, the statement associated with that if is executed, and the rest of the C else-if ladder is bypassed. If none of the conditions is true, then the final else statement will be executed.
It is a predefined function in "conio.h" (console input output header file) used to clear the console screen. It is a predefined function, by using this function we can clear the data from console (Monitor). Using of clrscr() is always optional but it should be place after variable or function declaration only. It is often used at the beginning of the program (mostly after variable declaration but not necessarily) so that the console is clear for our output.
Logical Operators are used to compare and connect two or more expressions or variables, such that the value of the expression is completely dependent on the original expression or value or variable. We use logical operators to check whether an expression is true or false. If the expression is true, it returns 1 whereas if the expression is false, it returns 0. Assume variable A holds 1 and variable B holds 0:
In computer programming, we use the if statement to run a block code only when a certain condition is met. An if statement can be followed by an optional else statement, which executes when the boolean expression is false. There are three forms of if...else statements in C++: • if statement, • if...else statement, • if...else if...else statement, The if statement evaluates the condition inside the parentheses ( ). If the condition evaluates to true, the code inside the body of if is executed. If the condition evaluates to false, the code inside the body of if is skipped.
#include is a way of including a standard or user-defined file in the program and is mostly written at the beginning of any C/C++ program. This directive is read by the preprocessor and orders it to insert the content of a user-defined or system header file into the following program. These files are mainly imported from an outside source into the current program. The process of importing such files that might be system-defined or user-defined is known as File Inclusion. This type of preprocessor directive tells the compiler to include a file in the source code program.
In C++, goto is a jump statement and sometimes also referred as unconditional jump statement. It can be used to jump from goto to a labeled statement within the same function. The target label must be within the same file and context. Please note that the use of goto statement is highly discouraged in any programming language because it makes difficult to trace the control flow of a program, making hard to understand and modify the program.
The getch() is a predefined non-standard function that is defined in conio.h header file. It is mostly used by the Dev C/C++, MS- DOS's compilers like Turbo C to hold the screen until the user passes a single value to exit from the console screen. It can also be used to read a single byte character or string from the keyboard and then print. It does not hold any parameters. It has no buffer area to store the input character in a program. The getch() function does not accept any parameter from the user. It returns the ASCII value of the key pressed by the user as an input.
In C++ programming we are using the iostream standard library, it provides cin and cout methods for reading from input and writing to output respectively. To read and write from a file we are using the standard C++ library called fstream. Let us see the data types define in fstream library is: • ofstream: This data type represents the output file stream and is used to create files and to write information to files. • ifstream: This data type represents the input file stream and is used to read information from files. • fstream: This data type represents the file stream generally, and has the capabilities of both ofstream and ifstream which means it can create files, write information to files, and read information from files.
C++ Language program sample which passes a "Reference" to a Function and Function has a 'pointer' as argument. Keep the return type void & display result in main body. Apply any
C++ Program demonstrates implementation of "Multiset in STL". "Insert element" into the multiset. "Delete element" from the multiset. "Find element" in a multiset. Count elements
First Input the length of a side of the triangle. Then Input the 'length of another side' of the triangle, then input the angle between these sides of the triangle. The area of the Scalene
Example of using this 'pointer' is to return the "Reference of current object" so that you can chain "function calls", this way you can call all the functions for the current object in one go.