3 Replies - 2567 Views - Last Post: 30 May 2009 - 07:43 PM Rate Topic: -----

#1 morgog  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 23-April 09

How to pass a string array to a function?

Post icon  Posted 28 May 2009 - 05:06 AM

:blink:

I have been set an exercise in C (NOT C++) to read a comma delimited file (Column 1 - Alphabet Letter, Column 2 - Morse Code for the letter) into an array. There is also a 2nd file with an encoded message(1 Morse code per line).
The task was to read the MORSE.txt file into an array and then to read the CODE.txt .. decoding the secret message and displaying onto the screen.
The code below accomplishes the task of decoding the message, BUT

MORSE.CPP

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <windows.h>
#include "CtrMsge.h"

void main()
{
	FILE * Morse;
	FILE * Msg;

	char Morse_Code[26][6];
	char Letter[26];
	int Morse_Ctr = 0, Records_Ctr=50, MsgCtr = 0;
	char Msge_code[50][6];

	Msg=fopen("CODE.TXT","r");
	Records_Ctr = ReadMsg();							// Number of records in the message file


	Morse=fopen("MORSE.TXT","r");					// Reading the morse code into memory array
	fscanf(Morse,"%c,%[^\n]%*c",&Letter[Morse_Ctr],Morse_Code[Morse_Ctr]);
																			 
	while(Morse_Ctr < 25)
	{
		Morse_Ctr++;
		fscanf(Morse,"%c,%[^\n]%*c",&Letter[Morse_Ctr],&Morse_Code[Morse_Ctr]);
	}
	fclose(Morse);								// Morse code finished loading into memory array
	while (MsgCtr < Records_Ctr)							// while there are still records in the msge
	{										// file, read each line
		fscanf(Msg,"%[^\n]%*c",Msge_code[MsgCtr]);
		Morse_Ctr=0;

		while(Morse_Ctr < 25)						// while ever the last letter of the alphabet
		{										   	// has not been tested
			if(strcmp(Msge_code[MsgCtr],Morse_Code[Morse_Ctr])==0)		
																						// compare the string read to each string
			{								// that has been read from the Morse.txt file.
				printf("%c ",Letter[Morse_Ctr]);		// This will find the morse character &
			}								 		// convert it to an alphabet character.
			Morse_Ctr++;
		}
		MsgCtr++;								 		// continue until the message count is completed
	}
	fclose(Msg);
	scanf("%*c");
}


CTRMSGE.H															  // Counts the number of records in the CODE.txt file
{
int ReadMsg()

	FILE * Message;
	Message=fopen("CODE.TXT","r");

	char Decode[6];
	int Ctr=0, Records_Ctr=0;

	while(!feof(Message))
	{
		fscanf(Message,"%[^\n]%*c",&Decode);
		Ctr++;
	}
	fclose(Message);
	Records_Ctr = Ctr;
	return Records_Ctr;
}



My problem stems from me having 'no clue' about how to:

1 - declare the array in the .cpp file,
2- Declare a struct header for the Morse.txt file fields
3 - pass the initial array (reading the MORSE.txt) to a function,
4 - decode the Code.txt file within the function and then return to the main .cpp file.


A second part of the exercise is to write an alpha message & encode into Morse code & write to a file (again passing the array to a function for encoding). I haven't completed this as yet.

I have googled, asked the teacher, but my mental process must be approaching the "cretin" stage, cause I don't have a clue.
Any help .. especially a "REALLY dumbed-down" example would be gratefully appreciated..

TIA

Is This A Good Question/Topic? 0
  • +

Replies To: How to pass a string array to a function?

#2 AmitTheInfinity  Icon User is offline

  • C Surfing ∞
  • member icon

Reputation: 117
  • View blog
  • Posts: 1,559
  • Joined: 25-January 07

Re: How to pass a string array to a function?

Posted 28 May 2009 - 05:24 AM

Wow, you did that much of coding and you are stuck at THIS??? well I did searched with google only to find how many links I get and there were plenty. But I will link you to links that are explaining the basics [as you said]. I hope this will help you. :)

1 - declare the array in the .cpp file,
2- Declare a struct header for the Morse.txt file fields
3 - pass the initial array (reading the MORSE.txt) to a function,
(reading the MORSE.txt)
4 - decode the Code.txt file within the function and then return to the main .cpp file. - It's like combination of above things and the logic from your code.

This post has been edited by AmitTheInfinity: 28 May 2009 - 05:25 AM

Was This Post Helpful? 1
  • +
  • -

#3 morgog  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 23-April 09

Re: How to pass a string array to a function?

Posted 30 May 2009 - 07:41 PM

I know it must seem strange, but I just can't my head around the syntax or maybe I'm not thinking.
Thank you for the links.
I appreciate you taking the time to answer, what must seem to be a simple question.

Cheers

:^:
Was This Post Helpful? 0
  • +
  • -

#4 no2pencil  Icon User is offline

  • Dic Head
  • member icon

Reputation: 5159
  • View blog
  • Posts: 26,768
  • Joined: 10-May 07

Re: How to pass a string array to a function?

Posted 30 May 2009 - 07:43 PM

View Postmorgog, on 28 May, 2009 - 06:06 AM, said:

I have been set an exercise in C (NOT C++)
...
1 - declare the array in the .cpp file,

These are contradicting.

Anyhow... can you pass the array through a pointer?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1