Array strings C++ - c++

I don't remember how to do this in C++:
#include <iostream>
#include <conio.h>
#include <string.h>
int main(){
char categorias[3][20];
/*char pais[3][20];
char movimiento[3][50];
char obras[100][50]; */
categorias[0]="Alta";
categorias[1]="Media";
categorias[2]="Baja";
}
This throws this error: 19 15 C:\Users\dell\Desktop\Subasta.cpp [Error] incompatible types in assignment of 'const char [5]' to 'char [20]';
Long time ago I don't use C++ and I can´t solve the problem.

Use C++ abstractions and containers from the standard library:
int main()
{
using namespace std::string_literals;
auto categorias = std::vector{"Alta"s, "Media"s, "Baja"s};
// Or if you know you have a fixed number of categories:
auto categorias = std::array{"Alta"s, "Media"s, "Baja"s};
}

To copy the string literal into the char array
strcpy(categorias[0], "Alta");

Related

Simple Class with Constructor throws an warning: ISO C++ forbids converting a string constant to 'char*' [duplicate]

This question already has answers here:
Conversion from string literal to char* is deprecated [duplicate]
(2 answers)
Closed 2 years ago.
So I'm building a Regex class, with a simple constructor:
regex.hpp
#ifndef REGEX_CUSTOM_CLASS
#define REGEX_CUSTOM_CLASS
#include <stdio.h>
using namespace std;
class Regex
{
private:
/* data */
public:
char *regex;
Regex(char str[]);
};
#endif // REGEX_CUSTOM_CLASS
regex.cpp
#include <iostream>
#include <list>
#include <assert.h>
#include <ctype.h>
#include <stdio.h>
#include "regex.hpp"
using namespace std;
Regex::Regex(char str[])
{
regex = str;
}
tools.cpp
#include <iostream>
#include <stdio.h>
#include "lib/regex/regex.cpp"
using namespace std;
int main() {
Regex my_regex("//");
cout << my_regex.regex << endl;
return 0;
}
But after I compile it to .exe file and run it, I get this error message:
warning: ISO C++ forbids converting a string constant to 'char*' Regex my_regex("//");
I think the problem is with the data types. What is the problem?
You cannot pass arrays by value. When you write:
Regex::Regex(char str[])
this actually is
Regex::Regex(char* str)
Moreover string literals are of type const char [N] (where N is length of the string including the null terminator) and when passed to functions they decay to const char*. Getting a char* from a const char* (pointer to constant char, not to be confused with constant pointer to char) would break const correctness.
Either use std::string as argument type or change it to const char*.

Why can't I provide a string argument to printw in ncurses? [duplicate]

This question already has answers here:
How to convert a std::string to const char* or char*
(11 answers)
Closed 3 years ago.
For an application that I'm writing, I have a string type variable that I want to display within an ncurses window:
#include <iostream>
#include <ncurses.h>
#include <string>
int main(){
std::string mystring = "A sample string\n";
// Entering the ncurses window
initscr();
printw(mystring);
getch();
endwin();
}
which throws the following error at compilation:
test_app.cpp: In function ‘int main()’:
test_app.cpp:12:18: error: cannot convert ‘std::__cxx11::string {aka std::__cxx11::basic_string<char>}’ to ‘const char*’ for argument ‘1’ to ‘int printw(const char*, ...)’
printw(mystring);
Where am I going wrong? How can I rectify this?
Some key concepts in c++:
A string literal declaration (aka "this is a string literal") has a type const char[N], where N is the size of the string, including the null terminator.
std::string != const char[]
However, a std::string can be constructed with a const char[] using this constructor (found here):
basic_string( const CharT* s,
const Allocator& alloc = Allocator() );
Where CharT is your implementation specific char equivalent.
Now, notice how printw takes a const char*. You aren't passing a const char * to printw, you're passing a std::string, and they aren't implicitly convertible to a const char *.
We have two options to solve your problem...
1) Store the string as a char[] (aka char *):
#include <iostream>
#include <ncurses.h>
#include <string>
int main(){
char mystring[] = "A sample string\n"; // Can decay to a char * implicitly.
// Entering the ncurses window
initscr();
printw(mystring);
getch();
endwin();
}
2) Get a representation of the std::string as a char *:
#include <iostream>
#include <ncurses.h>
#include <string>
int main(){
std::string mystring = "A sample string\n";
// Entering the ncurses window
initscr();
// Since c++ 11, mystring.data() is required to return a null-terminated char *.
// If c++ version < c++11, use mystring.c_str().
printw(mystring.data());
getch();
endwin();
}

Can't write to ostream in gnu linux c++ error: invalid operands of types ofstream and ‘const char [2]’ to binary ‘operator

Can't compile this small code on Linux:
#include <fstream>
#include <string>
#include <iostream>
#include <stdio.h>
using namespace std;
int main(){
char fileName[512];
sprintf(fileName, "test");
ofstream iriFile(string(fileName));
iriFile<<",";
return 0;
}
I am compiling like this: g++ test.cpp and am getting this:
test.cpp:12:11: error: invalid operands of types
‘std::ofstream(std::__cxx11::string) {aka
std::basic_ofstream(std::__cxx11::basic_string)}’ and
‘const char [2]’ to binary ‘operator<<’ iriFile<<",";
What might be the reason?
Ok, the solution is to remove implicit string() creation:
string sFileName(fileName)
ofstream iriFile(sFileName);
First of all you do not need to explicitly convert const char * to std::string there is std::ifstream constructor for it:
std::ofstream iriFile(fileName);
but if you want to be extra safe and verbose use proper C++ then:
std::ofstream iriFile( static_cast<std::string>(fileName) );
not C style cast.
As you pointed out, removing the explicit string creation fixes it.
It could be worth to add that this can also be fixed for types with explicit constructors by using list initialization, like so:
ofstream iriFile(string{sFileName});

strtoull was not declared in this scope while converting?

I am working with C++ in eclipse CDT and I am trying to convert string to uint64_t by using strtoull but everytime I get below error message -
..\src\HelloTest.cpp:39:42: error: strtoull was not declared in this scope
Below is my C++ example
#include <iostream>
#include <cstring>
#include <string>
using namespace std;
int main() {
string str = "1234567";
uint64_t hashing = strtoull(str, 0, 0);
cout << hashing << endl;
}
return 0;
}
Is there anything wrong I am doing?
Why your solution doesn't work has already been pointed out by others. But there hasn't been a good alternative suggested yet.
Try this for C++03 strtoull usage instead:
#include <string>
#include <cstdlib>
int main()
{
std::string str = "1234";
// Using NULL for second parameter makes the call easier,
// but reduces your chances to recover from error. Check
// the docs for details.
unsigned long long ul = std::strtoull( str.c_str(), NULL, 0 );
}
Or, since C++11, do it directly from std::string via stoull (which is just a wrapper for the above, but saves on one include and one function call in your code):
#include <string>
int main()
{
std::string str = "1234";
// See comment above.
unsigned long long ul = std::stoull( str, nullptr, 0 );
}
Never use char[] or pointers if you have a working alternative. The dark side of C++, they are. Quicker, easier, more seductive. If once you start down the dark path, forever will it dominate your destiny, consume you it will. ;-)
the structure for strtoull is: strtoull(const char *, char * *, int)
You have given it a std::string as pointed out by #juanchopanza
This is the solution I came up with is
#include <iostream>
#include <cstring>
#include <string>
#include <cstdlib>
using namespace std;
int main() {
char str[] = "1234567";
unsigned long long ul;
char* new_pos;
charDoublePointer = 0;
ul = strtoull(str, &new_pos, 0);
cout << ul << endl;
return 0;
}
The output I got was: 1234567
Straight from the eclipse console.
Also at the end of your program you have return 0 out of scope with an extra curly brace.

string Comparison

I want to compare two user input strings, but not able to do so...
#include "stdafx.h"
#include "iostream"
#include "string"
using namespace std;
int _tmain(int argc, _TCHAR* argv0[])
{
string my_string;
string my_string2;
cout<<"Enter string"<<endl;
cin>>my_string;
cout<<"Enter 2nd string"<<endl;
cin>>my_string2;
cout<<my_string<<" "<<my_string2;
strcmp(my_string,my_string2);
int result;
result= strcmp(my_string,my_string2);
cout<<result<<endl;
return 0;
}
This error is appearing.
Error 1 error C2664: 'strcmp' : cannot convert parameter 1 from 'std::string' to 'const char *' c:\users\asad\documents\visual studio 2008\projects\string\string\string.cpp 23 String
Since you're using std::string, strcmp is unnecessary -- you can just use <, ==, !=, etc.
Your includes:
Since you are including standard headers, they should be in <>
#include <string>
#include <iostream>
#include with "" is generally used for your own header files, not standard header files.
You are using C++, and therefore need not use strcmp. In C++, you can simply use == & != to compare two strings.
if (my_string == my_string2) result = 0;
else result = 1;
Also, if you want to convert a string to a const char*, you can use mystring.c_str()
If you want to use strcmp note that it takes different parameters than the ones you used.
http://www.cppreference.com/wiki/c/string/strcmp
Another way to do this is also
result= strcmp(my_string.c_str(),my_string2.c_str());