Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

changing a var 1

Status
Not open for further replies.

jimjake

Technical User
Oct 30, 2004
25
US
I want to pad the variable PWD with two spaces to make the char set 10
All is well until the last statement then the complier goes berserk telling me the variable Pwd
Is incompatible.









#include <iostream>
#include <cstring>

int main()
{

int len;
std::cout << "password";
char pwd[40];
std::cin >> pwd;
len = std::strlen(pwd);
std::cout << len;
if (len == 8)
pwd = pwd + (' ');


return 0;
}
 
C++ does not have high level string features built into the language. Concatination is NOT built in to the language, so the + doesn't work. Do it like this using the std::string class:

Code:
#include <iostream>
#include <cstring>
#include <string>

int main()
{
  
  int len;
  std::cout << "password";
  char pwd[40];
  std::cin >> pwd;
  std::string password = pwd;
  len = password.length();
  std::cout << len;
  if (len == 8)
    password += "  ";
  
   
 return 0;
}
 
Function strlen() (and others from C library from <c...> headers) is not a member of namespace std. It's in the global namespace, true reference is ::strlen(...) or simply strlen().
 
Of course, the old C technique would be -

if (len==8) {
password[8]=' ';
password[9]=' ';
password[10]='\0';
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top