Could somebody please explain to me where I went wrong?

This is a discussion on Could somebody please explain to me where I went wrong? within the C++ Programming forums, part of the General Programming Boards category; Hi guys, could somebody please explain to me where this code is going wrong and why? Code: #include <iostream> using ...

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    5

    Could somebody please explain to me where I went wrong?

    Hi guys,

    could somebody please explain to me where this code is going wrong and why?

    Code:
    #include <iostream>
    
    using namespace std;
    
    void mult (double x, double y);
    void add (double x, double y);
    void sub (double x, double y);
    void div (double x, double y);
    
    int main()
    {
    	int selection = 0;
    	double x = 0;
    	double y = 0;
    		
    	//Main menu option
    	do
    	{
    	cout << "Please select from one of the following options: " << endl 
    		 << "1 - multiplication" << endl
    		 << "2 - addition" << endl
    		 << "3 - subtraction" << endl
    		 << "4 - division" << endl
    		 << "5 - exit" << endl
    		 << "Please enter make your selection: ";
    	cin >> selection;
    	cin.ignore();
    
    		if (selection != 5)
    		{
    			cout << "Please enter the first number: ";
    			cin >> x;
    			cin.ignore();
    
    			cout << "Please enter the second number: ";
    			cin >> y;
    			cin.ignore();
    
    			switch (selection)
    			{
    			case 1:	void mult (double x, double y);
    				break;
    
    			case 2:	void add (double x, double y);
    				break;
    
    			case 3:	void sub (double x, double y);
    				break;
    
    			case 4:	void div (double x, double y);
    				break;
    
    			case 5:	
    				break;
    
    			default:	cout << "Invalid selection!" << endl;
    				break;
    			}
    
    		}
    
    	}while (selection != 5);
    
    	cout << "Thank you for using this calculator!" << endl;
    
    	return 0;
    }
    
    void mult (double x, double y)
    {
    	cout << "The product of your two numbers is " << x * y << endl;
    }
    
    void add (double x, double y)
    {
    	cout << "The sum of your two numbers is " << x + y << endl;
    }
    
    void sub (double x, double y)
    {
    	cout << "The subtract of your two numbers is " << x - y << endl;
    }
    
    void div (double x, double y)
    {
    	cout << "The divisor of your two numbers is " << x / y << endl;
    }

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,433
    It would help if you explain what you think it is doing wrong. Then someone might be able to take a crack at telling you why (presumably) the code is not doing what you expect.

  3. #3
    Maz
    Maz is offline
    Registered User Maz's Avatar
    Join Date
    Nov 2005
    Location
    Finland
    Posts
    194
    cin.ignore()
    I do not know what it does...

    but when you call functions here :
    Code:
    switch (selection)
    			{
    			case 1:	void mult (double x, double y);
    				break;
    
    			case 2:	void add (double x, double y);
    				break;
    
    			case 3:	void sub (double x, double y);
    				break;
    
    			case 4:	void div (double x, double y);
    				break;
    
    			case 5:	
    				break;
    you need not to specify the type of the x & y.
    just use
    Code:
    switch (selection)
    			{
    			case 1:	void mult(x,y);
    				break;
    
    			case 2:	void add(x,y);
    				break;
    
    			case 3:	void sub(x,y);
    				break;
    
    			case 4:	void div(x,y);
    				break;
    
    			case 5:	
    				break;
    And do not use space between the name of the function and () (although I'm not sure if it's allowed, but I think it's not.)

  4. #4
    Registered User
    Join Date
    Nov 2005
    Posts
    5

    Sorry!

    Sorry about the confusion, I have been trying to fix it myself for a day now and I still haven't figured it out, so I am a bit frustrated as I am new to programming.

    The functions are meant to be printing the result of the calculation to the screen and they are not doing so, it is just going back to the menu once the numbers have been entered. I have no idea why, I thought that void functions were correct. Have I made a pass mistake? Should I be passing by reference or something? If so, could somebody please explain to me how to?

    Thank you very much guys!

  5. #5
    Registered User
    Join Date
    Nov 2005
    Posts
    5
    Maz,

    my compiler, which is Visual C++ won't allow me to do that, I have tried. It gives me some obscure answer about incorrect overloading.

    cin.ignore() is supposed to wipe the previous part of the memory that the data was stored into. So I hear anyway...

  6. #6
    Maz
    Maz is offline
    Registered User Maz's Avatar
    Join Date
    Nov 2005
    Location
    Finland
    Posts
    194
    odd... I've never really used M$ products, but I assume they should operate as other compilers...

    I do not know if this is related to your problem, but why using do there?

    If I was doing the code, I would use
    Code:
    int selection=0;
    .
    .
    .
    while(selection != 5)
    {
               cout << options; //well, I'm too lazy to type your menu :D
               cin >> selection;
               //switch - case structure
    
    }// end of while
    .
    .
    .

  7. #7
    Logic Programmer logicwonder's Avatar
    Join Date
    Nov 2005
    Location
    Kerala, India
    Posts
    52
    your code has lot of syntax errors, try learning excat syntax. I have also included division by zero check.
    Here is the corrected code

    Code:
    #include <iostream.h>
    #include <conio.h>
    
    
    void mult (double x, double y);
    void add (double x, double y);
    void sub (double x, double y);
    void div (double x, double y);
    
    int main()
    {
    	int selection = 0;
    	double x = 0;
    	double y = 0;
    
    	//Main menu option
    	do
    	{
    	cout << "Please select from one of the following options: " << endl
    		 << "1 - multiplication" << endl
    		 << "2 - addition" << endl
    		 << "3 - subtraction" << endl
    		 << "4 - division" << endl
    		 << "5 - exit" << endl
    		 << "Please enter make your selection: ";
    	cin >> selection;
    	cin.ignore();
    
    		if (selection != 5)
    		{
    			cout << "Please enter the first number: ";
    			cin >> x;
    			cin.ignore();
    
    			cout << "Please enter the second number: ";
    			cin >> y;
    			cin.ignore();
    
    			switch (selection)
    			{
    			case 1:	mult (x, y);
    				break;
    
    			case 2:	add (x, y);
    				break;
    
    			case 3:	sub (x, y);
    				break;
    
    			case 4:	div ( x,  y);
    				break;
    
    			case 5:
    				break;
    
    			default:	cout << "Invalid selection!" << endl;
    				break;
    			}
    
    		}
    
    	}while (selection != 5);
    	cout << "Thank you for using this calculator!" << endl;
    	getch();
    	return 0;
    }
    
    void mult (double x, double y)
    {
    	cout << "The product of your two numbers is " << x * y << endl;
    	getch();
    }
    
    void add (double x, double y)
    {
    	cout << "The sum of your two numbers is " << x + y << endl;
    	getch();
    }
    
    void sub (double x, double y)
    {
    	cout << "The subtract of your two numbers is " << x - y << endl;
    	getch();
    }
    
    void div (double x, double y)
    {
    	if(y==0) cout<<"Cannot divide by Zero"<<endl; //division by zero check
    	else
    	cout << "The divisor of your two numbers is " << x / y << endl;
    	getch();
    }
    L GIK wins!!!
    Salutes from logicwonder
    Enjoy programming

  8. #8
    Registered User
    Join Date
    Nov 2005
    Posts
    5
    I was using the do while loop there as I want the menu to display at least once.

    From what I can understand it is not a compiler issue that I am having.

  9. #9
    Logic Programmer logicwonder's Avatar
    Join Date
    Nov 2005
    Location
    Kerala, India
    Posts
    52
    if u r using VC++ compiler u dont need conio.h and the getch(); used in DOS compilers.

    Finally u can code it more simple and efficient!
    Last edited by logicwonder; 11-22-2005 at 02:59 AM.
    L GIK wins!!!
    Salutes from logicwonder
    Enjoy programming

  10. #10
    Registered User
    Join Date
    Nov 2005
    Posts
    5
    Thanks for your help logic wonder,

    I have implemented the code that you have assigned and although I had to change it slightly, I have now understoof where my code went wrong, thank you very much for your help.

    Issue resolved.

  11. #11
    Maz
    Maz is offline
    Registered User Maz's Avatar
    Join Date
    Nov 2005
    Location
    Finland
    Posts
    194
    mm.. Just a question that came into my mind..
    Isn't it better to use
    #include <iostream>
    using namespace std;
    than
    # include <iostream.h>
    ?

    AFAIK iostream.h is older, and using global namespace should be avoided??
    (Well, at least as far as I remember, I had troubles with iostream.h with g++ compiler when I did more c++ than nowadays.)

    please correct me if I am wrong

  12. #12
    Logic Programmer logicwonder's Avatar
    Join Date
    Nov 2005
    Location
    Kerala, India
    Posts
    52
    well i use TCC DOS compiler C89 . And using namespace std is not valid. May be u r right. Could u explain about using namespace std?

  13. #13
    Maz
    Maz is offline
    Registered User Maz's Avatar
    Join Date
    Nov 2005
    Location
    Finland
    Posts
    194
    using namespace std; just sets the code to operate in standard namespace as a default.
    so instead of typing std::cout you can just type cout.

    AFAIK iostream.h libraries operate in "general namespace", which is old (deprecated?) way, so when using iostream.h, one does not need to specify namespaces. Anyways, I have, from somewhere, absorbed the idea that not using namespaces (using general namespace) is old way, and against newest standards.

    As always, I may be wrong tho.

    If I remember right, my g++ compiler complained about using iostream.h, and although I am not sure, I think that all functions did not work with old iostream.h

  14. #14
    Registered User
    Join Date
    Nov 2005
    Posts
    52
    Maz: you are correct. <iostream.h> is a deprecated header in C++, and <iostream> is the preferred one. The using namespace std; line merely imports all of the symbols from the std namespace into the global namespace, which you should be very careful of doing in any but the most trivial of projects.

  15. #15
    Registered User
    Join Date
    Jan 2005
    Posts
    7,340
    Actually <iostream.h> is not deprecated in C++. It is simply non-standard. Deprecated means that it is must be supported but you should use the newer way. However, since <iostream.h> is not deprecated, and not standard at all, it doesn't have to be suported by standards-compliant compilers. Several modern compilers (e.g. VC++ 2003 or 2005) do not support <iostream.h> at all.

    So unless you are using a very old compiler like logicwonder is, then you should always use <iostream>. And if you are using <iostream>, you must specify the std namespace somehow, with the using directive being the simplest way.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Confused: What is wrong with void??
    By Machewy in forum C++ Programming
    Replies: 19
    Last Post: 04-15-2003, 12:40 PM
  2. pow function returns wrong value
    By ckbales in forum C Programming
    Replies: 5
    Last Post: 02-01-2003, 08:46 PM
  3. God
    By datainjector in forum A Brief History of Cprogramming.com
    Replies: 746
    Last Post: 12-22-2002, 11:01 AM
  4. What's wrong with this???
    By beginner in forum C++ Programming
    Replies: 12
    Last Post: 11-25-2002, 09:52 PM
  5. Whats wrong?
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 07-14-2002, 01:04 PM

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