Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8867757
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T17:07:04+00:00 2026-06-14T17:07:04+00:00

This code basically takes what a user puts (a-z) in and converts it into

  • 0

This code basically takes what a user puts (a-z) in and converts it into morse code. But the return string always has ‘m’ at the end tried to do a millions of stuff to fix it can you see what I have done wrong, thanks.

//Functions
#include "stdafx.h"
#include <ctype.h>

#include <stdio.h>
#include <string.h>
#include <malloc.h> 
void morse(void);   //prototype
int _tmain(int argc, _TCHAR* argv[])
{
    for(;;){
    morse(); // function call
    }
}

void morse(void){

    char *ss= (char*)malloc(110); // allocating dynamic memory
    strcpy(ss, ".-  -...-.-.-.. .   ..-.--. ......  .----.- .-..--  -.  --- .--.--.-.-. ... -   ..- ...-.-- -..--.----..");
    char *pp =(char*)malloc(110);
    int i = 0;
    int n = 0;
    printf("Enter text to convert to morse code: ");
    scanf("%s", pp);
    char *q =(char*)malloc(110);
    while (*pp != '\0'){//intiate while loop
        *pp = *(pp + n); 
        int f = (*pp - 97)*4; //find letters postion in morse code string
        int a = 0;

        while (a != 4) //copy morse code for the letter into new string
        {
            *(q + i) = *(ss + f);
            i++;
            a++;
            f++;
        }
        n++;

    }
    q[i] = '\0';    
    printf("%s", q); //return morse code
    printf("\n");
    free(ss); //free the allocated memory
    free(pp);
    free(q);
    return;
} 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T17:07:05+00:00Added an answer on June 14, 2026 at 5:07 pm

    Your loop goes around for one extra character. The outer loop looks for *pp being \0, but *pp is still the prior character’s value.

    Change it to:

    while (*(pp+n) != '\0') {  // test next character
        char c = *(pp + n);    // fetch next character
        int f = (c - 97)*4;    // find character's index into morse array
    

    The way I found this was to run under the debugger and watch what was going on. It soon became apparent when it went around the loop twice even though I’d entered only one character. Use the debugger, it’s a great tool. It’s worth stepping through code through the debugger, even if you think it’s working. Sometimes you get a surprise!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this small piece of code that basically takes a list and runs
This code basically translates characters based on position in one string to the character
I'm sure this has been covered but I can't find it anywhere, I basically
This is a follow up from my previous question I have this code basically
This is the first time I got this error. This code basically gets the
I have (basically) this code - <script type=text/javascript language=javascript> $(document).ready(function() { $(#loadDiv).load(mypage.html, function(){ alert($(#someText).text())
So basically this code: class A { }; class B { B (const B&
So basically this code was working fine before. I had some computer issues and
I am using this code to alter the title of an image. Basically it
I am having a problem getting this code to work. Basically I Go from

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.