Jump to content
Sign in to follow this  
BLuFeNiX

help me please... C++

Recommended Posts

BLuFeNiX

I just started c++ programming a few days ago, and am trying to make my own command prompt just for fun. It's about 95% complete, and I need to fix the cd command.

string dirString;

cout << "Where? ";
getline(cin, dirString);   // get directory input from user

int i = strlen(dirString.c_str());   // set i to the length of the dirString

while (dirString.at(i)=="\\") {   // is the last character of dirString a "\" ?   THIS IS WHERE THE ERROR IS
    dirString = dirString.substr(0, dirString.length() - 1);   // if so, remove last character in string
};

int start = dirString.find("\\");           //
int length = string("\\").size();           // change "\" to "\\" so that it can be read correctly.
dirString.replace(start, length, "\\\\");   //
system("cd");   // print new directory using system() call

_chdir(dirString.c_str());   // change current working directory

the errors are as follows:

error C2446: '==' : no conversion from 'const char *' to 'int'

There is no context in which this conversion is possible

error C2040: '==' : 'int' differs in levels of indirection from 'const char [2]'

can somebody help me out? (if you decide to optimize the code, please post it seperate from the fixed code, thanks)

Share this post


Link to post
Share on other sites
Richard Robertson

Is that std::string? If so, at() returns a character, not a string. You should compare it to '\\'.

Share this post


Link to post
Share on other sites
BLuFeNiX

thanks for te help : ) , but it seems to have fixed one thing and broken another : (

string dirString;

    cout << "Where? ";
    getline(cin, dirString);   // get directory input from user

    int i = strlen(dirString.c_str());   // set i to the length of dirString

    while (dirString.at(i)=='\\') {   // is the last character of dirString a "\" ?
        dirString = dirString.substr(0, dirString.length() - 1);   // remove last character in string
        i--;   //THIS WAS MISSING FROM LOGIC BEFORE
    };

    int start = dirString.find("\\");           //
    int length = string("\\").size();           // change "\" to "\\" so that it can be read correctly.
    dirString.replace(start, length, "\\\\");   //
    system("cd");   // print new directory using system() call
    
    _chdir(dirString.c_str());   // change current working directory

now i get this popup error when i run the program:

Unhandled exception at 0x7c812aeb in BLuCMD.exe: Microsoft C++ exception: std::out_of_range at memory location 0x0012fb90..

it does that no matter what i type into the directory prompt...help!?

Share this post


Link to post
Share on other sites
Valik

The best help anybody can give you is just stop, think and start over. You really need to think about your code. It's awful. Yes, I know you're just starting out. But really, how did you even come to the conclusion to write that? What are you reading to learn C++ that made you go that way to write your code? Don't you think your while loop construct resembles something else? Did you bother reading all the documentation for std::string::at()? Clearly you read about it somewhere because you are using it instead of the more conventional operator[] but yet you didn't see where it throws an exception?

Over half the lines of code you've written just don't make sense. Stop and think about them. Better yet, find something good to read because you're in way over your head. It's very clear you don't understand native types and here you are trying to use STL which is a bit too advanced for your skill level.

Share this post


Link to post
Share on other sites
BLuFeNiX

well, would you (or anyone else) be willing to contribute a more logical way of solving this problem? My original code, before trying to keep the string from having a trailing "\", is as follows

string dirString;

    cout << "Where? ";
    getline(cin, dirString);   // get directory input from user

    int start = dirString.find("\\");           //
    int length = string("\\").size();           // change "\" to "\\" so that it can be read correctly.
    dirString.replace(start, length, "\\\\");   //
    _chdir(dirString.c_str());   // change current working directory
    system("cd");   // print new directory using system() call

is there anything non-sensical about that part?

Share this post


Link to post
Share on other sites
Valik

Yes.

Share this post


Link to post
Share on other sites
BLuFeNiX

okay, lets try this again, but next time you answer, lets have just a little bit more feeling, and maybe some constructive context. aaaaand. action.

but, seriously, what is wrong with it?

Share this post


Link to post
Share on other sites
Valik

okay, lets try this again, but next time you answer, lets have just a little bit more feeling, and maybe some constructive context. aaaaand. action.

but, seriously, what is wrong with it?

Why? Why should I bother doing a line-by-line analysis just to show you what's wrong? You've already demonstrated you don't really want to learn or you would have stopped asking for answers and gone back and read something so you can understand how the language works.

You're trying to build a house without a foundation. Even if we help you prop it up it's just going to collapse. Go learn the foundation on your own. If you need books then see here.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×