Solved

strings - super newbie very easy

Posted on 2003-11-18
4
360 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
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 how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

734 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