-
April 20th, 2010, 08:29 PM
#1
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.
-
April 20th, 2010, 08:42 PM
#2
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.
-
April 20th, 2010, 08:50 PM
#3
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 %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.
-
April 20th, 2010, 09:06 PM
#4
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.
-
April 20th, 2010, 09:06 PM
#5
Re: Variable function problem
Originally Posted by bigmao
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 %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.
-
April 20th, 2010, 10:24 PM
#6
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|