linux system function - c++

Below is a program i have written that ran fine when I type what linux command I wanted it to perform
include iostream
include string
using namespace std;
int main()
{
cout << "The directory!";
system("cd CS_204");
return 0;
}
However below I tried to make it so a user can type in the command that they wanted and I get they can not convert std::string to const char* This is my first time using the function and I am desperately trying to understand it. Help!!
int main()
{
cout << "The directory!";
string word;
cin >> word
if(word != "A")
system(word);
return 0;
}

In the second case, word is of type std::string and is not equivalent to const char* . You need to get the c-style string using the member function std::string::c_str()
system(word.c_str()); // This will convert to a c style string.

Related

error: template is declared here. class _LIBCPP_TEMPLATE_VIS basic_stringstream;

I am writing a c++ program to find the number of words in a string using stringstream in c++ but my compiler is giving the above error.
I am using vsCode on my macbook air to do this and using the Xcode's GNU compiler.
I also tried to write the code in the CLION ide which also gives the same error.
Here is the code.
#include <iostream>
using namespace std;
int countWords(string str)
{
// breaking input into word using string stream
stringstream s(str); // Used for breaking words
string word; // to store individual words
int count = 0;
while (s >> word)
count++;
return count;
}
// Driver code
int main()
{
string s = "using stringstream class in cplusplus";
cout << " Number of words are: " << countWords(s);
return 0;
}
Please help me with this.

how to convert all in file to lowercase?

I am having trouble getting started with a program. I need to read in each word from a file, then convert it to lower case. I would like to std::cout each word after I find it. I assume I need to use c_str() some how. I am guessing I should use something like:
ofs.open(infile.c_str());
but how to lower case?
string[i] = tolower(string[i]);
You can use the std::tolower() function from locale. Not sure if this is what you are looking for, but here is a quick solution to your problem (as i understand it).
#include<iostream>
#include<string>
#include<fstream>
#include<locale>
int main(){
std::string input;
std::ifstream inputStream;
inputStream.open("input.txt", std::ifstream::in);
while(inputStream >> input){
for(auto s : input)
{
std::cout << std::tolower(s, std::locale());
}
std::cout << " ";
}
return 0;
}

Including value of int variable inside of string

I am trying to print the date that the user enters in a program I am working on. In this very simplified example, I am trying to get the value of an int variable inside of a string variable. Here, you can see I have tried static_cast<char>(int).
I have also tried
myStr = num;
myStr = num + 0;
myStr = num + '0';
as well as many other things that do not make sense just to see what the compiler does and what the program does - if I can get it to run.
Here's the few lines I have in this shortened example:
#include <iostream>
#include <string>
using namespace std;
int main()
{
int num = 100;
string myStr = static_cast<char>(num);
cout << myStr;
return 0;
}
In my other program, I am trying to insert the year 2017 (saved as an int variable) into a string that contains the rest of the date. I'm just having problems with numbers bigger than 9.
Thanks for any help.
Use the standard library function std::to_string to convert your number to string form.
stringstream ss;
ss << num;
cout << ss.str();
Don't forget to include sstream
As of the C++11 standard, string-to-number conversion and vice-versa are built in into the standard library and you could use to_string method.
You can use to_string to convert int to String.
#include<String>
std::string int_string = std::to_string(num);
If you have older version of c++,this will work by compiling with the flag -stdc++=11 or higher e.g
g++ filename.cpp -stdc++=11

Unexpected behaviour converting a string to a const char*

I need to convert a std::string to a const char*.
To do so, I used the c_str() method on the string, as in the following code :
#include <iostream>
#include <string>
using namespace std;
int main(void) {
string testStr;
cin >> testStr;
const char* testStrConst = testStr.c_str();
cout << testStrConst << endl;
return 0;
}
If I type "Hey hello" in the terminal, when this code is running, the output is only "Hey".
Why is the second word ignored?
Because it was never a part of the std::string in the first place.
The >> operator reads only a single, whitespace delimited word.
Use std::getline() instead of >> to read the entire line of text entered on standard input.
string testStr;
getline(cin, testStr);

Error: C++ literal memory mishandling

I have tried a lot to debug my code but it is still not working.The whole code just crashes but there is no allover error I am presenting the code please try to debug that one.
Code:
#include <iostream>
#include<conio.h>
#include<string.h>
#include<fstream>
using namespace std;
void write(char fname[],char text[])
{
strcat(fname,".txt");
ofstream w(fname,ios::app);
w<<text;
w<<"\n";
w.flush();
w.close();
cout<<" sippy "<<fname<<" ";
}
int main ()
{
int login=0;
char t[100],id[100]="Its id ",pass[100]="Its password";
login=1;
strcpy(t,id);
strcat(t,"\n");
strcat(t,pass);
cout<<" finally ";
write("database",t);
getch();
strcpy(t,id);
getch();
cout<<t<<" showing t here";
getch();
cout<<" hope this works for now ";
getch();
cout<<"\nEnter the text"<<endl;
write(id,t);
}
The above mentioned code does not work on tdm gcc code blocks
Edit 1:
Ok so now the major problem has been detected it is a minor bug usually caused because of drawback of a bad programming style. As it is often suggested that if a string is passed to a function then that particular function allocates a new string at the memory of the passed string. Since the passed string is a literal the code editing the newly formed string would try to edit a read only literal memory which is an error
Literals are read only because if compiler finds the use of same literal at some different place then it would be able to use same memory to flash the contents of literal therefore it becomes a necessity to make a literal read only and use of c-style string carefully(rather std::string should be used)
Thanks to all
If you are facing a SegFault I think this line could be the problem :
write("database",t);
because in your write function you use strcat on fname but you pass a read-only string.
Also, I think it might be best to use real c++ instead of c+ like :
#include <string>
#include <iostream>
#include <fstream>
void my_write(std::sting & fname, std::string & text) {
std::string file = fname + ".txt";
std::osftream w(file, std::ios::app);
w << text << "\n";
w.flush();
w.close();
}
int main() {
std::string t = "";
std::string id = "Its id";
std::string pass = "Its password";
std::string fname = "database";
int login = 1;
t = id + "\n" + pass;
my_write( fname, t);
}
I haven't test it but the idea is here.