Jump to content

Home

Computer Science / Programming Thread


BongoBob

Recommended Posts

EDIT: I'm going to be nothing for an hour and a half later today during school, and was wondering if anyone had an idea for a program that I could do. To know what I can do, I've completed everything up to strings on the cprogramming.com site.

You could try XML parsing or exploring the DirectX SDK, there'll never be a shortage of tutorials and/or sample code on either of those topics.

Link to comment
Share on other sites

Well the computers at my school were down when I had the free time. Oh well, gotta clean the house up a little bit and then maybe I'll figure something out.

 

EDIT: I ended up just making my program loop, but I compiled it on Windows and uploaded it.

 

http://www.geocities.com/bongobob7686/circleprogram.zip (right click save as)

 

And as for the source...

#include <iostream>

using namespace std;

int mainmenu();			// Main menu of the program
int cmenu();			// Menu for the circumference choice
int amenu();			// Menu for the area choice
int cmchoice1();		// Finds the circumference given the diameter
int cmchoice2();		// Finds the circumference given the radius
int amchoice1();		// Finds the area given the radius
int amchoice2();		// Finds the area given the diameter
int mmenuinpt;			// Directs the user to the selected part of the program
int cmenuinpt;			// Directs the user to the selected part of the program
int amenuinpt;			// Directs the user to the selected part of the program
int exitmsg();			// Message displayed upon exit
float pi = 3.14;		// Tells the program that Pi equals 3.14
float r;			// Variable for radius
float d;			// Variable for diameter
int cform();			// Formula to find the circumference of a circle
int aform();			// Formula to find the area of a circle
int rfromd();			// Derives a radius from the diameter
int dfromr();			// Derives a diameter from the radius
float c;			// Variable for circumference
float a;			// Variable for the true area
int errormsg();			// Message displayed upon a user error
char stayleave;			// Variable for looping the program.


int main()
{
do {
mainmenu();
if ( mmenuinpt == 1 ) {
	cmenu();
	if ( cmenuinpt == 1 ) {
		cmchoice1();
	}
	else if ( cmenuinpt == 2 ) {
		cmchoice2();
	}
	else {
		errormsg();
	}
}
else if ( mmenuinpt == 2 ) {
	amenu();
	if ( amenuinpt == 1 ) {
		amchoice1();
	}
	else if ( amenuinpt == 2 ) {
		amchoice2();
	}
	else {
		errormsg();
	}
}
else if ( mmenuinpt == 3 ) {
	stayleave = 101;
}
else {
	errormsg();
}
} while ( stayleave != 101 && stayleave != 69 );
exitmsg();
}



int mainmenu()			// Main menu of the program
{
cout<<"1. Find the circumference of a circle\n";
cout<<"2. Find the area of a circle\n";
cout<<"3. Exit\n";
cout<<"Please enter your choice: ";
cin>> mmenuinpt;
cin.ignore();
}

int cmenu()			// Menu for the circumference choice
{
cout<<"\nWhich do you know about the circle, (1) the diameter, or (2) the radius: ";
cin>> cmenuinpt;
cin.ignore();
}

int cmchoice1()			// Finds the circumference given the radius
{
cout<<"\nPlease enter the diameter of the circle: ";
cin>> d;
cin.ignore();
cform();
cout<<"\nThe circumference of the circle is "<< c <<"\n";
cout<<"Enter m to return to the main menu, or enter e to exit: ";
cin>> stayleave;
cin.ignore();
}

int cmchoice2()			// Finds the circumference given the diameter
{
cout<<"\nPlease enter the radius of the circle: ";
cin>> r;
cin.ignore();
dfromr();
cform();
cout<<"\nThe circumference of the circle is "<< c <<"\n";
cout<<"Enter m to return to the main menu, or enter e to exit: ";
cin>> stayleave;
cin.ignore();
}

int amenu()			// Menu for the area choice
{
cout<<"\nWhich do you know about the circle, (1) the radius, or (2) the diameter: ";
cin>> amenuinpt;
cin.ignore();
}

int amchoice1()			// Finds the area given the diameter
{
cout<<"\nPlease enter the radius of the circle: ";
cin>> r;
cin.ignore();
aform();
cout<<"\nThe area of the circle is "<< a <<"\n";
cout<<"Enter m to return to the main menu, or enter e to exit: ";
cin>> stayleave;
cin.ignore();
}

int amchoice2()			// Finds the area given the radius
{
cout<<"\nPlease enter the diameter of the circle: ";
cin>> d;
cin.ignore();
rfromd();
aform();
cout<<"\nThe area of the circle is "<< a <<"\n";
cout<<"Enter m to return to the main menu, or enter e to exit: ";
cin>> stayleave;
cin.ignore();
}

int exitmsg()			// Message displayed upon exit
{
cout<<"\nThank you for using this program.\n";
cout<<"Press enter to exit.\n";
cin.get();
}

int rfromd()			// Derives a radius from the diameter
{
r = d / 2;
}

int dfromr()			// Derives a diameter from the radius
{
d = r * 2;
}

int cform()			// Formula to find the circumference of a circle
{
c = d * pi;
}

int aform()			// Formula to find the area of a circle
{
a = r * r;
a = a * pi;
}

int errormsg()
{
cout<<"\nError.\n";
char stayleave = 101;
}

 

Let me know what you think/if you have problems with it :)

Link to comment
Share on other sites

Well the computers at my school were down when I had the free time. Oh well, gotta clean the house up a little bit and then maybe I'll figure something out.

 

EDIT: I ended up just making my program loop, but I compiled it on Windows and uploaded it.

 

http://www.geocities.com/bongobob7686/circleprogram.zip (right click save as)

 

Let me know what you think/if you have problems with it :)

Hell yah, that stuff looks like fun, I might want to check out that site you're using some time. :p

Link to comment
Share on other sites

I took up one of the challenges on cprogramming.com, and made a little calculator program now in my first period. I havn't been able to compile it (why isn't there an online C++ compiler : | ), but I'm confident that it will compile and work pretty nicely.

 

#include <iostream>

using namespace std;

int add ( int x, int y )
{
result = x + y;
return result;
}

int subtract ( int x, int y )
{
result = x - y;
return result;
}

int multiply ( int x, int y )
{
result = x * y;
return result;
}

int divide ( int x, int y)
{
result = x * y;
return result;
}

void numinput()
{
cout<<"Enter the first number: ";
cin>> x;
cin.ignore();
cout<<"Now enter the second number: ";
cin>> y;
cin.ignore();
}

void exitmsg();
{
cout<<"Thank you for using this program.\n";
cout<<"Press enter to exit\n";
cin.get();
}

int main()
{
int x;
int y;
char choice;

do {
	cout<<"What would you like to do?\n";
	cout<<"[A]dd	       [s]ubtract\n"
	cout<<"[M]ultiply      [D]ivide\n";
	cout<<"          [E]xit\n";
	cin>> choice;
	cin.ignore();
	switch (choice) {
		case 65:
			numinput();
			cout<< x << " + " << y << " = " << add ( x, y );
			cout<<"\n";
			break;
		case 83:
			numinput();
			cout<< x << " - " << y << " = " << subtract ( x, y );
			cout<<"\n";
			break;
		case 77:
			numinput();
			cout<< x << " * " << y << " = " << multiply ( x, y );
			cout<<"\n";
			break;
		case 68:
			numinput();
			cout<< x << " / " << y << " = " << divide ( x, y );
			cout<<"\n";
			break;
		case 69:
			exitmsg();
			break;
		default:
			cout<<"Error, try again";
			break;
	}
} while ( choice != 69 );
}

 

If someone wants to compile this and see if it works, go right ahead :)

Link to comment
Share on other sites

Suggestions: I didn't try compiling the code, though I doubt it will work the way you're hoping it will.

 

numinput();
cout<< x << " + " << y << " = " << add ( x, y );
cout<<"\n";

 

Dollars to doughnuts x and y will be garbage values, and your math functions will return garbage values.

 

In fact, I'd be willing to bet you won't get it to compile, and the error lies in your numinput function. You should try to figure out what the problem is yourself, but I will tell you that it's a problem.

Link to comment
Share on other sites

Thanks, I'll try compiling it after I eat :)

 

EDIT: I realized I messed up some stuff, so I fixed it and everything compiled smoothly :)

 

Here's the updated source code:

 

#include <iostream>

using namespace std;

int x;
int y;
float result;

int add ( int x, int y );
int subtract ( int x, int y );
int multiply ( int x, int y );
int divide ( int x, int y);
void numinput();
void exitmsg();
void resetinpt();

int main()
{
char choice;

do {
       cout<<"What would you like to do?\n";
	cout<<"[A]dd	       [s]ubtract\n";
	cout<<"[M]ultiply      [D]ivide\n";
	cout<<"          [E]xit\n";
	cin>> choice;
	cin.ignore(); 
       cout<<"\n";
	switch (choice) {
		case 65:
			numinput();
			cout<< x << " + " << y << " = " << add ( x, y );
			break;
		  case 83:
			numinput();
	    	cout<< x << " - " << y << " = " << subtract ( x, y ) << "\n";
			break;
		  case 77:
			numinput();
			cout<< x << " * " << y << " = " << multiply ( x, y );
			break;
		  case 68:
			numinput();
			cout<< x << " / " << y << " = " << divide ( x, y );
			cout<<"\n";
			break;
		case 69:
			exitmsg();
			break;
		default:
			cout<<"Error, try again";
			break;
	}
	resetinpt();
} while ( choice != 69 );
}

int add ( int x, int y )
{
return x + y;
}

int subtract ( int x, int y )
{
   return x - y;
}

int multiply ( int x, int y )
{
   return x * y;
}

int divide ( int x, int y )
{
   return x / y;
}

void numinput()
{
    cout<<"Please enter the first number: ";
    cin>> x;
    cin.ignore();
    cout<<"Please enter the second number: ";
    cin>> y;
    cin.ignore();
}

void resetinpt()
{
    x = 0;
    y = 0;
}

void exitmsg()
{
    cout<<"Thanks for using this program.\n";
    cout<<"Press enter to exit.\n";
    cin.get();
}

Link to comment
Share on other sites

Global variables make me die a little inside.

 

Also, your math functions are entirely superfluous. why not just

cout<< x << " + " << y << " = " << x+y;

for the addition, etc.

 

Aaand furthermore, are you using the ascii numerical values for the character in your switch statement? I would suggest perhaps

 

switch(toupper(choice))
{
   case 'A':
   etc. etc.
}

Link to comment
Share on other sites

Global variables make me die a little inside.

 

Also, your math functions are entirely superfluous. why not just

cout<< x << " + " << y << " = " << x+y;

for the addition, etc.

 

I was just messin around with functions. I know I could just do that, but I didn't wanna :p

 

Aaand furthermore, are you using the ascii numerical values for the character in your switch statement? I would suggest perhaps

 

switch(toupper(choice))
{
   case 'A':
   etc. etc.
}

 

What's toupper? I havn't learned about that yet :confused:

 

Btw, I'm goin to borders to get a C++ book today :D

Link to comment
Share on other sites

toupper is a nifty function that takes the input (not entirely sure if it only takes a char, or if it can take a char*, or if it can take strings aswell...I'd have to look it up)...anywho, takes that input and if it's already an upercase character it does nothing, but if it's lowercase it returns the same character only capitalized. If you want a user to be able to input 'a' or 'A' and get the same functionality, toupper is prettttty useful.

 

I recommend eliminating your global variables, it'd give you practice with pass-by-reference, and global variables are ugly.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...