• 0

[C++] Yet another simple program


Question

Yes it's me again and another one of my little college programs that I can't get working.. I really wish they'd start teaching us with an up to date compiler rather than one that's 8 years old.. Anywayz, why doesn't this work.. According to what we've been taught in class it should so I'm confused.. The exercise is to accept the password borland regardless of case.. Thanks!

#include <cstdio>
#include <cstdlib>
#include <iostream>
using namespace std;

int main(int nNumberofArgs, char* pszArgs[])
{
    string szPassword;
    cout << "Please enter the password"<<endl;
    cin >> szPassword;
    if (stricmp(szPassword, "borland")
    {
        cout << "Access Granted"<<endl;
    }    
    else
    {
        cout <<"Access Denied"<<endl;
    }
    system("PAUSE");
    return 0;    
}

Link to comment
Share on other sites

10 answers to this question

Recommended Posts

  • 0

Oh yeah and I also missed off that bracket.. But I still get this error:

11 C:\Documents and Settings\mark52230\My Documents\College\cprogs\unit5p5.cpp cannot convert `std::string' to `const char*' for argument `1' to `int 

I feel so stupid asking simple stuff like this :blush:

Link to comment
Share on other sites

  • 0

The thing is that stricmp accepts pointer to chars like this:

int stricmp( const char *, char * );

you need to find a way to cast your string as a pointer to a char array try this:

(char *) szPassword so you get it like this:

if( !stricmp( (char *) szPassword, "borland" ) {

Link to comment
Share on other sites

  • 0

i'm not sure if you can use string szPassword;

is there a string class defined in one of those includes? I usually use character character strings.

if you use character strings, you would replace "string szPassword;" with "char szPassword[200]='\0';"

this makes an array of characters that is 200 characters long and is filled with \0, which is the terminating character for strings.

I haven't used cin for a little while, and am not sure if it would allow you to overflow the array or not, that's why i say do 200 since people are not likely to type a 200 letter password

Link to comment
Share on other sites

  • 0

huh? I don't think I really understood that but do you mean that stricmp doesn't work with strings? if that is what you meant is there not something I could use instead of stricmp? Or shall I just create an array of char's to hold the password? I don't really understand what's going on in that bit of code to be honest

Sorry if I've completely mis understood

Edit: sorry I'm missing peoples posts b4 i reply

Link to comment
Share on other sites

  • 0

GatorV's solution is wrong on so many levels :no:

You have a string object. If you want to get the primitive representation from it (i.e. const char*), use c_str().

if( !stricmp(szPassword.c_str(),"borland") )

Link to comment
Share on other sites

  • 0
Oh yeah and I also missed off that bracket.. But I still get this error:

I feel so stupid asking simple stuff like this :blush:

584720042[/snapback]

string objects have a c_str method that returns a pointer to a C-style string(char*).

hehe.. what Oogle said! ^^ :)

Edited by weenur
Link to comment
Share on other sites

  • 0
I feel so stupid asking simple stuff like this

You need to look at the error message and try to deduce your error from it. It's hard at first, but you'll get better over time.

cannot convert `std::string' to `const char*' for argument `1'

Quite apparently there's something wrong with the first argument (szPassword) that you pass to stricmp. Check your docs, you'll see that stricmp expects both parameters to be "const char*". Older docs may omit the "const" bit. A "const char*" is a pointer to a character (or to an array of characters). szPassword, however, is a "string" - which is obviously not the correct type.

Now you know what/where the error is and why the compiler spit out that particular error message. To fix it, you need to figure out how you can convert the "string" to a "const char*". Since I'm not sure whether you've already learned what classes and member function are, I'll tell you instead of having you look it up yourself: The standard "string" type has a "member function" called c_str() which does just the conversion you want. You use it as follows:

if (stricmp(szPassword.c_str(), "borland"))

HTH

EDIT: Typing such a post takes some time. Five new posts in the meantime. Meh.

Link to comment
Share on other sites

  • 0
GatorV's solution is wrong on so many levels  :no:

You have a string object.  If you want to get the primitive representation from it (i.e. const char*), use c_str().

if( !stricmp(szPassword.c_str(),"borland") )

584720150[/snapback]

:blush: Sorry I didn't now about the c_str() method, I program mostly in Java and/or PHP :D

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.