Function not working, is it my compiler

This is a discussion on Function not working, is it my compiler within the C++ Programming forums, part of the General Programming Boards category; Im trying to write this program and i think i am close to being complete but im getting 2 errors ...

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    5

    Function not working, is it my compiler

    Im trying to write this program and i think i am close to being complete but im getting 2 errors C2448, C3861. I am using microsoft visual studio 2005 and when i look up the definitions for the code it doesnt help much anyone have an idea of whats going on
    Code:
    #include "stdafx.h"
    #include <iostream>
    using namespace std;
    
    
    
    int _tmain(int argc, _TCHAR* argv[])
    {
    int *x;
    int *y;
    int *a;
    int *b;
    int distance;
    
    	cout<<"Enter the x and y values for first coordinate: ";
    	cin>>*x, *y;
    	cout<<"Enter the x and y values for the second coordinate: ";
    	cin>>*a, *b;
    
    
    	int getcoords(*x, *y, *a, *b, distance )
    	{
    		distance = sqrt((a-x)^2+(b-y)^2);
    		cout<<"The distance between the two coordnates is  "<< distance;
    	}
    
    
    
    	return 0;
    }
    thanks for any help

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,541
    1. Remove all those * from all your integer variables.
    2. You're trying to declare a function inside a function - this isn't allowed.
    3. Your getcoords function has no parameter types.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >
    >
    > cin>>*x, *y;
    And use >> to separate inputs to cin, not a comma.

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    > distance = sqrt((a-x)^2+(b-y)^2);
    And ^ is the xor operator. Use the pow() function to raise to a power, or in this case you could do a multiplication.

  5. #5
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,239
    Yes, clearly the compiler is at fault

  6. #6
    Registered User
    Join Date
    Oct 2007
    Posts
    5
    Well im trying to use pointers and have them work with the function. I am a student and i am having trouble. I am not asking for anyone to do my homework for me, thats my job, but if anyone has any tips or can provide any explanation on this because i am a little lost and my teacher isn't much help because he won't respond to email frequently and i only have class once a week. If this is a problem or if anyone believes i should not be asking anyone here for help please tell me so i can stop posting and you wont have to hear about it.

  7. #7
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,541
    So when you declare a pointer, make sure it points somewhere.

    Code:
    int *a = new int;
    cin >> *a;
    delete a;
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  8. #8
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,239
    Quote Originally Posted by bill.thompson65 View Post
    Well im trying to use pointers and have them work with the function. I am a student and i am having trouble. I am not asking for anyone to do my homework for me, thats my job, but if anyone has any tips or can provide any explanation on this because i am a little lost and my teacher isn't much help because he won't respond to email frequently and i only have class once a week. If this is a problem or if anyone believes i should not be asking anyone here for help please tell me so i can stop posting and you wont have to hear about it.
    There's no problem with what you're asking, you're doing fine. But the chances of you bumping into a compiler bug with simple code like this are astronomically low. That's all I mean.

  9. #9
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,304
    This is the second thread today I've read where someone is just writing random code and praying for it to make sense to the compiler. STOP! You just can't learn to program like that.
    For every little thing you don't know how to do, DON'T JUST GUESS how to do it. You won't get it right. To learn C or C++ you absolutely MUST spend most of your time learning (e.g. from a book) and when you've read how to do something then and only then do you actually write the line of code to do it.
    There are no shortcuts.

    I work on a project with close to a million lines of code in VS2005 and afaik we haven't seen any compiler bug. Chances are you never will either!
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  10. #10
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Yes, blaiming the compiler in this case is like blaiming the tyre manufacturer for a puncture. If you ran over a nail or screw, it's not their fault. If you can SHOW that the tyre was incorrectly produced, it's their fault. Most punctures you can't - you may not be able to find the nail/screw/glass or whatever that caused the puncture, but it doesn't make it Dunlop's or Michelin's fault.

    Like iMalc, I work on large projects. I have, in the last 20 years as a professional programmer, seen half a dozen or so compiler bugs. It is slightly more likely if you do "strange things". One that comes to mind was a "bad optimization" in a windows x86 compiler (Not Microsoft). It generated code that would restore the stack first, then restore registers from a negative offset on the stack. Which works fine when you are in user-mode, but not when the code runs in kernel mode - especially not when this code is JUST after the "enable interrupt" - because sooner or later, an interrupt happens, splat - the top of the stack is now containing your interrupt context, then we restore registers from the splatted area, and bang, bad things happen. That was about 7 years ago, and I was doing really unusual things with this compiler. [and I managed to work around it by fiddling with some optimization switches to make the compiler choose a different way to produce the function epilogue].

    Also, the more "unusual" the compiler is, the more likely it is that it's got bugs.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  11. #11
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398
    I agree with iMalc, you are trying to do way to much at once.

    - I suggest you start-out with a program that gets one value from the user, and saves that value using a pointer. Then write a cout line that displays the value. When that works, do the same with the other 3 coordinates.

    - Then, write a function that does nothing but displays the 4 coordinates. Don't forget to make a function portotype, a function definition, and a function call.

    - When that works, add the calculation to your function.

    ...Or, you might want to simplify it even more... You can try first writing the program without using pointers or a seperate function. Then, you can add those "features" one at a time. The idea is to write and debug one or two lines at a time, so that you know where your errors are, you will have fewer errors to deal with at one time, and you can more easily debug them.

    int _tmain(int argc, _TCHAR* argv[])
    Is this the way you learned in class? It's microsoft-specific. (int main() also works with the Microsoft compiler.)


    Quoting myself...
    Quote Originally Posted by DougDbug
    I have some general advice for you...

    Don't try to write the whole program at once. If you write the whole program before compiling, programming is going to be very frustrating.

    Always start-out with a little Hello World type program.... When that's working, add one or two lines of code, then test-compile and test-run.... Repeat 'till done.

    If you use this method, you won't get a bunch of errors at one time, and you will know exactly which lines have caused the error.

    This does NOT mean that you can write the code in "line-number order" a few lines at a time! (A program won't compile if you simply chop-off the 2nd half!)....

    ....If you use this technique, programming will be a lot more fun! Professional programmers use similar techniques. Of course, they are not test-compiling every one or two lines. But, they are working on huge programs and they do write (and test) their code in managable chunks.

    I'm NOT saying that "trial-and-error" is a good programming technique. You should have a design and a plan, and you should understand what you are doing. But, you probably won't make it through beginning programming without some "experimentation". And, it is good programming practice to test your code as you are developing it.
    Last edited by DougDbug; 11-16-2007 at 07:19 PM.

  12. #12
    Registered User
    Join Date
    Oct 2007
    Posts
    5
    Ok well i was not trying to blame the complier, someone told me compilers can have bugs. I am very new at this and i am trying my best. I have been reading, i do have books. I would like to thank you all for the help as well. The explanation of starting simple with one variable then working the program up is one of the best tips i have heard and i am putting it to use, thank you all again.

  13. #13
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Yes, it happens that compilers have bugs. One problem I was loosely involved with in the past was when a "machine generated" function of 90000+ lines of code would generate code that crashed. But for your average small piece of code, using a "good" compiler, it's very unlikely that you will hit a compiler bug.

    Glad that we could help you get "wiser".

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  14. #14
    Registered User
    Join Date
    Nov 2007
    Location
    Stoneham,Québec
    Posts
    10
    try it this way
    Code:
    #include <iostream>
    #include <cmath>
    using namespace std;
    
    
    float getcoords(int x1, int y1, int a1, int b1);
    int main()
    {
    int x,y,a,b,distance;
    
    
    	cout<<"Enter the x and y values for first coordinate: ";
    	cin>> x >> y;
    	cout<<"Enter the x and y values for the second coordinate: ";
    	cin>> a >> b;
    
        cout<<"The distance between the two coordnates is  "<< getcoords(x,y,a,b)<<endl;
    	
    
    
        system("PAUSE");
    	return 0;
    }
    float getcoords(int x1, int y1, int a1, int b1)
    {
        return sqrt(((a1-x1)*(a1-x1))+((b1-y1)*(b1-y1)));
    }
    that shud work...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Calling a Thread with a Function Pointer.
    By ScrollMaster in forum Windows Programming
    Replies: 6
    Last Post: 06-10-2006, 08:56 AM
  2. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 01:28 PM
  3. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM
  4. Replies: 5
    Last Post: 02-08-2003, 06:42 PM
  5. qt help
    By Unregistered in forum Linux Programming
    Replies: 1
    Last Post: 04-20-2002, 09:51 AM

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