0

Below is my code. I want my program to take in the first character of an input word and save as number1 and take the second character as number2. However I am getting an error message. "Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast"

char number1; char number2; int all; cout << "Enter romans to change to numbers. " << endl; n1 = cin.get(); n2 = cin.get(); 
5
  • Assuming you will sometime-soon realize roman numerals can easily be comprised of more than two characters (or less than two), is there a specific reason you're not using a std::string and std::getline() ? Commented Apr 26, 2013 at 3:49
  • For practice I am supposed to only compare two characters at a time and add to a running total. That is the only reason. Commented Apr 26, 2013 at 3:51
  • If those are the instructions, it does not change the appropriateness of using std::string. Just accumulate the total as you walk through the string with an iterator. Commented Apr 26, 2013 at 3:53
  • The way I'm supposed to write the code is explicitly to use two char variables and one int variable Commented Apr 26, 2013 at 3:55
  • What line is the error on? That code compiles, so it must be in the code you haven't shown us. Commented Apr 26, 2013 at 4:04

1 Answer 1

1

There is nothing wrong (syntactically) with the code you've shown, as evidenced by the following fully self-contained example:

#include <iostream> int main(){ char n1; char n2; int total; std::cout << "Enter roman numerals to change to numbers." << std::endl; n1 = std::cin.get(); n2 = std::cin.get(); return 0; } 

This both compiles and runs fine, therefore your problem must lie elsewhere.

You need to take better note of the error message your compiler is giving you, specifically the line number. Then examine the file at or around that point (and show us if you cannot figure it out).

That's the sort of error message I'd expect to see if you accidentally declared your n1 or n2 variables as char * rather than char. In that case gcc gives me something similar to what you state:

qq.cpp: In function 'int main()': qq.cpp:9: error: invalid conversion from 'int' to 'char*' qq.cpp:10: error: invalid conversion from 'int' to 'char*' 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.