Complicated Calculator

A simple calculator program I made hard by accident

  • (2 Pages)
  • +
  • 1
  • 2

24 Replies - 9605 Views - Last Post: 03 January 2011 - 09:10 PM Rate Topic: -----

#1 profezzer  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 27-December 10

Complicated Calculator

Posted 27 December 2010 - 11:03 PM

I tried to make a simple calculator but some how after 6 hours of programming and almost 600 lines of code later. This is what i have come up with. Im a noob and have only been programming about a week. All my code is original and my question is. Is there and easier way that i could have done this also being a noob what do you more advance programmer think of this code?

/*
  Name: Complicated Calculator
  Copyright: www.SuprheroHoodTec.com
  Author: Larry Lewis
  Date: 27/12/10 06:01
  Description: Overly Complicated Calculator.  My second program in C
*/

#include <stdio.h>
#include <stdlib.h>
#include <assert.h> 
#include <float.h>
#include <math.h>
#include <stdarg.h> 
#include <stdlib.h>
#include <ctype.h>
#include <limits.h> 
#include <setjmp.h>
#include <stddef.h>
#include <string.h>
#include <errno.h>
#include <locale.h>
#include <signal.h> 
#include <stdio.h>
#include <time.h>

//External Reference
extern int    add,precaladd ,subtract, precalsubtract, multiply,precalmultiply, divide,precaldivide, precal;

//External Function
int mainint;
int h;

//Internal Function
main()
{
	printf("Welcome to the Simple calulator\n\n");
	printf("Please make a selection\n");
	printf("1. Addition\n2. Subtraction\n3. Multiplication\n4. Division\n5. Exit\n\n");
	scanf("%d",&h);
		switch(h)
	          {
	               case 1:
	               precaladdfunc();
	               break;
				
				case 2:
				precalsubtractfunc();
				break;
				
				case 3:
				precalmultiplyfunc();
				break;

				case 4:
				precaldividefunc();
				break;

				case 5:
	               exit(1);
	               break;

				default:
				printf("You have not given a correct response\npress any key to continue\n\n");
				break;
	          }
	getchar();
	getchar();
}
//External Reference
extern int     mainint, subtract,precalsubtract, multiply,precalmultiply, divide, precaldivide, precaladd;

//Internal Reference

//Addition Controller
float a,b;
int c,d;
int add;

//Subtraction Controller
int g,h;
float e,f;
int subtract;

//Multiplication Controller
float i,j;
int k,l;
int multiply;

//Division controller
float m,n;
int o,p;
int divide;

//Addition Internal Functions
addfunc()
	{
	 	printf("Enter the First Number(s) ");
		scanf("%d",&c);
		getchar();
		
		printf("Enter the Second Number(s) ");
		scanf("%d",&d);
		getchar();

		printf("Your total is %d\n\n", c+d);

		printf("Would you like to ADD another set of numbers\n1. Yes\n2. No\n\n");
		scanf("%d",&add);
		switch(add)
		     {
			 case 1:
			 precaladdfunc();
			 break;

			 case 2:
			 getchar();
			 main();
			 break;
			 
			 default:
			 printf("You have entered an invalid response\nPress the 'Enter' key to return to the Main Menu.\n\n");
			 getchar();
			 getchar();
			 main();
			 break;
			}
		
	}


daddfunc()
	{
	 	printf("Enter the First Number(s) ");
		scanf("%f",&a);
		getchar();

		printf("Enter the Second Number(s) ");
		scanf("%f",&B)/>;
		getchar();

		printf("Your total is %f\n\n", a+B)/>;

		printf("Would you like to ADD another set of numbers\n1. Yes\n2. No\n\n");
		scanf("%d",&add);
		switch(add)
		     {
			 case 1:
			 precaladdfunc();
			 break;

			 case 2:
			 getchar();
			 main();
			 break;
			 
			 default:
			 printf("You have entered an invalid response.\nPress the 'Enter' key to return to the Main Menu.\n\n");
			 getchar();
			 getchar();
			 main();
			 break;
			}
		
	}

//Subtraction Internal Controller
dsubtractfunc()
		{
	 	printf("Enter the First Number(s) ");
		scanf("%f",&e);
		getchar();
		
		printf("Enter the Second Number(s) ");
		scanf("%f",&f);
		getchar();

		printf("Your total is %f\n\n", e-f);

		printf("Would you like to SUBTRACT another set of numbers\n1. Yes\n2. No\n\n");
		scanf("%d",&subtract);
		switch(subtract)
		     {
			 case 1:
			 precalsubtractfunc();
			 break;

			 case 2:
			 getchar();
			 main();
			 break;
			 
			 default:
			 printf("You have entered an invalid response\nPress the 'Enter' key to return to the Main Menu.\n\n");
			 getchar();
			 getchar();
			 main();
			 break;
			}
		
	}


subtractfunc()
	{
	 	printf("Enter the First Number(s) ");
		scanf("%d",&g);
		getchar();

		printf("Enter the Second Number(s) ");
		scanf("%d",&h);
		getchar();

		printf("Your total is %d \n\n", g-h);

		printf("Would you like to sUBTRACT another set of numbers\n1. Yes\n2. No \n\n");
		scanf("%d",&subtract);
		switch(subtract)
		     {
			 case 1:
			 precalsubtractfunc();
			 break;

			 case 2:
			 getchar();
			 main();
			 break;
			 
			 default:
			 printf("You have entered an invalid response.\nPress the 'Enter' key to return to the Main Menu.\n\n");
			 getchar();
			 getchar();
			 main();
			 break;
			}
}

//Multiplication Controller
dmultiplyfunc()
		{
	 	printf("Enter the First Number(s) ");
		scanf("%f",&i);
		getchar();
		
		printf("Enter the Second Number(s) ");
		scanf("%f",&j);
		getchar();

		printf("Your total is %f\n\n", i*j);

		printf("Would you like to MULTIPLY another set of numbers\n1. Yes\n2. No\n\n");
		scanf("%d",&multiply);
		switch(multiply)
		     {
			 case 1:
			 precalmultiplyfunc();
			 break;

			 case 2:
			 getchar();
			 main();
			 break;
			 
			 default:
			 printf("You have entered an invalid response\nPress the 'Enter' key to return to the Main Menu.\n\n");
			 getchar();
			 getchar();
			 main();
			 break;
			}
	}

multiplyfunc()
	{
	 	printf("Enter the First Number(s) ");
		scanf("%d",&k);
		getchar();

		printf("Enter the Second Number(s) ");
		scanf("%d",&l);
		getchar();

		printf("Your total is %d \n\n", k*l);

		printf("Would you like to MULTIPLY another set of numbers\n1. Yes\n2. No \n\n");
		scanf("%d",&multiply);
		switch(multiply)
		     {
			 case 1:
			 precalmultiplyfunc();
			 break;

			 case 2:
			 getchar();
			 main();
			 break;
			 
			 default:
			 printf("You have entered an invalid response.\nPress the 'Enter' key to return to the Main Menu.\n\n");
			 getchar();
			 getchar();
			 main();
			 break;
			}
	}

//Division Controller
ddividefunc()
	{
	 	printf("Enter the First Number(s) ");
		scanf("%f",&m);
		getchar();
		
		printf("Enter the Second Number(s) ");
		scanf("%f",&n);
		getchar();

		printf("Your total is %f\n\n", m/n);

		printf("Would you like to DIVIDE another set of numbers\n1. Yes\n2. No\n\n");
		scanf("%d",&divide);
		switch(divide)
		     {
			 case 1:
			 precaldividefunc();
			 break;

			 case 2:
			 getchar();
			 main();
			 break;
			 
			 default:
			 printf("You have entered an invalid response\nPress the 'Enter' key to return to the Main Menu.\n\n");
			 getchar();
			 getchar();
			 main();
			 break;
			}
	}

dividefunc()
	{
	 	printf("Enter the First Number(s) ");
		scanf("%d",&o);
		getchar();

		printf("Enter the Second Number(s) ");
		scanf("%d",&p);
		getchar();

		printf("Your total is %d \n\n", o/p);

		printf("Would you like to DIVIDE another set of numbers\n1. Yes\n2. No \n\n");
		scanf("%d",&divide);
		switch(divide)
		     {
			 case 1:
			 precaldividefunc();
			 break;

			 case 2:
			 getchar();
			 main();
			 break;
			 
			 default:
			 printf("You have entered an invalid response.\nPress the 'Enter' key to return to the Main Menu.\n\n");
			 getchar();
			 getchar();
			 main();
			 break;
			}
	}
//Internal Reference
int precaladd;
int down;		//D.O.W.N. (Decimal or Whole Number)
int divide;
int precaldivide;
int mainint, subtract,precalsubtract, multiply,precalmultiply, divide, precaldivide, precaladd;

//Internal Function
//Pre Addition Calculator
precaladdfunc()
	{
	 	printf("What type of Numbers would you like to use?\n1. Decimals\n2. Whole Numbers (Without Decimals)\n\n");
		scanf("%d", &down);
		switch(down)
			{
			 	 case 1:
				 daddfunc();
				 break;

				 case 2:
				 addfunc();
				 break;

				 case 3:
				 getchar();
				 printf("Returning To main Menu\n\n");
				 main();
				 break;

				 default:
				 printf("This is an invalid entry. Please retry\n\n");
				 getchar();
				 getchar();
				 precaladdfunc();
				 break;
			}
	getchar();
	getchar();
	}

//Pre-Subtraction Calculator
precalsubtractfunc()
	{
	 	printf("What type of Numbers would you like to use?\n1. Decimals\n2. Whole Numbers (Without Decimals)\n\n");
		scanf("%d", &down);
		switch(down)
			{
			 	 case 2:
				 dsubtractfunc();
				 break;

				 case 1:
				 subtractfunc();
				 break;

				 case 3:
				 getchar();
				 printf("Returning To main Menu\n\n");
				 main();
				 break;

				 default:
				 printf("This is an invalid entry. Please retry\n\n");
				 getchar();
				 getchar();
				 precaladdfunc();
				 break;
			}
	getchar();
	getchar();
	}

//Pre Multiplication Calculator
precalmultiplyfunc()
	{
	 	printf("What type of Numbers would you like to use?\n1. Decimals\n2. Whole Numbers (Without Decimals)\n\n");
		scanf("%d", &down);
		switch(down)
			{
			 	 case 1:
				 dmultiplyfunc();
				 break;

				 case 2:
				 multiplyfunc();
				 break;

				 case 3:
				 getchar();
				 printf("Returning To main Menu\n\n");
				 main();
				 break;

				 default:
				 printf("This is an invalid entry. Please retry\n\n");
				 getchar();
				 getchar();
				 precalmultiplyfunc();
				 break;
			}
	getchar();
	getchar();
	}

//Pre Division Calculator
precaldividefunc()
	{
	 	printf("What type of Numbers would you like to use?\n1. Decimals\n2. Whole Numbers (Without Decimals)\n\n");
		scanf("%d", &down);
		switch(down)
			{
			 	 case 1:
				 ddividefunc();
				 break;

				 case 2:
				 dividefunc();
				 break;

				 case 3:
				 getchar();
				 printf("Returning To main Menu\n\n");
				 main();
				 break;

				 default:
				 printf("This is an invalid entry.");
				 getchar();
				 getchar();
				 main();
				 break;
			}
	getchar();
	getchar();
	}



Sorry I meant 500 lines of code not 600

Is This A Good Question/Topic? 0
  • +

Replies To: Complicated Calculator

#2 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 991
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: Complicated Calculator

Posted 28 December 2010 - 12:38 AM

So what language did you come to C from?
There is no way this is your second ever program in any language (unless you copied large chunks of it from somewhere). If you are copying it then stop because your source is a terrible example of how to write C. You are getting basic stuff wrong while trying to do stuff that is too clever by half and getting a poor program as a result.

Whatever language it was it seems to have taught you some ideas that are going to bite you really hard in C.

As just one example, what is with all the calls to main().
That's a very bad idea that you definitely should drop out of your coding as a matter of high urgency and importance.

The other thing to do is get a modern compiler and learn how to get it to display warning messages to you and act on the messages you get.
Look at what GCC says about your code:
dic.c:36: warning: return type defaults to ‘int’
dic.c: In function ‘main’:
dic.c:44: warning: implicit declaration of function ‘precaladdfunc’
dic.c:48: warning: implicit declaration of function ‘precalsubtractfunc’
dic.c:52: warning: implicit declaration of function ‘precalmultiplyfunc’
dic.c:56: warning: implicit declaration of function ‘precaldividefunc’
dic.c:69: warning: control reaches end of non-void function
dic.c: At top level:
dic.c:97: warning: return type defaults to ‘int’
dic.c: In function ‘addfunc’:
dic.c:129: warning: control reaches end of non-void function
dic.c: At top level:
dic.c:133: warning: return type defaults to ‘int’
dic.c: In function ‘daddfunc’:
dic.c:139: error: ‘B’ undeclared (first use in this function)
dic.c:139: error: (Each undeclared identifier is reported only once
dic.c:139: error: for each function it appears in.)
dic.c:165: warning: control reaches end of non-void function
dic.c: At top level:
dic.c:169: warning: return type defaults to ‘int’
dic.c: In function ‘dsubtractfunc’:
dic.c:201: warning: control reaches end of non-void function
dic.c: At top level:
dic.c:205: warning: return type defaults to ‘int’
dic.c: In function ‘subtractfunc’:
dic.c:236: warning: control reaches end of non-void function
dic.c: At top level:
dic.c:240: warning: return type defaults to ‘int’
dic.c: In function ‘dmultiplyfunc’:
dic.c:271: warning: control reaches end of non-void function
dic.c: At top level:
dic.c:274: warning: return type defaults to ‘int’
dic.c: In function ‘multiplyfunc’:
dic.c:305: warning: control reaches end of non-void function
dic.c: At top level:
dic.c:309: warning: return type defaults to ‘int’
dic.c: In function ‘ddividefunc’:
dic.c:340: warning: control reaches end of non-void function
dic.c: At top level:
dic.c:343: warning: return type defaults to ‘int’
dic.c: In function ‘dividefunc’:
dic.c:374: warning: control reaches end of non-void function
dic.c: At top level:
dic.c:385: warning: return type defaults to ‘int’
dic.c: In function ‘precaladdfunc’:
dic.c:413: warning: control reaches end of non-void function
dic.c: At top level:
dic.c:417: warning: return type defaults to ‘int’
dic.c: In function ‘precalsubtractfunc’:
dic.c:445: warning: control reaches end of non-void function
dic.c: At top level:
dic.c:449: warning: return type defaults to ‘int’
dic.c: In function ‘precalmultiplyfunc’:
dic.c:477: warning: control reaches end of non-void function
dic.c: At top level:
dic.c:481: warning: return type defaults to ‘int’
dic.c: In function ‘precaldividefunc’:
dic.c:509: warning: control reaches end of non-void function



So basically your posting doesn't really add up. There is something deeply wrong with what you are telling us. So, what is the real story?
Was This Post Helpful? 2
  • +
  • -

#3 Aphex19  Icon User is offline

  • Born again Pastafarian.
  • member icon

Reputation: 617
  • View blog
  • Posts: 1,873
  • Joined: 02-August 09

Re: Complicated Calculator

Posted 28 December 2010 - 12:44 AM

First thing I noticed in that you have some variables declared external, but they are not actually external, they are in the same file. On the face of things, this seems redundant. The most odd thing I noticed though is that your functions have no return values specified.
Was This Post Helpful? 0
  • +
  • -

#4 profezzer  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 27-December 10

Re: Complicated Calculator

Posted 28 December 2010 - 08:24 AM

I seriously have never programmed before just read books on it so C is my first language. And this is my second program the first was hello world. Also I know that my code is terrible which is why i asked for thoughts and opinions. So you dont have to be nasty and accuse me of being fake. You would think that as a more advance programmer you would give advice to the noobs. Not tear them down, also it would seem that you may have miscalculated the fact that everyone may not catch on as slow as you might have.

And just to make you feel better i read 2 books 'Learn C in 24 Hours' and the second one although not totally finished reading it was 'The C Programming Language second edition'.

But over all thank you for your opinion and clarify I didnt copy code from anywhere it was all hand written. The code calls to main because main is where the program starts after it has done all its calculations and the user has finished the program. Also I have no idea what GCC is can you elaborate. I've been using the DEV C Compiler

View Postjanotte, on 27 December 2010 - 11:38 PM, said:

So what language did you come to C from?
There is no way this is your second ever program in any language (unless you copied large chunks of it from somewhere). If you are copying it then stop because your source is a terrible example of how to write C. You are getting basic stuff wrong while trying to do stuff that is too clever by half and getting a poor program as a result.

Whatever language it was it seems to have taught you some ideas that are going to bite you really hard in C.

As just one example, what is with all the calls to main().
That's a very bad idea that you definitely should drop out of your coding as a matter of high urgency and importance.

The other thing to do is get a modern compiler and learn how to get it to display warning messages to you and act on the messages you get.
Look at what GCC says about your code:
dic.c:36: warning: return type defaults to ‘int’
dic.c: In function ‘main’:
dic.c:44: warning: implicit declaration of function ‘precaladdfunc’
dic.c:48: warning: implicit declaration of function ‘precalsubtractfunc’
dic.c:52: warning: implicit declaration of function ‘precalmultiplyfunc’
dic.c:56: warning: implicit declaration of function ‘precaldividefunc’
dic.c:69: warning: control reaches end of non-void function
dic.c: At top level:
dic.c:97: warning: return type defaults to ‘int’
dic.c: In function ‘addfunc’:
dic.c:129: warning: control reaches end of non-void function
dic.c: At top level:
dic.c:133: warning: return type defaults to ‘int’
dic.c: In function ‘daddfunc’:
dic.c:139: error: ‘B’ undeclared (first use in this function)
dic.c:139: error: (Each undeclared identifier is reported only once
dic.c:139: error: for each function it appears in.)
dic.c:165: warning: control reaches end of non-void function
dic.c: At top level:
dic.c:169: warning: return type defaults to ‘int’
dic.c: In function ‘dsubtractfunc’:
dic.c:201: warning: control reaches end of non-void function
dic.c: At top level:
dic.c:205: warning: return type defaults to ‘int’
dic.c: In function ‘subtractfunc’:
dic.c:236: warning: control reaches end of non-void function
dic.c: At top level:
dic.c:240: warning: return type defaults to ‘int’
dic.c: In function ‘dmultiplyfunc’:
dic.c:271: warning: control reaches end of non-void function
dic.c: At top level:
dic.c:274: warning: return type defaults to ‘int’
dic.c: In function ‘multiplyfunc’:
dic.c:305: warning: control reaches end of non-void function
dic.c: At top level:
dic.c:309: warning: return type defaults to ‘int’
dic.c: In function ‘ddividefunc’:
dic.c:340: warning: control reaches end of non-void function
dic.c: At top level:
dic.c:343: warning: return type defaults to ‘int’
dic.c: In function ‘dividefunc’:
dic.c:374: warning: control reaches end of non-void function
dic.c: At top level:
dic.c:385: warning: return type defaults to ‘int’
dic.c: In function ‘precaladdfunc’:
dic.c:413: warning: control reaches end of non-void function
dic.c: At top level:
dic.c:417: warning: return type defaults to ‘int’
dic.c: In function ‘precalsubtractfunc’:
dic.c:445: warning: control reaches end of non-void function
dic.c: At top level:
dic.c:449: warning: return type defaults to ‘int’
dic.c: In function ‘precalmultiplyfunc’:
dic.c:477: warning: control reaches end of non-void function
dic.c: At top level:
dic.c:481: warning: return type defaults to ‘int’
dic.c: In function ‘precaldividefunc’:
dic.c:509: warning: control reaches end of non-void function



So basically your posting doesn't really add up. There is something deeply wrong with what you are telling us. So, what is the real story?

Was This Post Helpful? 0
  • +
  • -

#5 profezzer  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 27-December 10

Re: Complicated Calculator

Posted 28 December 2010 - 08:46 AM

Thank you like i said I just started programming about a week ago and I dont quite have the hang of it yet. The external variables were because I originally had each variable set as and external file but I wondered what would happen if I put them all in the same file so thats what I did and it compiled and ran like it was supposed to so I left it.

And as far as return value I'll have to go look at my notes I must have forgotten something. I was thinking that the output after the calculation was the return value. Can you refer me to and example or explain what you mean.

Also thanks for not beating me up too bad with your response and being to critical on the noob.

View PostAphex19, on 27 December 2010 - 11:44 PM, said:

First thing I noticed in that you have some variables declared external, but they are not actually external, they are in the same file. On the face of things, this seems redundant. The most odd thing I noticed though is that your functions have no return values specified.

Was This Post Helpful? 0
  • +
  • -

#6 hpfreak080  Icon User is offline

  • New D.I.C Head

Reputation: 5
  • View blog
  • Posts: 14
  • Joined: 16-November 09

Re: Complicated Calculator

Posted 28 December 2010 - 08:49 AM

Dev C is an outdated and no longer updated IDE. I would recommend using either Microsoft Visual C++ 2010 Express (even though it says C++ in the name, the program contains a C compiler too) or Code::Blocks . Both are free and are modern, updated compilers. (I didn't take the time to find the specific download page for Code::Blocks so if anyone wants to add it, feel free).

Also, calling main() from another function is considered bad programming. You need to give your functions return types and maybe read some on loops and how to use them in order to return to main().

Here are some resources you can read to help you along with the above:

CPlusPlus -- Functions (I)
CPlusPlus -- Functions (II)
CPlusPlus -- Control Structures <-- talks about if/else notation as well as loops

there are also great tutorials on this site:

DreamInCode -- Understanding Loops

The above tutorials are for C++, but the general information (as far as return types for functions and basic use for loops) is the same.

Hopefully that will get you going on the right path.

This post has been edited by hpfreak080: 28 December 2010 - 08:51 AM

Was This Post Helpful? 0
  • +
  • -

#7 Electron_Volt  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 21
  • Joined: 30-November 09

Re: Complicated Calculator

Posted 28 December 2010 - 08:52 AM

It sounds like you read the books but didn't do any coding while you read them. When I learn from a book it helps to program some examples in the book to get the feel of what they teach.

One thing I would recommend is declaring the variables inside the functions. Right now they are all global variables and are accessible by every function. Also the functions need return values. It might benefit you to reread the chapter on function calls.
Was This Post Helpful? 0
  • +
  • -

#8 profezzer  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 27-December 10

Re: Complicated Calculator

Posted 28 December 2010 - 09:11 AM

Thanks i have visual studios 2010. I am glad to know that it will compile in c as well as c++. The reason I use C is because Im going into micro processor programming and I have to know C in order to get my micro chip to function. For a project im working on. Your insight has been helpful thank you.

View Posthpfreak080, on 28 December 2010 - 07:49 AM, said:

Dev C is an outdated and no longer updated IDE. I would recommend using either Microsoft Visual C++ 2010 Express (even though it says C++ in the name, the program contains a C compiler too) or Code::Blocks . Both are free and are modern, updated compilers. (I didn't take the time to find the specific download page for Code::Blocks so if anyone wants to add it, feel free).

Also, calling main() from another function is considered bad programming. You need to give your functions return types and maybe read some on loops and how to use them in order to return to main().

Here are some resources you can read to help you along with the above:

CPlusPlus -- Functions (I)
CPlusPlus -- Functions (II)
CPlusPlus -- Control Structures <-- talks about if/else notation as well as loops

there are also great tutorials on this site:

DreamInCode -- Understanding Loops

The above tutorials are for C++, but the general information (as far as return types for functions and basic use for loops) is the same.

Hopefully that will get you going on the right path.

Was This Post Helpful? 0
  • +
  • -

#9 profezzer  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 27-December 10

Re: Complicated Calculator

Posted 28 December 2010 - 09:18 AM

LOL I feel stupid now because that would have been simple and you are correct I did just read the book and memorized what I needed to know but as it seems as if i didnt grasp the full concept. I will most definitely go over it again. I also didnt know I could declare variable inside of functions

Thanks for your help

View PostElectron_Volt, on 28 December 2010 - 07:52 AM, said:

It sounds like you read the books but didn't do any coding while you read them. When I learn from a book it helps to program some examples in the book to get the feel of what they teach.

One thing I would recommend is declaring the variables inside the functions. Right now they are all global variables and are accessible by every function. Also the functions need return values. It might benefit you to reread the chapter on function calls.

Was This Post Helpful? 0
  • +
  • -

#10 Aphex19  Icon User is offline

  • Born again Pastafarian.
  • member icon

Reputation: 617
  • View blog
  • Posts: 1,873
  • Joined: 02-August 09

Re: Complicated Calculator

Posted 28 December 2010 - 02:52 PM

Quote

I was thinking that the output after the calculation was the return value. Can you refer me to and example or explain what you mean.


In C and C++, all function have a minimum of three "parts" to them, that being the data type that the function returns, the name of the function, and any arguments that the function takes, in that order. Here's a more visual example.

// layout of a function
return_type function_name arguments

// example of a function that returns an integer and takes two integers as arguments
int add(int a, int B)/>
{
    return a + b;
}

// example of a function which returns nothing at all and takes a pointer to a string as an argument
void Print(char *str)
{
    printf("%s\n", str);
}



Global variables should be used carefully and it's generally thought that you should use them sparingly. Passing values to an from function should be an intuitive process for any programmer, be sure to make good use of it.

Quote

Also thanks for not beating me up too bad with your response and being to critical on the noob.


If you're refering to janotte's answer, I think he was just giving some tough love, I don't think he means to offend you, just to show you that the code you posted does have major flaws. He did also have some valid points, it does seem that you're trying to run before you can walk, which is often a sign of copy/paste coding, although I think that this may just be an exception.
Was This Post Helpful? 1
  • +
  • -

#11 profezzer  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 27-December 10

Re: Complicated Calculator

Posted 28 December 2010 - 04:01 PM

Wow your examples were very simple and as I thought I really complicated the code a lot. I will work on trying to make it simpler now that I see the concept.

Yes I was offended at being accused of coping a pasting code especially after the fact that I spent 6 hours typing and testing code, not to mention I was accused of studying another language and trying to switch to C which is also untrue. In fact i thought for my code wasnt that bad considering ive only been programming for a week. But your explanation of what he was trying to say is now understandable. Also I know that im trying to run before I can walk, because I have too in order to get another project done.

In all I really do thank you and everyone else that responded to my post for your help.

View PostAphex19, on 28 December 2010 - 01:52 PM, said:

Quote

I was thinking that the output after the calculation was the return value. Can you refer me to and example or explain what you mean.


In C and C++, all function have a minimum of three "parts" to them, that being the data type that the function returns, the name of the function, and any arguments that the function takes, in that order. Here's a more visual example.

// layout of a function
return_type function_name arguments

// example of a function that returns an integer and takes two integers as arguments
int add(int a, int B)/>
{
    return a + b;
}

// example of a function which returns nothing at all and takes a pointer to a string as an argument
void Print(char *str)
{
    printf("%s\n", str);
}



Global variables should be used carefully and it's generally thought that you should use them sparingly. Passing values to an from function should be an intuitive process for any programmer, be sure to make good use of it.

Quote

Also thanks for not beating me up too bad with your response and being to critical on the noob.


If you're refering to janotte's answer, I think he was just giving some tough love, I don't think he means to offend you, just to show you that the code you posted does have major flaws. He did also have some valid points, it does seem that you're trying to run before you can walk, which is often a sign of copy/paste coding, although I think that this may just be an exception.

Was This Post Helpful? 0
  • +
  • -

#12 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 991
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: Complicated Calculator

Posted 28 December 2010 - 05:45 PM

View Postprofezzer, on 29 December 2010 - 08:01 AM, said:

Yes I was offended at being accused of coping a pasting code


I only said you copied ideas. Not copy-paste. That means you took an idea from somewhere and used it in your program. Does that make sense? Do you see that you are adding in stuff that was not said.

There is no way you independently thought up the ideas you are using in your code if this is your second program. You are copying the ideas from somewhere. That must be true (unless you are an autodidactic genius and I think we can safely rule that out).

I asked if you had previous experience in another language because that would explain where the ideas came from. You are adamant that that is not the case. Therefore you must have another source where you are getting these harmful ideas from and I am suggesting that you stop using that source exactly because it is and will continue to do you harm.

To characterise that as "accusing" is pretty much bizarre. You need to read what is being said and not add in things that are not being said.

As I was saying, and has now been said in a way you were happy to listen to, you are writing code that has no place in the code of someone starting out in programming.

So my suggestion was and is that you get a better source to learn your coding from and learn the basics first.

Wherever you are getting ideas like external from is doing you harm. Those ideas have no place in your code at this stage of your learning. Go back to the basics and learn them first before you try and do things that are more advanced. As has been said to you already "Learn to walk before you try to run".

The other point is if you are going to put your code up for strangers to comment on you will have to have a thicker skin about the comments you get. If you don't want to be challenged and criticised then going into programming is probably a mistake and posting on public forums is definitely a mistake.
Was This Post Helpful? 2
  • +
  • -

#13 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6246
  • View blog
  • Posts: 24,014
  • Joined: 23-August 08

Re: Complicated Calculator

Posted 28 December 2010 - 06:19 PM

More info on functions can be found at the links in my signature below.
Was This Post Helpful? 0
  • +
  • -

#14 profezzer  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 27-December 10

Re: Complicated Calculator

Posted 28 December 2010 - 06:38 PM

Dude i'm cool with being criticized the part that irritated me was this

Quote

So what language did you come to C from?
There is no way this is your second ever program in any language (unless you copied large chunks of it from somewhere). If you are copying it then stop because your source is a terrible example of how to write C. You are getting basic stuff wrong while trying to do stuff that is too clever by half and getting a poor program as a result.

Note the portion in which you state specifically state

Quote

There is no way this is your second ever program in any language (unless you copied large chunks of it from somewhere).

Now you say I copied the ideas when you specifically state "large chunk of it from somewhere". Make up your mind. However, what programmer starting out hasn't done a simple a+b=c math problem in C, and what programmer in starting out in C has not read "The C Programming Language" and saw a function and tried it. In this case I wanted to do a calculator and was not sure on all of the more advance features so this code is what i came up with to fit my needs and it served its purpose.

As stated by me and another user of this forum I am trying to run before I can walk, and as far as my skin goes it is pretty thick the part that offended me was your statement which implied that I could not have formed these ideas on my own.

Quote

There is no way you independently thought up the ideas you are using in your code if this is your second program. You are copying the ideas from somewhere. That must be true (unless you are an autodidactic genius and I think we can safely rule that out).


This statement Totally implies that my idea nor my code is original. As i stated before everyone doesnt learn at the same rate as others. What may have taken you a year may only take me a week, but I can say this I posted to a public forum to get help and test my theory and to get opinions but you are not doing either all you seem to want to do is belittle my code and imply that its too far advance for me because its only been a week for me.

I think my code is good for a beginners second program and I also know that there has to be a better way, but it was the only way i knew how from the top of my head. But thank you for your comments I have learned something from you and what i learned is that I have to keep pushing on my own cause in the programming world the people you expect to lead and guide can be the biggest jerks on the planet and I shouldn't expect to learn much from them.

As I said before thanks for the comments good or bad I'm just starting out and its a learning process.

View Postjanotte, on 28 December 2010 - 04:45 PM, said:

View Postprofezzer, on 29 December 2010 - 08:01 AM, said:

Yes I was offended at being accused of coping a pasting code


I only said you copied ideas. Not copy-paste. That means you took an idea from somewhere and used it in your program. Does that make sense? Do you see that you are adding in stuff that was not said.

There is no way you independently thought up the ideas you are using in your code if this is your second program. You are copying the ideas from somewhere. That must be true (unless you are an autodidactic genius and I think we can safely rule that out).

I asked if you had previous experience in another language because that would explain where the ideas came from. You are adamant that that is not the case. Therefore you must have another source where you are getting these harmful ideas from and I am suggesting that you stop using that source exactly because it is and will continue to do you harm.

To characterise that as "accusing" is pretty much bizarre. You need to read what is being said and not add in things that are not being said.

As I was saying, and has now been said in a way you were happy to listen to, you are writing code that has no place in the code of someone starting out in programming.

So my suggestion was and is that you get a better source to learn your coding from and learn the basics first.

Wherever you are getting ideas like external from is doing you harm. Those ideas have no place in your code at this stage of your learning. Go back to the basics and learn them first before you try and do things that are more advanced. As has been said to you already "Learn to walk before you try to run".

The other point is if you are going to put your code up for strangers to comment on you will have to have a thicker skin about the comments you get. If you don't want to be challenged and criticised then going into programming is probably a mistake and posting on public forums is definitely a mistake.


Thanks for the Help. I'm going to go read it now.

View PostJackOfAllTrades, on 28 December 2010 - 05:19 PM, said:

More info on functions can be found at the links in my signature below.

Was This Post Helpful? 0
  • +
  • -

#15 hpfreak080  Icon User is offline

  • New D.I.C Head

Reputation: 5
  • View blog
  • Posts: 14
  • Joined: 16-November 09

Re: Complicated Calculator

Posted 28 December 2010 - 08:40 PM

Look, I work with C# in the "real world" of programming and I have copied and pasted some code that I've found on the internet if it does the job I want. I realize that you probably didn't copy/paste your code, I'm just trying to show you that it doesn't always mean something bad. The only time it's bad is when it is not standard/portable and that's what people here then try to tell you (such as your calls to main() and other things in your code).

Also, I can see why Janotte asked if you had come from another language. When I started taking JAVA after only knowing C/C++, I started out doing things that weren't JAVA specific. I was a lot less Object-Oriented than I should have been for JAVA. It was something I had to learn because it was a huge part of JAVA (C++ is object-oriented, but we didn't deal so much with that side of C++ in my class). Again, I realize that you probably have not come from another programming language, but I'm just trying to give you insight.

I have the book that you said you are learning from. I opened it and realized that it does, indeed, just use:

main()
{

}


instead of:

int main()
{
     return 0;
}


I'll just tell you now that "main()" without a return type is really not good and maybe that isn't really the best book to use as a resource since the author doesn't feel that using standard "main()" notation is important. Maybe buy another book or use legitimate online resources more for guidance instead of the book by Vine.

Good luck to you.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2