Answer to Question #256260 in C++ for ALI

Question #256260

Q: Create a function “square”, which calculate and displays square of any number entered through the keyboard.  

1
Expert's answer
2021-10-25T03:13:07-0400
#include <iostream>
#include <string>
#include <sstream>
using namespace std;


void square()
{
    int x, x2;
    string str;
    istringstream iss;

    while (true) {
        cout << "Enter a number (empty line to exit) ";
        getline(cin, str);
        if (str.size() == 0) {
            break;
        }
    
        iss.str(str);
        iss.clear();
        
        iss >> x;
        x2 = x*x;
        cout << "The squere of " << x << " is " << x2 
             << endl << endl;
    }
        
}

int main() {
    square();
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS