Reputation: 35
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;
Upvotes: 2
Views: 15302
Reputation: 1420
(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
Upvotes: 0
Reputation:
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
Upvotes: 0
Reputation: 673
"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);
....
Upvotes: 2
Reputation: 348
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
Upvotes: 0