Here is my code:
#include<stdio.h>
#define MAXLINE 100
/*print the reverse of the input*/
int getline1(char line[], int maxline);
char *reverse(char);
main(){
int len;
char line[MAXLINE];
char *rp;
while ((len = getline1(line, MAXLINE)) > 0)
rp = reverse(line);
printf("%s", *rp);
return 0;
}
int getline1(char s[], int lim){
int c, i;
for (i = 0; (c=getchar()) != EOF && c != '\n'; i++)
if (i > lim-1)
continue;
else
s[i] = c;
if (c == '\n'){
s[i] = c;
i++;
}
s[i] = '\0';
return i;
}
char *reverse(char ca[]){
int i;
int i1 = 0;
char *rp;
char reversed[MAXLINE];
for (i = MAXLINE-1; i >= 0; i--){
reversed[i1] = ca[i];
i1++;
}
rp = reversed;
return rp;
}
But when I try to compile it, I get the following errors:
reverse.cpp: In function ‘int main()’:
reverse.cpp:14:20: error: invalid conversion from ‘char*’ to ‘char’ [-fpermissive]
reverse.cpp:7:7: error: initializing argument 1 of ‘char* reverse(char)’ [-fpermissive]
reverse.cpp:15:19: warning: format ‘%s’ expects argument of type ‘char*’, but argument 2 has type ‘int’ [-Wformat]
I don't have much experience with C++. What am I doing wrong? I just want to make a pointer to a char array and return it.
I just want to make a pointer to a char array and return it.
You appear to want to return a string. That is not a pointer to a char array. Even if your program compiled, you would invoke UB, as you return a pointer to an automatic object- and there are quite a few other runtime errors in your code as well. You got lucky that you also made a compile-time error so the compiler did not accept your program. This C++ program achieves what you intend:
#include <string>
#include <iostream>
std::string reverse(std::string val) {
return std::string(val.rbegin(), val.rend());
}
int main() {
std::string str;
while(std::getline(std::cout, str))
std::cout << reverse(str);
}
What am I doing wrong?
You're learning C89 intead of C++11. They're really different things.
If you wish to learn to code C++, you must learn std::string, and the rest of the Standard library. You will not get anywhere with char*, char[], and MAGIC_BUFFER_SIZE.
You first declare the function prototype
char *reverse(char);
But the actual function is declared as
char *reverse(char ca[])
That's your problem.
What are you trying to achieve ? There are logical errors in the code ...
while ((len = getline1(line, MAXLINE)) > 0)
rp = reverse(line);
printf("%s", *rp);
this part will call reverse on every /n character but printf will never be called...
Also you have string of 100 chars and your reverse will put leading char on end of reverse string.. so if you have string of 5 chars you will have garbage on first 95 positions and then 5 chars you need ...
Related
#include <iostream>
#include <string>
using namespace std;
float findSimilarityScore(string A, string B)
{
string C;
if (A.length() != B.length())
{
return -1;
}
if (A.length() == 0 and B.length() == 0)
{
return -1;
}
else
{
int i;
for (i = 0; A.length() - 1; i = i+1)
{
if (A[i] == B[i])
{
C.append(A[i]);
}
if (A[i] != B[i])
{
return 0;
}
}
cout << C << endl;
}
}
int main()
{
findSimilarityScore("DDS","DAS");
}
when i try to run my code, my IDE show this:
/home/ubuntu/workspace/hmwk4/hmwk4-1.cpp: In function ‘float findSimilarityScore(std::string, std::string)’:
/home/ubuntu/workspace/hmwk4/hmwk4-1.cpp:24:30: error: invalid conversion from ‘char’ to ‘const char*’ [-fpermissive]
Why?
I want : if the first char in A is same to the first char in B, then add this char to the string C. if the second char in A is same to the second char in B, then add this char to the string C. And so on.
In the line
C.append(A[i]);
std::string::append has no overload that takes a single char. Use push_back instead:
C.push_back(A[i]);
This will fix your compilation error, but your function is still logically incorrect. It returns 0 the first time it finds any character that doesn't match between the two strings, and if the two strings are identical then it will invoke undefined behavior by reaching the end of a non-void function without returning a value.
string::append() with a single parameter is expecting a string argument, you are passing a single char. Try
C.append(1, A[i]);
Also, once you find a non-matching char, you return without printing the new string. Move the cout just before the return
if (A[i] != B[i])
{
cout << C << endl;
return 0;
}
And be sure to add a return at the end of your function in case you don't find any characters which are unequal.
I'm new to C++, been programming a while in C though.
Trying to read in a string and then convert the string into int by using strtol.
Iam using the gcc compiler.
And I get the following error message: "c++ error: cannot convert 'std::__cxx11::string {aka std::__cxx11::basic_string}' to 'const char*' for argument '1' to 'long int strtol(const char*, char**, int)' int c = strtol(str[j], &p, 10);".
I have tried different types of conversions but really like the strtol for future reference. Is there something to do with my vector string?
#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;
using std::string;
int main()
{
int a = 0;
int i = 0;
int size = 0;
int* big;
cin>>a;
size = a*2;
int sizes[size];
string *str = new string[size];
while(i < a){
cin>>str[i];
i++;
}
i = 0;
while(i < a){
cout << str[i] << endl; // just for checking
i++;
}
for (int j =0; j<size-1;j++){
char* p;
char* q;
int c = strtol(str[j], &p, 10);
if (!*p) {
sizes[j] = *p;
}else{
sizes[j] = *p/2;
}
}
return 0;
}
Thanks in advance!
You can use strtol(str[j].c_str(), &p, 10); the call to c_str() returns a const char* that points at the contents of the string object, and strtol wants a const char*. Or you can write more idiomatic code, and call std::stol(str[j]).
I am having trouble with a two-dimensional array comparison. I need to create a pseudo login system that asks the user for a username and password and then compares the input to a predefined list of usernames.
In the function, the predefined usernames are represented by the *s and the user input is *s1. When I try to compile it, this pesky trouble-maker appears:
68 D:\Personal\Dev-Cpp\projects\loginSysTest\main.cpp
invalid conversion from char' toconst
regarding the strncmp function in the if statement.
This is the code:
#define nameLenght 30
#define User 10
char usernames[User][User] = {{"student"}, {"admin"}, {"Deus Ex Machina"}};
//=====================================================================
int main(int argc, char *argv[])
{
char usernameInput[nameLenght + 1] = {0};
gets(usernameInput);
int login = compS(*usernames, usernameInput);
if(login == 0)
printf("Access Granted! \n");
else
printf("Access Denied!");
system("PAUSE");
return 0;
}
//=====================================================================
int compS(char *s, char *s1)
{
for(int k = 0 ;k < nameLenght; k++)
{
if(strncmp(s[k], s1, strlen(s1)) == 0)
return 1;
}
}
Thank you in advance.
Just use std::vector and std::string along the lines of:
std::vector<std::string> usernames({"student", "admin", "Deus Ex Machina"});
std::string input;
std::cin >> input;
if (std::find(begin(usernames), end(usernames), input) != end(usernames))
std::cout << "Access Granted!\n";
else std::cout << "Access Denied!\n";
Live demo
If you want (or need) to keep your C-style not-pretty-nor-very-safe code, you could write
strncmp(s + (k+nameLenght), s1, strlen(s1))
That would compile and perhaps work with some more efforts, but you'll have to correct the usernames sizes and actually call compS.
See that other answer for a safer C++-styled code.
There are a few ways you can accomplish this. This is the first way that comes to mind.
char* usernames[] = { "student" , "admin", "Deus Ex Machina", NULL };
int compS(char **s, char *s1)
{
for (int k = 0; s[k] != NULL; k++)
{
if (strncmp(s[k], s1, strlen(s1)) == 0)
return 0;
}
return 1;
}
You have to pass a pointer to a string to strncmp() so I changed your array to an array of pointers to strings. The array has a null pointer at the end to allow the loop to know when it has reached the end.
I am trying to use the function rrd_update_r of the round robin database.
int rrd_update_r(const char *filename,const char *_template,
int argc,const char **argv);
The function accepts the as 3rd and 4th argument the well known argc, argv.
Even though I am using C++ (and g++) for this project, rrd is written in C and consequently I could use the function wordexp(char *, wordexp_t*) provided in GNY/Linux to split the arguments of a string into an argv array.
The problem is that wordexp_t returns a member of char ** type (as argv), which is incompatible apparently with the rrd_update_r function call.
/usr/include/rrd.h:238:15: error: initializing argument 4 of ‘int rrd_update_r(const char*, const char*, int, const char**)’ [-fpermissive]
To my surprise I could find no help on the matter either. This Why can't I convert 'char**' to a 'const char* const*' in C? solution did not work.
So I am left wondering: how can I pass the char ** into const char ** ?
The full function is
#include <errno.h> // Error number definitions
#include <rrd.h>
#include <wordexp.h>
void splitToArgs(string& parametersString) //parametersString contains space separated words (parameters).
{
wordexp_t we;
int er = 0;
if ( (er=wordexp(parametersString.c_str() , &we, 0)) != 0)
{
cout << "error in word expansion " << er << endl;
}
else
{
if (we.we_wordc>0)
{
char * filename = we.we_wordv[1]; //filename is part of the parameters string
rrd_clear_error();
int ret = rrd_update_r( filename , NULL , we.we_wordc, we.we_wordv );
if ( ret != 0 )
{
cout << "rrd_update error # = " << ret << " error string = " << rrd_get_error() ;
}
}
}
wordfree(&we);
}
This use of const_cast (if correct) also does not work
error: invalid conversion from ‘char**’ to ‘const char**’ [-fpermissive]
const char **w = const_cast<char**>(we.we_wordv);
const_cast<const char**>(whatever)
is the correct way to const_cast in this case.
Im going to give you an example passing a char to a enum ( which is in fact a constant )
enum TIngrediente
{
TOMATE,
QUESO,
NATA,
CEBOLLA,
POLLO,
HUEVO,
SALAMI,
ANCHOA,
BACON,
GAMBA
};
string tolower(string s)
{
string r = s;
for (int i = 0; i < s.size(); ++i)
r[i] = tolower(r[i]);
return r;
}
TIngrediente StrToIngrediente(string s)
{
s=tolower(s);
int i;
while (i < INGREDIENTES.size() and INGREDIENTES[i] != s)
++i;
return (TIngrediente)i;
}
Now the only thing you need to do is make another function changing again from const char to char ( very easy)
This way youll trans form the char constant to a const char, be ware youll need to add a library, cctype and local to make this work.
I am new to STL and want to get used to string datatype instead of the char datatype. I am trying to map a string to a char in this code but it gives 2 compilation errors at lines 13 and 14 - "error: invalid conversion from 'char' to 'const char*' and error: invalid conversion from 'int' to 'const char*'". How can I fix these errors?
I have written the correct code in commented lines (using a map of char,char) to show what I want to implement with strings. I think the problem is probably that a string is not taken as an array of characters in C++.
What is the best way to implement the same without using char datatype?
#include <bits/stdc++.h>
using namespace std;
int main()
{
//map<char,char> M;
map<string, char> M;
string S,R;
cin>>S>>R;
for(int i=0;i<26;i++)
{
//M[S[i]]=(char)(i+'a');
//M[(char)toupper(S[i])]=(char)(i+'A');
M[S[i]]=(char)(i+'a');
M[toupper(S[i])]=(char)(i+'A');
}
for(int i=0;i<R.size();i++)
cout<<M[R[i]];
return 0;
}
Thanks in advance.
S[i] returns a single char, while toupper() returns an int. std::string does not have a constructor that accepts a single char by itself (but it does have an = operator that does) or an int at all. You need to use something more like this instead:
// using string(const char* s, size_type count)
char c = S[i];
M[string(&c, 1)] = (char)(i+'a');
c = (char) toupper(c);
M[string(&c, 1)] = (char)(i+'A');
Or:
// using string(size_type count, char ch)
char c = S[i];
M[string(1, c)] = (char)(i+'a');
c = (char) toupper(c);
M[string(1, c)] = (char)(i+'A');
Or:
// using operator=(char ch)
string tmp;
tmp = S[i];
M[tmp] = (char)(i+'a');
tmp = (char) toupper(c);
M[tmp] = (char)(i+'A');