Post your C/C++ Programs Here

Status
Not open for further replies.

Gigacore

Dreamweaver
Hi guys n gals,

If you are a good at C/C++ Programming or if you are a programmer or just know this language then post you Programs here. By this way it helps learners a lot. Members can post their programs and get suggestions if there is anything wrong it......

Use this Pattern:

A Program to find the largest of three numbers using nested if

Code:
#include <stdio.h>
main()
{    
    int    a,b,c,big;
    printf("Enter Three numbers");
    scanf("%d%d%d",&a,&b,&c);
    if(a>b)
        if(a>c)
            big=a;
        else
            big=c;
    else
        if(b>c)
            big=b;
        else
            big=c;
    printf("Largest of %d,%d and %d = %d",a,b,c,big);
return 0;
}
==================================================
List of all the C Programs posted in this thread..
==================================================

● A Program to find the largest of three numbers using nested if.
● A program to findcalc area, surface area, volume, total surface area etc. of numerous 2D and 3D figures using functions.
● A Program to perform basic calculations.
● Program To Reverse a Number
● What is this?
● A Program that Binary, Decimal, Octal Inter-Conversions
● A bulls and cows game
● A program that selects a number which is of 4 digits and all are unique numbers.
● Write a C Program to sort numbers of an Array using Bubble Sort Method.
● Reusable java class to send email through pop3
● Simple Switch-Case Example.
● Swapping of two numbers without a Temporary variable in 1 line.
● A program that creates a vB list out of a file given in a particular format.
● Digit Forum URL Bulleted List Generator
● Write a C program to find an element from an Array using Binary Search Method.
● TOWER OF HANOI Simulator
● A Simple C Program to read and print a one-dimensional array
● C Program to find the largest element in an array and the position of its occurrence.
● A C program to find the maximum and minimum elements in an array having N elements
● Program which compares two strings, concatenate them and then gives the length of string.
● Program to kill a software /demo version expire in C
● Program To arrange numbers in Pyramid Pattern
● Program: a simple shop using string, integers, float, & algebraic operators
● Character Eater
● Program to insert a node in a single linked list.
● A C Program to convert decimal number to its Binary Number Equivalent
● Introduction to C++ - Stanford Video Tutorials and Other Lectures (Not a program)
● A Program to remind that u r using other account
● Typedef Example Program
● A C Program to check whether a given word is Palindrome or not.
● A Simple C Program to Display the number and its square from 1 to 10 using register variable.
● A C Program to find the sum of two matrix using two dimensional array.
TURBO CALC 15000
● A Command Line Calculator.
● Function 2 input a matrix
● Read an int & an int array & search for that int in the int array & return its position.
● Python version of the above program.
● Program to perform operations on a binary search tree.
● Program to calculate the computer sales man salary. (Correction)
● Basic Inventory tracker software program code.
● Sudoku
● Simple DOS Text-based alien shooter game
● A program for unit conversions.

===========================================
Last Update: 12 August 2008
--------------------------------------------------------

Some good free C/C++ Compilers


● Relo
● Bloodshed Dev C++
● GCC
● Borland C++ 5.5
● MinGW
● Open Watcom
 
Last edited by a moderator:

xbonez

NP : Crysis
Re: Post ur C/C++ Programs Here

here's a prog to calc area, surface area, volume, total surface area etc. of numerous 2D and 3D figures using functions

i had made this one for a friend - it was his project

Code:
#include<iostream.h>
#include<conio.h>
#include<math.h>

void line(void);
void cuboid(void);
void cube(void);
void cylinder(void);
void cone(void);
void sphere(void);
void hemisphere(void);
void triangle(void);
void rectangle(void);
void square(void);
void circle(void);
void parallelogram(void);

void main()
{
	clrscr();
	int o1,o2;

	menu:
	do{
		clrscr();
		cout<<"\n\n1. 3-D Figures"
		    <<"\n\n2. Plane Figures"
		    <<"\n\n3. Exit"
		    <<"\n\nEnter your choice -->\t";
		cin>>o1;

		switch(o1)
		{
			case 1:         clrscr();
					cout<<"\n\n";
					line();
					cout<<"\n\t\t\t\t3-D Figures\n\n";
					line();
					cout<<"\n\n1. Cuboid"
					    <<"\n\n2. Cube"
					    <<"\n\n3. Cylinder"
					    <<"\n\n4. Cone"
					    <<"\n\n5. Sphere"
					    <<"\n\n6. Hemisphere"
					    <<"\n\n7. Main Menu"
					    <<"\n\nEnter your choice -->\t";
					    cin>>o2;

					    switch(o2)
					    {
						case 1:		clrscr();
								cout<<"\n\n";
								line();
								cout<<"\n\t\t\t\tCuboid\n\n";
								line();
								cuboid();
								getch();
								break;

						case 2:		clrscr();
								cout<<"\n\n";
								line();
								cout<<"\n\t\t\t\tCube\n\n";
								line();
								cube();
								getch();
								break;

						case 3:		clrscr();
								cout<<"\n\n";
								line();
								cout<<"\n\t\t\t\tCylinder\n\n";
								line();
								cylinder();
								getch();
								break;

						case 4:		clrscr();
								cout<<"\n\n";
								line();
								cout<<"\n\t\t\t\tCone\n\n";
								line();
								cone();
								getch();
								break;

						case 5:		clrscr();
								cout<<"\n\n";
								line();
								cout<<"\n\t\t\t\tSphere\n\n";
								line();
								sphere();
								getch();
								break;

						case 6:		clrscr();
								cout<<"\n\n";
								line();
								cout<<"\n\t\t\t\tHemisphere\n\n";
								line();
								hemisphere();
								getch();
								break;

						case 7:		goto menu;


						default:	cout<<"\n\nInvalid Input\a\a\a";
								goto menu;
					    }
					    getch();
					    break;

			case 2:		    clrscr();
					    cout<<"\n\n";
					    line();
					    cout<<"\n\t\t\t\tPlane Figures\n\n";
					    line();
					    cout<<"\n\n1. Traingle"
						<<"\n\n2. Rectangle"
						<<"\n\n3. Sqaure"
						<<"\n\n4. Circle"
						<<"\n\n5. Parallelogram"
						<<"\n\n6. Main Menu"
						<<"\n\nEnter your choice -->\t";
					    cin>>o2;

					    switch(o2)
					    {
							case 1:		clrscr();
									cout<<"\n\n";
									line();
									cout<<"\n\t\t\t\tTraingle\n\n";
									line();
									triangle();
									getch();
									break;

							case 2:		clrscr();
									cout<<"\n\n";
									line();
									cout<<"\n\t\t\t\tRectangle\n\n";
									line();
									rectangle();
									getch();
									break;

							case 3:        	clrscr();
									cout<<"\n\n";
									line();
									cout<<"\n\t\t\t\tSquare\n\n";
									line();
									square();
									getch();
									break;

							case 4:		clrscr();
									cout<<"\n\n";
									line();
									cout<<"\n\t\t\t\tCircle\n\n";
									line();
									circle();
									getch();
									break;

							case 5:		clrscr();
									cout<<"\n\n";
									line();
									cout<<"\n\t\t\t\tParallelogram\n\n";
									line();
									parallelogram();
									getch();
									break;

							case 6:		goto menu;


							default:	cout<<"\n\nInvalid Input\a\a\a";
									goto menu;
					    }

					    getch();
					    break;

			case 3:             cout<<"\n\nPress any key to exit...\a";
					    getch();
					    break;

			default:	    cout<<"\n\nInvalid Input\a\a\a\a";
					    goto menu;



		}

	}while(o1!=3);

}


void line()
{
	int i;
	for (i=0;i<80;i++)
		cout<<"*";
}

void cuboid()
{
	float l,b,h,a,v;
	cout<<"\n\nEnter length of cuboid -->\t";
	cin>>l;
	cout<<"\n\nEnter breadth of cuboid -->\t";
	cin>>b;
	cout<<"\n\nEnter height of cuboid -->\t";
	cin>>h;
	v=l*b*h;
	a=2*(l*b+b*h+l*h);
	cout<<"\n\nVolume of cuboid is "<<v;
	cout<<"\n\nSurface area of cuboid is "<<a;
}

void cube()
{
	float l,a,v;
	cout<<"\n\nEnter length of side of cube -->\t";
	cin>>l;
	v=l*l*l;
	a=6*l*l;
	cout<<"\n\nVolume of cube is "<<v;
	cout<<"\n\nSurface area of cube is "<<a;
}

void cylinder()
{
	float r,h,v,c,t;
	cout<<"\n\nEnter radius of cylinder -->\t";
	cin>>r;
	cout<<"\n\nEnter height of cylinder -->\t";
	cin>>h;
	v=3.14*r*r*h;
	c=2*3.14*r*h;
	t=3.14*r*(2*h+r);
	cout<<"\n\nVolume of cylinder is "<<v;
	cout<<"\n\nCurved Surface Area of cylinder is "<<c;
	cout<<"\n\nTotal Surface Area of cylinder is "<<t;
}

void cone()
{
	float r,h,l,v,c,t;
	cout<<"\n\nEnter radius of cone -->\t";
	cin>>r;
	cout<<"\n\nEnter height of cone -->\t";
	cin>>h;
	l=sqrt(r*r+h*h);
	v=(3.14*r*r*h)/3;
	c=3.14*r*l;
	t=3.14*r*(l+r);
	cout<<"\n\nSlant height of cone is "<<l;
	cout<<"\n\nVolume of cone is "<<v;
	cout<<"\n\nCurved Surface Area of cone is "<<c;
	cout<<"\n\nTotal Surface Area of cone is "<<t;
}

void sphere()
{
	float r,v,a;
	cout<<"\n\nEnter radius of sphere -->\t";
	cin>>r;
	v=(4*3.14*r*r*r)/3;
	a=4+3.14*r*r;
	cout<<"\n\nVolume of sphere is "<<v;
	cout<<"\n\nSurface Area of sphere is "<<a;
}

void hemisphere()
{
	float r,v,c,t;
	cout<<"\n\nEnter radius of hemisphere -->\t";
	cin>>r;
	v=(2*3.14*r*r*r)/3;
	c=2*3.14*r*r;
	t=3*3.14*r*r;
	cout<<"\n\nVolume of hemisphere is "<<v;
	cout<<"\n\nCurved Surface Area of hemisphere is "<<c;
	cout<<"\n\nTotal Surface Area of hemisphere is "<<t;
}

void triangle()
{
	float b,h,a;

	cout<<"\n\nEnter base of triangle -->\t";
	cin>>b;
	cout<<"\n\nEnter height of triangle -->\t";
	cin>>h;

	a=(b*h)/2;
	cout<<"\n\nArea of the triangle is "<<a;

}

void rectangle()
{
	float l,b,a,p;
	cout<<"\n\nEnter length of rectangle -->\t";
	cin>>l;
	cout<<"\n\nEnter breadth of rectangle -->\t";
	cin>>b;
	a=l*b;
	p=2*(l+b);
	cout<<"\n\nArea of rectangle is "<<a;
	cout<<"\n\nPerimeter of rectangle is "<<p;
}

void square()
{
	float s,a,p;
	cout<<"\n\nEnter length of side of square -->\t";
	cin>>s;
	a=s*s;
	p=4*s;
	cout<<"\n\nArea of square is "<<a;
	cout<<"\n\nPerimeter of square is "<<p;
}

void circle()
{
	float r,a,c;
	cout<<"\n\nEnter radius of circle -->\t";
	cin>>r;
	a=3.14*r*r;
	c=2*3.14*r;
	cout<<"\n\nArea of circle is "<<a;
	cout<<"\n\nCircumference of circle is "<<c;
}

void parallelogram()
{
	float b,h,a,p;
	cout<<"\n\nEnter base of parallelogram -->\t";
	cin>>b;
	cout<<"\n\nEnter height of parallelogram -->\t";
	cin>>h;
	a=b*h;
	p=2*(b+h);
	cout<<"\n\nArea of parallelogram is "<<a;
	cout<<"\n\nPerimeter of parallelogram is "<<p;
}

i've put up a lot more progs at my forum. classmates really find it useful since none of them is interested in computers. its so sad...
 

max_demon

IM AS MAD AS HELL!!
Re: Post ur C/C++ Programs Here

My Program to calculate Addition

Code:
#include <stdlib.h>
main()
{
float a,b,c,total,multiply,add,input;
printf("Welcome to Demon Calculator \n\n");
printf("Enter the values-\n\n");
printf("a:");
scanf("%f",&a);
printf("b:");
scanf("%f",&b);
printf("c:");
scanf("%f",&c);

total=a*b*c;
if (total>80000)
printf("Meri Marzi , tu khud solve kar.\a\a\a\a\a\a\a\a\a\n\n");
else
printf("The answer is %f\n\n\a\a",total);
system("pause");
}
 

Ron

||uLtiMaTE WinNER||
Re: Post ur C/C++ Programs Here

scanf("%d%d%d",&a,&b,&c);
New to c++.......Whta those this line means
 

Yamaraj

The Lord of Death
Re: Post ur C/C++ Programs Here

Intel_Gigacore said:
Code:
#include <stdio.h>
main()
{	
	int	a,b,c,big;
	printf("Enter Three numbers");
	scanf("%d%d%d",&a,&b,&c);
	if(a>b)
		if(a>c)
			big=a;
		else
			big=c;
	else
		if(b>c)
			big=b;
		else
			big=c;
	printf("Largest of %d,%d and %d = %d",a,b,c,big);
}
Your program lacks proper braces and a return statement.

xbonez said:
here's a prog to calc area, surface area, volume, total surface area etc. of numerous 2D and 3D figures using functions

i had made this one for a friend - it was his project

Code:
...

i've put up a lot more progs at my forum. classmates really find it useful since none of them is interested in computers. its so sad...
Your code doesn't conform to either ISO C or C++ standards.

Ron said:
scanf("%d%d%d",&a,&b,&c);
New to c++.......Whta those this line means
scanf() is a C input function, which is considered unsafe except for formatted data. I advise learning C first, before opting for a much larger, bloated and complex language as C++.
 
Last edited:

QwertyManiac

Commander in Chief
Re: Post ur C/C++ Programs Here

Yamaraj said:
Your code doesn't conform to either ISO C or C++ standards.

Don't be so hard on him :D He doesn't know TC++'s evils :D

Anyway, here's xbonez's program in proper code :)

Code:
#include<iostream>
#include<math.h>

using namespace std;

void line(void);
void cuboid(void);
void cube(void);
void cylinder(void);
void cone(void);
void sphere(void);
void hemisphere(void);
void triangle(void);
void rectangle(void);
void square(void);
void circle(void);
void parallelogram(void);

int main()
{ 
    int o1,o2;
    do{
         
        cout<<"\n\n1. 3-D Figures"
            <<"\n\n2. Plane Figures"
            <<"\n\n3. Exit"
            <<"\n\nEnter your choice -->\t";
        cin>>o1;

        switch(o1)
        {
            case 1:         cout<<"\n\n";
                    line();
                    cout<<"\n\t\t\t\t3-D Figures\n\n";
                    line();
                    cout<<"\n\n1. Cuboid"
                        <<"\n\n2. Cube"
                        <<"\n\n3. Cylinder"
                        <<"\n\n4. Cone"
                        <<"\n\n5. Sphere"
                        <<"\n\n6. Hemisphere"
                        <<"\n\n7. Main Menu"
                        <<"\n\nEnter your choice -->\t";
                        cin>>o2;
                        switch(o2)
                        {
                        case 1:        cout<<"\n\n";
                                line();
                                cout<<"\n\t\t\t\tCuboid\n\n";
                                line();
                                cuboid();
                                
                                break;

                        case 2:        cout<<"\n\n";
                                line();
                                cout<<"\n\t\t\t\tCube\n\n";
                                line();
                                cube();
                                
                                break;

                        case 3:        cout<<"\n\n";
                                line();
                                cout<<"\n\t\t\t\tCylinder\n\n";
                                line();
                                cylinder();
                                
                                break;

                        case 4:        cout<<"\n\n";
                                line();
                                cout<<"\n\t\t\t\tCone\n\n";
                                line();
                                cone();
                                break;

                        case 5:        cout<<"\n\n";
                                line();
                                cout<<"\n\t\t\t\tSphere\n\n";
                                line();
                                sphere();
                                break;

                        case 6:        cout<<"\n\n";
                                line();
                                cout<<"\n\t\t\t\tHemisphere\n\n";
                                line();
                                hemisphere();
                                break;

                        case 7:        break;


                        default:    cout<<"\n\nInvalid Input\a\a\a";
                        }
                        
                        break;

            case 2:            
                        cout<<"\n\n";
                        line();
                        cout<<"\n\t\t\t\tPlane Figures\n\n";
                        line();
                        cout<<"\n\n1. Traingle"
                        <<"\n\n2. Rectangle"
                        <<"\n\n3. Sqaure"
                        <<"\n\n4. Circle"
                        <<"\n\n5. Parallelogram"
                        <<"\n\n6. Main Menu"
                        <<"\n\nEnter your choice -->\t";
                        cin>>o2;

                        switch(o2)
                        {
                            case 1:        cout<<"\n\n";
                                    line();
                                    cout<<"\n\t\t\t\tTraingle\n\n";
                                    line();
                                    triangle();
                                    
                                    break;

                            case 2:        cout<<"\n\n";
                                    line();
                                    cout<<"\n\t\t\t\tRectangle\n\n";
                                    line();
                                    rectangle();
                                    
                                    break;

                            case 3:         cout<<"\n\n";
                                    line();
                                    cout<<"\n\t\t\t\tSquare\n\n";
                                    line();
                                    square();
                                    
                                    break;

                            case 4:        cout<<"\n\n";
                                    line();
                                    cout<<"\n\t\t\t\tCircle\n\n";
                                    line();
                                    circle();
                                    break;

                            case 5:        cout<<"\n\n";
                                    line();
                                    cout<<"\n\t\t\t\tParallelogram\n\n";
                                    line();
                                    parallelogram();
                                    break;

                            case 6:        break;


                            default:    cout<<"\n\nInvalid Input\a\a\a";
                        }

                        break;

            case 3:             cout<<"\n\nPress any key to exit...\a";
                        break;

            default:        cout<<"\n\nInvalid Input\a\a\a\a";



        }

    }while(o1!=3);
return 0;
}


void line()
{
    int i;
    for (i=0;i<80;i++)
        cout<<"*";
}

void cuboid()
{
    float l,b,h,a,v;
    cout<<"\n\nEnter length of cuboid -->\t";
    cin>>l;
    cout<<"\n\nEnter breadth of cuboid -->\t";
    cin>>b;
    cout<<"\n\nEnter height of cuboid -->\t";
    cin>>h;
    v=l*b*h;
    a=2*(l*b+b*h+l*h);
    cout<<"\n\nVolume of cuboid is "<<v;
    cout<<"\n\nSurface area of cuboid is "<<a;
}

void cube()
{
    float l,a,v;
    cout<<"\n\nEnter length of side of cube -->\t";
    cin>>l;
    v=l*l*l;
    a=6*l*l;
    cout<<"\n\nVolume of cube is "<<v;
    cout<<"\n\nSurface area of cube is "<<a;
}

void cylinder()
{
    float r,h,v,c,t;
    cout<<"\n\nEnter radius of cylinder -->\t";
    cin>>r;
    cout<<"\n\nEnter height of cylinder -->\t";
    cin>>h;
    v=3.14*r*r*h;
    c=2*3.14*r*h;
    t=3.14*r*(2*h+r);
    cout<<"\n\nVolume of cylinder is "<<v;
    cout<<"\n\nCurved Surface Area of cylinder is "<<c;
    cout<<"\n\nTotal Surface Area of cylinder is "<<t;
}

void cone()
{
    float r,h,l,v,c,t;
    cout<<"\n\nEnter radius of cone -->\t";
    cin>>r;
    cout<<"\n\nEnter height of cone -->\t";
    cin>>h;
    l=sqrt(r*r+h*h);
    v=(3.14*r*r*h)/3;
    c=3.14*r*l;
    t=3.14*r*(l+r);
    cout<<"\n\nSlant height of cone is "<<l;
    cout<<"\n\nVolume of cone is "<<v;
    cout<<"\n\nCurved Surface Area of cone is "<<c;
    cout<<"\n\nTotal Surface Area of cone is "<<t;
}

void sphere()
{
    float r,v,a;
    cout<<"\n\nEnter radius of sphere -->\t";
    cin>>r;
    v=(4*3.14*r*r*r)/3;
    a=4+3.14*r*r;
    cout<<"\n\nVolume of sphere is "<<v;
    cout<<"\n\nSurface Area of sphere is "<<a;
}

void hemisphere()
{
    float r,v,c,t;
    cout<<"\n\nEnter radius of hemisphere -->\t";
    cin>>r;
    v=(2*3.14*r*r*r)/3;
    c=2*3.14*r*r;
    t=3*3.14*r*r;
    cout<<"\n\nVolume of hemisphere is "<<v;
    cout<<"\n\nCurved Surface Area of hemisphere is "<<c;
    cout<<"\n\nTotal Surface Area of hemisphere is "<<t;
}

void triangle()
{
    float b,h,a;

    cout<<"\n\nEnter base of triangle -->\t";
    cin>>b;
    cout<<"\n\nEnter height of triangle -->\t";
    cin>>h;

    a=(b*h)/2;
    cout<<"\n\nArea of the triangle is "<<a;

}

void rectangle()
{
    float l,b,a,p;
    cout<<"\n\nEnter length of rectangle -->\t";
    cin>>l;
    cout<<"\n\nEnter breadth of rectangle -->\t";
    cin>>b;
    a=l*b;
    p=2*(l+b);
    cout<<"\n\nArea of rectangle is "<<a;
    cout<<"\n\nPerimeter of rectangle is "<<p;
}

void square()
{
    float s,a,p;
    cout<<"\n\nEnter length of side of square -->\t";
    cin>>s;
    a=s*s;
    p=4*s;
    cout<<"\n\nArea of square is "<<a;
    cout<<"\n\nPerimeter of square is "<<p;
}

void circle()
{
    float r,a,c;
    cout<<"\n\nEnter radius of circle -->\t";
    cin>>r;
    a=3.14*r*r;
    c=2*3.14*r;
    cout<<"\n\nArea of circle is "<<a;
    cout<<"\n\nCircumference of circle is "<<c;
}

void parallelogram()
{
    float b,h,a,p;
    cout<<"\n\nEnter base of parallelogram -->\t";
    cin>>b;
    cout<<"\n\nEnter height of parallelogram -->\t";
    cin>>h;
    a=b*h;
    p=2*(b+h);
    cout<<"\n\nArea of parallelogram is "<<a;
    cout<<"\n\nPerimeter of parallelogram is "<<p;
}
 

Yamaraj

The Lord of Death
Re: Post ur C/C++ Programs Here

Intel_Gigacore said:
Guys... is my program rite now?
Try this:
Code:
#include <stdio.h>

int main(void)
{	
   int a = 0,
       b = 0,
       c = 0,
       greatest = 0;

   printf("Enter three numbers:\n");
   scanf("%d %d %d", &a, &b, &c);

   greatest = a;
   if(b > greatest)
      greatest = b;
   if(c > greatest)
      greatest = c;

   printf("greatest of %d, %d and %d = %d", a, b, c, greatest);
   return 0;
}
 

utsav

damn busy...
Re: Post ur C/C++ Programs Here

this is a program in which u will hav 2 enter any 5 nos between 1 to 49 and if the number matches with the numbers chosen by the computer, u will win the lottery




#include <cstdlib>
#include <iostream>
using namespace std;
void lotto();
void calc(int * a, int * b);
void menu();
// Main function. Seeds random function
// and calls menu function.
int main()


{
srand((unsigned)time(0)); // Seed the random function......
menu();
}
// This function prompts the user to pic
// k five numbers between 1 and 49.
// The function makes sure that the user
// picks numbers between 1 and 49,
// and that the user does not choose dup
// licate numbers. Then the computer
// chooses five random numbers. Error co
// ntrol is used to check if the
// computer chooses the same number twic
// e. Then the numbers are displayed
// to the screen. Finally the calc funct
// ion is called to calculate and
// output the results of the game...
void lotto()


{
int i, k, j, b, x, y;// variables for all the for loops....
int s, t, user_error;
int user[5], comp[5];// arrays for user and computers numbers....
int user_error_check, comp_error_check; // to find if numbers are duplicates....
// Prompt user to choose numbers
cout << "Please choose five numbers from 0 to 49" << endl;
for (i=0; i<5; i++)


{
cout << "Enter number " << i + 1 << ":" ;
cin >> user;
if (user > 49 || user < 0)


{
cout << "You did not enter a number from 0 to 49" << endl;
i--;
}

}
// ERROR CONTROL, to make sure that the
// user does not choose the
// same number twice.................
for(s=0; s<5; s++)
for(t=0; t<s; t++)


{
user_error_check = user;
if(user_error_check == user[t])


{
cout << "You have chosen duplicate numbers, please choose another number: " << endl;
cin >> user_error;
user = user_error;
}
}

// Now the computer chooses numbers.....
// ........
for (k=0; k<5; k++)


{
comp[k] = (rand( ) % 49) + 1; // get comp to randomly generate 1 -49
}
// ERROR CONTROL, to make sure that the
// computer does not pick
// the same number twice.......

for(x=0; x<5; x++)
for(y=0; y<x; y++)


{
comp_error_check = comp[x];
if (comp_error_check == comp[y])


{
comp[x] = (rand() % 49) + 1;
}
} // no guarentee that the computer will not choose another number
// already in the array, but it's the be
// st i can do?????

cout << "You chose: " ;// output users numbers...
for (j=0; j<5; j++)


{
cout << user[j] << ", " ;
}

cout << endl;

cout << "The computer chose: "; // output computers numbers...
for (b=0; b<5; b++)


{
cout << comp << ", " ;
}
cout << endl << endl;

calc(user, comp);// Obtain the results of the game......
}
// This function compares the computers
// random numbers to the numbers
// chosen by the user and lets the user
// know how many (if any) they
// have matched..............
void calc(int * a, int * b)


{
int i, k, match =0;
for (i=0; i<5; i++)
for (k=0; k<5; k++)
if ( a == b[k] )


{
match += 1;
cout << "You have matched: " << a << endl;
}
if (match == 0)


{
cout << "Sorry you have not matched any of the computers numbers..."
<< endl << endl;
}
if (match == 5)


{
cout << "CONGRATULATIONS, YOU HAVE WON THE LOTTERY!!!!!!!" << endl << endl;
}
}
// This function informs the user how to
// play the game and then allows
// the user to play the game until they
// enter 'q' or 'Q' at the prompt.
void menu()


{
char choice;
cout << endl;
cout << "$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$" << endl;
cout << "$(C)$" << endl;
cout << "$ $" << endl;
cout << "$ *The computer will randomly choose five numbers $" << endl;
cout << "$ between 1 and 49.$" << endl;
cout << "$ $" << endl;
cout << "$ *You can choose five numbers and try to match$" << endl;
cout << "$ the computers numbers...... $" << endl;
cout << "$ $" << endl;
cout << "$ **Do not pick the same number twice or pick a$" << endl;
cout << "$number less than 1 or greater than 49, o.k.?$" << endl;
cout << "$ $" << endl;
cout << "$ $" << endl;
cout << "$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$" << endl;
cout << endl;
while (choice != 'q' || choice != 'Q')


{

cout << "Make a selection from the menu: " << endl;
cout << "[P]- Play the clotto against the computer." << endl;
cout << "[Q]- Exit this program." << endl;
cin >> choice;
switch(choice)


{
case 'p':
case 'P':
lotto();
break;
case 'q':
case 'Q':
cout << "No lotto for you!" << endl;
exit(0);
break;
}
}
}
 

ratedrsuperstar

The Sexy Beast
Re: Post ur C/C++ Programs Here

^^this is totally wrong what if b>c>gr8test there's no condition to test that

use this thread to request programs and then pm to the requester rather than posting so much code over here it really looks a mess and these are quite basic too
 

Yamaraj

The Lord of Death
Re: Post ur C/C++ Programs Here

ratedrsuperstar said:
^^this is totally wrong what if b>c>gr8test there's no condition to test that
Rules of programming:
1. Read the fscking code. And mine is pretty readable.
2. Test it before complaining.
3. If you still don't get it, it's time to buy a decent C book.
 

xbonez

NP : Crysis
Re: Post ur C/C++ Programs Here

@yamraj : what was wrong with my prog?? :( it seemed to run perfectly fine
 

Yamaraj

The Lord of Death
Re: Post ur C/C++ Programs Here

xbonez said:
@yamraj : what was wrong with my prog?? :( it seemed to run perfectly fine
Using implementation dependent libraries and functions like clrscr(), and the nonstandard "void main()" violates the rules of the ISO C++ standard. Your executable may run fine on a system, but your code needs more love.

I recommend a decent and recent C++ book, like C++: How To Program, C++ Primer, 4th/e or C++ Primer Plus.
 

utsav

damn busy...
Re: Post ur C/C++ Programs Here

@ gigacore it executed nicely on devc++. tell me the problm with my program
 

Sykora

I see right through you.
Re: Post ur C/C++ Programs Here

And for heaven's sake indent the code like the first post. It's unreadable otherwise.
 
Status
Not open for further replies.
Top Bottom