Thread: Help with my calculater program.

  1. #1
    Registered User kogh's Avatar
    Join Date
    Oct 2002
    Posts
    4

    Help with my calculater program.

    Code:
    /////////////////////////////
    //////////////////////    ///
    //                 //     ///
    //  Calculater     //     ///
    //  Program v 1.1  //     ///
    //  Coded by:      //     ///
    //  James AKA Kogh //     ///
    //                 //     ///
    /////////////////////     ///
    /////////////////////////////
    
    #include <iostream.h>
    
    int mult(int x, int y);
    int add(int x, int y);
    int div(int x, int y);
    int sub(int x, int y);
    
    
    int main()
    
    {
    	char desc;
    	int x,y;
    	for (;;)
    	{
    
    	cout<<"Calc Program 1.1 Menu:";
    	cout<<endl;
    	cout<<"A.Add";
    	cout<<endl;
    	cout<<"B.Subtract";
    	cout<<endl;
    	cout<<"C.Multiply";
    	cout<<endl;
    	cout<<"D.Divide";
    	cout<<endl;
    
    	if(desc=='a'|| desc=='A')
    	{
    	cout<<"Enter two numbers to be added: ";
    	cin>>x>>y;
    	cout<<add(x,y);
    	}
    
    	if(desc=='b'|| desc=='B') 
    	{
    	cout<<"Enter two numbers to be subtacted: ";
    	cin>>x>>y;
    	cout<<sub(x,y);
    	}
    
    	if(desc=='c'|| desc=='C') 
    	{
    	cout<<"Enter two numbers to be multiplied: ";
    	cin>>x>>y;
    	cout<<mult(x,y);
    	}
    
    	if(desc=='d'|| desc=='D') 
    	{
    	cout<<"Enter two numbers to be divided: ";
    	cin>>x>>y;
    	cout<<div(x,y);
    	}
    
    	return 0;
    }
    
    int add(int x, int y)
    {
    	return x+y;
    }
    
    int sub(int x, int y)
    {
    	return x-y;
    }
    
    int mult(int x, int y)
    {
    	return x*y;
    }
    
    int div(int x, int y)
    {
    	return x/y;
    }
    I'm a fairly new C++ programmer, and I was wondering why this code doesn't work... :0
    If anyone could point out where i screwed it up at, it'd be greatly appretiated!

    -kogh

  2. #2
    C > C++ duders ggs's Avatar
    Join Date
    Aug 2001
    Posts
    435
    you never allow your users to choose which option they want (so the value of desc is never set) :O
    .sect signature

  3. #3
    CS Author and Instructor
    Join Date
    Sep 2002
    Posts
    511
    Yep ggs is correct so:

    Code:
      cin >> desc;
    before the first if should work.
    Mr. C: Author and Instructor

  4. #4
    Registered User kogh's Avatar
    Join Date
    Oct 2002
    Posts
    4
    Ohh. yeah, I didn't catch that, thanks ;D

    but, I meant I got a compiling error:

    Code:
    --------------------Configuration: startingover1 - Win32 Debug--------------------
    Compiling...
    startingover1.cpp
    E:\Programming\Visual Studio 6\MSDev98\MyProjects\startingover1\startingover1.cpp(75) : error C2601: 'add' : local function definitions are illegal
    E:\Programming\Visual Studio 6\MSDev98\MyProjects\startingover1\startingover1.cpp(80) : error C2601: 'sub' : local function definitions are illegal
    E:\Programming\Visual Studio 6\MSDev98\MyProjects\startingover1\startingover1.cpp(85) : error C2601: 'mult' : local function definitions are illegal
    E:\Programming\Visual Studio 6\MSDev98\MyProjects\startingover1\startingover1.cpp(90) : error C2601: 'div' : local function definitions are illegal
    E:\Programming\Visual Studio 6\MSDev98\MyProjects\startingover1\startingover1.cpp(93) : fatal error C1004: unexpected end of file found
    Error executing cl.exe.
    
    startingover1.exe - 5 error(s), 0 warning(s)
    I just don't understand how those functions cause the errors.

  5. #5
    Sir Mister Insane Sako Klinerr1's Avatar
    Join Date
    May 2002
    Posts
    608
    cin>>x>>y;

    i dont think visual likes that try doing cin >>x then a sepereate cin >> y (i know someone is going to say something against my word)
    Email: [email protected] || AIM: MisterSako || MSN: [email protected]

    -the shroom has spoken

  6. #6
    Registered User kogh's Avatar
    Join Date
    Oct 2002
    Posts
    4
    Nope, I still get the same errors. =(

  7. #7
    CS Author and Instructor
    Join Date
    Sep 2002
    Posts
    511
    Also, that code:

    Code:
    for (;;)
    	{

    where do you close it (missing a })

    and why do you need the for loop?
    Mr. C: Author and Instructor

  8. #8
    Registered User kogh's Avatar
    Join Date
    Oct 2002
    Posts
    4
    OHHH. Ok, I forgot to close it, heh. Thanks Mister C. =)

    I needed the "for" loop so it would go back to the menu after you do something. And I put the return 0 inside the for loop, which messed it up. so I just fixed it ^^.

    here's the finished code now:

    Code:
    /////////////////////////////
    //////////////////////    ///
    //                 //     ///
    //  Calculater     //     ///
    //  Program v 1.1  //     ///
    //  Coded by:      //     ///
    //  James AKA Kogh //     ///
    //                 //     ///
    /////////////////////     ///
    /////////////////////////////
    
    #include <iostream.h>
    
    int mult(int x, int y);
    int add(int x, int y);
    int div(int x, int y);
    int sub(int x, int y);
    
    
    int main()
    
    {
    	char desc;
    	int x,y;
    	for (;;)
    	{
    
    	cout<<"Calc Program 1.1 Menu:";
    	cout<<endl;
    	cout<<"A.Add";
    	cout<<endl;
    	cout<<"B.Subtract";
    	cout<<endl;
    	cout<<"C.Multiply";
    	cout<<endl;
    	cout<<"D.Divide";
    	cout<<endl;
    
    
    	cin >> desc;
    
    
    	if(desc=='a'|| desc=='A')
    	{
    	cout<<"Enter two numbers to be added: ";
    	cin>>x;
    	cin>>y;
    	cout<<add(x,y);
    	}
    
    	if(desc=='b'|| desc=='B') 
    	{
    	cout<<"Enter two numbers to be subtacted: ";
    	cin>>x;
    	cin>>y;
    	cout<<sub(x,y);
    	}
    
    	if(desc=='c'|| desc=='C') 
    	{
    	cout<<"Enter two numbers to be multiplied: ";
    	cin>>x;
    	cin>>y;
    	cout<<mult(x,y);
    	}
    
    	if(desc=='d'|| desc=='D') 
    	{
    	cout<<"Enter two numbers to be divided: ";
    	cin>>x;
    	cin>>y;
    	cout<<div(x,y);
    	}
    
    	
    	}
    	return 0;
    }
    
    int add(int x, int y)
    {
    	return x+y;
    }
    
    int sub(int x, int y)
    {
    	return x-y;
    }
    
    int mult(int x, int y)
    {
    	return x*y;
    }
    
    int div(int x, int y)
    {
    	return x/y;
    }

  9. #9
    CS Author and Instructor
    Join Date
    Sep 2002
    Posts
    511
    It may work -but I do not like using an infinite for loop.
    Why don't you use a do/while loop with the menu?
    Mr. C: Author and Instructor

  10. #10
    Registered User
    Join Date
    Oct 2002
    Posts
    155
    Code:
    #include <iostream.h>
    #include <conio>
    #include <ctype>
    
    double mult(double x, double y);
    double add(double x, double y);
    double divi(double x, double y);
    double sub(double x, double y);
    
    
    double main()
    
    {
    	char desc, yesOrNo;
    	double x,y;
    	
    	do{
    
    		cout<<"Calc Program 1.1 Menu:\n"
    		    <<"A.Add\n"
    		    <<"B.Subtract\n"
    		    <<"C.Multiply\n"
    		    <<"D.Divide\n";
    	
    		cin >> desc;
    		desc = tolower(desc);
    	
    		if(desc=='a')
    		{
    		cout << "Enter two numbers to be added: ";
    		cin >> x >> y;
    		cout << "ANSWER IS: ";
    		cout << add(x,y);
    		}
    	
    		if(desc=='b') 
    		{
    		cout << "Enter two numbers to be subtacted: ";
    		cin >> x >> y;
    		cout << "ANSWER IS: ";
    		cout << sub(x,y);
    		}
    	
    		if(desc=='c') 
    		{
    		cout << "Enter two numbers to be multiplied: ";
    		cin >> x >> y;
    		cout << "ANSWER IS: ";
    		cout << mult(x,y);
    		}
    	
    		if(desc=='d') 
    		{
    		cout << "Enter two numbers to be divided: ";
    		cin >> x >> y;
    		cout << "ANSWER IS: ";
    		cout << divi(x,y);
    		}
    		
    		getch();
    		clrscr();
    		cout << "Wish to retry? (y or n): ";
    		cin >> yesOrNo;
    		yesOrNo = tolower(yesOrNo);
    		clrscr();
    	}while(yesOrNo == 'y');
    		
    
    	return 0;
    }
    
    double add(double x, double y)
    {
    	return x+y;
    }
    
    double sub(double x, double y)
    {
    	return x-y;
    }
    
    double mult(double x, double y)
    {
    	return x*y;
    }
    
    double divi(double x, double y)
    {
    	return x/y;
    }
    Hope this helps :P

  11. #11
    Registered User
    Join Date
    Oct 2002
    Posts
    155
    BTW I am using Borland compiler with Quincy so some syntax may not work if you are using Visual C++ or something. If it does not work, take out all my getch and clrscr statements

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Issue with program that's calling a function and has a loop
    By tigerfansince84 in forum C++ Programming
    Replies: 9
    Last Post: 11-12-2008, 01:38 PM
  2. Need help with a program, theres something in it for you
    By engstudent363 in forum C Programming
    Replies: 1
    Last Post: 02-29-2008, 01:41 PM
  3. Replies: 4
    Last Post: 02-21-2008, 10:39 AM
  4. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM