Variable function problem
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6

Thread: Variable function problem

  1. #1
    Join Date
    Apr 2010
    Posts
    5

    Variable function problem

    Hi ive been making my C++ program for several hours and I cant seem to make it work.

    heres the code

    Code:
    #include <stdio.h>
    #include <conio.h>
    #include "again.h"
    
    int main()
    {
    	int x=0,y=0;
    	char cmd;
    	do{
    	printCommand();
    	cmd = getch();
    
    	switch(cmd){
    
    	case 'a'
    		:initialize(&x,&y);
    		 printf("\nx=%d,y=%d",x,y);
                break;
    	case 'b'
    		:printLocation(x,y);
    			break;
    	case 'c'
    		:move(&x,&y);
    		printf("\nThe current location is x=%d,y=%d",x,y);
    			break;
    
    	
    	}		
    	}
    	while(cmd!='d');
    	printf("Goodbye:");
    		exit();
    		
    		
        
    }
    
    char printCommand(void){
    
    printf("Enter the command you want to execute\nA.]Origin\nB.]Location\nC.]MOVE\nD.]EXIT\n");
    return 0;
    
    }
    
    void initialize(int *x,int *y){
    
    	*x=0;
    	*y=0;
    
    }
    
    void printLocation (int x, int y){
    
    	
    	printf("\nthe current location is %d,%d",&x,&y);
    
    
    
    }
    
    void move(int *x, int *y){
    
    	int direction,points;
    
    	printf("\nEnter the direction you want to move: ");
    	scanf("%d",&direction);
    	printf("\nhow many points you want to move?:");
    	scanf("%d",&points);
    
    
    		if(direction==1)
    			*x+=points;
    		else if(direction==2)
    			*y+=points;
    		else if(direction==3)
    			*x-=points;
    		else if(direction==4)
    			*y-=points;
    		else
    
    		printf("\nINVALID DIRECTION!!!:");
    
    }
    
    
    void exit(void)
    {
    
    }
    I have a one or two problem, the main problem is that the fuction printLocation it doesnt return the correct value of x and y instead it gave me a very huge amount, the other problem is how can i terminate a program using a fuction. thanks in advance.

  2. #2
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Fairfax, VA
    Posts
    10,888

    Re: Variable function problem

    You're trying to print the address of x and y. Lose the ampersands.

    Also, I don't understand your second question.

  3. #3
    Join Date
    Apr 2010
    Posts
    5

    Re: Variable function problem

    What do you mean lose the ampersands, theres no ampersand in my function PrintLocation.

    Code:
    case 'b'
    		:printLocation(x,y);
    			break;
    void printLocation (int x, int y){
    
    	
    	printf("\nthe current location is &#37;d,%d",&x,&y);
    
    
    
    }
    About my 2nd question, Ive been assigned to use this function void exit(void) which terminate/exit the program when called.

  4. #4
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Fairfax, VA
    Posts
    10,888

    Re: Variable function problem

    Look closer, they're there.

    Using exit(0) really isn't a good practice, I wouldn't recommend it. You should exit by returning from main().

    However, if it's required, just call the exit(int) library function.

  5. #5
    Join Date
    Aug 2008
    Posts
    902

    Re: Variable function problem

    Quote Originally Posted by bigmao View Post
    What do you mean lose the ampersands, theres no ampersand in my function PrintLocation.

    Code:
    case 'b'
    		:printLocation(x,y);
    			break;
    void printLocation (int x, int y){
    
    	
    	printf("\nthe current location is &#37;d,%d",&x,&y);
    
    
    
    }
    About my 2nd question, Ive been assigned to use this function void exit(void) which terminate/exit the program when called.
    yes there is...

    printf("\nthe current location is %d,%d",&x,&y);

    EDIT:

    Also this is really a C program, not a C++.

    stdio.h is C and so is all of it's functions, like printf, sprintf and scanf.

    In C++ you use the C++ standard library, not the C standard library. You can use std::cout, std::stringstream and std:cin instead.

    conio.h is highly antiquated and was never standard. I don't suggest you use this, ever.
    Last edited by Chris_F; April 20th, 2010 at 09:12 PM.

  6. #6
    Join Date
    Apr 2010
    Posts
    5

    Re: Variable function problem

    Forgive to unable to see it, thanks for all the help now it working fine now

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center