I give up! can someone help me out please?

This is a discussion on I give up! can someone help me out please? within the C++ Programming forums, part of the General Programming Boards category; I am quite new to c++ and I am triying to make a console calculator, but I cant make it ...

  1. #1
    Registered User
    Join Date
    Jan 2003
    Posts
    118

    I give up! can someone help me out please?

    I am quite new to c++ and I am triying to make a console calculator, but I cant make it work! here is the code:

    Code:
    #include <iostream>
    using namespace std;
    //Prototypes
    float GetKey(char input2);
    float multiplication(float num1, float num2);
    float sum(float num1, float num2);
    float divition(float num1, float num2);
    float substraction(float num1, float num2);
    void CalcFont();
    
    int main()
    {
    	int num1, num2;
    
    	float input1;
    	char input2;
    	float input3;
    
    	do
    	{
    		CalcFont();
    		cin >> input1 >> input2 >> input3;
    	} while (input2 != 'c' || input2 != 'C');
    
    	return 0;
    }
    
    GetKey(&input2)
    {
    	if (*input2 == '*')
    	{
    		multiplication(num1, num2);
    		return multiplication(num1, num2);
    	}
    
    	if (*input2 == '+')
    	{
    		sum(num1, num2);
    		return sum(num1, num2)
    	}
    
    	if (*input2 == '/')
    	{
    		divition(num1, num2);
    		return divition(num1, num2);
    	}
    
    	if (*input2 == '-')
    	{
    		substraction(num1, num2);
    		return substraction(num1, num2);
    	}
    }
    
    multiplication(num1, num2)
    {
    	return num1 * num2;
    }
    
    sum(num1, num2)
    {
    	return num1 + num2;
    }
    
    divition(num1, num2)
    {
    	return num1 / num2;
    }
    
    substraction(num1, num2)
    {
    	return num1 - num2;
    }
    
    CalcFont()
    {
    	cout << "---------\n";
    	cout << "|" << GetKey() << "       |\n";
    	cout << "|-------|---\n";
    	cout << "| 1 | 2 | * |\n";
    	cout << "|---|---|---\n";
    	cout << "| 3 | 4 | + |\n";
    	cout << "|---|---|---\n";
    	cout << "| 5 | 6 | / |\n";
    	cout << "|---|---|---\n";
    	cout << "| 7 | 8 | - |\n";
    	cout << "|---|---|---\n";
    	cout << "| 9 | C |\n";
    	cout << "---------\n";
    }
    I alwas get many error keys and I cant figure out how to fix them, any help?
    Why drink and drive when you can smoke and fly?

  2. #2
    Cheesy Poofs! PJYelton's Avatar
    Join Date
    Sep 2002
    Location
    Boulder
    Posts
    1,728
    Tell us what the errors are, that'll make our jobs to help a lot easier!

  3. #3
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>GetKey(&input2)
    Your function must match its prototype.
    >>float GetKey(char input2)
    Similarly for the other functions.

    >>multiplication(num1, num2);
    num1 and num2 are in main, they don't exist in the GetKey function.

    >>if (*input2 == '*')
    Your indirection isn't needed:
    >>if (input2 == '*')

    >>return sum(num1, num2)
    Missing a semi-colon

    >>GetKey() return value
    The GetKey() function doesn't have a guaranteed return value. What happens if the user doesn't enter a valid symbol?


    >>cout << "|" << GetKey() << " |\n";
    Are you failed to pass any parameters to the GetKey() function.

    >>multiplication(num1, num2);
    >>return multiplication(num1, num2);
    Why are you calling the multiplication function twice?


    There's some logic problems too, but if you sort out the above, it might at least compile
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  4. #4
    Registered User abrege's Avatar
    Join Date
    Nov 2002
    Posts
    369
    thats...a lot...of errors
    I am against the teaching of evolution in schools. I am also against widespread
    literacy and the refrigeration of food.

  5. #5
    Registered User
    Join Date
    Jan 2003
    Posts
    118

    Smile

    Thnks so much
    Why drink and drive when you can smoke and fly?

  6. #6
    Registered User
    Join Date
    Jan 2003
    Posts
    118
    I made some changes and this is how I got the code now:

    Code:
    #include <iostream>
    using namespace std;
    //Prototypes
    float GetKey(char input2);
    float multiplication(float num1, float num2);
    float sum(float num1, float num2);
    float divition(float num1, float num2);
    float substraction(float num1, float num2);
    void CalcFont();
    
    int main()
    {
    	int num1, num2;
    
    	float input1;
    	char input2;
    	float input3;
    
    	do
    	{
    		CalcFont();
    		cin >> input1 >> input2 >> input3;
    	} while (input2 != 'c' || input2 != 'C');
    
    	return 0;
    }
    
    GetKey(char input2)
    {
    	if (input2 == '*')
    	{
    		return multiplication(num1, num2);
    	}
    
    	if (input2 == '+')
    	{
    		return sum(num1, num2);
    	}
    
    	if (input2 == '/')
    	{
    		return divition(num1, num2);
    	}
    
    	if (input2 == '-')
    	{
    		return substraction(num1, num2);
    	}
    }
    
    multiplication(float num1, float num2)
    {
    	return num1 * num2;
    }
    
    sum(float num1, float num2)
    {
    	return num1 + num2;
    }
    
    divition(float num1, float num2)
    {
    	return num1 / num2;
    }
    
    substraction(float num1, float num2)
    {
    	return num1 - num2;
    }
    
    CalcFont()
    {
    	cout << "---------\n";
    	cout << "|" << GetKey() << "       |\n";
    	cout << "|-------|---\n";
    	cout << "| 1 | 2 | * |\n";
    	cout << "|---|---|---\n";
    	cout << "| 3 | 4 | + |\n";
    	cout << "|---|---|---\n";
    	cout << "| 5 | 6 | / |\n";
    	cout << "|---|---|---\n";
    	cout << "| 7 | 8 | - |\n";
    	cout << "|---|---|---\n";
    	cout << "| 9 | C |\n";
    	cout << "---------\n";
    }
    This are the errors I get now:

    D:\Program Files\DevStudio\MyProjects\Calculator\Main calculator.cpp(26) : warning C4101: 'num1' : unreferenced local variable
    D:\Program Files\DevStudio\MyProjects\Calculator\Main calculator.cpp(26) : warning C4101: 'num2' : unreferenced local variable
    D:\Program Files\DevStudio\MyProjects\Calculator\Main calculator.cpp(29) : error C2556: 'GetKey' : overloaded functions only differ by return type
    D:\Program Files\DevStudio\MyProjects\Calculator\Main calculator.cpp(32) : error C2065: 'num1' : undeclared identifier
    D:\Program Files\DevStudio\MyProjects\Calculator\Main calculator.cpp(32) : error C2065: 'num2' : undeclared identifier
    D:\Program Files\DevStudio\MyProjects\Calculator\Main calculator.cpp(52) : error C2556: 'multiplication' : overloaded functions only differ by return type
    D:\Program Files\DevStudio\MyProjects\Calculator\Main calculator.cpp(57) : error C2556: 'sum' : overloaded functions only differ by return type
    D:\Program Files\DevStudio\MyProjects\Calculator\Main calculator.cpp(62) : error C2556: 'divition' : overloaded functions only differ by return type
    D:\Program Files\DevStudio\MyProjects\Calculator\Main calculator.cpp(67) : error C2556: 'substraction' : overloaded functions only differ by return type
    D:\Program Files\DevStudio\MyProjects\Calculator\Main calculator.cpp(72) : error C2556: 'CalcFont' : overloaded functions only differ by return type
    Error executing cl.exe.

    Calculator.exe - 8 error(s), 2 warning(s)

    So, how do I make num1 and num2 to exist in the functions (I know this might be very newbish questions, but hey! I have been studying c++ for less tha 2 weeks!)
    Why drink and drive when you can smoke and fly?

  7. #7
    Registered User abrege's Avatar
    Join Date
    Nov 2002
    Posts
    369
    Follow your errors!! Include the return types in front of the names of your functions, and make sure that the functions which use the variables num1/num2 have access to them.
    I am against the teaching of evolution in schools. I am also against widespread
    literacy and the refrigeration of food.

  8. #8
    Registered User
    Join Date
    Jan 2003
    Posts
    118
    Sorry to bother all of you like this, but I am really dying here!!

    Code:
    #include <iostream>
    using namespace std;
    
    //Prototypes
    float GetKey(char input2);
    float multiplication(float num1, float num2);
    float sum(float num1, float num2);
    float divition(float num1, float num2);
    float substraction(float num1, float num2);
    void CalcFont();
    
    struct numbers
    {
    	float num1;
    	float num2;
    };
    
    
    
    
    int main()
    {
    	float input1;
    	char input2;
    	float input3;
    
    	do
    	{
    		CalcFont();
    		cin >> input1 >> input2 >> input3;
    	} while (input2 != 'c' || input2 != 'C');
    
    	return 0;
    }
    
    float GetKey(char input2)
    {
    	numbers nums;
    	if (input2 == '*')
    	{
    		return multiplication(nums.num1, nums.num2);
    	}
    
    	if (input2 == '+')
    	{
    		return sum(nums.num1, nums.num2);
    	}
    
    	if (input2 == '/')
    	{
    		return divition(nums.num1, nums.num2);
    	}
    
    	if (input2 == '-')
    	{
    		return substraction(nums.num1, nums.num2);
    	}
    }
    
    float multiplication(float nums.num1, float nums.num2)
    {
    	return nums.num1 * nums.num2;
    }
    
    float sum(float nums.num1, float nums.num2)
    {
    	return nums.num1 + nums.num2;
    }
    
    float divition(float nums.num1, float nums.num2)
    {
    	return nums.num1 / nums.num2;
    }
    
    float substraction(float nums.num1, float nums.num2)
    {
    	return nums.num1 - nums.num2;
    }
    
    void CalcFont()
    {
    	std::cout << "---------\n";
    	std::cout << "|" << GetKey() << "       |\n";
    	std::cout << "|-------|---\n";
    	std::cout << "| 1 | 2 | * |\n";
    	std::cout << "|---|---|---\n";
    	std::cout << "| 3 | 4 | + |\n";
    	std::cout << "|---|---|---\n";
    	std::cout << "| 5 | 6 | / |\n";
    	std::cout << "|---|---|---\n";
    	std::cout << "| 7 | 8 | - |\n";
    	std::cout << "|---|---|---\n";
    	std::cout << "| 9 | C |\n";
    	std::cout << "---------\n";
    }
    ERRORS:
    D:\Program Files\DevStudio\MyProjects\Calculator\Main calculator.cpp(58) : error C2202: 'GetKey' : not all control paths return a value
    D:\Program Files\DevStudio\MyProjects\Calculator\Main calculator.cpp(60) : error C2143: syntax error : missing ')' before '.'
    D:\Program Files\DevStudio\MyProjects\Calculator\Main calculator.cpp(60) : error C2059: syntax error : '.'
    D:\Program Files\DevStudio\MyProjects\Calculator\Main calculator.cpp(60) : error C2059: syntax error : ')'
    D:\Program Files\DevStudio\MyProjects\Calculator\Main calculator.cpp(63) : error C2143: syntax error : missing ';' before '}'
    D:\Program Files\DevStudio\MyProjects\Calculator\Main calculator.cpp(68) : error C2143: syntax error : missing ';' before '}'
    D:\Program Files\DevStudio\MyProjects\Calculator\Main calculator.cpp(73) : error C2143: syntax error : missing ';' before '}'
    D:\Program Files\DevStudio\MyProjects\Calculator\Main calculator.cpp(78) : error C2143: syntax error : missing ';' before '}'
    D:\Program Files\DevStudio\MyProjects\Calculator\Main calculator.cpp(83) : error C2501: 'cout' : missing decl-specifiers
    D:\Program Files\DevStudio\MyProjects\Calculator\Main calculator.cpp(83) : error C2239: unexpected token '<<' following declaration of 'cout'
    D:\Program Files\DevStudio\MyProjects\Calculator\Main calculator.cpp(83) : error C2059: syntax error : '<<'
    D:\Program Files\DevStudio\MyProjects\Calculator\Main calculator.cpp(84) : fatal error C1903: unable to recover from previous error(s); stopping compilation
    Error executing cl.exe.

    Calculator.exe - 12 error(s), 0 warning(s)

    I would follow my errors, but trust me, I AM a newbe I dont get what is wrong, it says "missing ';' before '}' but I cant find where I a missing it!!!
    Why drink and drive when you can smoke and fly?

  9. #9
    Cheesy Poofs! PJYelton's Avatar
    Join Date
    Sep 2002
    Location
    Boulder
    Posts
    1,728
    Change all of your
    Code:
    float multiplication(float nums.num1, float nums.num2)
    {
    	return nums.num1 * nums.num2;
    }
    to
    Code:
    float multiplication(float num1, float num2)
    {
    	return num1 * num2;
    }
    Do this for all four calculations. This should at least fix most of your errors, although your logic is still messed up in places.

  10. #10
    Registered User
    Join Date
    Nov 2002
    Posts
    1,109
    forget the struct, not needed.

    just pass the num1, and num2, to the GetKey() function as parameters.

  11. #11
    Registered User
    Join Date
    Dec 2001
    Posts
    479
    + GetKey() doesnt take 0 parameters!

  12. #12
    Registered User
    Join Date
    Jan 2003
    Posts
    118

    Smile

    Thanks for all of your help but I just keep getting error after error so I will throw this project to the garbage, but Thanks anyways
    Why drink and drive when you can smoke and fly?

  13. #13
    Registered User
    Join Date
    Nov 2002
    Posts
    1,109
    best way to get better is to debug...

  14. #14
    Registered User
    Join Date
    Jan 2003
    Posts
    118
    What is debug?
    Why drink and drive when you can smoke and fly?

  15. #15
    Registered User
    Join Date
    Nov 2002
    Posts
    1,109
    Originally posted by Marcos
    What is debug?
    getting rid or errors and warnings...
    it is part of learning programming, and is very crucial to being successful...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Give me some opinions on setting up a server
    By Shadow in forum A Brief History of Cprogramming.com
    Replies: 12
    Last Post: 04-19-2004, 10:38 AM
  2. Can you give me your tip plz :)
    By dionys in forum C Programming
    Replies: 6
    Last Post: 04-11-2004, 11:14 PM
  3. Can you give me an example of a new line character?
    By Golffor1 in forum C++ Programming
    Replies: 1
    Last Post: 04-10-2003, 11:59 AM
  4. How To Give A Font Colour ?
    By Unregistered in forum Windows Programming
    Replies: 1
    Last Post: 09-14-2001, 01:22 PM
  5. Just to give you an idea of what we're going through...
    By rick barclay in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 09-13-2001, 02:09 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21