error: template is declared here. class _LIBCPP_TEMPLATE_VIS basic_stringstream; - c++

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.

Related

I am trying to Find lonngest word in sentence

#include<bits/stdc++.h>
using namespace std;
int main(){
int n;
cin>>n;
char a[n+1]; // intializing array
cin.getline(a,n); // inputting array
cin.ignore();
int maxlen=0,curr=0,i=0;
while(1)
{
if (a[i] ==' ' || a[i] == '\0'){
maxlen=max(curr,maxlen);
curr=0;
}
curr++;
if (a[i]=='\0'){
break;
}
i++;
}
cout<<maxlen;
}
here i am trying to find the max length of word in this sentence but i am not able to save the sentence or input the array.
To check i put cout for array and its not printing the array i inputed i want to know the reason
error C2131: expression did not evaluate to a constant (6):
This is caused due to using non-standard C++ here:
cin>>n;
char a[n+1]; // intializing array
Arrays in C++ must be declared using a constant expression, not a runtime-based expression. Since n is only known at runtime, this is not legal C++.
As to solving the issue, you don't need raw char arrays. This can easily be done using std::string. In addition, using std::stringstream will make the code even simpler by not having to check for whitespace:
#include <algorithm>
#include <string>
#include <iostream>
#include <sstream>
int main()
{
std::string sentence;
// Read the sentence
std::getline(std::cin, sentence);
// Parse each word
std::stringstream strm(sentence);
std::string word;
size_t maxLen = 0;
while (strm >> word)
maxLen = std::max(maxLen, word.size());
// Output results
std::cout << sentence << "\n" << maxLen;
}
For this input:
This is a test of getting the longest word in a string
the output is:
This is a test of getting the longest word in a string
7
Finally, you should follow a good C++ book instead of going to websites that shows any coding using:
#include<bits/stdc++.h>
or the invalid array syntax mentioned above.
No reputable C++ book, or reputable C++ website that has been peer-reviewed will show code using that header, or the invalid array syntax.
If you go to a website that shows code like this, and the website's purpose is to teach C++, it isn't one you should learn any C++ from.

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

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.

How to read names into a pointer array and output them?

Here is what I got so far:
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
int characterList = 0;
char* dynamo = new char[1000];
char* buffer = dynamo;
ifstream input("wordlist.txt");
if (input.is_open())
{
input >> dynamo[characterList];
while (input.eof())
{
characterList++;
input >> dynamo[characterList];
cout << dynamo[characterList];
}
}
else
{
cout << "File not opened" << endl;
}
return;
}
I'm a beginner so I do apologize if this looks like terrible coding practice. I created a text file with a quote from Bill Cosby that I'm trying to read one word at a time. The quote is "I don't know the key to success, but the key to failure is trying to please everybody." I'm trying to read one word at a time from a text document ignoring punctuation. I know there are a lot of questions similar to this, but they are using code that I have not learned so I'm sorry for having a repeating question. I have not learned getline (I used cin.getline) and #include <string>.
Edit: I forgot to mention, so I'm sorry for not doing so earlier, but I'm studying dynamic memory allocation which is why I'm using the new char[1000].
I'd suggest you to use std::string instead of manually allocating buffers on the heap with new[] and trying to read text manually from the file into those buffers (and don't forget to free the buffer with proper delete[] calls!).
C++ input stream classes like std::ifstream can simply read text into std::string instances thanks to a proper overload of operator<<.
The syntax is as simple as:
string word;
while (inFile >> word)
{
cout << word << endl;
}
Here's a complete compilable sample code for you to experiment and learn:
#include <fstream>
#include <iostream>
#include <string>
using namespace std;
int main()
{
ifstream inFile("test.txt");
if (inFile.is_open())
{
string word;
while (inFile >> word)
{
cout << word << endl;
}
}
else
{
cout << "Can't open file." << endl;
}
}
This is the output I got on a test text file having the content specified in your question:
I
don't
know
the
key
to
success,
but
the
key
to
failure
is
trying
to
please
everybody.
NOTE
Of course, once you have your words read into a std::string instance, you can store them in a container like std::vector<std::string>, using its push_back() method.
I would do something like this:
#include <iostream>
#include <string>
#include <fstream>
int main() {
std::string array[6];
std::ifstream infile("Team.txt");
std::string line;
int i = 0;
while (std::getline(infile, line)) {
array[i++] = line;
}
return 0;
}
based on this answer.
Here, we assume we have to read 6 lines from the file "Team.txt". We use std:::getline() and we put inside a while so that we read all the file.
At every iteration, line holds the current line of the file read. Inside the body we store it in array[i].

linux system function

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.