Monday, May 24, 2010

C++ help please!! i don't that much about it - i am a beginner!!?

ok, the user types item number - the program displays the color of the item. like: 12b45 or abg44. the 3 character determines the name of the color. which in this instance will be: blue and green. THIS IS MY CODE:


//Ch12AppE03.cpp


//display color program


//Created/revised by Armando on 04/28/2008





#include %26lt;iostream%26gt;


#include %26lt;string%26gt;


#include %26lt;iomanip%26gt;





using std::cout;


using std::cin;


using std::endl;


using std::string;


using std::setprecision;


using std::fixed;








int main()


{


//declare variables


string itemNum = 0;


int location = 0;





//get input from user


cout %26lt;%26lt; "Enter an item number: " %26lt;%26lt; endl;


cin %26gt;%26gt; itemNum;





//calculate shipping charge


while (itemNum != "x" %26amp;%26amp; itemNum != "X")


{


if (itemNum.length() != 5)


cout %26lt;%26lt; "The item number must contain five digits! " %26lt;%26lt; endl;





else if transform(itemNum.begin(), itemNum.end(), itemNum.begin(), toupper);


{

C++ help please!! i don't that much about it - i am a beginner!!?
you have some logic errors in here, that I am going going to touch base on right now. I will let you firgure them out. But I will point out some other things.


1) towards the top, you may want to replace all of the "using std::xxxx;" statements you have with the one statement "using namespace std;" It is simpler to write that one statement than many of the using std:: statements and it solves one of the other problems you had regarding std::transform.





2 string itemNum = 0; will cause an error. Make it string itemNum = "";





3) you have:


else if transform(itemNum.begin(), itemNum.end(), itemNum.begin(), toupper);





is wrong because you need parenthesis after the "if" and at the end of the conditional. Also, you do not put a semi-colon there. You will not get the results you want if you do. Also, if you want to have multiple statements executed if that conditional is true, you need to use curly-brackets to group those blocks of code.





if(conditional)


{


statement;


statement


}


else if(conditional)


{


statement;


statement;


}





4) don't use transform. To get the tranformation you want, you would have to include two more libraries and change your transform to transform(itemNum.begin(), itemNum.end(), itemNum.begin(), (int(*)(int)) toupper)





instead (if the 3rd char is the letter you want) put #include %26lt;cctype%26gt; at the top and use the toupper(char c) function.


so in each if put:


if( 'B' == toupper(itemNum[2])


cout %26lt;%26lt; "The color is blue" %26lt;%26lt; endl;


else if ('G' == toupper(itemNum[2])


cout %26lt;%26lt; "the color is green" %26lt;%26lt; endl;





There are still logic problems in your code the way it is now... but if you make changes and get it to compile, then you will see them and be able to correct them.
Reply:you forgot an open parenthesis on the line:





else if transform(itemNum.begin(), itemNum.end(), itemNum.begin(), toupper);





right before transform.
Reply:Thank you for a nice tasty morsel to chew on. Since this is for school, I think a progress report rather than a solution is in order. Your compiler says:





Error1error C2061: syntax error : identifier 'transform





Error 2error C2181: illegal else without matching if





I frankly assume this means you are working on an IDE. If so look at the code window and the line with the first reported error should be illuminated. To add to the hint, since I'm working on GCC from the command line, my first set of errors from the unedited code are:





Ch12App03.cpp: In function ‘int main()’:


Ch12App03.cpp:33: error: expected `(' before ‘transform’


Ch12App03.cpp:38: error: expected primary-expression before ‘else’


Ch12App03.cpp:38: error: expected `;' before ‘else’


Ch12App03.cpp:41: error: expected primary-expression before ‘else’


Ch12App03.cpp:41: error: expected `;' before ‘else’





Okay. What it's looking for is the connection to the if. You and I know it's there. In my document it's about 30. When I go back up and look at it I notice something: for most of the ifs and else lines you have a colon in the end. No.





The lines:





for (i=0;i%26lt;40;i+=2);


and


if (i==16); do nothing. That is because the colon ends the statement. Everything that happens after is outside the scope of those two statements, and whatever you wanted to happen while i was an even number between -1 and 40 will happen once when i==40, while if i==16 the computer will do what you wanted it to -- but if i!=16 the computer will do the same stupid thing because the new commands are outside the scope of the if statement. That's what the illegal else without matching if statement means. DO NOT put a colon at the end of ANY test statement. Put them at the end of statements to be executed in test statements but DO NOT PUT THEM at the end of any test statement.





The syntax error for transform() is gnarlier. In essence you are warned 1. that the parentheses are very problematic, and 2, when I got them sorted out the compiler complained thus:





Ch12App03.cpp:42: error: could not convert ‘std::transform [with _InputIterator = __gnu_cxx::__normal_iterator%26lt;char*, std::basic_string%26lt;char, std::char_traits%26lt;char%26gt;, std::allocator%26lt;char%26gt; %26gt; %26gt;, _OutputIterator = __gnu_cxx::__normal_iterator%26lt;char*, std::basic_string%26lt;char, std::char_traits%26lt;char%26gt;, std::allocator%26lt;char%26gt; %26gt; %26gt;, _UnaryOperation = int (*)(int)throw ()](itemNum.std::basic_string%26lt;_CharT, _Traits, _Alloc%26gt;::begin [with _CharT = char, _Traits = std::char_traits%26lt;char%26gt;, _Alloc = std::allocator%26lt;char%26gt;](), itemNum.std::basic_string%26lt;_CharT, _Traits, _Alloc%26gt;::end [with _CharT = char, _Traits = std::char_traits%26lt;char%26gt;, _Alloc = std::allocator%26lt;char%26gt;](), itemNum.std::basic_string%26lt;_CharT, _Traits, _Alloc%26gt;::begin [with _CharT = char, _Traits = std::char_traits%26lt;char%26gt;, _Alloc = std::allocator%26lt;char%26gt;](), toupper)’ to ‘bool’








In other words, it doesn't like me saying it exists or it doesn't. Good luck.


No comments:

Post a Comment