LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-24-2009, 01:30 PM   #1
paulie1
LQ Newbie
 
Registered: Feb 2009
Posts: 6

Rep: Reputation: 0
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;

}
any help will be great

thanks
 
Old 02-24-2009, 01:53 PM   #2
raconteur
Member
 
Registered: Dec 2007
Location: Slightly left of center
Distribution: slackware
Posts: 276
Blog Entries: 2

Rep: Reputation: 44
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.
 
Old 02-24-2009, 01:54 PM   #3
crabboy
Senior Member
 
Registered: Feb 2001
Location: Atlanta, GA
Distribution: Slackware
Posts: 1,821

Rep: Reputation: 121Reputation: 121
a = "c42";

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?
 
Old 02-24-2009, 02:04 PM   #4
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197
Quote:
Originally Posted by raconteur View Post
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++

Last edited by johnsfine; 02-24-2009 at 02:06 PM.
 
Old 02-24-2009, 02:08 PM   #5
paulie1
LQ Newbie
 
Registered: Feb 2009
Posts: 6

Original Poster
Rep: Reputation: 0
hi

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.


thanks

Last edited by paulie1; 02-24-2009 at 02:11 PM.
 
Old 02-24-2009, 02:13 PM   #6
paulie1
LQ Newbie
 
Registered: Feb 2009
Posts: 6

Original Poster
Rep: Reputation: 0
sorry dont worry i found the problem woooops
 
Old 02-24-2009, 02:33 PM   #7
the_ultimate_samurai
Member
 
Registered: Jan 2006
Distribution: debian-lenny
Posts: 37

Rep: Reputation: 15
as a little aside, if that were a hex number, not a string, you could use:
Code:
a=0xc42
the 0x tells it this is a hex number, then it can be an int. of course i think you would have to make the cin like this:
Code:
cin >> hex >>code
just remember to have cin >> dec >> next time you want to input in decimal notation.
 
Old 02-24-2009, 05:47 PM   #8
paulie1
LQ Newbie
 
Registered: Feb 2009
Posts: 6

Original Poster
Rep: Reputation: 0
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

thanks

Last edited by paulie1; 02-24-2009 at 06:05 PM.
 
Old 02-24-2009, 06:53 PM   #9
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
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.
 
Old 02-24-2009, 07:01 PM   #10
raconteur
Member
 
Registered: Dec 2007
Location: Slightly left of center
Distribution: slackware
Posts: 276
Blog Entries: 2

Rep: Reputation: 44
uhhm... did you read what johnsfine wrote?

Code:
#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;
}
 
Old 02-25-2009, 05:14 AM   #11
paulie1
LQ Newbie
 
Registered: Feb 2009
Posts: 6

Original Poster
Rep: Reputation: 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

Last edited by paulie1; 02-25-2009 at 05:16 AM.
 
Old 02-25-2009, 10:56 AM   #12
raconteur
Member
 
Registered: Dec 2007
Location: Slightly left of center
Distribution: slackware
Posts: 276
Blog Entries: 2

Rep: Reputation: 44
Quote:
Originally Posted by paulie1 View Post
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
 
Old 02-25-2009, 11:22 AM   #13
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
Quote:
Originally Posted by johnsfine View Post
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

Last edited by ta0kira; 02-25-2009 at 11:24 AM.
 
Old 02-25-2009, 01:31 PM   #14
raconteur
Member
 
Registered: Dec 2007
Location: Slightly left of center
Distribution: slackware
Posts: 276
Blog Entries: 2

Rep: Reputation: 44
Quote:
Originally Posted by ta0kira View Post
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.
 
Old 02-25-2009, 02:50 PM   #15
the_ultimate_samurai
Member
 
Registered: Jan 2006
Distribution: debian-lenny
Posts: 37

Rep: Reputation: 15
Quote:
Originally Posted by graemef View Post
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)

or as i said before cin >> hex >> works as well.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Problems with "error: invalid conversion from `const char*' to `char*' " dave.f.one Linux - Newbie 11 02-11-2015 07:17 AM
C++ atoi error: invalid conversion from `int' to `const char*' bakana Programming 2 02-24-2009 03:29 AM
invalid conversion from `char' to `const ch predatorz Programming 3 11-15-2008 01:00 PM
about C++ invalid conversion from 'const char*' to 'char' teoporta Programming 3 07-17-2007 09:24 AM
If I get invalid conversion from `const char*' to `char' what should I be lookin for? RHLinuxGUY Programming 5 03-12-2006 10:35 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:44 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration