0

I was trying to make letters into numbers in c++. When I write in console it should count modulo and type out if ship is coming (i did all, but can't make letters into numbers :/ )

This what should happen: ABC a = 1; b=2; c=3 1*2*3=6....

So I need to write a word and it should be separated into letters and converted into numbers like that.

I am just learning and I don't know much :)

My current code:

int shipnum, groupnum, moduleship, modulegroup;

cout << "type ship number "; cin >> shipnum;
cout << "type group number "; cin >> groupnum;



/*shipnum dabar 5... (5 mod 2)
groupnum dabar 3... (3 mod 2)
*/

moduleship = shipnum % 47;        //skaiciuojam moduli...
modulegroup = groupnum % 47;

if (moduleship == modulegroup) {  
 cout << "YES ship is coming for you :)";
 }

else if (moduleship != modulegroup) {             //  "!=" reiskia "nelygu"
    cout << "SORRY, NO ship for you :(";
}

return 0;
Oussama Ben Ghorbel
  • 2,132
  • 4
  • 17
  • 34
Dokido
  • 15
  • 1
  • 1
  • 5
  • I see no string variables in the code you posted, even though your question is about strings. – PaulMcKenzie Jul 09 '17 at 18:28
  • in this code i show the rest i did, just asking what i need to do to convert into letters.... so i need to use string variables? – Dokido Jul 09 '17 at 18:34
  • 1
    Your question is not clear at all. You want to convert letters into numbers, but there is nothing in your code that represents any letters. You have `int` variables declared, but where is the character data declared? – PaulMcKenzie Jul 09 '17 at 18:36
  • Instead of describing parts of your program that are not relevant, a more concise question would be something like *"I would like to get the product of a string of characters, where `A=1, B=2, C=3`... up to `Z = 26` -- Here is my code but I am having problems"*. No need to mention ships, modulo, etc. – PaulMcKenzie Jul 09 '17 at 18:43
  • sure, so can u help? – Dokido Jul 09 '17 at 18:46
  • What you seem to be asking is for somebody to write a program for you. Please provide a complete (probably non-functional) program or at least a design associated with portions of a program that you cannot suss the code for and we will probably help you out. Meanwhile we are on to more productive interests... – Dr t Jul 09 '17 at 18:47

4 Answers4

0

Your question isn't precise, although I find it enough. Tip: Be precise with the information you provide, there's no need to show the rest of the code.

Lets say we have this char Ship[20]="ABCDEF";. If you encoding is as simple as A=1, B=2, etc, then you only need something like this:

char Ship[20]="ABCDEF";
int decoded=1;
for(int i=1; Ship[i]=!'/0'; ++i) {
    decoded = decoded * i
}
cout<<decoded;

This loop will run till it encounters the '\0' (null character) at the end of the string. So, you would have a factorial on the fact that your codeing (A=1,B=2, etc) represents a factorial.

otherwise, you could use a switch case, or if statements to check for individual characters and decode appropriately.

char Ship[20]="ABCDEF";
int decoded=1;
for(int i=1; Ship[i]=!'/0'; ++i) {
    switch(Ship[i]){
        case 'A'  :   decoded = decoded * 1;
                      break;
        case 'B'  :   decoded = decoded *2;
                      break;
              //So on
        default   :   break;
    }
}
cout<<decoded;

Output in both cases:

720

Alpha Mineron
  • 348
  • 1
  • 2
  • 13
0

"convert letters into numbers (A=1; B=2…)"

 string a{"ABC"};

    int a0 = a[0]; // 65
    int a1 = a[1]; // 66
    int a2 = a[2]; // 67
    .....

want to wrap A = 1, B = 2...

a0 = a0 - (65 - 1);
a1 = a1 - (65 - 1);
....
ytobi
  • 535
  • 1
  • 9
  • 19
0

The question is not clear but i think the question basically is to convert char into int which follows the encoding A=1, B=2, ......, Z=26 and do the required processing which is to multiply all the encodings.

So here is how you could do it:

#include <iostream>
#include <string>

using namespace std;

int main(){
    string s;       //Input string 
    cout << "enter the string(CAPITALS ONLY) :";
    cin >> s;       //read the input string 
    int result = 1;
    for (auto &elem : s){          //process all the characters of s
        result *= elem - 'A' + 1;  //corresponding int value is multiplied to the result
    }

    cout << "the result is :" << result;
}

Sample Output:

enter the string(CAPITALS ONLY) :AEF
the result is :30
0

(a+b)2=a2+b2+2ab: in cpp example

#include<iostream>

using namespace std;

//Declaring Function in scope

void firstFormula();

int main(void) //Main function
{
    cout << "Hello World!!" << endl; //sample test text printing

    firstFormula(); //executing function

    return 0;
}


//function implementation

void firstFormula(){

//initializing variables

    int a, b;

    cout << "Enter Value of A" << endl;

    cin >> a;//updating input values of a

    cout << "Enter Value of B" << endl;

    cin >> b;//updating input values of b

    int v1 = a + b; //(a+b)2

    int v2 = v1 * v1; //L.H.S

    cout << "Value of v1=" << v1 << endl << "V2=" << v2 << endl;


    cout << "Value of a=" << a << endl << "Value of B=" << b << endl;

    int v3 = a * a;

    int v4 = b * b;

    int v5 = 2 * a * b;

    int v6 = v3 + v4 + v5; //` R.H.S value after equation` `egfyufe`

    cout << "Output=" << "V3=" << v3 << endl << "V4=" << v4 << endl << "v5="
 << v5 << endl << "V6=" << v6 << endl;  

} //end
user2321864
  • 2,207
  • 5
  • 25
  • 35
Suresh B B
  • 1,387
  • 11
  • 13