Why does it not let me execute this? - c++

I'm new to C++ (1 week) , coding in general and I'm wondering how I can make this work, because I get errors whenever I compile & run. The idea is that the user inputs a gift code, and the program automatically opens it in a tab.
https://i.stack.imgur.com/4IKkm.png
#include <iostream>
#include <windows.h>
using namespace std;
int main()
{
string codes;
cout << "Input here: \n";
cin >> codes;
for (int i = 100; i > 0; i--)
{
string site;
site = "http://discord.gift/" << codes;
ShellExecute(NULL,
"open",
site,
NULL,
NULL,
SW_SHOWNORMAL);
Sleep(300);
}
}
Error message:
[Error] cannot convert 'std::string {aka std::basic_string<char>}' to 'LPCSTR {aka const char*}' for argument '3' to 'HINSTANCE__* ShellExecuteA(HWND, LPCSTR, LPCSTR, LPCSTR, LPCSTR, INT)'
[Error] no match for 'operator<<' (operand types are 'const char [21]' and 'std::string {aka std::basic_string<char>}')

There are three problems with your code:
you are using std::string without #include <string>. While it is permissible for standard headers to include each other, you should not rely on this. Always #include relevant headers that your code needs.
you can't use operator<< to concatenate a string literal with a std::string object. You need to use operator+ instead:
site = "http://discord.gift/" + codes;
the ShellExecute() function does not accept a std::string as a parameter value, it takes const char* pointers instead. A string literal implicitly decays into such a pointer, but std::string does not. However, std::string has a c_str() method to obtain such a pointer:
ShellExecute(NULL, "open", site.c_str(), NULL, NULL, SW_SHOWNORMAL);

Adding your code and errors in your question instead of just a pic would be a great idea.
Anyway, To add strings together you must use the + sign site = "http//..." + codes
Not sure if there is any other errors but I highly recommend to indent your code to make it easier to read

Related

C++ What type of pointer should I use to store output address of `algorithm` `remove` function?

I'm trying to store the address returned by the algorithm remove function in C++ in a variable, but have failed to find one. I've tried int* and char*. Both threw errors.
Using Visual Studio CL, the error is:
error C2440: '=': cannot convert from '_FwdIt' to 'int *'
Using MinGW, the error is:
cannot convert '__gnu_cxx::__normal_iterator<char*, std::__cxx11::basic_string<char> >' to 'int*' in assignment
How should I store such an address?
The code I'm trying:
#include <stdio.h>
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
int main (void) {
string line ("This is an example sentence.");
int* newEOL;
newEOL = remove(line.begin(), line.end(), ' ');
printf("%p\n", newEOL);
}
Why do you think it's a pointer?
As evidenced here: https://en.cppreference.com/w/cpp/algorithm/remove
it's an iterator, which can be implemented as a pointer, but doesn't have to.
You can use the auto keyword if you don't want to specify the type explicitly.

How to fix string declaration error in scope

I'm trying to run an interprocess communication program but it says string is not declared in the scope as is and when I add #inlcude I get an error that says:
receiver.cpp:25:35: error: invalid conversion from ‘char*’ to ‘int’ [-fpermissive]
string temp = to_string(argv[0]);
~~~~~~^
In file included from /usr/include/c++/7/string:52:0,
from receiver.cpp:14:
/usr/include/c++/7/bits/basic_string.h:6419:3: note: candidate: std::__cxx11::string std::__cxx11::to_string(unsigned int) <near match>
to_string(unsigned __val)
^~~~~~~~~
receiver.cpp:27:26: error: cannot convert ‘std::__cxx11::string {aka std::__cxx11::basic_string<char>}’ to ‘const char*’ for argument ‘1’ to ‘int atoi(const char*)’
int msgid = atoi(temp) //Converts message id from string to integer
^
receiver.cpp:45:32: error: ‘some_data’ was not declared in this scope
if (msgrcv(msgid, (void *)&some_data, BUFSIZ, msg_to_receive, 0) == -1) { //revieces message from message queue
^~~~~~~~~
receiver.cpp:49:29: error: ‘some_data’ was not declared in this scope
printf("You wrote: %s", some_data.some_text);
This is my code:
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.H>
#include <cstring.h>
#include <unist.h>
#include <string.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <cstdlib>
#inlcude <string>
using namespace std;
struct my_msg_st{
long int my_msg_type;
char some_text[BUFSIZ];
};
int main(int argc, char *argv[0]){
int running =1;
string temp = to_string(argv[0]);
int msgid = atoi(temp);
struct my_msg_st some_data;
long int msg_to_receive = 0;
....
if (strncmp(some_data.some_text, "end", 3) == 0){
running =0;
}
...
exit(0);
}
expecting for the code to print out the message sent from the sender file
Here are some fixes for your issues:
string temp = to_string(argv[0]);
1. to_string converts numbers to string. the argv[0] is a C-style string, not a number.
2. The std::string constructor already has a version to convert from char * to std::string.
atoi(temp)
1. The atoi function takes a parameter of type char * not std::string. You'll need to use atoi(temp.c_str()) or prefer std::ostringstream.
Please review the differences between char arrays (a.k.a. C-Style strings) and the std::string type. Prefer to use std::string, especially in structures.
Carefully read the library function descriptions before using them.
See also std::ostringstream. Since this is C++, prefer to use C++ I/O such as std::cout and operator <<.

How to use FindFirstFile [duplicate]

This question already has answers here:
cannot convert 'const char*' to 'LPCWSTR {aka const wchar_t*}'
(4 answers)
Closed 5 years ago.
I keep getting compile errors with the line at the bottom
hFind = FindFirstFile(fileFilter.c_str()), &FindFileData);
The compiler keeps throwing error C2664 back at me, : cannot convert argument 1 from 'const char *' to 'LPCWSTR'
How do I create a LPCWSTR to a std::string to pass to into FindFirstFile?
Zhe section of code is for reference.
The actual code follows below.
using namespace std;
void GetFileListing(string directory, string fileFilter, bool recursively = true)
{
if (recursively)
GetFileListing(directory, fileFilter, false);
directory += "\\";
WIN32_FIND_DATA FindFileData;
HANDLE hFind ;
string filter = directory + (recursively ? "*" : fileFilter);
string Full_Name;
string Part_Name;
// the line causing the compile error
hFind = FindFirstFile(fileFilter.c_str()), &FindFileData);
The WinAPI data types are lovely short abbreviations. LPCWSTR is short for:
Long
Pointer to the start of
Const
Wide
STRing
As such it is a pointer (long pointers are history) to the first character of a const wide string (const wchar_t*), meaning you need to use std::wstring::c_str() instead of std::string::c_str().
Side note: just be sure to #define UNICODE everywhere you use the WinAPI, otherwise you'll get other errors about conversion to LPCSTR. Alternatively, explicitly use the W versions of the WinAPI functions where they exist.

error: cannot convert 'std::basic_string<char>::iterator ...' to 'const char* for argument '1' ...'

I'm getting the following error:
error: cannot convert 'std::basic_string<char>::iterator {aka __gnu_cxx::__normal
_iterator<char*, std::basic_string<char> >}' to 'const char*' for argument '1'
to 'int remove(const char*)'
For some reason, my program compiles perfectly when I'm working on a Mac... but once I use a Linux machine, this error pops up in more than one place.
Here's one of the instances where the error pops up:
SomeClass::SomeClass(string t, string art, Time dur) {
char chars[] = ",";
t.erase(std::remove(t.begin(), t.end(), chars[0]), t.end());
art.erase(std::remove(art.begin(), art.end(), chars[0]), art.end());
// Some more code ...
}
More specifically, the error is coming from this line:
t.erase(std::remove(t.begin(), t.end(), chars[0]), t.end());
Does anyone know how to approach this problem?
You forgot to #include <algorithm>, where std::remove is located. Without that, your compiler only knows about this std::remove (I get the same error with Visual C++ 14), which is defined in indirectly included <cstdio> header.
Different behavior among compilers is a result of different #include hierarchies of the standard library implementations.

C++ in Ruby C extensions, pointer problems

I'm tryign to build a Ruby C extension that uses some c++ libraries. Problem is I can't even get a simple "hello world" to work.
//hello_world.cpp
#include <ruby.h>
static VALUE tosCore;
static VALUE my_function( VALUE self )
{
VALUE str = rb_str_new2( "Hello World!" );
return str;
}
extern "C"
void Init_hello_world( void )
{
tosCore = rb_define_module("Core");
rb_define_module_function(tosCore, "my_method", my_function, 0);
}
The output I get is
compiling hello_world.cpp
hello_world.cpp: In function 'void Init_hello_world()':
hello_world.cpp:17:67: error: invalid conversion from 'VALUE (*)(VALUE) {aka lon
g unsigned int (*)(long unsigned int)}' to 'VALUE (*)(...) {aka long unsigned in
t (*)(...)}' [-fpermissive]
In file included from c:/Ruby200/include/ruby-2.0.0/ruby.h:33:0,
from hello_world.cpp:2:
c:/Ruby200/include/ruby-2.0.0/ruby/ruby.h:1291:6: error: initializing argument
3 of 'void rb_define_module_function(VALUE, const char*, VALUE (*)(...), int)'
[-fpermissive]
make: *** [hello_world.o] Error 1
I'm no C/C++ expert. Ruby is my language. I have compiled a few thousand lines of C++ under Rice with no problem, but since I want this particular extension to compile under Windows, Rice is not an option.
It's because the function callback you present to rb_define_module_function is not what the compiler expects. It want a function looking like this:
VALUE my_function(...)
But your function is
VALUE my_function( VALUE self )
Notice the difference in the argument list.
One way to get rid of the error, is to type cast the argument to the type that rb_define_module_function expects:
rb_define_module_function(tosCore, "my_method",
reinterpret_cast<VALUE(*)(...)>(my_function), 0);
You can read about reinterpret_cast here.