Thread: returning arrays from functions

  1. #1
    Unregistered Leeman_s's Avatar
    Join Date
    Oct 2001
    Posts
    753

    returning arrays from functions

    here is the code:

    Code:
    #include <windows.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <conio.h>
    #include <fstream.h>
    #include <iostream.h>
    #include <string.h>
    
    int keypress=0;
    char message[50];
    
    int space1gone=0;
    int space2gone=0;
    
    void frame (int xmin,int ymin,int xmax,int ymax);
    void gotoxy(int,int);
    void getmove(char message[], int space1gone, int space2gone);
    char determinemessage(char message[], int keypress);
    
    void frame (int xmin, int ymin, int xmax, int ymax)
    {
       int count;
       gotoxy(xmin,ymin);
       putchar(218);
       gotoxy(xmax,ymin);
       putchar(191);
       gotoxy(xmin,ymax);
       putchar(192);
       gotoxy(xmax,ymax);
       putchar(217);
       gotoxy(xmin+1,ymin);
       for(count=xmin; count<xmax-1; count++)
          putchar(196);
       gotoxy(xmin+1,ymax);   
       for(count=xmin; count<xmax-1; count++)
          putchar(196);   
       for(count=ymin+1; count<=ymax-1; count++)
       {
          gotoxy(xmin,count);
          putchar(179);
       }   
       for(count=ymin+1; count<=ymax-1; count++)
       {
          gotoxy(xmax,count);
          putchar(179);
       }
    }
    
    void gotoxy(int x, int y)
    {
    	HANDLE hConsoleOutput;
    	COORD dwCursorPosition;
    
    	dwCursorPosition.X = x;
    	dwCursorPosition.Y = y;
    	hConsoleOutput = GetStdHandle(STD_OUTPUT_HANDLE);
    	SetConsoleCursorPosition(hConsoleOutput,dwCursorPosition);
    }
    
    void getmove(char message[], int space1gone, int space2gone)
    {
    	char tempmessage[50];
    
    	if(space2gone==0) //checks if first spot is open
    	{
    		if(space1gone==0) //checks if second spot is open
    		{
    			gotoxy(1,22);
    			cout<<message;
    			space1gone=1;
    		}
    		else if(space1gone==1) //first spot is used, second is open
    		{
    			gotoxy(1,23);
    			cout<<message;
    		}
    	}
    	else if(space2gone==1) //second spot is used
    	{
    		strcpy(tempmessage, message);
    		gotoxy(1,22);
    		cout<<tempmessage;
    		gotoxy(1,23);
    		cout<<message;
    	}
    
    }
    
    char determinemessage(char message[], int keypress)
    {
    	if(keypress==72)
    		strcpy(message, "You moved up");
    	if(keypress==75)
    		strcpy(message, "You moved left");
    	if(keypress==80)
    		strcpy(message, "You moved down");
    	if(keypress==77)
    		strcpy(message, "You moved right");
    	return message;
    }
    that's the header file, here is main:

    Code:
    #include <iostream.h>
    #include <windows.h>
    #include <conio.h>
    #include <fstream.h>
    #include "functions.h"
    
    int main()
    {
    	frame(0,0,55,20); //Biggest box
    	frame(56,0,79,24); //Right-side box
    	frame(0,21,55,24); //Message Box
    
    	for(;;)
    	{
    	    if(kbhit())
    		{
    		    keypress=_getch();
    			message=determinemessage(message, keypress);
    			getmove(message, space1gone, space2gone);
    		}
    	}
    	return 0;
    }
    i use msvc++. i think it is evident that i am trying to return the array of chars called message. how would i declare a function that returns an array of chars, not just a char?

    i tried 'determinemessage[](....etc)'

  2. #2
    Unregistered Leeman_s's Avatar
    Join Date
    Oct 2001
    Posts
    753
    anybody help?

  3. #3
    I don't know. Here's some advise though. When you post code, turn off smileys

  4. #4
    geek SilentStrike's Avatar
    Join Date
    Aug 2001
    Location
    NJ
    Posts
    1,141
    You don't need to return a char*. The contents of message are modified within the determine message function.
    Prove you can code in C++ or C# at TopCoder, referrer rrenaud
    Read my livejournal

  5. #5
    Unregistered Leeman_s's Avatar
    Join Date
    Oct 2001
    Posts
    753
    i don't know what you mean. what would the code look like?

  6. #6
    Unregistered Leeman_s's Avatar
    Join Date
    Oct 2001
    Posts
    753
    ok i got it. but, now when i run it, it goes EXTREMELY SLOW! I hit the arrow keys like 10 times, then like 5 seconds later they all print out.

  7. #7
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    2 problems that are just annoying me, and yes, i know am a complete perfectionist: 1, you're bumping posts, 2, youre asking people to write the code for you.

    I thought that you could return an array as long as you put return array_name;. If that doesn't work, you could try using a pointer, and then of course you could always have it modify a global variable.

  8. #8
    Unregistered Leeman_s's Avatar
    Join Date
    Oct 2001
    Posts
    753
    I figured it out. Here is the problem now though. IT GOES REALLY SLOW!

    Code:
    #include <iostream.h>
    #include <windows.h>
    #include <conio.h>
    #include <fstream.h>
    #include "functions.h"
    
    int main()
    {
    	frame(0,0,55,20); //Biggest box
    	frame(56,0,79,24); //Right-side box
    	frame(0,21,55,24); //Message Box
    
    	for(;;)
    	{
    	    if(kbhit())
    		{
    		    keypress=_getch();
    			determinemessage(message, tempmessage, keypress);
    			getmove(message, tempmessage, space1gone, space2gone);
    		}
    	}
    	return 0;
    }
    Code:
    #include <windows.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <conio.h>
    #include <fstream.h>
    #include <iostream.h>
    #include <string.h>
    
    int keypress=0;
    char message[50];
    char tempmessage[50];
    
    int space1gone=0;
    int space2gone=0;
    
    void frame (int xmin,int ymin,int xmax,int ymax);
    void gotoxy(int,int);
    void getmove(char message[], char tempmessage[], int space1gone, int space2gone);
    void determinemessage(char message[], char tempmessage[], int keypress);
    
    void frame (int xmin, int ymin, int xmax, int ymax)
    {
       int count;
       gotoxy(xmin,ymin);
       putchar(218);
       gotoxy(xmax,ymin);
       putchar(191);
       gotoxy(xmin,ymax);
       putchar(192);
       gotoxy(xmax,ymax);
       putchar(217);
       gotoxy(xmin+1,ymin);
       for(count=xmin; count<xmax-1; count++)
          putchar(196);
       gotoxy(xmin+1,ymax);   
       for(count=xmin; count<xmax-1; count++)
          putchar(196);   
       for(count=ymin+1; count<=ymax-1; count++)
       {
          gotoxy(xmin,count);
          putchar(179);
       }   
       for(count=ymin+1; count<=ymax-1; count++)
       {
          gotoxy(xmax,count);
          putchar(179);
       }
    }
    
    void gotoxy(int x, int y)
    {
    	HANDLE hConsoleOutput;
    	COORD dwCursorPosition;
    
    	dwCursorPosition.X = x;
    	dwCursorPosition.Y = y;
    	hConsoleOutput = GetStdHandle(STD_OUTPUT_HANDLE);
     	SetConsoleCursorPosition(hConsoleOutput,dwCursorPosition);
    }
    
    void getmove(char message[], char tempmessage[], int space1gone, int space2gone)
    {
    	if(space2gone==0) //checks if first spot is open
    	{
    		if(space1gone==0) //checks if second spot is open
    		{
    			gotoxy(1,22);
    			cout<<message;
    		}
    		else if(space1gone==1) //first spot is used, second is open
    		{
    			gotoxy(1,23);
    			cout<<message;
    		}
    		space1gone=1;
    	}
    	else if(space2gone==1) //second spot is used
    	{
    		gotoxy(1,22);
    		cout<<tempmessage;
    		gotoxy(1,23);
    		cout<<message;
    	}
    	space2gone=1;
    
    }
    
    void determinemessage(char message[], char tempmessage[], int keypress)
    {
    	strcpy(tempmessage,message);
    
    	if(keypress==72)
    		strcpy(message, "You moved up");
    	if(keypress==75)
    		strcpy(message, "You moved left");
    	if(keypress==80)
    		strcpy(message, "You moved down");
    	if(keypress==77)
    		strcpy(message, "You moved right");
    }
    i dont know why it does that. someone try compiling it please

  9. #9
    Registered User
    Join Date
    May 2002
    Posts
    317
    Why exactly are you creating a temp array? When you pass arrays by name you pass them by reference, therefore their is no need for a temp array or to assign a return value. This should speed up your program.

  10. #10
    Unregistered Leeman_s's Avatar
    Join Date
    Oct 2001
    Posts
    753
    I have a table that can hold two lines. I want to print on the first line, if it's take but the second line is free then print there, and if both are used, then I catch the PREVIOUS message in tempmessage, print that in line 1, and the new message in line 2. So it sort of 'scrolls'.

    I have two problems now.

    1) It goes *VERY* slow

    2) It doesn't keep printing in my box

    here is the code:

    Code:
    #include <iostream.h>
    #include <windows.h>
    #include <conio.h>
    #include <fstream.h>
    #include <stdlib.h>
    #include <string>
    #include <stdio.h>
    
    int keypress=0;
    char message[50];
    char tempmessage[50];
    
    int space1gone=0;
    int space2gone=0;
    
    void frame (int xmin,int ymin,int xmax,int ymax);
    void gotoxy(int,int);
    void printmove(char message[], char tempmessage[], int& space1gone, int& space2gone);
    void determinemessage(char message[], char tempmessage[], int keypress);
    
    ///////////////////////////////////////////////////////////////////////////////////////////
    ///////////////////////////////////////////////////////////////////////////////////////////
    int main()
    {
    	frame(0,0,55,20); //Biggest box
    	frame(56,0,79,24); //Right-side box
    	frame(0,21,55,24); //Message Box
    
    	for(;;)
    	{
    	    if(kbhit())
    		{
    		    keypress=_getch();
    			determinemessage(message, tempmessage, keypress); //Determine what to print
    			printmove(message, tempmessage, space1gone, space2gone); //print in correct spot
    		}
    	}
    	return 0;
    }
    ////////////////////////////////////////////////////////////////////////////////////////////
    ////////////////////////////////////////////////////////////////////////////////////////////
    ////////////////////////////////////////////////////////////////////////////////////////////
    
    void frame (int xmin, int ymin, int xmax, int ymax)
    {
       int count;
       gotoxy(xmin,ymin);
       putchar(218);
       gotoxy(xmax,ymin);
       putchar(191);
       gotoxy(xmin,ymax);
       putchar(192);
       gotoxy(xmax,ymax);
       putchar(217);
       gotoxy(xmin+1,ymin);
       for(count=xmin; count<xmax-1; count++)
          putchar(196);
       gotoxy(xmin+1,ymax);   
       for(count=xmin; count<xmax-1; count++)
          putchar(196);   
       for(count=ymin+1; count<=ymax-1; count++)
       {
          gotoxy(xmin,count);
          putchar(179);
       }   
       for(count=ymin+1; count<=ymax-1; count++)
       {
          gotoxy(xmax,count);
          putchar(179);
       }
    }
    
    void gotoxy(int x, int y)
    {
    	HANDLE hConsoleOutput;
    	COORD dwCursorPosition;
    
    	dwCursorPosition.X = x;
    	dwCursorPosition.Y = y;
    	hConsoleOutput = GetStdHandle(STD_OUTPUT_HANDLE);
     	SetConsoleCursorPosition(hConsoleOutput,dwCursorPosition);
    }
    
    void printmove(char message[], char tempmessage[], int& space1gone, int& space2gone)
    {
    	if(space2gone==0) //checks if first spot is open
    	{
    		if(space1gone==0) //checks if second spot is open
    		{
    			gotoxy(1,22);
    			cout<<message;
    		}
    		else if(space1gone==1) //first spot is used, second is open
    		{
    			gotoxy(1,23);
    			cout<<message;
    		}
    		space1gone=1;
    	}
    	else if(space2gone==1) //second spot is used
    	{
    		gotoxy(1,22);
    		cout<<tempmessage;
    		gotoxy(1,23);
    		cout<<message;
    	}
    	space2gone=1;
    
    }
    
    void determinemessage(char message[], char tempmessage[], int keypress)
    {
    	strcpy(tempmessage,message);
    
    	if(keypress==72)
    		strcpy(message, "You moved up");
    	if(keypress==75)
    		strcpy(message, "You moved left");
    	if(keypress==80)
    		strcpy(message, "You moved down");
    	if(keypress==77)
    		strcpy(message, "You moved right");
    }
    i would really appreciate some help

  11. #11
    Unregistered
    Guest
    It goes slow because of the kbhit() function. Plain and simple. Try and find away around using that particular function, though you might have to.

  12. #12
    Unregistered Leeman_s's Avatar
    Join Date
    Oct 2001
    Posts
    753
    well actually i figured out it wasnt the kbhit, it was that nothing was being displayed until a couple of hits.

    and I also learned that function CANNOT return arrays themselves.

    I've got a nice working program so far now

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Arrays and Functions
    By KunoNoOni in forum Game Programming
    Replies: 12
    Last Post: 10-04-2005, 09:41 PM
  2. passing 2dimensional arrays to functions
    By owi_just in forum C Programming
    Replies: 1
    Last Post: 04-25-2005, 08:08 AM
  3. Replies: 5
    Last Post: 09-19-2003, 03:47 AM
  4. Returning arrays from a function
    By mattbrrtt in forum C Programming
    Replies: 7
    Last Post: 12-14-2001, 08:29 PM
  5. elements of arrays; functions
    By sballew in forum C Programming
    Replies: 6
    Last Post: 09-03-2001, 01:48 AM