Monday, July 27, 2009

How to resolve this c++ error?

Hey, why is this cpp file returning an error and how do I fix it?





error:


I:\C++\Programs\Project7\Cpp.cpp(44) : 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





txt version:


http://www.hcaelxxam.com/txt.txt


cpp version:


http://www.hcaelxxam.com/Cpp.cpp


input file:


http://www.hcaelxxam.com/input.txt

How to resolve this c++ error?
StudentInfo * records = new StudentInfo[y];





//we have to null-terminate our string


array[y][x] = 0x00;





//now make a pointer and point it to the array. I think this will work, you might have to tweak the following line though.


char * tmpstr = %26amp;array[0][0];





//now this will work.


records[1].SerialNumber = atoi(tmpstr);
Reply:char input[200];


cout %26lt;%26lt; "Please input the location of the input file: ";


cin %26gt;%26gt; input;











input here is an array of character not a character variable so you have to specify the element index (subscript) you want to stoer the location in





example


cin %26gt;%26gt; input[10];





good luck ! :)
Reply:const char * is like a string. I am not going to tell you, but I'll tell you some stuff to get your answer. A const char pointer, hint pointer is your key word, is basically a string. Knowing anything about arrays and where they are in memory. What is the similarity of what pointers do and arrays are?





You should read your book, there is always example of this in your book.





If you need more help just shoot a message.





P.S. You don't anything with your file. Your array is empty, there is a null value and that's it, when it reaches line 44. I don't know why it is complain about it then.
Reply:The problem isn't your cin, using an array is prefectly fine.





The problem as stated by the error message is your atoi call in line 44. This one


records[1].SerialNumber = atoi(array[0][0]);


atoi accepts a const array of characters (string), your giving it a single character.


No comments:

Post a Comment