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

Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Sep 2006
Posts: 20
Reputation: n3st3d_l00p is an unknown quantity at this point 
Solved Threads: 0
n3st3d_l00p's Avatar
n3st3d_l00p n3st3d_l00p is offline Offline
Newbie Poster

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

 
0
  #1
Sep 24th, 2006
  1. cin.ignore();
  2. cout << "Enter lecture notes/tutorials location.\n";
  3. cout << "e.g C:/Lecture Notes/lecture01.ppt";
  4. cout << ">>>";
  5. getline(cin,lectut_loca);
  6. cout << "Enter lecture notes/tutorials name: ";
  7. cout << "e.g lecture01.ppt";
  8. cout << ">>>";
  9. getline(cin,lectut_name);
  10. string copy = "copy";
  11. copier = copy + " " + lectut_loca + " " + lectut_name;
  12. const char *copier;
  13. cout << copier;
  14. getch();
  15. system(copier);
  16. cout << "\nLecture notes/tutorials added.";

the const 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?
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,651
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 474
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

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

 
0
  #2
Sep 24th, 2006
Originally Posted by n3st3d_l00p View Post
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.

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

Hope ti helped, bye.

PS: looks like you are trying to implement the copy function of DOS.
I don't accept change; I don't deserve to live.

Jo Tujhe Jagaaye, Nindein Teri Udaaye Khwaab Hai Sachcha Wahi.
Nindon Mein Jo Aaye Jise To Bhul Jaaye Khawab Woh Sachcha Nahi.
Khwaab Ko Raag De, Nind Ko Aag De
Reply With Quote Quick reply to this message  
Join Date: Sep 2006
Posts: 20
Reputation: n3st3d_l00p is an unknown quantity at this point 
Solved Threads: 0
n3st3d_l00p's Avatar
n3st3d_l00p n3st3d_l00p is offline Offline
Newbie Poster

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

 
0
  #3
Sep 24th, 2006
Originally Posted by ~s.o.s~ View Post
Look at the things i have mentioned above.
And maybe you should look at this simple expample.

  1. string my_copy = "copy" ;
  2. my_copy += " " + lectut_loca + " " + lectut_name ;
  3. const char* copier = my_copy.c_str() ;
  4. 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!
Reply With Quote Quick reply to this message  
Join Date: Sep 2006
Posts: 20
Reputation: n3st3d_l00p is an unknown quantity at this point 
Solved Threads: 0
n3st3d_l00p's Avatar
n3st3d_l00p n3st3d_l00p is offline Offline
Newbie Poster

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

 
0
  #4
Sep 24th, 2006
add

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



hahaha.. compile error!
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,651
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 474
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

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

 
0
  #5
Sep 24th, 2006
I dont get you...
Are you saying even after making the changes you get a syntax error ???
I don't accept change; I don't deserve to live.

Jo Tujhe Jagaaye, Nindein Teri Udaaye Khwaab Hai Sachcha Wahi.
Nindon Mein Jo Aaye Jise To Bhul Jaaye Khawab Woh Sachcha Nahi.
Khwaab Ko Raag De, Nind Ko Aag De
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 751
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

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

 
0
  #6
Sep 24th, 2006
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.
Reply With Quote Quick reply to this message  
Join Date: Sep 2006
Posts: 20
Reputation: n3st3d_l00p is an unknown quantity at this point 
Solved Threads: 0
n3st3d_l00p's Avatar
n3st3d_l00p n3st3d_l00p is offline Offline
Newbie Poster

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

 
0
  #7
Sep 24th, 2006
yes.. after making the change.

error in runtime.. The syntax of the command is incorrect.
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,651
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 474
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

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

 
0
  #8
Sep 24th, 2006
Originally Posted by n3st3d_l00p View Post
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.
I don't accept change; I don't deserve to live.

Jo Tujhe Jagaaye, Nindein Teri Udaaye Khwaab Hai Sachcha Wahi.
Nindon Mein Jo Aaye Jise To Bhul Jaaye Khawab Woh Sachcha Nahi.
Khwaab Ko Raag De, Nind Ko Aag De
Reply With Quote Quick reply to this message  
Join Date: Sep 2006
Posts: 20
Reputation: n3st3d_l00p is an unknown quantity at this point 
Solved Threads: 0
n3st3d_l00p's Avatar
n3st3d_l00p n3st3d_l00p is offline Offline
Newbie Poster

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

 
0
  #9
Sep 24th, 2006
the staff module...
  1. //the staff module
  2.  
  3. #include "staff.h"
  4. #include <iostream>
  5. #include <fstream>
  6. #include <cstring>
  7. using namespace std;
  8.  
  9. class sfunction
  10. {
  11. private:
  12. string lectut_loca;
  13. string lectut_id;
  14. string lectut_name;
  15.  
  16. string announce_id;
  17. string announce_subject;
  18. string announce_desc;
  19.  
  20. public:
  21. void add_lec_tut()
  22. {
  23. string copier;
  24. display_title("Add Lecture Notes/Tutorials");
  25. cout << "Please enter course id: ";
  26. cin >> lectut_id;
  27. for(int i=1; i<=count_line("course.MOLS"); i++)
  28. {
  29. if(gettok( read("course.MOLS", i), ';', 1) == lectut_id)
  30. {
  31. cin.ignore();
  32. cout << "Enter lecture notes/tutorials location.\n";
  33. cout << "e.g C:/Lecture Notes/lecture01.ppt";
  34. cout << ">>>";
  35. getline(cin,lectut_loca);
  36. cout << "Enter lecture notes/tutorials name: ";
  37. cout << "e.g lecture01.ppt";
  38. cout << ">>>";
  39. getline(cin,lectut_name);
  40. string my_copy = "copy" ;
  41. my_copy += " " + lectut_loca + " " + lectut_name ;
  42. const char* copier = my_copy.c_str() ;
  43. cout << copier;
  44. system(copier);
  45. cin.get();
  46. cout << "\nLecture notes/tutorials added.";
  47. getch(); break;
  48. }
  49. else if(i==count_line("course.MOLS"))
  50. {
  51. cout << lectut_id << " not found!";
  52. getch();
  53. }
  54. }
  55.  
  56.  
  57.  
  58. getch();
  59.  
  60. };
  61. void edit_lec_tut()
  62. {
  63. display_title("Edit Lecture Notes/Tutorials");
  64. getch();
  65. };
  66. void del_lec_tut(){};
  67.  
  68. void add_announ()//add announcement(s)
  69. {
  70. display_title("Add Announcements");
  71. cout << "Please enter course id: ";
  72. cin >> announce_subject;
  73. for(int i=1; i<=count_line("course.MOLS"); i++)
  74. {
  75. if(gettok( read("course.MOLS", i), ';', 1) == announce_subject)
  76. {
  77. cout << "Plase enter announcement id: ";
  78. cin >> announce_id;
  79. cin.ignore();
  80. cout << "Please enter your announcement: ";
  81. getline(cin,announce_desc);
  82. write("Lecturer/announcement.MOLS", i, announce_id + ";" + announce_subject + ";" + announce_desc);
  83. cout << "\nAnnouncement added.";
  84. getch(); break;
  85. }
  86. else if(i==count_line("course.MOLS"))
  87. {
  88. cout << announce_subject << " not found!";
  89. getch();
  90. }
  91. }
  92. };
  93.  
  94. void edit_announ()
  95. {
  96. display_title("Add Announcements");
  97. cout << "Enter course id: ";
  98. cin >> announce_subject;
  99. for(int i=1; i<=count_line("Lecturer/announcement.MOLS"); i++)
  100. {
  101. if(gettok( read("Lecturer/announcement.MOLS", i), ';', 2) == announce_subject)
  102. {
  103. cout << "Previous announcement:\n"
  104. << gettok( read("Lecturer/announcement.MOLS", i), ';', 3) << endl;
  105. cout << "\nEnter new announcement id: ";
  106. cin >> announce_id;
  107. cin.ignore();
  108. cout << "Enter new announcement: ";
  109. getline(cin,announce_desc);
  110. write("Lecturer/announcement.MOLS", i, announce_id + ";" + announce_subject + ";" + announce_desc);
  111. cout << "\nAnnouncement added.";
  112. getch(); break;
  113. }
  114. else if(i==count_line("Lecturer/announcement.MOLS"))
  115. {
  116. cout << announce_subject << " not found!";
  117. getch();
  118. }
  119. }
  120. };
  121. void del_announ()
  122. {
  123. display_title("Delete Announcements");
  124. cout << "Enter announcement id: ";
  125. cin >> announce_id;
  126. for(int i=1; i<=count_line("Lecturer/announcement.MOLS"); i++)
  127. {
  128. if(gettok( read("Lecturer/announcement.MOLS", i), ';', 1) == announce_id)
  129. {
  130. del("Lecturer/announcement.MOLS", i);
  131. cout << "Announcement succesfully deleted!";
  132. getch();
  133. }
  134. }
  135. };
  136. void add_assign(){};
  137. void edit_assign(){};
  138. void del_assign(){};
  139. };

and the staff header

  1. #ifndef _STAFF_H_
  2. #define _STAFF_H_
  3. #include "staff.cpp"
  4. #endif
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,651
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 474
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

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

 
0
  #10
Sep 24th, 2006
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.
I don't accept change; I don't deserve to live.

Jo Tujhe Jagaaye, Nindein Teri Udaaye Khwaab Hai Sachcha Wahi.
Nindon Mein Jo Aaye Jise To Bhul Jaaye Khawab Woh Sachcha Nahi.
Khwaab Ko Raag De, Nind Ko Aag De
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum


Views: 6086 | Replies: 25
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC