0

I have actually this code for an embedded application. I am 1st trying to make it for plain c++ so that I may have concept clear.

I have a digit code assigned to each English alphabet. I want that my program outputs equivalent digit code for a sentence. A loop actually should iterate number of times for each digit of the code for sentence. Bec I need to ON/OFF pins for me embedded application for each digit. I have to add time difference between digits of same letter, between two letters, & between two sentences later. 1st I wanted plain output for each digit of resultant code.

Here is my code. I have assigned alphabets to String & code to a String array type. Then I search for equivalent digit code of character from String array & save it in string type. Now I am wanting to assign each digit from string to int & loop for it. But I am having trouble in assigning string value to int. I have not much experience of C++ programming.

EDIT I have trouble in converting string to int at 1st place, & overall does this logic for solving my problem seems fine.

Here is my code, this is how I am trying to solve my problem.

    #include <iostream>
    #include <string>
    using namespace std;


        string texttopattern(char c)
        {

    //Hello world again
    //125-15-123-123-135 1346-135-1235-123-145 1-1245-1-24-1345

        string alphabet = "abcdefghijklmnopqrstuvwqyz"; //osv
        string code[] = {"1","12","14","145", "15", "124", "1245",
                          "125", "24", "245", "13", "123", "134",
                          "1345", "135", "1234", "12345", "1235", "234", "2345",
                          "136", "1236", "1346", "13456", "1356", "12346"};
          int index = alphabet.find(c);
            if(index!=-1)
                return code[index];
             else
                return " ";
        }

int main()
{
    string ord;
    getline(cin, ord);
    string code="";
        for(int i=0; i<ord.length(); i++)
        {
        code += texttopattern(ord[i]);
        }

        for(int i=0; i<code.length(); i++) {
            int n = code[i]; //assign a single digit value from string to an   
                             //int,string2int assign value is problem here !!
              while(n>0){    //loop for n times & manipulate PIN in each iteration
                cout<<"loop";
                n--;      }
        //cout<<code[i]<<endl;
        }

        return 0;
}
enterprize
  • 1,179
  • 8
  • 29
  • 43

4 Answers4

0

I think you should not use a variable named int which is a keyword in C++ and C.

So at least you have an error on the while loop. Change the while loop to

       int j = code[i] - '0'; // this will get the number from the char 
       while(j>0){
           cout<<"loop";
           --j;      
       }
CS Pei
  • 10,869
  • 1
  • 27
  • 46
  • I just for example put int bec I have trouble in assigning string vale to int, that is also part of my problem – enterprize Aug 28 '13 at 13:35
  • @enterprize: You can't simply assign string values to ints, or animals to colors. C++ has a fairly strict type system. However, there's a function `std::stoi` which stands for "String TO Int". – MSalters Aug 28 '13 at 15:03
  • @enterprize, if my understanding is correct, you can use `int j = code[i] - '0';` to get the number from a char. – CS Pei Aug 28 '13 at 17:57
0

Edit: In response to your comment I have now gone back to using strings as patterns:

#define UNKNOWN_LETTER_PATTERN ""

string texttopattern(char c)
{
    string code[] = {"1","12","14","145", "15", "124", "1245",
                      "125", "24", "245", "13", "123", "134",
                      "1345", "135", "1234", "12345", "1235", "234", "2345",
                      "136", "1236", "1346", "13456", "1356", "12346"};
    if (c >= 'a' && c <= 'z')
        return code[c - 'a'];
    else
        return UNKNOWN_LETTER_PATTERN ;
}

First part of main() stays the same:

int main()
{
    string ord;
    getline(cin, ord);
    vector<string> code;
    for(int i=0; i<ord.length(); i++)
    {
        code.push_back(texttopattern(ord[i]));
    }

Updated the rest and added a few comments, hopefully explaining what I am doing:

    // Loop patterns
    for (vector<string>::iterator it = code.begin(); it != code.end(); it++) 
    {
        if (*it == UNKNOWN_LETTER_PATTERN )
        {
            // Don't know what to do with unknown chars like spaces - your problem
            cout << "unknown letter" << endl;
        }
        else
        {
            // Loop every digit in the current  pattern
            for (int i = 0; i < it->size(); i++)
            {
                char c = it->at(i);

                // There surely is a better way to do the following, atoi or
                // stringstream come to mind
                // But for a single digit number, this should be enough for
                // now to convert a char to a number.
                int number = c - '0'; 

                // loop until number is reached
                for (int j = 0; j < number; j++)
                    cout << "I";
                cout << endl;
            }
            cout << "End of letter" << endl;
        }
    }

    cin.get();

    return 0;
}

EDIT: I changed the above sample. What it now does is, for example: input is 'd' -> resolve to code "145" -> loop every digit and turn to number [1, 4, 5] -> print 'I' number times for each digit "I IIII IIIII". After rereading your comment, I believe this is what you wanted.

Wutz
  • 2,246
  • 13
  • 15
  • it is raising error,but before that it appears that this code is only printing pattern? That's not what I need,my OP code is fine with only printing,but I need is a loop that iterates/outputs for each digit of digit_pattern equivalent to each digit bec I need to ON/OFF pin equivalent numbers in my embedded app(e.g. for 24, iterate 2 times then 4 times,later I add delay in between).That's is why I need code in int & what I am trying/mentioned to do in my code too,getting string value in int & then loop that many times.So each iteration/loop allow me do some thing with a PIN of my embedded app – enterprize Aug 28 '13 at 15:17
  • OK, I completely misunderstood your problem then. I will try to throw a solution together, or at least a starting point for you. – Wutz Aug 28 '13 at 15:51
0

I really hope that i understood what you meant. I tried to make a program as you showed here and i came up with this:

Macros.h:

#ifndef MY_MACROS_H
#define MY_MACROS_H

#define VALUES(base)                \
                ADD_VALUE(0,base)   \
                ADD_VALUE(1,base)   \
                ADD_VALUE(2,base)   \
                ADD_VALUE(3,base)   \
                ADD_VALUE(4,base)   \
                ADD_VALUE(5,base)   \
                ADD_VALUE(6,base)   \
                ADD_VALUE(7,base)   \
                ADD_VALUE(8,base)   \
                VALUE(9,base)

#define VALUE(val,base) +1

#define ADD_VALUE(val,base) VALUE(val, base)


#define DIGIT_COUNT (0 VALUES(1))
#define BASE_COUNT  (DIGIT_COUNT + 1)

#define ALL_VALUES   ADD_ARRAY_VALUES(1)                                  \
                     ADD_ARRAY_VALUES(BASE_COUNT)                         \
                     ADD_ARRAY_VALUES(BASE_COUNT * BASE_COUNT)            \
                     ARRAY_VALUES(BASE_COUNT * BASE_COUNT * BASE_COUNT)

#define ARRAY_VALUES(rank) +1

#define ADD_ARRAY_VALUES(rank) ARRAY_VALUES(rank)

#define UNKNOWN_LETTER_PATTERN ""
#define ARRAYS_COUNT (0 ALL_VALUES)

#endif

Header.h:

    #ifndef _MY_HEADER_H
#define _MY_HEADER_H

/*****
* Includes
******/
#include <iostream>
#include <string>
#include <vector>
#include "Macros.h"

using namespace std;

/*****
* Definitions
******/
#define FIRST_LETTER 'a'
#define LAST_LETTER  'z'

typedef unsigned char byte;

typedef int NumberValueType;

#define BASE_CHAR_VALUE '0'

// character 
#define CHAR_TO_CHAR_INDEX(character) (character - FIRST_LETTER) 
#define CHAR_TO_NUM_INDEX(character)  (character - BASE_CHAR_VALUE)

#define IS_CHAR_VALID(character) ((character >= FIRST_LETTER) && (character <= LAST_LETTER))

int ParseInput();
string texttopattern(char c);

#endif

And the implementation file: (of course that i also printed and haven't done anything important with the output).

Main.cpp:

    #include "Header.h"

int s_arValues[ARRAYS_COUNT][DIGIT_COUNT] =
{   
    #undef  VALUE
    #define VALUE(val,base) (base * val)

    #undef ADD_VALUE
    #define ADD_VALUE(val,base) VALUE(val, base),

    #undef ARRAY_VALUES
    #define ARRAY_VALUES(rank) { VALUES(rank) }

    #undef ADD_ARRAY_VALUES
    #define ADD_ARRAY_VALUES(rank) ARRAY_VALUES(rank),

    ALL_VALUES
};


string texttopattern(char c)
{
    string code[] = {"1","12","14","145", "15", "124", "1245",
                      "125", "24", "245", "13", "123", "134",
                      "1345", "135", "1234", "12345", "1235", "234", "2345",
                      "136", "1236", "1346", "13456", "1356", "12346"};
    if (IS_CHAR_VALID(c))
        return code[CHAR_TO_CHAR_INDEX(c)];
    else
        return UNKNOWN_LETTER_PATTERN ;
}

int ParseInput()
{
    string ord;
    getline(cin, ord);
    vector<string> code;

    for(int i=0; i<ord.length(); i++)
    {
        code.push_back(texttopattern(tolower(ord[i])));
    }

    // Loop inputs
    for (vector<string>::iterator itInputs = code.begin(); itInputs != code.end(); itInputs++) 
    {
        if (*itInputs == UNKNOWN_LETTER_PATTERN)
        {
            // Don't know what to do with unknown chars like spaces - your problem
            cout << "unknown letter" << endl;
            continue;
        }

        int number = 0;
        int nInputSize = itInputs->size();

        // Loop every digit in the current pattern
        for (int nDigitIndex = 0; nDigitIndex < nInputSize; nDigitIndex++)
        {
            if(nDigitIndex >= sizeof(NumberValueType))
            {
                cout << number;
                number = 0;
                continue;
            }

            char cChar = itInputs->at(nDigitIndex);

            number += s_arValues[nInputSize - nDigitIndex - 1][CHAR_TO_NUM_INDEX(cChar)]; 
        }

        // At this point you can use number
        cout << number;
        cout << endl;
        cout << "End of letter" << endl;
    }

    cin.get();

    return 0;
}

void main()
{   
    ParseInput();
}
TomF
  • 183
  • 11
0

Here is code how I solve my problem. Thanks to all people who replied though some replies were too complex for me. Please see it & point any shortcoming into it though its giving output I was looking.

#include <iostream>
#include <string>
#include <sstream>
using namespace std;

 string texttopattern(char c)
    {

//Hello world again
//125-15-123-123-135 1346-135-1235-123-145 1-1245-1-24-1345

    string alphabet = "abcdefghijklmnopqrstuvwqyz"; //osv
    string code[] = {"1","12","14","145", "15", "124", "1245",
                      "125", "24", "245", "13", "123", "134",
                      "1345", "135", "1234", "12345", "1235", "234", "2345",
                      "136", "1236", "1346", "13456", "1356", "12346"};
      int index = alphabet.find(c);
        if(index!=-1)
            return code[index];
         else
            return " ";
    }

int main()
{
    int n;
    string ord;
    getline(cin, ord);
    string code="";
        for(int i=0; i<ord.length(); i++)
        {
        code += texttopattern(ord[i]);
        }

         for(int i = 0; i<code.length(); i++){
              n = code[i] - '0';
                for(int i=0; i<n; i++){
                  cout<<"Here you go"<<"-"<<n<<endl;}}

        return 0;
        }
enterprize
  • 1,179
  • 8
  • 29
  • 43