Type Issue?

Hi there,

I am new (hence beginners). I am having issues with a program I am writing. Program: I have multiple questions to answer before getting to some calculations related to proposed ticket prices. I want to answer some questions with y/n (representing yes/no) responses and have found that I need to set type to char for these inputs. I also want to ask some questions that have integer or double type responses. While running the program, after entering top two char questions the program prints rest of questions without asking for inputs. Help! Losing sleep. ;)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47

//Purpose of this program: is to provide a way to calculate the fare of an //individual vehicle using the Anacortes to Friday Harbor Ferry.

#include <iostream>
#include <iomanip>
#include <cmath>

using namespace std;

int main()
{
	//declare variables

	int adultsMinusDriver = 0, youthsMinusDriver = 0, bikeRiders = 0, totalAdults = 0;
	int driverAge = 0, seniors = 0, n = 0, y = 0, response = 0, vehicleHeight = 0;
	double vehicleLength = 0, totalFare = 0,seniorsMinusDriver = 0, vehicleDriverFare = 0; 
	
	
	cout << "Welcome to Oliver's fare calculator! ";
	//assuming driver is an adult?  Can't someone between 16-18 drive onto the fairy?
	cout << "Are you driving a vehicle onto the ferry? (y/n) :  ";
	cin >> response;
	cout << endl;
	cout << "Is the driver a senior citizen (65 or older), disabled? (y/n) :  ";
	cin >> driverAge;
	cout << endl;
	cout << "How many passengers are in your vehicle? (excluding the driver)  ";
	cout << endl;
	cout << "Adults (age 19 - 64) :  ";
	cin >> adultsMinusDriver;
	cout << endl;
	cout << "Senior Citenzens (65 or older), or Disabled Persons:  ";
	cin >> seniorsMinusDriver;
	cout<< endl;
	cout << "Youth (age 5 - 18) :  ";
	cin >> youthsMinusDriver;
	cout << endl;
	cout << "Is your vehicle over 7 feet, 6 inches in height? (y/n)  ";
	cin >> vehicleHeight;
	cout << endl;
	cout << "How long is your vehicle in feet: ";
	cin >> vehicleLength;
	cout << endl;
	cout << "How many people in your group are traveling with a bicycle?  ";
	cin >> bikeRiders;
	cout << endl;


For starts you're missing the cin for:
 
cout << "How many passengers are in your vehicle? (excluding the driver)  ";


I also imagine this is meant to be two questions.
 
cout << "Is the driver a senior citizen (65 or older), disabled? (y/n) :  ";
Last edited on
Huh, where is your logic. also, when asking for y or n, you should preferably use a char, or a static_cast will be needed to convert the int to a char.
I need to set type to char for these inputs

So just do it :D

1
2
3
4
int adultsMinusDriver = 0, youthsMinusDriver = 0, bikeRiders = 0,
    totalAdults = 0, seniors = 0, n = 0, y = 0;
double vehicleLength = 0, totalFare = 0, seniorsMinusDriver = 0, vehicleDriverFare = 0;
char response = 0, driverAge = 0, vehicleHeight = 0;


program prints rest of questions without asking for inputs

Your program will now work, but only if user follow the rules and doesn't enter e. g. "bannana" as answer to "How long is your vehicle in feet".
hold on, a char cannot equal 0
printf("%d", '\0' == 0);
1

? :-D
Last edited on
wow
yea 0 is an
int
char = 'Y'
don't forget ' ' around your characters
Billy
My intention on the "how many passengers in vehicle" was to follow with allowing user to input #seniors, #adults, #youth separately.

The intention with the "seniors or disabled" together is that they cost the same price for admittance. Sorry, you wouldn't of seen this, I didn't post entire program.
Thanks!

Runner, I added char now. Thank you!

naaissus, are you suggesting I turn all variables to char, or just y/n questions? Thanks!

I seem to have it working though. Thank you all. Used the following:

int main()
{
//declare variables

int adultsMinusDriver = 0, youthsMinusDriver = 0, bikeRiders = 0, totalAdults = 0;
int driverAge = 0, seniors = 0, n = 0, y = 0;
double vehicleLength = 0, totalFare = 0,seniorsMinusDriver = 0, vehicleDriverFare = 0;
char response = 0, driverInfo = 0, vehicleHeight = 0;

cout << "Welcome to Oliver's fare calculator! " << endl;
//assuming driver is an adult? Can't someone between 16-18 drive onto the fairy?
cout << "Are you driving a vehicle onto the ferry? (y/n) : ";
cin >> response;
cout << endl;
cout << "Is the driver a senior citizen (65 or older), disabled? (y/n) : ";
cin >> driverInfo;
cout << endl;
cout << "How many passengers are in your vehicle? (excluding the driver) ";
cout << endl;
cout << "Adults (age 19 - 64) : ";
cin >> adultsMinusDriver;
cout << endl;
cout << "Senior Citenzens (65 or older), or Disabled Persons: ";
cin >> seniorsMinusDriver;
cout<< endl;
cout << "Youth (age 5 - 18) : ";
cin >> youthsMinusDriver;
cout << endl;
cout << "Is your vehicle over 7 feet, 6 inches in height? (y/n) ";
cin >> vehicleHeight;
cout << endl;
cout << "How long is your vehicle in feet: ";
cin >> vehicleLength;
cout << endl;
cout << "How many people in your group are traveling with a bicycle? ";
cin >> bikeRiders;
cout << endl;

return 0;
}

hold on, a char cannot equal 0

Then how do we set the end marker for a c-string?
that's null termination '\0', it's in quotes
naaissus, are you suggesting I turn all variables to char, or just y/n questions? Thanks!

Just y/n questions of course.
so do it and show your code
Also,

If I would like the program to exit if n is entered for one of the y/n questions how would I make this happen? I tried exit(0); at the end of this code but this doesn't work. I also tried to place this statement just under the: cout << "Are you driving a vehicle onto the ferry? (y/n) : ";
cin >> response;

It doesn't work. hmmmmm.

Thanks



#include <iostream>
#include <iomanip>
#include <cmath>

using namespace std;

int main()
{
//declare variables

int adultsMinusDriver = 0, youthsMinusDriver = 0, bikeRiders = 0, totalAdults = 0;
int driverAge = 0, seniors = 0, y = 0, n = 0;
double vehicleLength = 0, totalFare = 0,seniorsMinusDriver = 0, vehicleDriverFare = 0;
char response = 0, driverInfo = 0, vehicleHeight = 0;

//Begin user input.

cout << "Welcome to Oliver's fare calculator! " << endl;
//assuming driver is an adult? Can't someone between 16-18 drive onto the fairy?
cout << "Are you driving a vehicle onto the ferry? (y/n) : ";
cin >> response;
cout << "Is the driver a senior citizen (65 or older), disabled? (y/n) : ";
cin >> driverInfo;
cout << endl;
cout << "How many passengers are in your vehicle? (excluding the driver) ";
cout << endl;
cout << "Adults (age 19 - 64) : ";
cin >> adultsMinusDriver;
cout << "Senior Citenzens (65 or older), or Disabled Persons: ";
cin >> seniorsMinusDriver;
cout << "Youth (age 5 - 18) : ";
cin >> youthsMinusDriver;
cout << "Is your vehicle over 7 feet, 6 inches in height? (y/n) ";
cin >> vehicleHeight;
cout << endl;
cout << "How long is your vehicle in feet: ";
cin >> vehicleLength;
cout << endl;
cout << "How many people in your group are traveling with a bicycle? ";
cin >> bikeRiders;
cout << endl;

if ( response = n )
exit(0);
else
cout << endl << endl;


return 0;
}

Or am I a lost cause? ;)
if ( response = n )
First, the '=' operator is used to assign a value. Here you need the '==' operator to test for equality.

Second, you need the character literal 'n' rather than the bare n on its own which would mean something quite different.


By the way, you can help us to help you by posting your code inside code tags.
[code]your code here[/code]
See link: http://www.cplusplus.com/articles/jEywvCM9/
Perfect. Thank you! I will do the code tags from now on.

Just as a side note. I am now in love with this forum. Thanks to all the people that participate. Maybe I could do the same someday (if I get good enough). For now I will soak up the help. Cheers.
Topic archived. No new replies allowed.