I have the below program written in C++:
#include <iostream>
using namespace std;
int main()
{
int age[5];
char name[5][10];
age[0]=10;
age[1]=20;
age[2]=30;
age[3]=25;
age[4]=40;
name[0]="abc";
name[1]="abc";
name[2]="abc";
name[3]="abc";
name[4]="abc";
cout<<name[0]<<" is "<<age[0]<<"years old";
cout<<"\n";
cout<<name[1]<<" is "<<age[1]<<"years old";
cout<<"\n";
cout<<name[2]<<" is "<<age[2]<<"years old";
cout<<"\n";
cout<<name[3]<<" is "<<age[3]<<"years old";
cout<<"\n";
cout<<name[4]<<" is "<<age[4]<<"years old";
cout<<"\n\n";
system("PAUSE");
}
When I compile and run it, I get these errors:
error C2440: '=' : cannot convert
from 'const char [3]' to 'char [10]'
There is no context in which this conversion is possible
error C2440: '=' : cannot convert
from 'const char [2]' to 'char [10]'
There is no context in which this conversion is possible
error C2440: '=' : cannot convert
from 'const char [2]' to 'char [10]'
There is no context in which this conversion is possible
error C2440: '=' : cannot convert
from 'const char [2]' to 'char [10]'
There is no context in which this conversion is possible
error C2440: '=' : cannot convert
from 'const char [2]' to 'char [10]'
There is no context in which this conversion is possible
I am running MSVC 2008 under Windows 7. I have tried many possible solutions but I failed in fixing this. Any help would be appreciated,
You are treating the name array as if it was defined thus:
char *name[5];
So either define it that way, or use the following code to populate it:
strcpy(name[0], "abc");
strcpy(name[1], "abc");
strcpy(name[2], "abc");
strcpy(name[3], "abc");
strcpy(name[4], "abc");
I prefer the former choice. The point being you are trying to assign a char * to a char [] which is what strcpy is for. Given you are manipulating initialized C strings in this case anyway, you might as well deal with char * throughout the code.
You should use std::string for this purpose. The use of char* and char[] to represent strings is deprecated in C++ for many good reasons.
Given the program snippet, name can be initialized at the declaration itself.
char name[5][10] = { "abc", "abc", "abc", "abc", "abc" } ;
// ^ index 5 is not necessary. char name[][10] = { .. } would also suffice.
Specified the length of each row is 10 but only using first 3 indexes of it. Every 3rd index ( i.e., 4th element in the array ) is automatically added with a '\0'.
Initialization can be done in case of age array too.
You can use also std::string name[10] instead of 2d char's array. In this case only you can assign new values to the strings through operator '='.
Otherwise you should to use array of char* and use strcpy() function for assignment.
Related
float diff = 0;
const char* str[] = {"Err: ZPROBE: ",diff};
LCD_ALERTMESSAGEPGM(str);
With the code above I get I get this error. Anyone know how to create a single string from "Err: ZPROBE: " and a (float) diff?
exit status 1
cannot convert 'float' to 'const char*' in initializatio
Sorry should add that in the environment i'm using - 'string' : is not a member of 'std',
Ok now trying this
String str = String("Err: ZPROBE: " , diff);
but get this
call of overloaded 'String(const char [14], float&)' is ambiguous
To convert float or double into a string, you can use dtostrf() which is available in the stdlib.h header. Also see the reference of String Object Constructors to construct String objects correctly.
I'm trying to make the program read the text file and use the line for function, but I get an error!
5 IntelliSense: operand types are incompatible ("BYTE" and "char *")
Error 1 error C2446: '==' : no conversion from 'char *' to 'int'
Error 2 error C2040: '==' : 'int' differs in levels of indirection from 'char [260]'
my code:
char* ReadINI(char* szSection, char* szKey, const char* szDefaultValue)
{
char* szResult = new char[255];
memset(szResult, 0x00, 255);
GetPrivateProfileString(szSection, szKey, szDefaultValue, szResult, 255, ".\\Config.ini");
return szResult;
}
int main (Classdata* Cdata)
{
BYTE ByteID = Cdata->ByteType;
static char ReadByte[MAX_PATH];
sprintf(ReadByte, "%s", ReadINI("CONFIG", "Key", "0"));
if (ByteID == ReadByte)
{
printf("Byte Value: %p", ReadByte);
}
}
Firstly, you are comparing ByteId (an unsigned char `) with ReadByte (a char*) which is unlikely to be meaningful.
On an unrelated note, szResult is created with new but never deleted in main.
Couldn't be much clearer ByteId is a BYTE, ReadByte is a char[260]. You cannot compare integers and arrays. Perhaps (only a guess) you meant ByteId == ReadByte[0].
Plus you have a memory leak in ReadIni, and ReadByte is declared static for no good reason I can see, and your declaration of main is not legal.
I used another type of function to read the file! Sorry for the bad explanation is that I'm working on a private project! thanks for the help everyone
fuction:
UINT value = GetPrivateProfileInt("Section", "Key", DEFAULT_VALUE, "program.ini");
thx for all!
I have a string array and an integer array. I want to convert the elements of string array to integer and then store them in the integer array. I wrote this code :
string yuzy[360];
int yuza[360];
for(int x = 0;x<360;x++)
{
if(yuzy[x].empty() == false)
{
yuza[x]=atoi(yuzy[x]);
cout<<yuza[x]<<endl;
}
else
continue;
}
this piece of code gives this error:
error: cannot convert 'std::string {aka std::basic_string}' to 'const char*' for argument '1' to 'int atoi(const char*)'
When I write the content of the string (-75dbm) in atoi function it works fine. But when I write (yuzy[x]), I get the error. How can I make atoi works well with string array?
Thanks.
atoi() takes C strings (char pointers) and not C++ string objects. Use
atoi(yuzy[x].c_str());
instead.
As an alternative to atoi, you could use std::stoi and related functions, if you have C++11 support.
yuza[x] = std::stoi(yuzy[x]);
atoi accept a c-style string as parametter, so, you could use atoi(yuzy[x].c_str());
When I try and compile this program, I get errors (included below the code) about strcpy's second argument. I'm honestly stumped on what to do to fix it. And I'm sorry if my code is not efficient or pretty to look at; I'm just a beginning CS student.
#include "stdafx.h"
#include <iostream>
#include <ctime>
using namespace std;
int main(){
int r = 0;
char *article[]={"the", "a", "one", "some", "any"};
char *noun[]={"boy","girl","dog","town","car"};
char *verb[]={"drove","jumped","ran","walked","skipped"};
char *preposition[]={"to","from","over","under","on"};
char sentence [80];
srand(time(NULL));
for(int i=0;i<=20;i++){
r = (rand()%5);
strcpy(sentence,*article[r]);
strcat(sentence," ");
r = (rand()%5);
strcat(sentence,*noun[r]);
strcat(sentence," ");
r = (rand()%5);
strcat(sentence,*verb[r]);
strcat(sentence," ");
r = (rand()%5);
strcat(sentence,*preposition[r]);
strcat(sentence," ");
r = (rand()%5);
strcat(sentence,*article[r]);
strcat(sentence," ");
r = (rand()%5);
strcat(sentence,*noun[r]);
strcat(sentence,".");
}
sentence[0]= toupper(sentence[0]);
cout<<sentence <<endl;
system("pause");
return 0;}
1>Compiling...
1>assignment 8.cpp
1>e:\assignment 8\assignment 8\assignment 8.cpp(16) : warning C4244: 'argument' : conversion from 'time_t' to 'unsigned int', possible loss of data
1>e:\assignment 8\assignment 8\assignment 8.cpp(20) : error C2664: 'strcpy' : cannot convert parameter 2 from 'char' to 'const char *'
1> Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
1>e:\assignment 8\assignment 8\assignment 8.cpp(23) : error C2664: 'strcat' : cannot convert parameter 2 from 'char' to 'const char *'
1> Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
1>e:\assignment 8\assignment 8\assignment 8.cpp(26) : error C2664: 'strcat' : cannot convert parameter 2 from 'char' to 'const char *'
1> Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
1>e:\assignment 8\assignment 8\assignment 8.cpp(29) : error C2664: 'strcat' : cannot convert parameter 2 from 'char' to 'const char *'
1> Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
1>e:\assignment 8\assignment 8\assignment 8.cpp(32) : error C2664: 'strcat' : cannot convert parameter 2 from 'char' to 'const char *'
1> Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
1>e:\assignment 8\assignment 8\assignment 8.cpp(35) : error C2664: 'strcat' : cannot convert parameter 2 from 'char' to 'const char *'
1> Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
DANGER. strcat() AND strcpy() ARE THE LEADING CAUSES OF CODE CANCER. Using them exposes you to all kinds of buffer overflows. Use strncat()/strncpy(), or (even better) just use std::string, since you're using C++!
strcat() and strcpy() expect their arguments to be strings. *article[r] is a single char--article[r] is the string you want. So, drop the leading asterisks.
You have one asterisk too many - noun[r] already gives you a char* so you don't need to put an additional * in the second parameter.
Also, strcat is an unsafe function and can crash your program unexpectedly if your buffer (in your case, sentence) is ever too small for the content.
Please use strncat instead - you'll need to add one more parameter to that function, which is the buffer size - in this case, 80. Then in case of undersized buffer instead of program crash you would just notice that your sentence is clipped at the end.
Your articles, nouns, and verbs are arrays of char pointers. When selecting the item in the array to use, you get a char* to the word to use. This char* is what strcpy expects - when you dereference the char* (i.e. article[r]), you end up with a char, not a char.
Also, strcpy is an unsafe string operator, so it can overwrite large clumps of memory or otherwise open gaping security holes. Is there any reason you're not allowed to use std::string for this assignment?
Too much de-referencing, e.g. change:
strcpy(sentence,*article[r]);
to
strcpy(sentence, article[r]);
and similarly for the other instances.
*article[r] is a value of type char. It's the first character of the string. strcpy expects the address of the string which is simply article[r].
Instead of
strcpy(sentence,*article[r]);
you want
strcpy(sentence,article[r]);
for (int v = 0; v <= WordChosen.length();v++)
{
if(Letter == WordChosen[v])
{
WordChosenDuplicate.replace(v,1,Letter);
}
}
I get this error
"Error 4 error C2664:
'std::basic_string<_Elem,_Traits,_Ax>
&std::basic_string<_Elem,_Traits,_Ax>::replace(__w64
unsigned int,__w64 unsigned int,const
std::basic_string<_Elem,_Traits,_Ax>
&)' : cannot convert parameter 3 from
'char' to 'const
std::basic_string<_Elem,_Traits,_Ax>
&' c:\documents and settings\main\my
documents\uni\2nd
year\tp2\hangman\hangman\hangman.cpp 147
"
I only got the error after putting this line in
WordChosenDuplicate.replace(v,1,Letter);
Or
WordChosenDuplicate.replace(v,1,std::string(Letter, 1));
The std::string::replace() function's parameters are incorrect or you need to invoke a different overload of replace. Something like:
WordChosenDuplicate.replace(v, // substring begining at index v
1, // of length 1
1, // replace by 1 copy of
Letter); // character Letter
What do you want to achieve? The version of replace that you are trying to call doesn't exist – as the compiler is telling you. Which of these versions do you mean?
It appears that WordChosenDuplicate is a std::string, in which case the 3rd parameter in the replace() method should be another std::string or a c-style const char*. You are trying to pass a single char instead ("Letter"). The error is saying that there is no version of replace() that takes a char as the 3rd parameter.