What's new

Help Pahelp naman po dito sa code C++

The_killer 01

Forum Expert
Elite
Joined
Aug 21, 2016
Posts
4,582
Solutions
1
Reaction
9,486
Points
2,080
Pahelp ako dito boss, medyo mahirap po di ko maumpisahan HAHAHAHAHA

Write a program using standard string functions that accepts a price of an item and display its coded value. The base of the key is:
X C O M P U T E R S
0 1 2 3 4 5 6 7 8 9
sample input/output:
Enter Price: 435.89
Coded Value: POP.ES

C++ na language po siya. Thank you po!
 
Hindi ba dapat PMU.RS siya? Bakit POP.ES ang coded value?

C++:
#include <iostream>
#include <string>

std::string replaceString(std::string str, std::string rep, std::string replacement){
    size_t position;
    while ((position = str.find(rep)) != std::string::npos) {
        str.replace(position, 1, replacement);
    }
    return str;
}

int main()
{
    std::string str = "435.89";

    str = replaceString(str, "0", "X");
    str = replaceString(str, "1", "C");
    str = replaceString(str, "2", "O");
    str = replaceString(str, "3", "M");
    str = replaceString(str, "4", "P");
    str = replaceString(str, "5", "U");
    str = replaceString(str, "6", "T");
    str = replaceString(str, "7", "E");
    str = replaceString(str, "8", "R");
    str = replaceString(str, "9", "S");

    std::cout << "Result: " << str;
    return 0;
}
 
Last edited:
pa check na lang, wala ako cpp at cellphone lang gamit eh

C++:
#include <iostream>

using namespace std;

void getCode(string input) {
    char codes[] = "XCOMPUTERS";
    for (int i = 0; i < input.size(); i++) {
        if (input[i] == '.') {
            cout<<input[i];
        } else {
            cout<<codes[input[i]-48];
        }
    }
}

int main() {
    string input;
    
    cout<<"Enter code: ";
    cin>>input;
    cout<<getCode(input);
}
 
  • since gumamit ka ng string, include the #include <string> header
  • instead of using cout<<getCode(input), use getCode(input); to call the function removing 'cout<<' instead since naka void yung function mo.
 
  • since gumamit ka ng string, include the #include <string> header
  • instead of using cout<<getCode(input), use getCode(input); to call the function removing 'cout<<' instead since naka void yung function mo.
saakin no need na ang string header

ay uu nga ano hindi ko nabago 😂 function kasi yan nung una, tapos ginawa kung method/void nalang, nalimutan ko na baguhin yung sa main hehe 👍👍
 

Similar threads

Back
Top