0

I am trying to make a password prompt using C++. My code is given below:

#include <iostream>
#include <string>
std::string operator * (std::string a, unsigned int b) {
    std::string output = "";
    while (b--) {
        output += a;
    }
    return output;
}
int main(){
    std::string pword;
    std::string user;
    std::cout << "Enter username: ";
    std::cin >> user;
    std::cout << "Enter password for user '" << user << "': ";
    std::cin >> pword;
    std::string pword_asterix = ("*") * pword.length();  // ERROR
    std::clog << "Noted user '" << user << "' and password '" << pword_asterix << "'.";

But I'm getting the following error from Visual Studio Code:

expression must have arithmetic or unscoped enum type(Line 17)

What should I do?

cigien
  • 57,834
  • 11
  • 73
  • 112
T cube
  • 27
  • 1
  • 10

2 Answers2

2

in

 std::string pword_asterix = ("*") * pword.length();

you multiply a pointer, you want something like :

std::string pword_asterix(pword.length(), '*');

after that :

pi@raspberrypi:/tmp $ g++ -Wall c.cc
pi@raspberrypi:/tmp $ ./a.out
Enter username: aze
Enter password for user 'aze': qsdqsd
Noted user 'aze' and password '******'.pi@raspberrypi:/tmp $ 

Adding << std::endl to have

std::clog << "Noted user '" << user << "' and password '" << pword_asterix << "'." << std::endl;` 

makes output more clear in a shell :

pi@raspberrypi:/tmp $ g++ -Wall c.cc
pi@raspberrypi:/tmp $ ./a.out
Enter username: aze
Enter password for user 'aze': qsdqsd
Noted user 'aze' and password '******'.
pi@raspberrypi:/tmp $ 
bruno
  • 32,421
  • 7
  • 25
  • 37
  • 1
    I'm not sure why you're suggesting that `std::string("*") * pword.length()` fixes the problem. – cigien Nov 01 '20 at 15:03
  • @cigien you are right, strange because I tried ... or I missed to compile ? I edited my answer – bruno Nov 01 '20 at 15:20
  • @cigien I like c++ and is my main language, but clearly python is more user friendly and allows to do that. THank you for warning me about the wrong solution – bruno Nov 01 '20 at 15:24
  • I understand that point of view; python can be easier to use at times, I wouldn't disagree with that. Personally, I find the value of static type checking in C++ far outweighs the benefits of ease of coding. It does depend on the context, which language one might choose, of course. – cigien Nov 01 '20 at 15:27
  • @cigien type checking at compile time avoid a lot of errors, including error else detected long time after the writting the first time the code finally run where the cpu never put its feet before ;-) Anyway C++ is not dead at all and I see few seconds before your answer https://stackoverflow.com/a/64624069/2458991 – bruno Nov 01 '20 at 15:31
  • 1
    Yes, I've always *liked* C++, but in the last 5-10 years, I've grown to *love* it, because it's evolving in ways that make it much easier, and a lot more fun to write :) – cigien Nov 01 '20 at 15:52
0

On line 17 you have this line

("*") * pword.length();

where T(*) = std::string" and T(pword.length()) = int. We can find in the documentation that string cannot be multiplied (operator * is not supported): http://www.cplusplus.com/reference/string/string/string/.

If you're trying to construct a string with length = pword.length() and initialized with *s, take a look at the "fill constructor" in the documentation. In your case, it would be:

std::string pword_asterix(pword.length(), '*');
Casper Dijkstra
  • 1,615
  • 10
  • 37