#include <iostream>
#include <string>
#include <sstream>
using namespace std;
std::wstring inputfield;
void function(uint32_t val)
{
std::wstring keystring;
keystring = std::to_wstring(val);
inputfield = inputfield + keystring;
std::wstringstream s;
s << L"(" << inputfield << L") ";
std::wstring str = s.str();
std::wcout << str << "\n";
return;
}
int main()
{
uint32_t x ;
while(cin>>x)
{
function(x);
cout<< *(&x)<< endl;
}
return 0;
}
I am trying string format to get output like (000)-000-000 or USA phonenumber format, But I am not able to achieve that, Please help, Thanks
If you want to change string 000000000 or simillar (and you do, even if input is a number, simply convert it with std::to_string) then just add a few characters in proper places.
std::string format_number(std::string str) {
if(str.size() != 9) {
throw std::logic_error{"Phone number is not 9-characters long"};
}
str.insert(str.begin(), '(');
str.insert(str.begin() + 4, ')');
str.insert(str.begin() + 5, '-');
str.insert(str.begin() + 9, '-');
return str;
}
You can allocate input phone numbers and then pass structures like this;
struct numbers {
int A;
int B;
int C;
} // then get (A)-B-C
#include <iostream>
#include <thread>
#include <string>
//---- The following function will be invoked by the thread library
void thread_proc(std::string msg)
{
std::cout << "ThreadProc msg:" << msg;
}
int main()
{
// creates a new thread and execute thread_proc on it.
std::thread t(thread_proc, "Hello World\n");
// Waiting for the thread_proc to complete its execution
// before exiting from the program
t.join();
}
How to resolve this error
$ g++ Dummy.cpp -o Dummy -std=c++1z -pthread
Dummy.cpp: In function 'int main()':
Dummy.cpp:12:10: error: 'thread' is not a member of 'std'
12 | std::thread t(thread_proc, "Hello World\n");
| ^~~~~~
Dummy.cpp:3:1: note: 'std::thread' is defined in header '<thread>'; did you forget to '#include <thread>'?
2 | #include <thread>
+++ |+#include <thread>
3 | #include <string>
Dummy.cpp:15:5: error: 't' was not declared in this scope; did you mean 'tm'?
15 | t.join();
| ^
| tm
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
Improve this question
I am making a program that hopefully removes tags from html files. But when I am copiling the program I get the following error message :
tag_remover.cc:11:1: error: ‘TagRemover’ does not name a type
11 | TagRemover::TagRemover(std::istream& in) {
| ^~~~~~~~~~
tag_remover.cc:21:14: error: ‘TagRemover’ has not been declared
21 | std::string& TagRemover::remove(std::string& s) {
| ^~~~~~~~~~
tag_remover.cc: In function ‘std::string& remove(std::string&)’:
tag_remover.cc:32:1: warning: no return statement in function returning non-void [-Wreturn-type]
32 | }
| ^
tag_remover.cc: At global scope:
tag_remover.cc:34:13: error: ‘TagRemover’ has not been declared
34 | std::string TagRemover::print(std::ostream& out) const {
| ^~~~~~~~~~
tag_remover.cc:34:50: error: non-member function ‘std::string print(std::ostream&)’ cannot have cv-qualifier
34 | std::string TagRemover::print(std::ostream& out) const {
| ^~~~~
tag_remover.cc: In function ‘std::string print(std::ostream&)’:
tag_remover.cc:35:9: error: ‘str’ was not declared in this scope; did you mean ‘std’?
35 | out << str << endl;
| ^~~
| std
make: *** [<builtin>: tag_remover.o] Error 1
And I can´t figure it out. Any help would be greatly appreciated.
This is my main for testing the tag remover:
#include <iostream>
#include <fstream>
#include <string>
#include "tag_remover.h"
using std::string;
int main() {
std::cout << "Opening html file" << std::endl;
std::ifstream in1("tags.html");
std::cout << "Removing tags ....." << std::endl;
TagRemover test1(in1);
test1.print(std::cout);
std::cout << "Test Done!" << std::endl;
}
This is tag_remover.cc script:
#include <iostream>
#include <istream>
#include <ostream>
#include <fstream>
#include "tag_remover.h"
using std::endl;
using std::cout;
using std::string;
using std::istream;
TagRemover::TagRemover(std::istream& in) {
//remove white spaces
std::noskipws(in);
std::istream_iterator <char> itr1(in);
std::istream_iterator <char> itr2{};
std::string s(itr1, itr2);
str = remove(s);
}
std::string& TagRemover::remove(std::string& s) {
while(s.find("<") != std::string::npos) {
auto startpos = s.find("<");
auto endpos = s.find(">");
if(endpos == std::string::npos) {
break;
}
auto eraseTo = (endpos - startpos) +1;
s.erase(startpos, eraseTo);
}
return s;
}
std::string TagRemover::print(std::ostream& out) const {
out << str << endl;
return str;
}
This is the header file :
#ifndef TAG_REMOVER
#define TAG_REMOVER
#include <string>
#include <iostream>
//#include <iterator>
class TagRemover {
public:
TagRemover();
TagRemover(std::istream& in);
std::string print(std::ostream& out) const;
std::string& remove(std::string& s);
private:
std::string str;
//const std::string inLine;
};
#endif
First thing I notice:
#include <iterator>
Is nowhere to be found. It should be in tag_remover.cc
std::istream_iterator<>
Is defined in… <iterator>
Also the TagRemover default constructor seems to be declared but not defined.
Furthermore, you can define TagRemover::TagRemover(std::istream&) like this:
TagRemover::TagRemover(std::istream& in)
: str{ [] (auto& is) {
std::skipws(is); // ignore spaces
using iser = std::istream_iterator<char>;
return std::string(iser(is), iser());
}(in) }
{}
I'm trying to create a folder named by todays date (on Ubuntu) and then check if it's empty or not.
The empty-or-not check will be done several times daily.
#include <cstdlib>
#include <unistd.h>
#include <stdio.h>
#include <iostream>
#include <typeinfo>
#include <chrono>
#include <time.h>
#include <iomanip>
using namespace std;
int main() {
//Pull out system date and create a folder named by system date
auto const now = std::chrono::system_clock::now();
auto const in_time_t = std::chrono::system_clock::to_time_t(now);
std::stringstream ss;
ss << std::put_time(std::localtime(&in_time_t), "%d_%m_%Y");
// Creating todays date folder with entry folder
string str_2=std::string("mkdir -p " + string(ss.str()) + "/entry");
const char *com2=str_2.c_str();
system(com2);
//check if directory is empty or not
int check;
char is_empty[100];
FILE * output;
output = popen("ls " + ss.str() + "/entry | wc -l","r") ;
fgets (is_empty, 100, output); //write to the char
pclose (output);
check = atoi(is_empty);
if (check == 0) {
cout << "The folder is empty" << endl;
}
}
I'm getting this error when compiling this code:
error: no match for ‘operator+’ (operand types are ‘const char [4]’
and ‘std::stringstream {aka std::__cxx11::basic_stringstream<char>}’)
output = popen("ls " +ss+ "/entry | wc -l","r") ;
This solved it for me
int check;
char is_empty[100];
FILE * output;
string cmd = std::string("ls " + string(ss.str())+ "/entry | wc -l") ;
const char *com4 = cmd.c_str();
output = popen(com4,"r");
fgets(is_empty, 100, output);
pclose (output);
check = atoi(is_empty);
//cout << check<<endl;
if (check == 0){
cout << "The folder is empty" << endl;
}
We have a char. We need to replace all ab characters from our char with the letter c.
Example we have :
abracadabra
the output will be :
cracadcra
I tried to use replace() function from C++, but no success.
#include <iostream>
#include <cstring>
using namespace std;
int main()
{
string test;
cin>>test;
for(int i=0;i<(strlen(test)-1);i++)
{
if((test[i]=='a')&&(test[i+1]=='b')){
test.replace( test[i], 'c' );
test.replace( test[i+1] , ' ' );
}
}
cout << test << endl;
return 0;
}enter code here
You can use C++11 regex:
#include <iostream>
#include <regex>
#include <string>
int main() {
std::string str = "abracadabra";
std::regex r("ab");
std::cout << std::regex_replace(str, r, "c") << "\n"; // cracadcra
}
Problem:
That is not the syntax of std::string::replace.
Solution:
As is mentioned here the syntax is std::string::replace(size_t pos, size_t len, const string& str). Do test.replace(i, 2, "c" ) instead of test.replace(test[i],'c').
Or use regular expressions as dtell pointed.
Adittional information:
using namespace std; is considered a bad practice (More info here).
You should use std::string::size instead of strlen when you're working with std::string.
To work with std::string you should use #include <string> instead of #include <cstring>.
Full code:
#include <iostream>
int main()
{
std::string test;
std::cin >> test;
for(unsigned int i = 0; i < test.size() - 1; i++)
{
if((test[i]=='a') && (test[i+1]=='b'))
{
test.replace(i, 2, "c" );
}
}
std::cout << test << std::endl;
return 0;
}
The simplest thing you can do by using the standard library is first to find ab and then replace it. The example code I wrote is finding string ab unless there is None in the string and replacing it with c.
#include <iostream>
#include <string>
int main()
{
std::string s = "abracadabra";
int pos = -1;
while ((pos = s.find("ab")) != -1)//finding the position of ab
s.replace(pos, sizeof("ab") - 1, "c");//replace ab with c
std::cout << s << std::endl;
return 0;
}
//OUTPUT
cracadcra
My c++ program is this:
#include <iostream>
#include <cstdlib>
#include <dos.h>
using namespace std;
void arr(int pts,string *splt_msg){
for(int x=-1;x<pts-1;x++){
string input;
cout<<"Enter part "<<x<<endl;
cin>>input;
(*splt_msg)[x]=input;
}
}
void print_arr(string *splt_msg,int del,int parts){
for(int x=-1;x<parts-1;x++){
cout<<(*splt_msg)[x];
delay(del);
}
}
int main(){
cout<<"Parts in message: ";
int parts;
cin>>parts;
cout<<"Delay between parts(ms): ";
int del;
cin>>del;
parts--;
string splt_msg[parts];
string (*Parr)[parts]=&splt_msg;
arr(parts,Parr);
print_arr(Parr,del,parts);
return 0;
}
Here is the error code(on windows and mingw):
c:\Users\User\Desktop\c++>g++ -o program test1.cpp
In file included from test1.cpp:3:0:
c:\mingw\include\dos.h:54:2: warning: #warning "<dos.h> is obsolete; consider using <direct.h> instead." [-Wcpp]
^~~~~~~
test1.cpp: In function 'void arr(int, std::__cxx11::string*)':
test1.cpp:18:14: error: 'delay' was not declared in this scope
delay(del);
^
test1.cpp: In function 'int main()':
test1.cpp:32:17: error: cannot convert 'std::__cxx11::string (*)[parts] {aka std::__cxx11::basic_string<char> (*)[parts]}' to 'std::__cxx11::string* {aka std::__cxx11::basic_string<char>*}' for argument '2' to 'void arr(int, std::__cxx11::string*)'
arr(parts,Parr);
^
test1.cpp:33:27: error: cannot convert 'std::__cxx11::string (*)[parts] {aka std::__cxx11::basic_string<char> (*)[parts]}' to 'std::__cxx11::string* {aka std::__cxx11::basic_string<char>*}' for argument '1' to 'void print_arr(std::__cxx11::string*, int, int)'
print_arr(Parr,del,parts);
^
Does someone know what i have done wrong and the solution?
Thanks for the help, I'm just trying to write a simple program to test some concepts out like passing pointers into functions but it doesn't seem to be working.
This code is more C style then C++. You are doing difficult error prone things. Try using vectors and such
string splt_msg[parts];
should not even evaluate, as parts is not a constant expression.
Try using things like std::vector. and passing by reference. E.g.:
#include <iostream>
#include <string>
#include <vector>
#include <Windows.h>
template<typename T>
T Read() noexcept {
T output{};
std::cin >> output;
return output;
}
void arr(std::vector<std::string>& splt_msg) {
for (auto& msg : splt_msg) {
std::cout << "Enter part \n";
std::cin >> msg;
}
}
void print_arr(std::vector<std::string> const& splt_msg, int delay) {
for (auto& msg : splt_msg) {
std::cout << msg;
Sleep(delay);
}
}
int main() {
std::cout << "Parts in message: ";
auto parts = Read<int>();
std::cout << "Delay between parts(ms): ";
auto delay = Read<int>();
std::vector<std::string> splt_msg(parts);
arr(splt_msg);
print_arr(splt_msg, delay);
}
edit: or even better: use an object
#include <iostream>
#include <string>
#include <vector>
#include <Windows.h>
template<typename T>
T Read() noexcept {
T output{};
std::cin >> output;
return output;
}
class SplitMessage {
private:
std::vector<std::string> splitMessage;
public:
SplitMessage(size_t length) noexcept : splitMessage(length) {}
void GetData() noexcept;
void PrintData(int delay) const noexcept;
};
void SplitMessage::GetData() noexcept {
for (auto& msg : splitMessage) {
std::cout << "Enter part \n";
std::cin >> msg;
}
}
void SplitMessage::PrintData(int delay) const noexcept {
for (auto& msg : splitMessage) {
std::cout << msg;
Sleep(delay);
}
}
int main() {
std::cout << "Parts in message: ";
auto parts = Read<int>();
std::cout << "Delay between parts(ms): ";
auto delay = Read<int>();
SplitMessage splt_msg(parts);
splt_msg.GetData();
splt_msg.PrintData(delay);
}
Both functions require a std::string* (a pointer to a string) as first parameter, but both times you pass std::string*[] (an array of pointers to a string) as first paramater. The solution depends on what you want to achieve, but I guess, you want to call the function for one element of the array with an indexer: print_arr(Parr[0],del,parts);
Some other notes:
for(int x=-1;x<parts-1;x++){cout<<(*splt_msg)[x];...} will access the -1st character of a string, which is UB, but probably just crashes. Should be for(int x=0;x<parts;x++)
string splt_msg[parts]; is no valid C++, since parts is not constant at compile time.
I'm trying to make a basic REPL that parse for special characters entered by a user. This post shows how to split on whitespace but I get this compiling error when I try to store the stringstream into the vector of strings.
repl.cpp: In function ‘int main(int, char**)’:
repl.cpp:52:25: error: range-based ‘for’ expression of type ‘std::__cxx11::basic_istringstream<char>’ has an ‘end’ member but not a ‘begin’
for (string s : iss)
^~~
repl.cpp:52:25: error: ‘std::ios_base::end’ cannot be used as a function
make: *** [repl.o] Error 1
Here is the full code below:
#include <cstdlib>
#include <iostream>
#include <string>
#include <sstream>
#include <vector>
#include <fstream>
#include <stdlib.h>
#include <unistd.h>
#include <dirent.h>
#include <sys/stat.h>
using namespace std;
int main(int argc, char *argv[])
{
size_t pos;
int pipe = 0;
int pid = 0;
vector <size_t> positions;
vector <string> arguments;
do
{
cout << "repl$ ";
getline(cin, cmd);
pos = cmd.find( "|", 0);
while ( pos != string::npos )
{
positions.push_back(pos);
pos = cmd.find( "|", pos+1);
pipe += 1;
pid += 1;
}
istringstream iss(cmd);
while (iss >> cmd)
arguments.push_back(cmd);
for (string s : iss)
cout << s << endl;
} while (cmd != "q");
return EXIT_SUCCESS;
}
You need to use a std::istream_iterator<std::string> to read successive strings. Boost has a wrapper to create a pseudo-container representing the sequence of objects read from an istream; for example:
for (const auto& s : boost::range::istream_range<std::string>(iss))
std::cout << s << '\n';
An alternative in this specific case would be to copy directly to an output iterator:
std::copy(std::istream_iterator<std::string>{iss},
std::istream_iterator<std::string>{},
std::ostream_iterator<std::string>{std::cout, '\n'});