getting error: invalid conversion from ‘const char*’ to ‘int’ while compling
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
getting error: invalid conversion from ‘const char*’ to ‘int’ while compling
hi all,
i am a newbie with c++ and i have read though a few articles + looking at my c++ book to get my head around the language but i have had a go at writing my own bit of code and im getting:-
error: invalid conversion from ‘const char*’ to ‘int’
Code:
#include <iostream>
using namespace std;
int main() {
int code;
int a;
a = "c42";
cout << "Please enter your error code";
cin >> code;
if (code == a)
cout << "Worked";
else
cout << "Your selection was invalid";
return 0;
}
Unfortunately, you've provided a perfect platform for my favorite rant. Learning C++ without a fundamental understanding of C and the standard C library is a recipe for disaster.
The errors in this particular example are not C++ specific, but they do illustrate a fairly fundamental problem.
I don't want to sound condescending or confrontational but the above code displays a very serious lack of understanding of the basics.
I know you are new at this, and these sorts of mistakes are common at that stage of learning.
You've declared the variable 'a' as an integer, and are assigning the string "c42" to it. The problems with that really need to be obvious to you, if you are to be at all successful at this.
There are other declaration and usage errors in the above code, but you should start with that first.
Any of us here could fix it with a couple lines of example code, but I don't believe it would help you in the long run, so I'm afraid I will just tell you to get a good fundamentals book on C and learn that first. Others here may have different opinions, and there is some merit in learning C++ alone if the study path includes the fundamentals, but in my observation the end result is less desirable than getting your head around C, the C preprocessor, and the standard C library before tackling C++.
Ok, after reading my own words above, here, it seems a bit harsh, so I will give you these hints... declare your variables in this case to be character pointers or arrays of type char. One of my concerns about simply giving you that info is that you may miss out on important info about type checking, promotion, and conversion in your learning process.
a is defined as an integer. the compiler sees the character sequence "c42" as a character pointer (char *). the character pointer can't be assigned to an integer.
you can assign a an integer like:
a = 42;
What exactly is the example trying to do, compare the user input to a * character?
Unfortunately, you've provided a perfect platform for my favorite rant. Learning C++ without a fundamental understanding of C and the standard C library is a recipe for disaster.
I disagree. If paulie1 were learning C first, he'd have exactly the same problem, but the solution should be harder.
A beginning C++ programmer should not need to learn all the stuff a beginning C programmer needs about working with char*.
He needs some basic concept of what data type are.
Then just use string instead of int (for "a" and "code". "main" should still be declared int).
Quote:
The errors in this particular example are not C++ specific
In a strong sense they are, because in C++ the error is using int instead of string. But in C the errors are more serious.
Quote:
but they do illustrate a fairly fundamental problem.
I don't want to sound condescending or confrontational but the above code displays a very serious lack of understanding of the basics.
True for sure.
Data types are a critical basic concept in C and even more so in C++
thanks for that crabboy it complied fine but can you see anything which is out of place within the if statement as when i enter c42(correct) and ie c44(wrong) the output is the same which is the correct response (Worked) displays all the time, i know what you mean raconteur, you will learn quick by trying to solve problem your self instead of asking , but i couldnt work out what was causing it.
right i have got my code/program to work but having another problem:
i have this code :-
Code:
#include <iostream>
using namespace std;
int main() {
int code;
int a;
int b;
a = 00;
b = 11;
do {
cout << "Please enter your error code without the C or 9 to quit\n\n";
cin >> code;
if (code == a)
cout << "error1\n\n";
else if (code == b)
cout << "error2\n\n";
else
cout << "The error code you enter was invalid\n";
}while (code != 9);
return 0;
}
as far has it goes the program works fine if i enter 00 its displays error1 and 11 displays error2 and 9 quits great, but has soon as i enter letters ie c the programs seems it run weird and never stops so i need it close the console and reset it
any idea why it could be happening or notice anything else wrong with my code
The problem that you are facing is that the letter can not be put into the number (variable code) and so it doesn't get read. This means that the letter is still in the input queue and the loop comes back to the cin statement and sure enough it is still a letter and so it moves on leaving the letter on the queue. So what you need to do is ctrl + c to stop the program (rather than closing the console) and change the code. You need to read in the data as a string and then convert it to a number.
Use the C++ string class
Use the C style atoi() function and the string method c_str() to convert the string to a character array which atoi() requires
You should then find that your code will work, however you will find that an error code of 0 is not the most useful because a string of letters will be converted to zero.
All the best, I hope that my clues are sufficient for you to get to the next stage of your program.
#include <iostream>
using namespace std;
int main()
{
string code;
string a;
string b;
a = "c00";
b = "c11";
do {
cout << "Please enter your error code or CTRL-C to quit\n\n";
cin >> code;
if (code == a)
cout << "error1\n\n";
else if (code == b)
cout << "error2\n\n";
else
cout << "The error code you enter was invalid\n";
} while (true);
return 0;
}
Thanks for that graemef, and thanks raconteur for writing it out, Sorry i didnt see that properly when reading his post woooooops It works great now Thanks again
Thanks for that graemef, and thanks raconteur for writing it out, Sorry i didnt see that properly when reading his post woooooops It works great now Thanks again
You're quite welcome, and thank you for your kind words. Please take to heart what johnsfine and graemef have posted above, their suggestions are pure gold and will help you learn and understand the basics.
Last edited by raconteur; 02-25-2009 at 10:58 AM.
Reason: fix typo
A beginning C++ programmer should not need to learn all the stuff a beginning C programmer needs about working with char*.
Not only that, but this technically isn't a code error in C (although a logic error,) probably for compatibility with older-style function prototypes (that's whay I've always assumed, anyway.) In the absence of C++, gcc will compile the assignment with a warning.
Kevin Barry
Not only that, but this technically isn't a code error in C (although a logic error,) probably for compatibility with older-style function prototypes (that's whay I've always assumed, anyway.) In the absence of C++, gcc will compile the assignment with a warning.
Kevin Barry
It is the reasons that it does is why I hold to my opinion that the basics of C are essential. I was and am concerned that this subject is better for a blog entry so this will be my last post about it here with the fervent hope that the OP is successful.
The problem that you are facing is that the letter can not be put into the number (variable code) and so it doesn't get read. This means that the letter is still in the input queue and the loop comes back to the cin statement and sure enough it is still a letter and so it moves on leaving the letter on the queue. So what you need to do is ctrl + c to stop the program (rather than closing the console) and change the code. You need to read in the data as a string and then convert it to a number.
Use the C++ string class
Use the C style atoi() function and the string method c_str() to convert the string to a character array which atoi() requires
You should then find that your code will work, however you will find that an error code of 0 is not the most useful because a string of letters will be converted to zero.
All the best, I hope that my clues are sufficient for you to get to the next stage of your program.
Graeme.
strtol() is also good for this kind of thing because it can take a base 16 number (which is the number the op is putting in)
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.