Solved

strings - super newbie very easy

Posted on 2003-11-18
4
342 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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
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.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

813 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

15 Experts available now in Live!

Get 1:1 Help Now