473,382 Members | 1,359 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

Cin user option to variable.

Hi,

I was just wondering if you could help me. I would like the user to select "Y" or "N" as to whether their motorbike has a sidecar and then store this answer into the variable.

Could you please help me adapt this;

Expand|Select|Wrap|Line Numbers
  1.  
  2. cout << "Sidecar (Y/N): ";
  3. motorbikeSideCar=getche();  
  4.  
  5. if (toupper(motorbikeSideCar) == 'Y')  
  6. {
  7. cin >> "Yes" >> endl;  
  8. }
  9.  
  10. else if (toupper(motorbikeSideCar) == 'N')  
  11. {
  12. cin >> "No" >> endl;  
  13. }
  14.  
Thanks in advance!
May 5 '11 #1
8 2220
horace1
1,510 Expert 1GB
you were using cin >> where it should be cout << , should it be?
Expand|Select|Wrap|Line Numbers
  1.  
  2. cout << "Sidecar (Y/N): ";
  3. motorbikeSideCar=getche();  
  4.  
  5. if (toupper(motorbikeSideCar) == 'Y')  
  6. {
  7. cout << "Yes" << endl;  
  8. }
  9.  
  10. else if (toupper(motorbikeSideCar) == 'N')  
  11. {
  12. cout << "No" << endl;  
  13. }
  14.  
  15.  
May 5 '11 #2
When the user inputs "Y" or "N" I need the answer to be stored in the variable "motorbikeSideCar".
Expand|Select|Wrap|Line Numbers
  1.  
  2. motorbikeSideCar=getche();  
  3.  
  4.  
But I also would like the answer to be displayed;
Expand|Select|Wrap|Line Numbers
  1.  
  2. cout << "Yes" << endl;   
  3.  
  4.  
But I'm not sure if this is correct?
May 5 '11 #3
horace1
1,510 Expert 1GB
yes, the code in my post would work.
However, it is advised not to use getche() as it is none standard.
Also you don't need the second if, you can assume if the answer is not Y or y it is no, e.g.
Expand|Select|Wrap|Line Numbers
  1. char motorbikeSideCar;
  2. cout << "Sidecar (Y/N): ";
  3. if (cin >> motorbikeSideCar, toupper(motorbikeSideCar) == 'Y')
  4.     cout << "Yes" << endl;
  5. else
  6.     cout << "No" << endl;
May 5 '11 #4
Thank you! This is very helpful :)

I was also wondering whether you could help me with something else. The system I am making I want the user to firstly chose a vehicle they would like to enter details about.

Expand|Select|Wrap|Line Numbers
  1.  
  2. void vehicleMenu()
  3. {
  4.     int myVehicle = 0;
  5.  
  6.     cout << "Please Chose a Vehicle: " << endl;
  7.     cout << "\tOption 1: Car" << endl;
  8.     cout << "\tOption 2: Bus" << endl;
  9.     cout << "\tOption 3: Lorry" << endl;
  10.     cout << "\tOption 4: Motorbike" << endl;
  11.     cout << "\tOption 5: Quit the program" << endl;
  12.     cout << "Option: ";
  13.     cin >> myVehicle;
  14.  
  15.     if (myVehicle == 1)
  16.         fileReader();
  17. }
  18.  
I have not completed the code, but I am unsure of how to get the vehicle sections to open seperatley. For example, the user can enter details about a car, bus, motorbike or lorry, which is where the cin/cout code comes into play.

Thanks in advance!
May 5 '11 #5
weaknessforcats
9,208 Expert Mod 8TB
This is done several ways but the easiest way to start is to use a switch:

Expand|Select|Wrap|Line Numbers
  1. switch (choice)
  2. {
  3.    case 1:
  4.      Car();  //do car stuff here
  5.      break;
  6.    case 2: 
  7.      Bus();   /do bus stuff here
  8.      break;
  9. etc...
  10. }
You can use the default case to display a message that you have made an invalid choice.

All you need do is have the switch inside a loop that gets a choice on each cyle.
May 5 '11 #6
I've used cases in my main cpp file to display details already present;

Expand|Select|Wrap|Line Numbers
  1.  
  2. switch (tags[i])
  3.         {
  4.             case 'C': displayC((CarC*) vehicles[i]); break;
  5.             case 'B': displayB((BusB*) vehicles[i]); break;
  6.             case 'L': displayL((LorryL*) vehicles[i]); break;
  7.             case 'M': displayM((MotorbikeM*) vehicles[i]); break;
  8.             default: cout << "error with tag " << endl;break;
  9.          }
  10.     }
  11.     return 0;
  12. }
  13.  
  14. void displayC(CarC* cPtr)
  15. {
  16.     cout << "Car: " <<  cPtr->carRegistration << endl;
  17.     cout << "Owner: " << cPtr->carOwner << endl;
  18.     cout << "Address: " << cPtr->carOwnerAddress << endl;
  19.     cout << "Manufacturer: " << cPtr->carManufacturer << endl;
  20.     cout << "Model: " << cPtr->carModel << endl;
  21.     cout << "Number of Doors: " << cPtr->numCarDoors << endl;
  22.     cout << "Number of Seats: " << cPtr->numCarSeats << endl;
  23.     cout << "Car Type: " << cPtr->carType << endl;
  24.     cout << "Other Information: " << cPtr->carInformation << endl;
  25. }
  26.  
  27.  
I'm guessing it would be similar?
May 5 '11 #7
weaknessforcats
9,208 Expert Mod 8TB
Yep. Looks like it to me.

Later on you will learn how polymorphism can avoid that nasty cast you do with vehicles[i].
May 5 '11 #8
Just looked up "polymorphism". Thanks for the help!
May 6 '11 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: GriffithsJ | last post by:
Hi Very new to this... I would like to set up the following: The web user does a search of a database and the results are returned in an HTML table. I want to present the user with a table...
3
by: Miso Hlavac | last post by:
Hello, I need to create user defined variable in every database session. In Sybase ASA is equivalent: CREATE VARIABLE name TYPE; I need use this in views. Is it possible??? thanx, miso
3
by: Sridhar | last post by:
Hi, I am having a trivial problem which is not that important but I would like to know if I am missing something. I have created a user control. When I drag the user control into one of the web...
0
by: burgensteen | last post by:
Hi I am after adding a NO THANKS option (that does nothing, just a default select) to the top of my variable option dropdown. My code: <? $query= mysql_query("SELECT DISTINCT username FROM...
0
by: shapper | last post by:
Hello, I can I get a profile variable value, for example, city, for a certain user which is not the one that has login. I get that user by its username: Dim user As MembershipUser =...
2
by: niskin | last post by:
Hi I want to combine these variables: char computera; //this variable contains input from the user char path= "\\\\"; char location = "\\C$\\Windows\\System32\\chat.txt"; The order I want...
5
by: jjeanj1 | last post by:
I am having issue using a user defined variable i an a mysql statement. here is the issue that i am facing: i have a variable called $date in php.This variable is assigned a valued from a dropdown...
3
by: anniebai | last post by:
In myTable, there're several columns as Avg1, Avg2 ....Avg92, they all are float type. in my SQL code: declare @counter int declare @parm nvarchar(20) set @counter=0 while @counter<10
0
antonopn
by: antonopn | last post by:
Hello there! I'm really confused about this! I have created a project (DTS in SQL SERVER 2000) which imports a txt file and runs some SQL statements. I want the user who runs it each time to...
3
by: govind161986 | last post by:
I am creating a stored procedure which has multiple joins. Now the problem is I want some joins to be executed for a given condition and some other join for other condition. For example select...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.