Cin user option to variable.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Laura Wilkinson
    New Member
    • Dec 2010
    • 17

    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;

    Code:
    cout << "Sidecar (Y/N): ";
    motorbikeSideCar=getche();  
    		
    if (toupper(motorbikeSideCar) == 'Y')  
    {
    cin >> "Yes" >> endl;  
    }
    		
    else if (toupper(motorbikeSideCar) == 'N')  
    {
    cin >> "No" >> endl;  
    }
    Thanks in advance!
  • horace1
    Recognized Expert Top Contributor
    • Nov 2006
    • 1510

    #2
    you were using cin >> where it should be cout << , should it be?
    Code:
     
    cout << "Sidecar (Y/N): ";
    motorbikeSideCar=getche();  
     
    if (toupper(motorbikeSideCar) == 'Y')  
    {
    cout << "Yes" << endl;  
    }
     
    else if (toupper(motorbikeSideCar) == 'N')  
    {
    cout << "No" << endl;  
    }

    Comment

    • Laura Wilkinson
      New Member
      • Dec 2010
      • 17

      #3
      When the user inputs "Y" or "N" I need the answer to be stored in the variable "motorbikeSideC ar".
      Code:
      motorbikeSideCar=getche();
      But I also would like the answer to be displayed;
      Code:
      cout << "Yes" << endl;
      But I'm not sure if this is correct?

      Comment

      • horace1
        Recognized Expert Top Contributor
        • Nov 2006
        • 1510

        #4
        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.
        Code:
        char motorbikeSideCar;
        cout << "Sidecar (Y/N): ";
        if (cin >> motorbikeSideCar, toupper(motorbikeSideCar) == 'Y')
            cout << "Yes" << endl;
        else
            cout << "No" << endl;

        Comment

        • Laura Wilkinson
          New Member
          • Dec 2010
          • 17

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

          Code:
          void vehicleMenu()
          {
          	int myVehicle = 0;
          
          	cout << "Please Chose a Vehicle: " << endl;
          	cout << "\tOption 1: Car" << endl;
          	cout << "\tOption 2: Bus" << endl;
          	cout << "\tOption 3: Lorry" << endl;
          	cout << "\tOption 4: Motorbike" << endl;
          	cout << "\tOption 5: Quit the program" << endl;
          	cout << "Option: ";
          	cin >> myVehicle;
          
          	if (myVehicle == 1)
          		fileReader();
          }
          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!

          Comment

          • weaknessforcats
            Recognized Expert Expert
            • Mar 2007
            • 9214

            #6
            This is done several ways but the easiest way to start is to use a switch:

            Code:
            switch (choice)
            {
               case 1:
                 Car();  //do car stuff here
                 break;
               case 2: 
                 Bus();   /do bus stuff here
                 break;
            etc...
            }
            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.

            Comment

            • Laura Wilkinson
              New Member
              • Dec 2010
              • 17

              #7
              I've used cases in my main cpp file to display details already present;

              Code:
              switch (tags[i])
              		{
              			case 'C': displayC((CarC*) vehicles[i]); break;
              			case 'B': displayB((BusB*) vehicles[i]); break;
              			case 'L': displayL((LorryL*) vehicles[i]); break;
              			case 'M': displayM((MotorbikeM*) vehicles[i]); break;
              			default: cout << "error with tag " << endl;break;
               		}
              	}
              	return 0;
              }
              
              void displayC(CarC* cPtr)
              {
              	cout << "Car: " <<  cPtr->carRegistration << endl;
              	cout << "Owner: " << cPtr->carOwner << endl;
              	cout << "Address: " << cPtr->carOwnerAddress << endl;
              	cout << "Manufacturer: " << cPtr->carManufacturer << endl;
              	cout << "Model: " << cPtr->carModel << endl;
              	cout << "Number of Doors: " << cPtr->numCarDoors << endl;
              	cout << "Number of Seats: " << cPtr->numCarSeats << endl;
              	cout << "Car Type: " << cPtr->carType << endl;
              	cout << "Other Information: " << cPtr->carInformation << endl;
              }
              I'm guessing it would be similar?

              Comment

              • weaknessforcats
                Recognized Expert Expert
                • Mar 2007
                • 9214

                #8
                Yep. Looks like it to me.

                Later on you will learn how polymorphism can avoid that nasty cast you do with vehicles[i].

                Comment

                • Laura Wilkinson
                  New Member
                  • Dec 2010
                  • 17

                  #9
                  Just looked up "polymorphi sm". Thanks for the help!

                  Comment

                  Working...