943,917 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 7039
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Sep 24th, 2006
0

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

Expand Post »
C++ Syntax (Toggle Plain Text)
  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?
Similar Threads
Reputation Points: 22
Solved Threads: 0
Newbie Poster
n3st3d_l00p is offline Offline
20 posts
since Sep 2006
Sep 24th, 2006
0

Re: 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;
// 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.

C++ Syntax (Toggle Plain Text)
  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.
Super Moderator
Featured Poster
Reputation Points: 3233
Solved Threads: 719
Failure as a human
~s.o.s~ is offline Offline
8,871 posts
since Jun 2006
Sep 24th, 2006
0

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

Click to Expand / Collapse  Quote originally posted by ~s.o.s~ ...
Look at the things i have mentioned above.
And maybe you should look at this simple expample.

C++ Syntax (Toggle Plain Text)
  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!
Reputation Points: 22
Solved Threads: 0
Newbie Poster
n3st3d_l00p is offline Offline
20 posts
since Sep 2006
Sep 24th, 2006
0

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

add

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



hahaha.. compile error!
Reputation Points: 22
Solved Threads: 0
Newbie Poster
n3st3d_l00p is offline Offline
20 posts
since Sep 2006
Sep 24th, 2006
0

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

I dont get you...
Are you saying even after making the changes you get a syntax error ???
Super Moderator
Featured Poster
Reputation Points: 3233
Solved Threads: 719
Failure as a human
~s.o.s~ is offline Offline
8,871 posts
since Jun 2006
Sep 24th, 2006
0

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

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.
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005
Sep 24th, 2006
0

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

yes.. after making the change.

error in runtime.. The syntax of the command is incorrect.
Reputation Points: 22
Solved Threads: 0
Newbie Poster
n3st3d_l00p is offline Offline
20 posts
since Sep 2006
Sep 24th, 2006
0

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

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.
Super Moderator
Featured Poster
Reputation Points: 3233
Solved Threads: 719
Failure as a human
~s.o.s~ is offline Offline
8,871 posts
since Jun 2006
Sep 24th, 2006
0

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

the staff module...
C++ Syntax (Toggle Plain Text)
  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

C++ Syntax (Toggle Plain Text)
  1. #ifndef _STAFF_H_
  2. #define _STAFF_H_
  3. #include "staff.cpp"
  4. #endif
Reputation Points: 22
Solved Threads: 0
Newbie Poster
n3st3d_l00p is offline Offline
20 posts
since Sep 2006
Sep 24th, 2006
0

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

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.
Super Moderator
Featured Poster
Reputation Points: 3233
Solved Threads: 719
Failure as a human
~s.o.s~ is offline Offline
8,871 posts
since Jun 2006

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Newbie in need of help!!!
Next Thread in C++ Forum Timeline: setprecision





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC