***C++*** I am writing a program that will allow the user to repackage their liq
ID: 3708059 • Letter: #
Question
***C++***
I am writing a program that will allow the user to repackage their liquid product by calculating the size of the item in order to maintain the same volume. I continue to get the errors "c++ forbids comparison between pointer and integer" or "warning: multi-character constant." I realize this is because I have declared repack_method as a char but the string function is not working (even when I change to double quotes and declare strings, that is when I encounter the pointer/integer error). I suspect I need to use the cctype library but am unsure how. How do I fix this?
#include #include #include #include #include using namespace std; int main () //declare variables float ounces fluid; char repack method; char cube method [5]-'c', 'u', 'b','e', '10' char can method [4] -'C', 'a', 'n', '10'y char ballmethod [5]= {'b', 'a', '1','1',''); - coutExplanation / Answer
Step 1: Change the declarations as follows
char repack_method[5];
char cube_method[5] = "cube";
char can_method[4] = "can";
char ball_method[5] = "ball";
Step 2: change the if() block as
if(strcmp(repack_method, cube_method) != 0 && strcmp(repack_method, can_method) != 0 && strcmp(repack_method, ball_method) != 0 )
{
//..... statemtn to show error message..... and input value again
}
It should now work for you. You are trying to compare "cube" against user input. Since you are entering the full word and not just one character for repack_method, we hould have it as an array of char. Also since you are comparing strings, you should use double quotes "...." and also strcmp() function from <cstring> library for string comparisons.
Hope it helps. Please do rate if it helped. Thank you.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.