Solved

strings - super newbie very easy

Posted on 2003-11-18
4
325 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 Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

707 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now