User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C section within the Software Development category of DaniWeb, a massive community of 455,968 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,739 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C advertiser: Programming Forums
Views: 3013 | Replies: 58 | Solved
Reply
Join Date: Nov 2007
Posts: 31
Reputation: r5ingh is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
r5ingh r5ingh is offline Offline
Light Poster

Help needed with loop.

  #1  
Nov 24th, 2007
Hello, i'm a noob C programmer, learning it at university. We have been given an assessment which i need to complete asap. I have figured out the solution to the porgram (almost) but need some assistace. I have a Java and VB background and i cannot seem to get my head around C & pointers!! i've been working on this problem for about 3 hours now. and i cant think any more. hence i cannot progress any further!!

Take a look at this.

#include "stdafx.h"
#include "stdio.h"
#include "ctype.h"
#include "string.h"

void encode(char *ch);
char alph[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
/* morsecode is an array of pointers to char. It is initialized
* so that each element points to a string literal with
* the ascii representation of each char of the array alph[]
*/

char *morsecode[] = { /* an array of pointers to char */
        ".- "  , "-..." , "-.-." , "-.."  , "."    ,    /* a-e */
        "..-." , "--."  , "...." , ".."   , ".---" ,    /* f-j */
        "-.-"  , ".-.." , "--"   , "-."   , "---"  ,    /* k-o */
        ".--." , "--.-" , ".-."  , "..."  , "-"    ,    /* p-t */
        "..-"  , "...-" , ".--"  , "-..-" , "-.--" ,    /* u-y */
        "--.." ,                                        /* z   */
};


	int main() {

		char *ch; 
		printf("please enter char to convert to morse: ");
		scanf_s("%s", &ch);
		encode(ch);
		return 0;
	}

	

	void encode(char *ch){   
		size_t i, j;   
		for ( i = 0; ch[i]; ++i )   {   
			for ( j = 0; j < sizeof ch; ++j )      {   
				if ( morsecode[(unsigned short)toupper(*ch) - alph[0]] )         {   
	
					printf("%c", morsecode);           
					break;       
				}     
			}  
		}  
		putchar('\n');
	} 

i have to follow set criteria and code the solution using the arrays above. my method works fine. IT WORKS FINE for decoding one acharacter. But i need it to decode the whole string...
I'm having difficulty in looping through the entered string and returning the morse code.
please assist.
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Aug 2005
Posts: 4,832
Reputation: iamthwee is a glorious beacon of light iamthwee is a glorious beacon of light iamthwee is a glorious beacon of light iamthwee is a glorious beacon of light iamthwee is a glorious beacon of light 
Rep Power: 17
Solved Threads: 324
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Industrious Poster

Re: Help needed with loop.

  #2  
Nov 24th, 2007
The code you have written isn't standard c.
Last edited by iamthwee : Nov 24th, 2007 at 1:03 pm.
... the hat of 'is this a cat in a hat?'
Reply With Quote  
Join Date: Nov 2007
Posts: 31
Reputation: r5ingh is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
r5ingh r5ingh is offline Offline
Light Poster

Re: Help needed with loop.

  #3  
Nov 24th, 2007
yes. We're being taught ANSI C first. tehn moving onto C++.. this assignment is being coded by the requirements of the lecturer. i was given the array, and i must implement my solution arounf it. The object is to have a program which converts a user input AND is able to read from a file and coverts that to morse code.
As you can see.. i'm stuck on the loop for the string.
Last edited by r5ingh : Nov 24th, 2007 at 1:05 pm.
Reply With Quote  
Join Date: Aug 2005
Posts: 4,832
Reputation: iamthwee is a glorious beacon of light iamthwee is a glorious beacon of light iamthwee is a glorious beacon of light iamthwee is a glorious beacon of light iamthwee is a glorious beacon of light 
Rep Power: 17
Solved Threads: 324
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Industrious Poster

Re: Help needed with loop.

  #4  
Nov 24th, 2007
#include <stdio.h>
#include <ctype.h>
#include <string.h>

void encode ( char ch );
char alph[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
/* morsecode is an array of pointers to char. It is initialized
* so that each element points to a string literal with
* the ascii representation of each char of the array alph[]
*/

char *morsecode[] =
  { /* an array of pointers to char */
    ".- "  , "-..." , "-.-." , "-.."  , "."    ,    /* a-e */
    "..-." , "--."  , "...." , ".."   , ".---" ,    /* f-j */
    "-.-"  , ".-.." , "--"   , "-."   , "---"  ,    /* k-o */
    ".--." , "--.-" , ".-."  , "..."  , "-"    ,    /* p-t */
    "..-"  , "...-" , ".--"  , "-..-" , "-.--" ,    /* u-y */
    "--.." ,                                        /* z   */
  }; 
int main(void)
{
  char ch;
  printf ( "please enter char to convert to morse: " );
  scanf ( "%c", &ch );
  encode ( ch );
  getchar();
  getchar();
} 

void encode ( char ch )
{
  size_t i, j;
  for ( i = 0; i < 26; ++i )
  {
      if ( ch == alph[i])
      {
        printf ( "%s", morsecode[i] );
        break;
      } 
  }
  putchar ( '\n' );
}
Last edited by iamthwee : Nov 24th, 2007 at 1:10 pm.
... the hat of 'is this a cat in a hat?'
Reply With Quote  
Join Date: Aug 2005
Posts: 4,832
Reputation: iamthwee is a glorious beacon of light iamthwee is a glorious beacon of light iamthwee is a glorious beacon of light iamthwee is a glorious beacon of light iamthwee is a glorious beacon of light 
Rep Power: 17
Solved Threads: 324
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Industrious Poster

Re: Help needed with loop.

  #5  
Nov 24th, 2007
Untested.
Last edited by iamthwee : Nov 24th, 2007 at 1:09 pm.
... the hat of 'is this a cat in a hat?'
Reply With Quote  
Join Date: Aug 2005
Posts: 4,832
Reputation: iamthwee is a glorious beacon of light iamthwee is a glorious beacon of light iamthwee is a glorious beacon of light iamthwee is a glorious beacon of light iamthwee is a glorious beacon of light 
Rep Power: 17
Solved Threads: 324
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Industrious Poster

Re: Help needed with loop.

  #6  
Nov 24th, 2007
well?
... the hat of 'is this a cat in a hat?'
Reply With Quote  
Join Date: Nov 2007
Posts: 31
Reputation: r5ingh is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
r5ingh r5ingh is offline Offline
Light Poster

Re: Help needed with loop.

  #7  
Nov 24th, 2007
hmm thanks for the solution. deosnt print anything out though! loool!... i can see what i was trying to do wrong.
hmm. i have not used getchar() before. .... this isnt returning anything to the console. i dont think morsecode[i] is correct.? that said. im in no state to be right.

confused like shit at the moment.
Last edited by r5ingh : Nov 24th, 2007 at 1:22 pm.
Reply With Quote  
Join Date: Aug 2005
Posts: 4,832
Reputation: iamthwee is a glorious beacon of light iamthwee is a glorious beacon of light iamthwee is a glorious beacon of light iamthwee is a glorious beacon of light iamthwee is a glorious beacon of light 
Rep Power: 17
Solved Threads: 324
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Industrious Poster

Re: Help needed with loop.

  #8  
Nov 24th, 2007
#include "stdafx.h"
#include "stdio.h"
#include "ctype.h"
#include "string.h"

void encode ( char ch );
char alph[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
/* morsecode is an array of pointers to char. It is initialized
* so that each element points to a string literal with
* the ascii representation of each char of the array alph[]
*/

char *morsecode[] =
  { /* an array of pointers to char */
    ".- "  , "-..." , "-.-." , "-.."  , "."    ,    /* a-e */
    "..-." , "--."  , "...." , ".."   , ".---" ,    /* f-j */
    "-.-"  , ".-.." , "--"   , "-."   , "---"  ,    /* k-o */
    ".--." , "--.-" , ".-."  , "..."  , "-"    ,    /* p-t */
    "..-"  , "...-" , ".--"  , "-..-" , "-.--" ,    /* u-y */
    "--.." ,                                        /* z   */
  }; 
int main(void)
{
  char ch;
  printf ( "please enter char to convert to morse: " );
  scanf_s ( "%c", &ch );
  encode ( toupper(ch) );
  return 0;
} 

void encode ( char ch )
{
  size_t i, j;
  for ( i = 0; i < 26; ++i )
  {
      if ( ch == alph[i])
      {
        printf ( "%s", morsecode[i] );
        break;
      } 
  }
  putchar ( '\n' );
}

And now?
Last edited by iamthwee : Nov 24th, 2007 at 1:24 pm.
... the hat of 'is this a cat in a hat?'
Reply With Quote  
Join Date: Nov 2007
Posts: 31
Reputation: r5ingh is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
r5ingh r5ingh is offline Offline
Light Poster

Re: Help needed with loop.

  #9  
Nov 24th, 2007
nada. still not returning anything after the user input
hmm
printf ( "please enter char to convert to morse: " );
scanf_s ( "%c", &ch );

this should be
printf ( "please enter string to convert to morse: " );
scanf_s ( "%s", &ch );

the object is to encode the entrie string to morse one char at a time?
Last edited by r5ingh : Nov 24th, 2007 at 1:28 pm.
Reply With Quote  
Join Date: Aug 2005
Posts: 4,832
Reputation: iamthwee is a glorious beacon of light iamthwee is a glorious beacon of light iamthwee is a glorious beacon of light iamthwee is a glorious beacon of light iamthwee is a glorious beacon of light 
Rep Power: 17
Solved Threads: 324
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Industrious Poster

Re: Help needed with loop.

  #10  
Nov 24th, 2007
what are you typing in?
... the hat of 'is this a cat in a hat?'
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb C Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the C Forum

All times are GMT -4. The time now is 9:10 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC