Thursday, July 9, 2009

C++ Kicking my butt?

Ok I have a Car, Truck and Senior. I have to input C for Car, T for Truck, S for Senior. Our instructor wants us to input the the One letter but show the whole word.





So I did


cout%26lt;%26lt;" Type.....";


cin%26gt;%26gt; type ;





From here I punch in C, and only C shows up. How can i punch in C, but have Car show up instead??? Is that possible





Someone suggest the switch statement, but it didn't work plus that is a little to advanced for where are class is now. All I can get is inputting C, just the letter C comes out. I did something with an if statement and Cars came out on the next line.





I want to punch in C but when I hit enter I don't want it to be there, just the word Car, Truck or Senior. Any help

C++ Kicking my butt?
I'm not exactly sure if I understand what your problem is...but based on what I think it is this is what I would do...





cout%26lt;%26lt;"Type....."; (assuming this means u want them to type in something)


cin%26gt;%26gt;type; (assuming you've declared type already)





if(type == 'c' || type == 'C') {


cout%26gt;%26gt;"Car";


else if (type == "t" || type == "T")


cout%26gt;%26gt;"Truck";


else if (type == "s" || type == "S")


cout%26gt;%26gt;"Senior";


}//end if





of course if you want it to say more than just car, or truck...you would say something like cout%26gt;%26gt;"The type is car" or something....hope that helps...if not, just clarify a bit more and I'll try to help
Reply:What you are dealing with, is a translation of a character to something else. Basically...





1) get the letter input from keyboard


2) if what you get = 'c' then print "car"


3) if what you get = 't' then print "truck"


4) if what you get is anything else, print "error"





Obviously, you have to do all these in C syntax. If you don't want to use the case statement, use if statement.
Reply:when you find the answer, let me know...
Reply:%26gt; From here I punch in C, and only C shows up. How can i punch in C, but have Car show up instead???


You mean to say, you want the letter C to be **replaced** by Cars? Not going to happen. Either you’ll need OS specific stuff like ncurses, or OS specific stuff in GUIs. Either way, I suggest you drop what you are trying to do.





All you need to do is basically have it output the appropriate word for the letter (Cars for C, Truck for T, etc.). If statements are one way, but a switch statement may be more appropriate. A lookup table may be useful, especially with something like std::map.
Reply:I think I found the answer. At the top of the file, type:


#include %26lt;conio.h%26gt;





And then, if the variable you store the character in is called "input", instead of:


cin %26gt;%26gt; input;


Do this:


input = getch();





This reads the character as soon as its typed, and doesn't wait for the user to press [ENTER], moving you onto the next line.
Reply:Sure, try this:





cin %26gt;%26gt; type;





switch (type) {


case 'C' : cout %26lt;%26lt; "Car" %26lt;%26lt; endl; break;


case 'T' : cout %26lt;%26lt; "Truck" %26lt;%26lt; endl; break;


case 'S' : cout %26lt;%26lt; "Senior" %26lt;%26lt; endl; break;


}





Switch statements are very handy for single-character input.


No comments:

Post a Comment