🔒 Closed C++

Status
Not open for further replies.

bie mp

Leecher
Input Code: COMPUTERS.X VALUE: 123456789.0 ============================ INPUT AGAIN? Type YES to input again, type NO to end program: YES Input Code: coMpUtErS.x VALUE: 123456789.0 ============================ INPUT AGAIN? Type YES to input again, type NO to end program: Yes Input Code: MOST.PP VALUE: 3296.44 ============================ INPUT AGAIN? Type YES to input again, type NO to end program: Y INVALID INPUT.Type YES to input again, type NO to end program: yes Input Code: Rest.xx VALUE: 8796.00 ============================ INPUT AGAIN? Type YES to input again, type NO to end program: YeS Input Code: MOST.ab UNABLE TO CONVERT YOUR INPUT ============================ INPUT AGAIN? Type YES to input again, type NO to end program: Yes Input Code: ters.cc VALUE: 6789.11 ============================ INPUT AGAIN? Typ e YES to input again, type NO to end program: No PROGRAM WILL BE TERMINATED

pahelp po string po
 
Ano po bang dapat na kinalabasan nito? Loop lang ba ng pag input hanggang sa mag No at ma exit?
eto po yung output po nya

Screenshot (538).webp
 
Ito yung nagawa ko. Ang problema lang kahit hindi No ang na input ng user (example "Joke") mag teterminate pa rin ang loop.

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

using std::cout; using std::cin;
using std::endl; using std::string;

string Convert(string);

int main() {
   
    string choice, code, value;

    do
    {
        cout << "Input Code: ";
        cin >> code;
        value = Convert(code);
        cout << "VALUE: " + value+ "\n";
        cout << "INPUT AGAIN? Type YES to input again, type NO to end program: ";
        cin >> choice;
    }while(strcasecmp(choice.c_str(), "YES") == 0);
   
    cout << "PROGRAM WILL BE TERMINATED";
   
    return 0;
}

string Convert(string code)
{
    int i, check = 1;
    string val;
   
    for(i=0; i<code.length(); i++)
    {
        char temp = toupper(code[i]);
       
        if(check == 0)
        {
            break;
        }
        else
        {
            switch(temp)
            {
                case 'C':
                    val += "1";
                    break;
                case 'O':
                    val += "2";
                    break;
                case 'M':
                    val += "3";
                    break;
                case 'P':
                    val += "4";
                    break;
                case 'U':
                    val += "5";
                    break;
                case 'T':
                    val += "6";
                    break;
                case 'E':
                    val += "7";
                    break;
                case 'R':
                    val += "8";
                    break;
                case 'S':
                    val += "9";
                    break;
                case '.':
                    val += ".";
                    break;
                case 'X':
                    val += "0";
                    break;
                default:
                    val = "UNABLE TO CONVERT YOUR INPUT";
                    check = 0;
                    break;
            }
        }
       
    }
   
    return val;
}
 
Ito yung nagawa ko. Ang problema lang kahit hindi No ang na input ng user (example "Joke") mag teterminate pa rin ang loop.

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

using std::cout; using std::cin;
using std::endl; using std::string;

string Convert(string);

int main() {
  
    string choice, code, value;

    do
    {
        cout << "Input Code: ";
        cin >> code;
        value = Convert(code);
        cout << "VALUE: " + value+ "\n";
        cout << "INPUT AGAIN? Type YES to input again, type NO to end program: ";
        cin >> choice;
    }while(strcasecmp(choice.c_str(), "YES") == 0);
  
    cout << "PROGRAM WILL BE TERMINATED";
  
    return 0;
}

string Convert(string code)
{
    int i, check = 1;
    string val;
  
    for(i=0; i<code.length(); i++)
    {
        char temp = toupper(code[i]);
      
        if(check == 0)
        {
            break;
        }
        else
        {
            switch(temp)
            {
                case 'C':
                    val += "1";
                    break;
                case 'O':
                    val += "2";
                    break;
                case 'M':
                    val += "3";
                    break;
                case 'P':
                    val += "4";
                    break;
                case 'U':
                    val += "5";
                    break;
                case 'T':
                    val += "6";
                    break;
                case 'E':
                    val += "7";
                    break;
                case 'R':
                    val += "8";
                    break;
                case 'S':
                    val += "9";
                    break;
                case '.':
                    val += ".";
                    break;
                case 'X':
                    val += "0";
                    break;
                default:
                    val = "UNABLE TO CONVERT YOUR INPUT";
                    check = 0;
                    break;
            }
        }
      
    }
  
    return val;
}
thank you po
 
C++:
#include <iostream>
#include <string>
using namespace std;


string result;
int main()
{
    string userInput;
    char decision[4]={};
    bool quit=false, valid=false, Convert(char input);

    do
    {
        cout << "Input Code: ";
        cin >> userInput;
        result = "";
        for (std::size_t i = 0; i < userInput.length(); ++i)
        {
            if (Convert(userInput[i]))
            {
                continue;
            }
            else
            {
                result = "UNABLE TO CONVERT YOUR INPUT";
                break;
            }
        }
        cout << "VALUE: " << result << endl;

        cout << "INPUT AGAIN? Type YES to input again, type NO to end program: " << endl;
        cin >> decision;
        
        for (int i = 0; i < strlen(decision); i++)
        {
            decision[i] = toupper(decision[i]);
        }

        do
        {
            valid = false;
            if (strcmp(decision, "YES") == 0)
            {
                quit = false;
                valid = true;
            }
            else if (strcmp(decision, "NO") == 0)
            {
                quit = true;
                valid = true;
            }
            else
            {
                valid = false;
                cout << "invalid input, please Type YES or NO" << endl;
                cout << "INPUT AGAIN? Type YES to input again, type NO to end program: " << endl;
                cin >> decision;
                for (int i = 0; i < strlen(decision); i++)
                {
                    decision[i] = toupper(decision[i]);
                }
            }
        } while (!valid);

    } while (!quit);

}

bool Convert(char input)
{
    char code[] = { 'C','O','M','P','U','T','E','R','S','.','X'};
    char value[] = { '1','2','3','4','5','6','7','8','9','.','0'};
    bool match = false;
    
    for (int j = 0; j < 11; j++)
    {
        if (toupper(input) == code[j])
        {
            result = result + value[j];
            match = true;
        }
    }
    return match;
}

in-expand ko lang yung ginawa ni H20_waterr dinagdagan ko lang ng loop para icheck kung YES or NO yung sagot ng user, at kailangan mag enter ulit ng sagot pag hindi valid.
 
Create 2 functions that has the same name GetAve
The first GetAve will have 3 parameters for WHOLE numbers.
The second GetAve will have 3 parameters for numbers with decimal
point.
The code of the two functions are just the same.
The code of your main() function should be:
int main()
{
GetAve(20, 60, 100);
GetAve(2.41, 3.333, 12.25);
int x,y,z;
cout<<"x = "; cin>>x;
cout<<"y = "; cin>>y;
cout<<"z = "; cin>>z;
GetAve(x,y,z);
float a,b,c;
cout<<"a = "; cin>>a;
cout<<"b = "; cin>>b;
cout<<"c = "; cin>>c;
GetAve(a,b,c);
double u,v,w;
cout<<"u = "; cin>>u;
cout<<"v = "; cin>>v;
cout<<"w = "; cin>>w;
GetAve(u,v,w);

return 0;
} hihingi sana ako tulong kung ano po pwedeng function dito (topic po function overload c++)
Screenshot (581).webp
 
C++:
#include <iostream>
using namespace std;

void GetAve(int a, int b, int c)
{
    cout << endl<< "Average of " << a << ", " << b << ", " << c << " is " << (a + b + c) / 3 << endl;
}

void GetAve(float a, float b, float c)
{
    cout << endl << "Average of " << a << ", " << b << ", " << c << " is " << (a + b + c) / 3 << endl;
}

void GetAve(double a, double b, double c)
{
    cout << endl << "Average of " << a << ", " << b << ", " << c << " is " << (a + b + c) / 3 << endl;
}


int main()
{
    GetAve(20, 60, 100);
    GetAve(2.41, 3.333, 12.25);
    int x, y, z;
    cout << "x = "; cin >> x;
    cout << "y = "; cin >> y;
    cout << "z = "; cin >> z;
    GetAve(x, y, z);
    float a, b, c;
    cout << "a = "; cin >> a;
    cout << "b = "; cin >> b;
    cout << "c = "; cin >> c;
    GetAve(a, b, c);
    double u, v, w;
    cout << "u = "; cin >> u;
    cout << "v = "; cin >> v;
    cout << "w = "; cin >> w;
    GetAve(u, v, w);

    return 0;
}
 
C++:
#include <iostream>
using namespace std;

void GetAve(int a, int b, int c)
{
    cout << endl<< "Average of " << a << ", " << b << ", " << c << " is " << (a + b + c) / 3 << endl;
}

void GetAve(float a, float b, float c)
{
    cout << endl << "Average of " << a << ", " << b << ", " << c << " is " << (a + b + c) / 3 << endl;
}

void GetAve(double a, double b, double c)
{
    cout << endl << "Average of " << a << ", " << b << ", " << c << " is " << (a + b + c) / 3 << endl;
}


int main()
{
    GetAve(20, 60, 100);
    GetAve(2.41, 3.333, 12.25);
    int x, y, z;
    cout << "x = "; cin >> x;
    cout << "y = "; cin >> y;
    cout << "z = "; cin >> z;
    GetAve(x, y, z);
    float a, b, c;
    cout << "a = "; cin >> a;
    cout << "b = "; cin >> b;
    cout << "c = "; cin >> c;
    GetAve(a, b, c);
    double u, v, w;
    cout << "u = "; cin >> u;
    cout << "v = "; cin >> v;
    cout << "w = "; cin >> w;
    GetAve(u, v, w);

    return 0;
}
thank you po
 
Status
Not open for further replies.

About this Thread

  • 10
    Replies
  • 778
    Views
  • 3
    Participants
Last reply from:
bie mp

Trending Topics

Online now

Members online
412
Guests online
1,192
Total visitors
1,604

Forum statistics

Threads
2,274,113
Posts
28,953,731
Members
1,235,080
Latest member
arkzx
Back
Top