Solved

strings - super newbie very easy

Posted on 2003-11-18
4
351 Views
Last Modified: 2010-04-02
ok this is stupid, but i need to test sometihng, just tell me what i should be doing to do what i want.

#include "StdAfx.h"
#include "iostream.h"
#include "windows.h"
char inputpass[20];
int something;
int main(int argc, char* argv[])
{

cout << "enter your password please\n \n";


cout <<"\n Password:";
cin >> inputpass;

if ( inputpass != "mysecretpass") something = 1;

return 0;
}
0
Comment
Question by:FatNewbie
  • 2
4 Comments
 
LVL 7

Expert Comment

by:jj819430
ID: 9774186
what do you want to do? What are the errors?

#include "StdAfx.h"
#include "iostream.h"
#include "windows.h"

int main()
{
string inputpass;
int something;

cout << "enter your password please\n \n";
cout <<"\n Password:";
cin >> inputpass;

if ( inputpass != "mysecretpass") something = 1;

return 0;
}

That is easier but not neccessarily wrong... Are you doing this in C or C++? You can use C standards with C++ (but you don't have to)
you can just use strings instead of char *;
0
 
LVL 86

Accepted Solution

by:
jkr earned 20 total points
ID: 9774193
#include <string.h>

if ( strcmp ( inputpass, "mysecretpass")) something = 1;

You cannot compare strings using '==' or '!=', at least not when you are using character arrays.
0
 
LVL 23

Expert Comment

by:brettmjohnson
ID: 9774525
> if ( inputpass != "mysecretpass")

When used with char * or char[], the == equality operator compares the
addresses of the character strings, not the contents of the strings.  Since
the address of the global character array iputpass will never equal the address
of the character string constant "mysecretpass", the above if statement will
always be true.  To compare the contents of two character strings you will
need to use the strcmp(), strncmp(), stricmp() ... family of library routines.

0
 
LVL 7

Expert Comment

by:jj819430
ID: 9774567
well if you wanted to use the char * method. You would just have to work your way through the list and compare each one.
not efficient but not rediculously troublesome.
and iin my previous comment you need the #include <string.h>

sorry
Jj
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

860 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question