I have a problem in compiling this source .
This is my source :
#include <iostream>
#include <string>
using namespace std;
char pass[7],d;
int v;
int isvalid(char pass);
int main(int argc, char** argv) {
cout<<"Enter Password :";
gets(pass);
cout<<endl;
v=isvalid (pass);
if(v==1){
cout<<"The Password is Correct . You can Come in"<<endl;
}
else{
cout<<"The Password in InCorrect ! You Can't Come In"<<endl;
}
system("PAUSE");
return 0;
}
/*-----------Functions--------------*/
int isvalid(char pass){
d="Pokerface";
if(pass==d){
return 1;
}
else{
return 0;
}
}
It should get a password from user and check it with isvalid function and say It is correct or not but compiler (DEV C++ 5) shows me these errors :
In function 'int main(int, char**)':
14 17 [Error] invalid conversion from 'char*' to 'char' [-fpermissive]
8 5 [Note] initializing argument 1 of 'int isvalid(char)'
In function 'int isvalid(char)':
26 3 [Error] invalid conversion from 'const char*' to 'char' [-fpermissive]
28 recipe for target 'main.o' failed
What is the problem ? please help me.
pass is an array of char ( char * ), and your isvalid funciton take a charas it's first argument. I suspect you want to change your isvalid function so that it takes a char* as an argument, like this :
int isvalid(char* pass){
Also, keep in mind that when you do this :
if (pass==d)
You are NOT comparing the strings, but the pointers. If you want to check that the two string are identical, that's not going to work
For strings comparisons use strcmp(str1, str2) in string.h.
Since you have tagged C++ to this question, I would suggest using std::string
#include <iostream>
#include <string>
using namespace std;
bool isvalid(const std::string & pass);
int main(int argc, char ** argv) {
string pass;
cout << "Enter Password" << endl;
cin >> pass;
if (isvalid(pass))
cout << "The Password is Correct. You can Come in" << endl;
else
cout << "The Password in InCorrect ! You Can't Come In" << endl;
return 0;
}
bool isvalid(const std::string & pass) {
return pass == "Pokerface";
}
int isvalid(char pass)
You probably meant to take in a char* there, but you shouldn't do that either: use std::string instead. Additionally, your function should return a bool instead of an int.
bool isvalid(const std::string& pass)
You should then update pass and d to be std::strings as well and use std::cin instead of gets to read from stdin. There is no reason for pass or d to be global variables, so make them local to your functions.
If you keep your function as taking a char* your comparison will not work, because you will just be comparing the pointers. You would need strncmp for that.
Related
I'm trying to convert all the words into capital letters. Here's the header:
#include <string.h>
#include <ctype.h>
using namespace std;
int Mayusculas(char texto)
{
int liCount;
for(liCount=0;liCount<strlen(texto);liCount++)
{
texto[liCount]=toupper(texto[liCount]);
}
}
Here is the definition in main
char Cadena[100];
and here is where I am using it
case 1:
Mayusculas(Cadena);
cout<<Cadena<<endl;
The error message is
error: invalid conversion from 'char' to 'const char*'
First of all, you have to pass the address of the string, so instead of char, you have to use char*, like this in your function:
void Mayusculas(char *text)
{
for(int post = 0; pos < std::strlen(text); pos++)
{
text[post] = (char) toupper(text[pos]);
}
}
Note: The char *text indicated the address of the first character in your string.
The definition in the main function is good, so you can use it like this:
int main() {
// ...
char Cadena[100];
Mayusculas(Cadena);
std::cout << Cadena << std::endl;
return 0;
}
I have also written an example that you can execute and test here.
TL;DR:
Demo using C-style strings
Demo using std::string
Details
Since we're primarily English-speaking, I'll note that it appears that Mayusculas indicates capital letters, and cadena is a series or chain - in this case a C-style string.
As Presented - C-style Strings
int Mayusculas(char texto) should be int Mayusculas(char *texto)
It needs to be a char * since you are working with a C-style string, and not a single character. Otherwise you have nothing to iterate through.
toupper() returns an int, so you should cast, i.e. change
texto[liCount]=toupper(texto[liCount]);
to
texto[liCount] = (char)toupper(texto[liCount]);
The function signature says you're returning an int, but you don't actually return anything. So either change it to return void, or return something. (liCount, maybe?)
Alternative: std::string
But you tagged this question as C++, so why not use std::string instead of C-style strings? They're safer, and easier to work with.
Quoting Pierre from Convert a String In C++ To Upper Case:
#include <algorithm>
#include <string>
std::string str = "Hello World";
std::transform(str.begin(), str.end(),str.begin(), ::toupper);
Or if you still want it in your own function,
#include <algorithm>
#include <string>
#include <iostream>
using namespace std;
string Mayusculas(std::string str)
{
transform(str.begin(), str.end(), str.begin(), ::toupper);
return str;
}
int main()
{
string Cadena = "Hola al Mundo";
cout << Mayusculas(Cadena) << endl;
return 0;
}
That way returns the result as a string. But if you want to modify it in place like your original, you can do this instead. See it work at Ideone.
void Mayusculas(std::string & str) // reference parameter
{
transform(str.begin(), str.end(), str.begin(), ::toupper);
}
int main()
{
string Cadena = "Hola al Mundo";
Mayusculas(Cadena);
cout << Cadena << endl;
return 0;
}
change your code to: as int may not fit into receiver type char
int Mayusculas(char *texto)
{
int liCount;
for(liCount=0;liCount<strlen(texto);liCount++)
{
texto[liCount]= (char) toupper(texto[liCount]);
}
}
I want to create a program capable of reading the command line in format:
SET x = ( expression ) <--- spaces between everywhere
without using strings. I wanted to cin in the command and simply compare it to a list of possible values,
so..
char a;
cin>>a;
if(a== 'SET'){
----
}
this obviously does not work because a is an array.
I thought i could do it this way (if a[0]=='S'), then check a[1] for 'E' etc. but I believe this is a widely inefficient way of doing this? or isnt it?
For your example, you can use std::string
std::string a;
cin >> a;
if (a.compare("SET"))
{
// do stuff
}
string::compare will do an equality comparison for the full string instead of character-by-character comparison.
You can use the strcmp function:
#include <iostream>
#include <cstring>
int main(int argc, char* argv[]) {
char * a;
std::cin >> a;
if (strcmp(a, "SET") == 0) {
std::cout << "check" << std::endl;
}
return 0;
}
I've cut out a lot of the code to make it easier to read. Specifically I'm trying to rewrite strcat
#include <iostream>
using namespace std;
char MYstrcat(char string1[],char string2[]);
int main()
{
char letter, letter_ans, again;
const int SIZE = 80;
char string1 [SIZE];
char string2 [SIZE];
getting input for the character arrays
cout << "Enter the first string: ";
cin >> string1;
cout << "Enter the second string: ";
cin >> string2;
letter_ans=MYstrcat(string1, string2);
cout << letter_ans;
cin.get(); cin.get();
return 0;
}
this is my version of the strcat function
char MYstrcat(char string1[],char string2[])
{
int i = 0; //Counters
int j = 0;
//Read until null character is encountered then append.
while (string1[i] != '\0')
{
i++;
}
while (string2[j]!= '\0'){
string1[i] = string2[j];
i++;
j++;
}
string1[i]='\0'; // Place a null character in string2.
i get the error in this next line: invalid conversion from 'char*' to 'char'
return string1;
}
char MYstrcat(char string1[],char string2[]); // returns a single character
char *MYstrcat(char string1[],char string2[]); // returns a pointer to a string that can be used like string1[] and string2[]
Likewise,
char letter_ans; // this gives space for a single char, like int int_ans;
char *letter_ans; // this makes a pointer to an array of char's, the proper return type
However, since you are returning string1, it may be possible that you don't even want to use a return type. If you declare your function as so:
void MYstrcat(char *string1,char *string2);
It will operate directly on string1, leaving no need to return a pointer to it.
Arrays decompose in to pointers in C++, so when you do return String1 you are returning a char*, hence the return type of your function should be char*.
You're returning a char in your header, not a string.
The function header char MYstrcat(char string1[],char string2[]);
declares the return type as char, but the function body is returning a string. return string1;
because string1 is an char array. ie:char[]... but your function's return type is char . so you got an error.you can change you function's return type to char*, then test your code. good luck.
EDIT: I also got an answer to make sector a vector of vectors:
vector<vector<char>>sector;
and that gets rid of the rest of my errors.
EDIT: I've made sector an array of pointers as someone suggested, and still get three errors:
EDIT: I have edited the program, but it has not fixed all of the errors:
I have this section of a program:
char* load_data(int begin_point,int num_characters);
ifstream mapdata("map_data.txt");
const int maxx=atoi(load_data(0,2));
const int maxy=atoi(load_data(2,2));
char** sector=new char[maxx][maxy];
char* load_data(int begin_point,int num_characters)
{
seekg(begin_point);
char* return_val=new char[num_characters+1];
mapdata.getline(return_val,num_characters);
return return_val;
}
And I get these errors:
line 5>error C2540: non-constant expression as array bound
line 5>error C2440: 'initializing' : cannot convert from 'char (*)[1]' to 'char **'
line 14>error C3861: 'seekg': identifier not found
per seekg: yes I know I have to include fstream, I included that in main.cpp, this is a separate .h file also included in main.cpp.
How do I fix the errors? Specifically, how to I fix the errors while keeping all my variables global?
Also, if it helps, this is map_data.txt:
10
10
00O
99!
1
55X
19
What is a question?
18
This is an answer
1
1
2
1
Well,
function load_data(int,int) returns a char.
You are passing that char to the atoi function, that takes a char*. In addition to that, you are probably not including stdlib.h header file!!
#include <cstdlib>
int atoi(const char*);
If you dont wan't to include stdlib.h, then you could declare atoi as extern, but be aware when you compile this module.
extern int atoi(const char*)
Take into account that the argument of atoi function must be a null-terminated string.
In order for your code to work, you should make function load data return a char*, not a char.
char* load_data(int,int);
So, now you could do
//notice these aren't const, they rely on non-compile time available data.
int maxx = atoi (load_data(....));
int maxy = atoi (load_data(....));
If you are in C++, load_data function could return a std::string.
std::string load_data(int,int)
and then use c_str() method, which returns a C-String from a C++ string.
const char* std::string:c_str()
int maxx = atoi(load_data(....).c_str());
int maxy = atoi(load_data(....).c_str());
In addition to that, you shouldn't
(regarding
line 5>error C2540: non-constant expression as array bound
line 5>error C2440: 'initializing' : cannot convert from 'char (*)[1]' to 'char **'
)
char sector[maxx][maxy];
You should
char** sector = new char[maxx][maxy]();
and dont forget to free this memory
delete[](sector);
You can't return a pointer to a stack variable. And arrays need to be returned as pointer types.
Try:
char* load_data(int begin_point,int num_characters)
{
seekg(begin_point);
char* return_val = new char[num_characters+1];
mapdata.getline(return_val, num_characters);
return return_val;
}
char* foo = load_data(...);
...
delete [] foo;
I'm not quite sure what is the goal of your exercise. But if you want to read 'stuff' from file and get it in format that you expect (like int, strings ...) you can just use operator>> and getline like this:
#include <fstream>
#include <string>
using namespace std;
int main()
{
ifstream ifs("data.txt");
if (!ifs.is_open()) return 0;
int maxx;
int maxy;
ifs >> maxx >> maxy;
cout << maxx << " " << maxy << endl;
// ----
char OO_0[4]; // can use char[] or string, see next
ifs >> OO_0;
OO_0[sizeof(OO_0)] = 0;
cout << OO_0 << endl;
// ----
string _99;
ifs >> _99;
cout << _99 << endl;
int one;
string _55_X;
int _19;
string what_is;
ifs >> one >> _55_X >> _19 >> ws;
// ws gets rid of white space at the end of the line ...
// this is because getline would only read that ws up to eol
getline(ifs,what_is);
cout << one << " " << _55_X << " " << _19 << " " << what_is << endl;
ifs.close();
}
And you get output like this:
10 12
00O
99!
1 55X 19 What is a question?
Is that what you were after? NOTE: I'm using c++ because I noticed you mentioned "main.cpp"
atoi() is giving me this error:
error C2664: 'atoi' : cannot convert parameter 1 from 'char' to 'const char *'
Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
from this line:
int pid = atoi( token.at(0) );
where token is a vector
how can i go around this?
token.at(0) is returning a single char, but atoi() is expecting a string (a pointer to a char.) Either convert the single character to a string, or to convert a single digit char into the number it represents you can usually* just do this:
int pid = token.at(0) - '0';
* The exception is when the charset doesn't encode digits 0-9 in order which is extremely rare.
You'll have to create a string:
int pid = atoi(std::string(1, token.at(0)).c_str());
... assuming that token is a std::vector of char, and using std::string's constructor that accepts a single character (and the number of that character that the string will contain, one in this case).
Your example is incomplete, as you don't say the exact type of the vector. I assume it is std::vector<char> (that, perhaps, you filled with each char from a C string).
My solution would be to convert it again on char *, which would give the following code:
void doSomething(const std::vector & token)
{
char c[2] = {token.at(0), 0} ;
int pid = std::atoi(c) ;
}
Note that this is a C-like solution (i.e., quite ugly in C++ code), but it remains efficient.
const char tempChar = token.at(0);
int tempVal = atoi(&tempChar);
stringstream ss;
ss << token.at(0);
int pid = -1;
ss >> pid;
Example:
#include <iostream>
#include <sstream>
#include <vector>
int main()
{
using namespace std;
vector<char> token(1, '8');
stringstream ss;
ss << token.at(0);
int pid = -1;
ss >> pid;
if(!ss) {
cerr << "error: can't convert to int '" << token.at(0) << "'" << endl;
}
cout << pid << endl;
return 0;
}