English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

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<<" Type.....";
cin>> 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

2007-07-10 07:18:22 · 6 answers · asked by Anonymous in Computers & Internet Programming & Design

Is it that hard, or not possible

2007-07-10 07:23:13 · update #1

The Swith Statement still leaves the C, T, or S there

2007-07-10 07:27:16 · update #2

6 answers

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<<"Type....."; (assuming this means u want them to type in something)
cin>>type; (assuming you've declared type already)

if(type == 'c' || type == 'C') {
cout>>"Car";
else if (type == "t" || type == "T")
cout>>"Truck";
else if (type == "s" || type == "S")
cout>>"Senior";
}//end if

of course if you want it to say more than just car, or truck...you would say something like cout>>"The type is car" or something....hope that helps...if not, just clarify a bit more and I'll try to help

2007-07-10 07:26:34 · answer #1 · answered by aguynamedgeoff 1 · 1 0

> 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.

2007-07-10 14:32:05 · answer #2 · answered by csanon 6 · 0 0

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.

2007-07-10 14:29:53 · answer #3 · answered by tkquestion 7 · 1 0

I think I found the answer. At the top of the file, type:
#include

And then, if the variable you store the character in is called "input", instead of:
cin >> 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.

2007-07-10 18:27:51 · answer #4 · answered by mblaine 5 · 0 0

Sure, try this:

cin >> type;

switch (type) {
case 'C' : cout << "Car" << endl; break;
case 'T' : cout << "Truck" << endl; break;
case 'S' : cout << "Senior" << endl; break;
}

Switch statements are very handy for single-character input.

2007-07-10 14:25:31 · answer #5 · answered by Justin B 4 · 1 0

when you find the answer, let me know...

2007-07-10 14:22:46 · answer #6 · answered by Its Me! 3 · 0 3

fedest.com, questions and answers