Friday, July 31, 2009

C++ help!!! i am getting these errors and don't know how to fix them - first time working with C++?

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


#include %26lt;iostream%26gt;


#include %26lt;string%26gt;


#include %26lt;iomanip%26gt;


#include %26lt;cctype%26gt;





using namespace std;





int main()


{


//declare variables


string itemNum = "";


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( 'B' == toupper(itemNum[2])


{


cout %26lt;%26lt; "Your color is Blue" %26lt;%26lt;endl;





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


{


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





else


cout %26lt;%26lt; "Invalid item!" %26lt;%26lt;endl;


}


}


}

C++ help!!! i am getting these errors and don't know how to fix them - first time working with C++?
You are missing right parenthesis. you have


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





if you take notice, you have two left parenthesis in that line, but only one right one. you need to have a left and right parenthesis around itemNum[2], like you do.... but you need to have parenthesis around the entire 'if' conditional. you have the one on the left, but your forgot the one on the right. In C++, anytime you have a left bracket or parenthesis, you must have a matching right bracket or parenthesis.


so make the line:


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





You do this another location as well. Change that line too.





You are putting curly braces at the wrong places.





You have:





if(conditional)


code


else if(conditional


{


code


else if( conditional)


{


code


else


code


}


}


}





It should be:


if(conditional)


{


code


}


else if(conditional)


{


code


}


else if(conditional)


{


code


}


else


{


code


}





you see the difference? you are putting them at the end of all the conditionals. The curly brackets are used to separate blocks of code. They say if this condition is met, carry out all of the code between these brackets. The brackets are also why you are getting an error stating you have an else without a matching if statement. If you look at how the brackets are placed you have blocks of code that have 'else' statements without an 'if' statement before it in that block of code. So change the placement of the curly brackets and those problems should go away.





Take note, if you are only doing one statement after an 'if' or 'else if', then you do not need curly brackets. if you have more than one statement that needs to be executed if a condition is met, then you need curly brackets. I often put them in even if I only have one statement after an 'if' conditional because it keeps the code clean and it is easy to add more lines in the future if I discover i need to do so.
Reply:#include %26lt;iostream%26gt;


#include %26lt;string%26gt;


#include %26lt;iomanip%26gt;


#include %26lt;cctype%26gt;





using namespace std;





int main()


{


//declare variables


string itemNum = "";


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( 'B' == toupper(itemNum[2])) %26lt;%26lt;- Added ')'


{


cout %26lt;%26lt; "Your color is Blue" %26lt;%26lt;endl;


} %26lt;%26lt;- Added '}'


else if ('G' == toupper(itemNum[2])) %26lt;%26lt;- Added ')'


{


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


} %26lt;%26lt;- Added '}'


else


cout %26lt;%26lt; "Invalid item!" %26lt;%26lt;endl;


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


cin %26gt;%26gt; itemNum;


} // Removed the extra }} and moved input within loop


return 0;


} //end of main function





This code should work now.
Reply:Both lines that begin with "else if" need a ) at the end. This is, by the way, explicitly stated in the error message you provided.





You are also missing closing braces after each of your else blocks.
Reply:find another profession, programming doesn't seem to be your strong skill..


No comments:

Post a Comment