Solved

strings - super newbie very easy

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

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

Title # Comments Views Activity
C++ to C# code conversion issue 4 119
Arduino EDI - Programming language 3 115
Autosar OS Multicore Share Resources confusion ? 2 105
C++ help/ Toy problem 19 52
Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
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 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 be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

749 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