954,505 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

first cannot conver std::string to const char, now runtime error!

cin.ignore();
cout << "Enter lecture notes/tutorials location.\n";
cout << "e.g C:/Lecture Notes/lecture01.ppt";
cout << ">>>";
getline(cin,lectut_loca);
cout << "Enter lecture notes/tutorials name: ";
cout << "e.g lecture01.ppt";
cout << ">>>";
getline(cin,lectut_name);
string copy = "copy";
copier = copy + " " + lectut_loca + " " + lectut_name;
const char *copier;
cout << copier;
getch();
system(copier);
cout << "\nLecture notes/tutorials added.";

theconst char *copier; i use copier = copier.c_str(); but get error on system(copier); when compiling. then i use const char *copier; no error compile but runtime error!

:lol:

what should i do?

n3st3d_l00p
Newbie Poster
20 posts since Sep 2006
Reputation Points: 22
Solved Threads: 0
 
cin.ignore();
cout << "Enter lecture notes/tutorials location.\n";
cout << "e.g C:/Lecture Notes/lecture01.ppt";
cout << ">>>";
getline(cin,lectut_loca);
cout << "Enter lecture notes/tutorials name: ";
cout << "e.g lecture01.ppt";
cout << ">>>";
getline(cin,lectut_name);
string copy = "copy";
 copier = copy + " " + lectut_loca + " " + lectut_name;
// using variable before declaring it is not allowed in your case copier.

 const char *copier; // initialisation of const variables must be done 
                            // during declaration intself eg. const int i = 8 ;
 cout << copier;
 getch(); // getch() is non portable use cin.get ()
system(copier);
cout << "\nLecture notes/tutorials added.";
what should i do?

Look at the things i have mentioned above.
And maybe you should look at this simple expample.

string my_copy = "copy" ;
my_copy += " " + lectut_loca + " " + lectut_name ;
const char* copier = my_copy.c_str() ;
system (copier) ;


Hope ti helped, bye.

PS: looks like you are trying to implement the copy function of DOS.
;)

~s.o.s~
Failure as a human
Administrator
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 734
 

Look at the things i have mentioned above. And maybe you should look at this simple expample.

string my_copy = "copy" ;
my_copy += " " + lectut_loca + " " + lectut_name ;
const char* copier = my_copy.c_str() ;
system (copier) ;

Hope ti helped, bye.

PS: looks like you are trying to implement the copy function of DOS. ;)


yah.. should be like that? urmm.. syntax error! :confused:

n3st3d_l00p
Newbie Poster
20 posts since Sep 2006
Reputation Points: 22
Solved Threads: 0
 

add

char a='"';
strcat(a, copier);
strcat(copier, a);

hahaha.. compile error!

n3st3d_l00p
Newbie Poster
20 posts since Sep 2006
Reputation Points: 22
Solved Threads: 0
 

I dont get you...
Are you saying even after making the changes you get a syntax error ???

~s.o.s~
Failure as a human
Administrator
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 734
 

Error messages come with line numbers.
Use those line numbers to find out where in the code the problem is. Any editor should have a "goto line" feature, or at least display line numbers in some manner.

Post code (annotated with line numbers if necessary) along with exact error messages if you're still stuck.

Salem
Posting Sage
Team Colleague
11,531 posts since Dec 2005
Reputation Points: 5,862
Solved Threads: 953
 

yes.. after making the change.

error in runtime.. The syntax of the command is incorrect.

n3st3d_l00p
Newbie Poster
20 posts since Sep 2006
Reputation Points: 22
Solved Threads: 0
 

add

char a='"'; strcat(a, copier); strcat(copier, a);

hahaha.. compile error!

Umm... buddy, what exactly are you trying to do. strcat funtion expects you to supply both parameters as char* ie char pointers and you supplying it a char.

You can get its prototype here: http://www.cppreference.com

Post your complete program from start to finish with the header files and then maybe i could pinpoint the mistake.

~s.o.s~
Failure as a human
Administrator
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 734
 

the staff module...

//the staff module

#include "staff.h"
#include <iostream>
#include <fstream>
#include <cstring>
using namespace std;

class sfunction
{
      private:
              string lectut_loca;
              string lectut_id;
              string lectut_name;
              
              string announce_id;
              string announce_subject;
              string announce_desc;
              
      public:
             void add_lec_tut()
             {
                  string copier;
                  display_title("Add Lecture Notes/Tutorials");
                  cout << "Please enter course id: ";
                  cin >> lectut_id;
                  for(int i=1; i<=count_line("course.MOLS"); i++)
                  {
                          if(gettok( read("course.MOLS", i), ';', 1) == lectut_id)
                          {
                               cin.ignore();
                               cout << "Enter lecture notes/tutorials location.\n";
                               cout << "e.g C:/Lecture Notes/lecture01.ppt";
                               cout << ">>>";
                               getline(cin,lectut_loca);
                               cout << "Enter lecture notes/tutorials name: ";
                               cout << "e.g lecture01.ppt";
                               cout << ">>>";
                               getline(cin,lectut_name);
                               string my_copy = "copy" ;
                               my_copy += " " + lectut_loca + " " + lectut_name ;
                               const char* copier = my_copy.c_str() ;
                               cout << copier;
                               system(copier);
                               cin.get();                               
                               cout << "\nLecture notes/tutorials added.";
                               getch(); break;
                          }
                          else if(i==count_line("course.MOLS"))
                          {
                               cout << lectut_id << " not found!";
                               getch();
                          }
                  }
                  
                  
                  
                  getch();
                  
             };
             void edit_lec_tut()
             {
                  display_title("Edit Lecture Notes/Tutorials");
                  getch();
             };
             void del_lec_tut(){};
             
             void add_announ()//add announcement(s)
             {
                  display_title("Add Announcements");
                  cout << "Please enter course id: ";
                  cin >> announce_subject;
                  for(int i=1; i<=count_line("course.MOLS"); i++)
                  {
                          if(gettok( read("course.MOLS", i), ';', 1) == announce_subject)
                          {
                               cout << "Plase enter announcement id: ";
                               cin >> announce_id;
                               cin.ignore();
                               cout << "Please enter your announcement: ";
                               getline(cin,announce_desc);
                               write("Lecturer/announcement.MOLS", i, announce_id + ";" + announce_subject + ";" + announce_desc);
                               cout << "\nAnnouncement added.";
                               getch(); break;
                          }
                          else if(i==count_line("course.MOLS"))
                          {
                               cout << announce_subject << " not found!";
                               getch();
                          }
                  }
             };
             
             void edit_announ()
             {
                  display_title("Add Announcements");
                  cout << "Enter course id: ";
                  cin >> announce_subject;
                  for(int i=1; i<=count_line("Lecturer/announcement.MOLS"); i++)
                  {
                          if(gettok( read("Lecturer/announcement.MOLS", i), ';', 2) == announce_subject)
                          {
                               cout << "Previous announcement:\n"
                               << gettok( read("Lecturer/announcement.MOLS", i), ';', 3) << endl;
                               cout << "\nEnter new announcement id: ";
                               cin >> announce_id;
                               cin.ignore();
                               cout << "Enter new announcement: ";
                               getline(cin,announce_desc);
                               write("Lecturer/announcement.MOLS", i, announce_id + ";" + announce_subject + ";" + announce_desc);
                               cout << "\nAnnouncement added.";
                               getch(); break;
                          }
                          else if(i==count_line("Lecturer/announcement.MOLS"))
                          {
                               cout << announce_subject << " not found!";
                               getch();
                          }
                  }
             };
             void del_announ()
             {
                  display_title("Delete Announcements");
                  cout << "Enter announcement id: ";
                  cin >> announce_id;
                  for(int i=1; i<=count_line("Lecturer/announcement.MOLS"); i++)
                  {
                          if(gettok( read("Lecturer/announcement.MOLS", i), ';', 1) == announce_id)
                          {
                                     del("Lecturer/announcement.MOLS", i);
                                     cout << "Announcement succesfully deleted!";
                                     getch();
                          }
                  }
             };
             void add_assign(){};
             void edit_assign(){};
             void del_assign(){};
};


and the staff header

#ifndef    _STAFF_H_
#define    _STAFF_H_
#include "staff.cpp"
#endif
n3st3d_l00p
Newbie Poster
20 posts since Sep 2006
Reputation Points: 22
Solved Threads: 0
 

So where is your int main (void) function and what problems are you facing with this code, and if facing compile time problems mark the line which flags the error in red so taht we can see it and if run time error then describe it.

~s.o.s~
Failure as a human
Administrator
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 734
 

run time error

after i input the lecture notes/tutorials name..
(getline(cin,lectut_name);

it show>>> The syntax of the command is incorrect.

n3st3d_l00p
Newbie Poster
20 posts since Sep 2006
Reputation Points: 22
Solved Threads: 0
 

[IMG]http://files.asmuiiazfar.com/error.gif[/IMG]

that's the error.. hhuhuhuhu...

n3st3d_l00p
Newbie Poster
20 posts since Sep 2006
Reputation Points: 22
Solved Threads: 0
 

> cout << copier;
Why didn't this appear to output anything?

> system(copier);
If the string is garbage in any way, then you'll get that error message.

> string my_copy = "copy" ;
> my_copy += " " + lectut_loca + " " + lectut_name ;
Starting with a string constant is perhaps a bad idea. + is oveloaded for both char * pointer addition (the type of " ") and std::string concatenation (your strings). The compiler might pick the wrong one.
Try
string my_copy = "copy " ;
my_copy += lectut_loca + " " + lectut_name ;

Salem
Posting Sage
Team Colleague
11,531 posts since Dec 2005
Reputation Points: 5,862
Solved Threads: 953
 

salem,
after i use your code

[IMG]http://files.asmuiiazfar.com/error.png[/IMG]
i may forgot the slash "/" but i hv tried with slash.. got the same error.

n3st3d_l00p
Newbie Poster
20 posts since Sep 2006
Reputation Points: 22
Solved Threads: 0
 

Windows works with backslashes "\" and forward slashes "/" but you better use Backslashes.

And also looking at your "command string" dump i can clearly see the problem. YOu are passing something like "copyA.txt B.txt" which is not the correct syntax fro the DOS copy command.

The actual syntax is "copy source dest" with spaces present in between. You can find the syntax for all commands here
http://www.easydos.com/copy.html

For testing purpose try out inputting
YServer.txt as the first input and server.txt as the second input and watch what output is displayed.

~s.o.s~
Failure as a human
Administrator
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 734
 

yah.. it's work with backslash.. hahaha.. pity me..

because no dest so the file copied to current folder which program is running..

thanks you guys!!!

n3st3d_l00p
Newbie Poster
20 posts since Sep 2006
Reputation Points: 22
Solved Threads: 0
 

Glad we could be of assistance and most of the credit of this problem solved goes to Mr. Salem for pointing out that it was actually the command string which was getting messed up, I just picked up his idea and completed it for you.

Best of luck.

~s.o.s~
Failure as a human
Administrator
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 734
 
copy c:\yserver.txt c:\documents and settings\luffy-san\desktop\tcp1241_assignment3\lecture notes\server.txt

not working...

copy c:\yserver.txt c:\temp\server.txt

working... what is the problem?

n3st3d_l00p
Newbie Poster
20 posts since Sep 2006
Reputation Points: 22
Solved Threads: 0
 

Well judging from the colour scheme from your output examples, my guess is you're using some crusty old Turbo C compiler on a nice new XP based system.

Problem number 1
Turbo C knows nothing about long filename, nor filenames with spaces.

It should be paying attention to the COMSPEC environment variable (which should be say ComSpec=C:\WINDOWS\system32\cmd.exe but I guess your compiler sets it to the far inferior command.com (because that's the historic answer for the historic compiler. Command.com didn't know anything about long filenames or space in filenames either.

And this is only one of many possible compatibility problems you'll face with this compiler.

Get a modern 32-bit compiler, one which is far more compatible with your operating system.

Salem
Posting Sage
Team Colleague
11,531 posts since Dec 2005
Reputation Points: 5,862
Solved Threads: 953
 

There is nothing wrong with your program, it is the command which is incorrect. Do some research on the DOS Copy command to find out the valid commands and the format of the command. Look at the link which i had given in my 4th or 5th post on the previous page.

Hope it helped, bye.

@Mr. Salem : What is the difference if the compiler is 16 bit or 32 bit since the "copy" command which is executed in DOS mode runs in 16 bit. Try out the commands written by the guy in the DOS window without the program, jsut type it and still it doesnt work so i guess no mistake of teh compiler.
But then again maybe i am wrong somewhere, please let me know where it is?

~s.o.s~
Failure as a human
Administrator
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 734
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You