Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Mar 6th, 2005, 6:40 PM   #1
layer
Hobbyist Programmer
 
Join Date: Feb 2005
Posts: 112
Rep Power: 4 layer is on a distinguished road
Thumbs down Weird compiler error (Dev-Cpp 4.9.9.2)

hello again...im making a little command line and need a little help... well, fist ill show the code, then the questions! (1 question actualy)
#include <cstdlib>
#include <iostream>
#include <windows.h>

using namespace std;

int main()
{
    string line; 
    cout << "EasyCommand v. 1.0.0 BETA\n";
    cout << "Copyright (c) 2005-2006\n";
    cout << "Type 'help' for help\n\n";
    do { 
        cout << ": ";
        cin >> line;
        if (line == "exit"){
                 return 0;
        }
        if (line == "box"){
                 string buffer;
                 cout << "\nEnter a title: ";
                 cin >> buffer;
                 string buffertwo;
                 cout << "\nEnter the text: ";
                 cin >> buffertwo;
                 MessageBox(NULL, buffertwo, buffer, 0);
                 }
        if (line == "clear"){
                 system("cls"); 
                 cout << "\n";
        }
        if (line == "pause"){
             system("pause"); 
             cout << "\n";
        }
        if (line == "help"){
             cout << "\nBOX   Used to display a simple messagebox\nExample - Box (Output)Title: TitleHere (Output)Text: TextHere\nCLEAR Used to clear the console screen\nExample - clear\nEXIT  Used to exit the console\nExample - exit\nHELP  Used to display a list of commands\nExample - help\nPAUSE Used to pause the console \nExample - pause\n";
      }
  } while (line != "exit");
  cout << "Exiting...";
  return 0;
}

ok, when i try to compile that, i get this error:
Quote:
26 C:\Documents and Settings\ryanmoore\Desktop\Goal\main.cpp cannot convert `std::string' to `const CHAR*' for argument `2' to `int MessageBoxA(HWND__*, const CHAR*, const CHAR*, UINT)'
now its obviously got something to do with the box section of the code... could someone PELASE explain to me what i am doing wrong with it? thanks a bunch!
layer is offline   Reply With Quote
Old Mar 6th, 2005, 7:24 PM   #2
Ooble
I eat cake for breakfast.
 
Ooble's Avatar
 
Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9 Ooble is on a distinguished road
In the message box, you can't use a string - you have to use a character array. You do this by converting the string to the array when you need it:
buffertwo becomes buffertwo.c_str() - this converts it into a C-style string (AKA a character array).
__________________
Me :: You :: Them
Ooble is offline   Reply With Quote
Old Mar 6th, 2005, 7:39 PM   #3
layer
Hobbyist Programmer
 
Join Date: Feb 2005
Posts: 112
Rep Power: 4 layer is on a distinguished road
hey! thanks Ooble! works great now... except one thing i had to change it to char, but before i did, i tried what you told me, but when i chose a title or the text there could be no spaces, because anything after the space wouldnt show up, so heres how i approched it:
        if (line == "box"){
                 char buffer[100];
                 cout << "\nEnter the text: ";
                 cin.getline(buffer,100);
                 char buffertwo[100];
                 cout << "\nEnter the title: ";
                 cin.getline(buffertwo,100);
                 MessageBox(NULL, buffer, buffertwo, 0);
                 }

now that works great, but it always skips the text section, it will be like this:
Quote:
Enter the text: \\ this is what gets skips, although it gets printed out, you cant input anything into it because it goes to title..
Enter the title: |cursorhere|
anymore suggestions? thanks!
layer is offline   Reply With Quote
Old Mar 6th, 2005, 7:53 PM   #4
layer
Hobbyist Programmer
 
Join Date: Feb 2005
Posts: 112
Rep Power: 4 layer is on a distinguished road
to explain it a little better... it doesnt let the user input anything for the text, it skips right over that and goes to title... please ask if you do not understand, i will explain it better if needed :o
layer is offline   Reply With Quote
Old Mar 7th, 2005, 9:43 AM   #5
Infinite Recursion
Programming Guru
 
Infinite Recursion's Avatar
 
Join Date: Jul 2004
Location: United States
Posts: 3,467
Rep Power: 8 Infinite Recursion is on a distinguished road
Send a message via MSN to Infinite Recursion Send a message via Yahoo to Infinite Recursion
changing your line to this:

MessageBox(NULL, buffertwo.c_str(), buffer.c_str(), 0);

works fine for me... could you explain your last problem a bit more, if this does not resolve it?
__________________
http://jasonpowers.net

"There are a thousand hacking at the branches of evil to one who is striking at the root."
Infinite Recursion is offline   Reply With Quote
Old Mar 7th, 2005, 3:15 PM   #6
layer
Hobbyist Programmer
 
Join Date: Feb 2005
Posts: 112
Rep Power: 4 layer is on a distinguished road
but see, heres the thing, if i use that... then if i type in "Hello World!", ONLY Hello shows up... sooo... any suggestions

and

while im here... ill ask this... how do i split a string in C++? lets say i typed in this to the console: "box TitleHere TextHere"... is there a way i can split that up so that i would end up with something like:
string title = TitleHere;
string text = TextHere;

? for now im going to try to google it, but if i could figure this out, then that would solve my first question.. so im going to reasearch it, but if anyone else knows, could you please share? thanks a bunch (please tell me if im asking too much)
layer is offline   Reply With Quote
Old Mar 7th, 2005, 3:24 PM   #7
BaroN NighT
Programmer
 
Join Date: Mar 2005
Location: USA
Posts: 60
Rep Power: 4 BaroN NighT is on a distinguished road
Quote:
Originally Posted by Ooble
In the message box, you can't use a string - you have to use a character array.
Why can't you use a string?
BaroN NighT is offline   Reply With Quote
Old Mar 7th, 2005, 3:47 PM   #8
Infinite Recursion
Programming Guru
 
Infinite Recursion's Avatar
 
Join Date: Jul 2004
Location: United States
Posts: 3,467
Rep Power: 8 Infinite Recursion is on a distinguished road
Send a message via MSN to Infinite Recursion Send a message via Yahoo to Infinite Recursion
as far as what the user types in... look into using the getline() function for your line variable.

The splitting of strings is possible via the standard template library... there are also a few snippets on the web that brute force it by looking for white space in character arrays and such.
__________________
http://jasonpowers.net

"There are a thousand hacking at the branches of evil to one who is striking at the root."
Infinite Recursion is offline   Reply With Quote
Old Mar 7th, 2005, 4:01 PM   #9
layer
Hobbyist Programmer
 
Join Date: Feb 2005
Posts: 112
Rep Power: 4 layer is on a distinguished road
thanks guys... i think i got all the info i need now.. although i may be posting back later, so keep checking the forums! but ive found a string splitting solution heres the website i got it at : http://www.codeguru.com/Cpp/Cpp/stri...cle.php/c9279/

although i had to remove the _T and Console::WriteLine because the _T gave me erros, but i removed it and it worked fine and, for Console::WriteLine i dont know if it would have worked or not but i just used cout thanks a million guys! (when im finished with this project, i will be posting it in the "Finished Projects" section, but i dont know when it will be finished, maybe tonight, maybe in a week, i cant tell the future, only sometimes!)
layer is offline   Reply With Quote
Old Mar 7th, 2005, 4:45 PM   #10
layer
Hobbyist Programmer
 
Join Date: Feb 2005
Posts: 112
Rep Power: 4 layer is on a distinguished road
k, i lied, now im getting frusterated! could someone please explain to me, why i get this error when trying to compile this code...:
#include <cstdlib>
#include <iostream>
#include <windows.h>

using namespace std;


void strtoktest()
{  
  char input[] = line;
  char delimiters[] = ("print ");

  for (char* token = strtok(input, delimiters);
       token != NULL;
       token = strtok(NULL, delimiters))
  {
    cout << "\n";   
    cout << (token);
  }
}

int main()
{
    string line; 
    cout << "EasyCommand v. 1.0.0 BETA\n";
    cout << "Copyright (c) 2005-2006\n";
    cout << "Type 'help' for help\n\n";
    do { 
        //cout << ": ";
        cin >> line;
        if (line == "exit"){
                 return 0;
        }
        if (line == "box"){
                 char buffer[100];
                 cout << "\nEnter the title of the messagebox: ";
                 cin.getline(buffer,100);
                 char buffertwo[100];
                 cout << "\nEnter the text of the messagebox: ";
                 cin.getline(buffertwo,100);
                 MessageBox(NULL, buffertwo, buffer, 0);
                 }
        if (line == "clear"){
                 system("cls"); 
                 cout << "\n";
        }
        if (line == "pause"){
             system("pause"); 
             cout << "\n";
        }
        if (line == "help"){
             cout << "\nBOX   Used to display a simple messagebox\nExample - Box 'Title Here' 'Text Here'\nCLEAR Used to clear the console screen\nExample - clear\nEXIT  Used to exit the console\nExample - exit\nHELP  Used to display a list of commands\nExample - help\nPAUSE Used to pause the console \nExample - pause\nPRINT Used to echo things out into the console\nExample - print 'text here'\n";
        }
        if (line == "print "){
             strtoktest();    
        }
  } while (line != "exit");
  cout << "Exiting...";
  return 0;
}

it says that line is undelared and to use this function first... now i know what that means... so i tried to put this function AFTER the int main and then main didnt know what strtoktest() meant so i put it back before main.. is there like a way, to make string line; global? (im used to autoit where you could go like: Global $variable... is there a way to do this, please ask if you do not understand what i mean... thanks
layer is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 5:07 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC