Where am doing wrong in this code? I need only in char types, please don't suggest to use std::string.
#include <iostream>
#include <string.h>
using namespace std;
int main()
{
char *mystring="C:/windows";
char last_char;
last_char = mystring[strlen(mystring)-1];
cout<<"Input: " <<mystring<<endl;
if(strcmp(last_char,";")!=0)
{
strcat(mystring,";");
}
cout<<"Output: "<<mystring<<endl;
return 0;
}
Output:
Compilation error time: 0 memory: 3340 signal:0
prog.cpp: In function ‘int main()’:
prog.cpp:7:17: warning: deprecated conversion from string constant to ‘char*’ [-Wwrite-strings]
char *mystring="C:/windows";
^
prog.cpp:11:25: error: invalid conversion from ‘char’ to ‘const char*’ [-fpermissive]
if(strcmp(last_char,";")!=0)
^
In file included from prog.cpp:2:0:
/usr/include/string.h:140:12: error: initializing argument 1 of ‘int strcmp(const char*, const char*)’ [-fpermissive]
extern int strcmp (const char *__s1, const char *__s2)
Don't use strcmp, it expects a null terminated characters sequence. Instead, use direct comparison:
if (last_char == ';') ...
Also, your code invokes undefined behavior in the strcat() call. my_string was initialized with a string literal, thus, you are not allowed to modify it, since the implementation is free to place it in read-only memory (and typically will do so).
You can declare it like this instead:
char mystring[12] = "C:/windows"; // space for one more char
last_char is not a string. It is a character. You can't compare a char with string.
Try this instead
if (last_char == ';') {...}
Statement
strcat(mystring,";");
invokes undefined behavior. You can't modify a string literal as it resides in read only section of the memory.
Related
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 <<.
I want to take string input with white spaces.
This is my code:
#include<iostream>
#include <stdio.h>
#include<string>
using namespace std;
int main()
{
string name;
printf("\nEnter the name : ");
scanf ("%[^\n]%*c", name); //it does not work
//getline(cin, name); //it works fine
cout<<name<<endl;
return 0;
}
Error Massage on hackerearth editor and codeblocks:
In function ‘int main()’:
16:29: error: cannot pass objects of non-trivially-copyable type ‘std::string {aka class std::basic_string}’ through ‘...’
16:29: warning: format ‘%[^
:string {aka std::basic_string}’ [-Wformat=]
16:30: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
string, or in fact std::string, is a C++ type from the 1990s (ish).
scanf is a C function from 1066. It has no idea what to do with a C++ type.
Don't mix and match; either use C++ features or use C features.
I get the following error:
2 smartcard.c: In member function ‘virtual bool cSmartCards::ParseLine(const char*, bool)’:
3 smartcard.c:1187:25: error: invalid conversion from ‘const char*’ to ‘char*’ [-fpermissive]
4 char *r=index(line,':');
5 ^
The code is:
1184
1185 bool cSmartCards::ParseLine(const char *line, bool fromCache)
1186 {
1187 char *r=index(line,':');
1188 if(!r) return false;
I included "string.h"
How can I rewrite line 1187?
index() can be found in string.h.
Either or both of the following:
index returns const char*, not a char*. So, make r a const char*, not a char*.
The function index is written to expect a char*, not a const char*. I cannot safely suggest a fix for this without knowing what index is and does.
The answer is the following:
Function index is found in string.h.
Change line 1187 to: const char *r=index(line,':');
I have a huge list of errors that come up when i try to compile source code under cygwin.. My best approach to learning programming is hitting it hard, and trail and error. So even if my C++ knowledge is very basic, I am still really new, so please when you explain can I please ask that you use baby talk for a lack of a better word lol. When I type in 'make' under the source directory is gives me these errors. A friend of mine, we are friends on a MUD, he has been a programmer for 35 years and he says to me that the compiler is not liking that the function is returning a pointer and to change all the "return ''''" to return strdup('''')
Please let me know what you guys think. Thanks
Underneath is only a very smalllll part of the syntax that was given to me after I typed in make in Cygwin. I hope someone has the time to explain this to me, thank you.
$ make
make -s smaug
-Compiling o/imc.o....
imc.c:106:1: error: deprecated conversion from string constant to ‘char*’ [-Werror=write-strings]
};
^
imc.c:106:1: error: deprecated conversion from string constant to ‘char*’ [-Werror=write-strings]
imc.c:106:1: error: deprecated conversion from string constant to ‘char*’ [-Werror=write-strings]
imc.c:106:1: error: deprecated conversion from string constant to ‘char*’ [-Werror=write--strings]
imc.c:106:1: error: deprecated conversion from string constant to ‘char*’ [-Werror=write-strings]
imc.c:106:1: error: deprecated conversion from string constant to ‘char*’ [-Werror=write-strings]
imc.c: In function ‘char* color_itom(const char*, CHAR_DATA*)’:
imc.c:393:14: error: deprecated conversion from string constant to ‘char*’ [-Werror=write-strings]
return "";
^
imc.c: In function ‘char* color_mtoi(const char*)’:
imc.c:414:14: error: deprecated conversion from string constant to ‘char*’ [-Werror=write-strings]
return "";
^
imc.c: In function ‘char* imccapitalize(const char*)’:
imc.c:525:35: error: conversion to ‘char’ from ‘int’ may alter its value [-Werror=conversion]
strcap[i] = tolower( str[i] );
^
imc.c:527:35: error: conversion to ‘char’ from ‘int’ may alter its value [-Werror=conversion]
strcap[0] = toupper( strcap[0] );
^
imc.c: In function ‘void imc_new_channel(const char*, const char*, const char*, const char*, const char*, bool, int, const char*)’:
imc.c:1089:13: error: conversion to ‘short int’ from ‘int’ may alter its value [-Werror=conversion]
c->level = perm;
^
^
cc1plus: all warnings being treated as errors
Makefile:101: recipe for target 'o/imc.o' failed
make[1]: *** [o/imc.o] Error 1
Makefile:46: recipe for target 'all' failed
make: *** [all] Error 2
Ok below is the code where it shows error for line 106: 1 and 393: Its a very lonnng .c file I am sure you guys don't want to upload the whole thing, but here is the portion of it, and according to Visual 2013 here is starting point line 106 and 393: i am not sure when cygwin says the line number where the error took place if that doesn't include white space and comments, but here is 106 and 393 according to VS:
line 106
SITEINFO *this_imcmud;
line 393
if( IMCIS_SET( IMCFLAG( ch ), IMC_COLORFLAG ) )
You should show your code, but your problems are:
Somewhere you're doing something like:
char *x = "hello";
It should be:
const char *x = "hello";
Similarly, char* color_itom(const char*, CHAR_DATA*) should return const char * if you want to return string literals from it.
strcap is defined as a char array, but you're putting the int values returned by tolower and toupper in there somewhere. Either change the type or put in an explicit cast.
Same for c->level = perm. Either add an explicit cast or change the type of c->level to match the type of perm.
The error:
deprecated conversion from string constant to ‘char*’
is caused by using string literal to initiaize char*, e.g.
char* str = "something";
This should read:
const char* str = "something";
This question already has answers here:
invalid conversion from ‘const char*’ to ‘char’
(2 answers)
Closed 6 years ago.
#include<stdio.h>
#include<iostream>
#include<fstream>
#include<string.h>
using namespace std;
class base {
public:
int lookup(char c);
}; // class base
int base::lookup(char c)
{
cout << c << endl;
} // base::lookup
int main() {
base b;
char c = "i";
b.lookup(c);
} // main
On Compiling above code I am getting below error :
g++ -c test.cpp test.cpp: In function ‘int main()’: test.cpp:20:10:
error: invalid conversion from ‘const char*’ to ‘char’ [-fpermissive]
Try replacing
char c = "i";
with
char c = 'i';
"i" is not a character, it's a character array that basically decays to a pointer to the first element.
You almost certainly want 'i'.
Alternatively, you may actually want a lookup based on more than a single character, in which case you should be using "i" but the type in that case is const char * rather than just char, both when defining c and in the base::lookup() method.
However, if that were the case, I'd give serious thought to using the C++ std::string type rather than const char *. It may not be necessary, but using C++ strings may make your life a lot easier, depending on how much you want to manipulate the actual values.
"i" is a string literal, you probably wanted a char literal: 'i'.
String literals are null terminated arrays of const char (which are implicitly converted to char const* when used in that expression, hence the error).
char literals are just chars