Thread: qwerty/azerty keyboard type problem + question about loop.

  1. #1
    Registered User
    Join Date
    Jul 2002
    Posts
    15

    qwerty/azerty keyboard type problem + question about loop.

    Well, first of all I'd like to ask how to make the program lookfor the keyboard type (azerty or qwerty) because since I have an azerty one, when I type q it shows up as a and so on...how can I solve this problem?
    Also I've written a morse translation program (the idea is from someone else, sorry, but it is just to exercise myself), and I have a problem, each time a letter is translated into morse, I'd like it to rework the whole system so if you type a letter and press enter it continues to give you the answer and then you can again until ESC is pressed, how do I do that? It's the first time I need to apply a loop to several actions, so it's hard. Here's the code:

    Code:
    #include <iostream.h>
    #include <conio.h>
    #include <time.h>
    #include <stdlib.h>
    #include <windows.h>
    
    void main()
    {
      char morse;
      cout<<"Welcome to the Morse Translator (c) v1.0 created by Patrick Marinkovic.";
      cout<<"To find  how a letter of the alphabet is spelt in the Morse language, please type a      letter and press Enter. Whenever you wish to exit, press ESC:";
      cin>>morse; //now will start the answers code
      if(morse=='a')
      {
       cout<<"._ is a. If you wish to translate another letter please type it and press Enter,otherwise press ESC to exit:";	 
      } 
      else if(morse=='b')
      {
       cout<<"_... is b. If you wish to translate another letter please type it and press Enter,otherwise press ESC to exit:";
      }
      else if(morse=='c')
      {
      cout<<"_._. is c. If you wish to translate another letter please type it and press Enter,otherwise press ESC to exit:";
      }
      else if(morse=='d')
      {
      cout<<"_.. is d. If you wish to translate another letter please type it and press Enter,otherwise press ESC to exit:";
      }
      else if(morse=='e')
      {
      cout<<". is e. If you wish to translate another letter please type it and press Enter,otherwise press ESC to exit:";
      }
      else if(morse=='f')
      {
      cout<<".._. is f. If you wish to translate another letter please type it and press Enter,otherwise press ESC to exit:";
      }
      else if(morse=='g')
      {
      cout<<"_ _. is g. If you wish to translate another letter please type it and press Enter,otherwise press ESC to exit:";
      }
      else if(morse=='h')
      {
      cout<<".... is h. If you wish to translate another letter please type it and press Enter,otherwise press ESC to exit:";
      }
      else if(morse=='i')
      {
      cout<<".. is i. If you wish to translate another letter please type it and press Enter,otherwise press ESC to exit:";
      }
      else if(morse=='j')
      {
      cout<<"._ _ _ is j. If you wish to translate another letter please type it and press Enter,otherwise press ESC to exit:";
      }
      else if(morse=='k')
      {
      cout<<"_._ is k. If you wish to translate another letter please type it and press Enter,otherwise press ESC to exit:";
      }
      else if(morse=='l')
      {
      cout<<"._.. is l. If you wish to translate another letter please type it and press Enter,otherwise press ESC to exit:";
      }
      else if(morse=='m')
      {
      cout<<"_ _ is m. If you wish to translate another letter please type it and press Enter,otherwise press ESC to exit:";
      }
      else if(morse=='n')
      {
      cout<<"_. is n. If you wish to translate another letter please type it and press Enter,otherwise press ESC to exit:";
      }
      else if(morse=='o')
      {
      cout<<"_ _ _ is o. If you wish to translate another letter please type it and press Enter,otherwise press ESC to exit:";
      }
      else if(morse=='p')
      {
      cout<<"._ _. is p. If you wish to translate another letter please type it and press Enter,otherwise press ESC to exit:";
      }
      else if(morse=='q')
      {
      cout<<"_ _._ is q. If you wish to translate another letter please type it and press Enter,otherwise press ESC to exit:";
      }
      else if(morse=='r')
      {
      cout<<"._. is r. If you wish to translate another letter please type it and press Enter,otherwise press ESC to exit:";
      }
      else if(morse=='s')
      {
      cout<<"... is s. If you wish to translate another letter please type it and press Enter,otherwise press ESC to exit:";
      }
      else if(morse=='t')
      {
      cout<<"_ is t. If you wish to translate another letter please type it and press Enter,otherwise press ESC to exit:";
      }
      else if(morse=='u')
      {
      cout<<".._ is u. If you wish to translate another letter please type it and press Enter,otherwise press ESC to exit:";
      }
      else if(morse=='v')
      {
      cout<<"..._ is v. If you wish to translate another letter please type it and press Enter,otherwise press ESC to exit:";
      }
      else if(morse=='w')
      {
      cout<<"._ _ is w. If you wish to translate another letter please type it and press Enter,otherwise press ESC to exit:";
      }
      else if(morse=='x')
      {
      cout<<"_.._ is x. If you wish to translate another letter please type it and press Enter,otherwise press ESC to exit:";
      }
      else if(morse=='y')
      {
      cout<<"_._ _ is y. If you wish to translate another letter please type it and press Enter,otherwise press ESC to exit:";
      }
      else if(morse=='z')
      {
      cout<<"_ _.. is z. If you wish to translate another letter please type it and press Enter,otherwise press ESC to exit:";
      }
      else
      {
      cout<<"Character not available in Morse language. If you are trying to translate numbers, please await the next version. Please type a letter of the alphabet or press ESC to exit:";
      } //here ends the answers code
      getch();
      return 0;
    }
    Hope anyone knows the answer.
    "Nobility is not a birtright, but is defined by someone's actions." Robin Hood

  2. #2
    Registered User moonwalker's Avatar
    Join Date
    Jul 2002
    Posts
    282

    Hmm

    I wrote a similar program a few days ago.
    I'll post the code once I get home.

    meanwhile, if you want to check the EXE version
    go to http://mahurshi.tripod.com/software.htm
    (and look for MORSE)
    [can't give you direct link because of tripod restrictions]

    But I did not use the ESC etc..

    here's how the ESC usually works

    while (kbhit() != 27)
    {
    //place your code here
    }

    so this one runs as long as ESC (code 27) is not pressed.

  3. #3
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680
    I think the keyboard layout is an OS issue. You have to configure that in you OS.

    void main is wrong - use int main(void)

    Is stead of if...elseif...elseif...elseif... you can also use the switch statement (or use an array).

    Here's an example:
    Code:
    #include <stdio.h> 
    #include <stdio.h>
    #include <conio.h>
    
    int main(void)
    {
    	char morse;
    
    	printf("Enter character or ESC to quit: ");
    	while((morse = getch()) != 27)
    	{
    		printf("%c\t", morse);
    		switch(morse)
    		{
    			case 'a': printf(". _"); break;
    			case 'b': printf("_ . . ."); break;
    			case 'z': printf("_ _ . ."); break;
    			default: printf("invalid character");
    		}
    		printf("\nEnter character or ESC to quit: ");
    	}
    	return 0;
    }
    I'm sure there is some kind of define for the Escape character but I can't find it.

  4. #4
    Registered User
    Join Date
    Jul 2002
    Posts
    15

    Thanks!

    Thanks both of you!µ
    moonwalker, who do you think I got the idea from? And thanks for proposing the code
    Thanks for pointing that out, Monster, I had kept the void main from a previous program.
    I have not yet learned switch neither how arrays work, but thanks, I'll maybe use that way in the next version (that will translate sentences I hope).
    I didn't know printf existed in C++!?!?
    Here's the code of my program if anyone's interested:

    Code:
    #include <iostream.h>
    #include <conio.h>
    #include <time.h>
    #include <stdlib.h>
    #include <windows.h>
    
    int main(void)
    {
      char morse;
      cout<<"Welcome to the Morse Translator (c) v1.0 created by Patrick Marinkovic.";
      cout<<"To find  how a letter of the alphabet is spelt in the Morse language, please type a      letter and press Enter. Whenever you wish to exit, press ESC:";
      while((morse = getch()) != 27)
     {
      cin>>morse;
      if(morse=='a')
      {
      cout<<"._ is a. If you wish to translate another letter please type it and press Enter,otherwise press ESC to exit:";	 
      } 
      else if(morse=='b')
      {
      cout<<"_... is b. If you wish to translate another letter please type it and press Enter,otherwise press ESC to exit:";
      }
      else if(morse=='c')
      {
      cout<<"_._. is c. If you wish to translate another letter please type it and press Enter,otherwise press ESC to exit:";
      }
      else if(morse=='d')
      {
      cout<<"_.. is d. If you wish to translate another letter please type it and press Enter,otherwise press ESC to exit:";
      }
      else if(morse=='e')
      {
      cout<<". is e. If you wish to translate another letter please type it and press Enter,otherwise press ESC to exit:";
      }
      else if(morse=='f')
      {
      cout<<".._. is f. If you wish to translate another letter please type it and press Enter,otherwise press ESC to exit:";
      }
      else if(morse=='g')
      {
      cout<<"_ _. is g. If you wish to translate another letter please type it and press Enter,otherwise press ESC to exit:";
      }
      else if(morse=='h')
      {
      cout<<".... is h. If you wish to translate another letter please type it and press Enter,otherwise press ESC to exit:";
      }
      else if(morse=='i')
      {
      cout<<".. is i. If you wish to translate another letter please type it and press Enter,otherwise press ESC to exit:";
      }
      else if(morse=='j')
      {
      cout<<"._ _ _ is j. If you wish to translate another letter please type it and press Enter,otherwise press ESC to exit:";
      }
      else if(morse=='k')
      {
      cout<<"_._ is k. If you wish to translate another letter please type it and press Enter,otherwise press ESC to exit:";
      }
      else if(morse=='l')
      {
      cout<<"._.. is l. If you wish to translate another letter please type it and press Enter,otherwise press ESC to exit:";
      }
      else if(morse=='m')
      {
      cout<<"_ _ is m. If you wish to translate another letter please type it and press Enter,otherwise press ESC to exit:";
      }
      else if(morse=='n')
      {
      cout<<"_. is n. If you wish to translate another letter please type it and press Enter,otherwise press ESC to exit:";
      }
      else if(morse=='o')
      {
      cout<<"_ _ _ is o. If you wish to translate another letter please type it and press Enter,otherwise press ESC to exit:";
      }
      else if(morse=='p')
      {
      cout<<"._ _. is p. If you wish to translate another letter please type it and press Enter,otherwise press ESC to exit:";
      }
      else if(morse=='q')
      {
      cout<<"_ _._ is q. If you wish to translate another letter please type it and press Enter,otherwise press ESC to exit:";
      }
      else if(morse=='r')
      {
      cout<<"._. is r. If you wish to translate another letter please type it and press Enter,otherwise press ESC to exit:";
      }
      else if(morse=='s')
      {
      cout<<"... is s. If you wish to translate another letter please type it and press Enter,otherwise press ESC to exit:";
      }
      else if(morse=='t')
      {
      cout<<"_ is t. If you wish to translate another letter please type it and press Enter,otherwise press ESC to exit:";
      }
      else if(morse=='u')
      {
      cout<<".._ is u. If you wish to translate another letter please type it and press Enter,otherwise press ESC to exit:";
      }
      else if(morse=='v')
      {
      cout<<"..._ is v. If you wish to translate another letter please type it and press Enter,otherwise press ESC to exit:";
      }
      else if(morse=='w')
      {
      cout<<"._ _ is w. If you wish to translate another letter please type it and press Enter,otherwise press ESC to exit:";
      }
      else if(morse=='x')
      {
      cout<<"_.._ is x. If you wish to translate another letter please type it and press Enter,otherwise press ESC to exit:";
      }
      else if(morse=='y')
      {
      cout<<"_._ _ is y. If you wish to translate another letter please type it and press Enter,otherwise press ESC to exit:";
      }
      else if(morse=='z')
      {
      cout<<"_ _.. is z. If you wish to translate another letter please type it and press Enter,otherwise press ESC to exit:";
      }
      else
      {
      cout<<"Character not available in Morse language. If you are trying to translate numbers, please await the next version. Please type a letter of the alphabet or press ESC to exit:";
      }
     }
      return 0;
    }
    Hope it helps someone
    "Nobility is not a birtright, but is defined by someone's actions." Robin Hood

  5. #5
    Registered User
    Join Date
    Jul 2002
    Posts
    16
    Wouldn't it be easier to take the "If you wish to translate another letter please type it and press Enter,otherwise press ESC to exit:" part out of the if/else/if/else/etc. bit and stick it below all of the branching statements, so you only have to have one instance of it?

  6. #6
    Registered User
    Join Date
    Jul 2002
    Posts
    15

    Yes,it is, thanks!

    Yes it's better, thanks!
    But since the program is finished, I'll leave it that way.
    "Nobility is not a birtright, but is defined by someone's actions." Robin Hood

  7. #7
    Registered User moonwalker's Avatar
    Join Date
    Jul 2002
    Posts
    282

    Here's my code

    here's my code, as i promised.... (it doesn't have ESC etc..)

    PHP Code:
    #include <stdio.h>
    #include <string.h>
    #include <ctype.h>
    #include <dos.h>

    void showwhirl()
    {
        
    int ij;
        
    char whirl[] = {'|','/','-','\\'};

        for (
    i=0;i<=2i++)
        {
            for (
    j=0j<=3j++)
            {
                
    printf("%c"whirl[j]);
                
    delay(25);
                
    printf("\b");
            }
        }

    }

    void morsecode(char character)
    {
        
    int iflag;

        
    struct {
        
    char symbol;
        
    char *code;
        } 
    morse[] = { {'A'".-"}, {'B'"-..."}, {'C'"-.-."}, {'D'"-.."},\
        {
    'E'"."}, {'F'"..-."}, {'G'"--."}, {'H'"...."}, {'I'".."}, {'J'".---"},\
        {
    'K'"-.-"}, {'L'".-.."}, {'M'"--"}, {'N'"-."}, {'O'"---"}, {'P'".--."},\
        {
    'Q'"--.-"}, {'R'".-."}, {'S'"..."}, {'T'"-"}, {'U'"..-"}, {'V'"...-"},\
        {
    'W'".--"}, {'X'"-..-"}, {'Y'"-.--"}, {'Z'"--.."}, \
        {
    '0'"-----"}, {'1'".----"}, {'2'"..---"}, {'3'"...--"}, {'4'"....-"},\
        {
    '5'"....."}, {'6'"-...."}, {'7'"--..."}, {'8'"---.." }, {'9'"----."}, \
        {
    '.'".-.-.-"}, {','"--..--"}, {':'"---..."}, {'?'"..--.."}, {'/'".----."},\
        {
    '-'"-....-"}, {' '"/"}, {'\''".----." } };

        
    showwhirl();

        for (
    i=0i<=43i++)
        {
             if (
    morse[i].symbol == toupper(character))
             {
               
    printf("%s "morse[i].code);
               
    flag 1;
               break;
             }
        }

        if (
    flag != 1)
        {
        
    printf("* ");
        }

    }

    void printatstart()
    {
        
    showwhirl();
        
    printf("Morse Code\n");
        
    showwhirl();
        
    printf("Programmed By Maharshi Akilla\n");
        
    showwhirl();
        
    printf("http://mahurshi.tripod.com\n\n");
    }


    void printatend()
    {
        
    printf("\n\n");
        
    printf("Where:\n");
        
    showwhirl();
        
    printf("1) ' ' => Letter Seperator\n");
        
    showwhirl();
        
    printf("2)  /  => Word Seperator (or) Blankspace\n");
        
    showwhirl();
        
    printf("3)  *  => Characters Not In Code Library\n");
        
    showwhirl();
        
    printf("4)  |  => Appears When End Of Line Obstructs Print\n");
    }

    main()
    {
        
    int i;
        
    char *string;

        
    printatstart();

        
    printf("Enter a string: ");
        
    gets(string);

        
    printf("\n");

        for (
    i=0i<strlen(string); i++)
        {
        
    morsecode(string[i]);
        }

        
    printatend();
        
    getchar();


  8. #8
    Registered User
    Join Date
    Jul 2002
    Posts
    15

    Thanks but

    Thanks but my compiler (DEV-C++ beta 5) makes this error when compiling:
    Executing g++.exe...
    C:\My Documents\moon.cpp:38: unterminated character constant
    0

    I have shown line 38 with a comment:

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <ctype.h>
    #include <dos.h>
    
    void showwhirl()
    {
        int i, j;
        char whirl[] = {'|','/','-',''};
    
        for (i=0;i<=2; i++)
        {
            for (j=0; j<=3; j++)
            {
                printf("%c", whirl[j]);
                delay(25);
                printf("b");
            }
        }
    
    }
    
    void morsecode(char character)
    {
        int i, flag;
    
        struct {
        char symbol;
        char *code;
        } morse[] = { {'A', ".-"}, {'B', "-..."}, {'C', "-.-."}, {'D', "-.."},
        {'E', "."}, {'F', "..-."}, {'G', "--."}, {'H', "...."}, {'I', ".."}, {'J', ".---"},
        {'K', "-.-"}, {'L', ".-.."}, {'M', "--"}, {'N', "-."}, {'O', "---"}, {'P', ".--."},
        {'Q', "--.-"}, {'R', ".-."}, {'S', "..."}, {'T', "-"}, {'U', "..-"}, {'V', "...-"},
        {'W', ".--"}, {'X', "-..-"}, {'Y', "-.--"}, {'Z', "--.."}, 
        {'0', "-----"}, {'1', ".----"}, {'2', "..---"}, {'3', "...--"}, {'4', "....-"},
        {'5', "....."}, {'6', "-...."}, {'7', "--..."}, {'8', "---.." }, {'9', "----."}, 
        {'.', ".-.-.-"}, {',', "--..--"}, {':', "---..."}, {'?', "..--.."}, {'/', ".----."}, //line 38
        {'-', "-....-"}, {' ', "/"}, {''', ".----." } };
    
        showwhirl();
    
        for (i=0; i<=43; i++)
        {
             if (morse[i].symbol == toupper(character))
             {
               printf("%s ", morse[i].code);
               flag = 1;
               break;
             }
        }
    
        if (flag != 1)
        {
        printf("* ");
        }
    
    }
    
    void printatstart()
    {
        showwhirl();
        printf("Morse Code\n");
        showwhirl();
        printf("Programmed By Maharshi Akilla\n");
        showwhirl();
        printf("http://mahurshi.tripod.com\n\n");
    }
    
    
    void printatend()
    {
        printf("\n\n");
        printf("Where:\n");
        showwhirl();
        printf("1) ' ' => Letter Seperator\n");
        showwhirl();
        printf("2)  /  => Word Seperator (or) Blankspace\n");
        showwhirl();
        printf("3)  *  => Characters Not In Code Library\n");
        showwhirl();
        printf("4)  |  => Appears When End Of Line Obstructs Print\n");
    }
    
    main()
    {
        int i;
        char *string;
    
        printatstart();
    
        printf("Enter a string: ");
        gets(string);
    
        printf("\n");
    
        for (i=0; i<strlen(string); i++)
        {
        morsecode(string[i]);
        }
    
        printatend();
        getchar();
    }
    What's the problem?
    "Nobility is not a birtright, but is defined by someone's actions." Robin Hood

  9. #9
    Registered User moonwalker's Avatar
    Join Date
    Jul 2002
    Posts
    282

    try this

    small glitch... there should have been a '\'

    delay also doesnt work with dev c++

    this will work for you:
    Code:
    #include <stdio.h>
    #include <string.h>
    #include <ctype.h>
    #include <dos.h>
    
    void morsecode(char character)
    {
        int i, flag;
    
        struct {
        char symbol;
        char *code;
        } morse[] = { {'A', ".-"}, {'B', "-..."}, {'C', "-.-."}, {'D', "-.."},
        {'E', "."}, {'F', "..-."}, {'G', "--."}, {'H', "...."}, {'I', ".."}, {'J', ".---"},
        {'K', "-.-"}, {'L', ".-.."}, {'M', "--"}, {'N', "-."}, {'O', "---"}, {'P', ".--."},
        {'Q', "--.-"}, {'R', ".-."}, {'S', "..."}, {'T', "-"}, {'U', "..-"}, {'V', "...-"},
        {'W', ".--"}, {'X', "-..-"}, {'Y', "-.--"}, {'Z', "--.."}, 
        {'0', "-----"}, {'1', ".----"}, {'2', "..---"}, {'3', "...--"}, {'4', "....-"},
        {'5', "....."}, {'6', "-...."}, {'7', "--..."}, {'8', "---.." }, {'9', "----."}, 
        {'.', ".-.-.-"}, {',', "--..--"}, {':', "---..."}, {'?', "..--.."}, {'/', ".----."},
        {'-', "-....-"}, {' ', "/"}, {'\'', ".----." } };
    
        //this forum is not printing, but above, near ''', put 'backslash'
        // replace backslash with \
    
        for (i=0; i<=43; i++)
        {
             if (morse[i].symbol == toupper(character))
             {
               printf("%s ", morse[i].code);
               flag = 1;
               break;
             }
        }
    
        if (flag != 1)
        {
        printf("* ");
        }
    
    }
    
    void printatstart()
    {
        
        printf("Morse Code\n");
        
        printf("Programmed By Maharshi Akilla\n");
        
        printf("http://mahurshi.tripod.com\n\n");
    }
    
    
    void printatend()
    {
        printf("\n\n");
        printf("Where:\n");
        
        printf("1) ' ' => Letter Seperator\n");
        
        printf("2)  /  => Word Seperator (or) Blankspace\n");
        
        printf("3)  *  => Characters Not In Code Library\n");
        
        printf("4)  |  => Appears When End Of Line Obstructs Print\n");
    }
    
    main()
    {
        int i;
        char *string;
    
        printatstart();
    
        printf("Enter a string: ");
        gets(string);
    
        printf("\n");
    
        for (i=0; i<strlen(string); i++)
        {
            morsecode(string[i]);
        }
    
        printatend();
        getchar();
    }
    i am attaching the code.... this forum is not printing all characters
    Last edited by moonwalker; 07-22-2002 at 01:00 PM.

  10. #10
    Registered User moonwalker's Avatar
    Join Date
    Jul 2002
    Posts
    282

    here

    see this... you'll understand

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Addition problem in loop
    By murjax in forum C Programming
    Replies: 3
    Last Post: 07-01-2009, 06:29 PM
  2. validation problem in a loop (newbie question)
    By Aisthesis in forum C++ Programming
    Replies: 11
    Last Post: 05-10-2009, 10:47 PM
  3. Please don't laugh...SIMPLE loop question!
    By the_lumin8or in forum C++ Programming
    Replies: 5
    Last Post: 03-31-2006, 01:08 PM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. KEYSCANNING within a LOOP, Newbie Question
    By Robert_Ingleby in forum C++ Programming
    Replies: 8
    Last Post: 11-23-2001, 06:49 AM