program with arrays

needing to write program for price of flights

Page 1 of 1

11 Replies - 1239 Views - Last Post: 20 December 2009 - 03:07 AM Rate Topic: -----

#1 severeduly   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 12
  • Joined: 15-November 09

program with arrays

Posted 18 December 2009 - 10:58 PM

I have been working on a program to give the price of a ticket. the program should prompt the user for the number of the desitination city and number of tickets they want. program should terminate if you enter invalid city number. program should display the total price of the ticket order, using an array. any please would be great, I have been working on this for hours and can't get it to work.
#include <iostream>
#include <iomanip>

using namespace std;

int Get_Desitination_City();

int main()
{
	double rate[6] = {56.79, 105.69, 93.49, 155.99, 87.49, 73.99};
	int region;
	double price,
		   tickets,
		   total_price;

	cout << setprecision(2)
		 << setiosflags(ios::fixed)
		 << setiosflags(ios::showpoint);

	cout << "Enter the Desitination City: ";
	cin >> price;
	region = Get_Desitination_City();

	cout << "Enter number of tickets: ";
	cin >> tickets;
	tickets = Get_Desitination_City();


	charge = price * tickets [region - 1];
	total_price = price + tickets;
	cout << endl << endl;
	cout << " Item Price:	" << setw(9) << price << endl;
	cout << "tickets Charge:   " << setw(9) << charge << endl;
	cout << "Toatl Price:	" << setw(9) << total_price << endl;
	return 0;
} //End of main()

int Get_Desitination_City()
{
	bool invalid_data;
	int region;

	do
	{
		cout << endl;
		cout << "Enter desitination city (1-6):  ";
		cin >> region;
		if (region < 1 || region > 6)
		{
			cerr << endl;
			cerr << "Region in invalid - please reenter. ";
			invalid_data = true;
		}
		else
			invalid_data = false;
	}
	while(invalid_data);
	
	return region;
}




Is This A Good Question/Topic? 0
  • +

Replies To: program with arrays

#2 janotte   User is offline

  • code > sword
  • member icon

Reputation: 991
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: program with arrays

Posted 18 December 2009 - 10:59 PM

Please give us some more details of your problem.
( a ) Does your code compile?
ANSWER - No it doesn't!

( b ) Any errors or warnings? If there are then share them with us.
WAITING for you to provide these.

( c ) Is the program producing any output?
ANSWER - No it doesn't even compile

( d ) How is the actual output different to what you want / expect? Give details and, ideally, examples.
ANSWER - No output. It doesn't even compile

This post has been edited by janotte: 18 December 2009 - 11:06 PM

Was This Post Helpful? 0
  • +
  • -

#3 severeduly   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 12
  • Joined: 15-November 09

Re: program with arrays

Posted 18 December 2009 - 11:12 PM

View Postjanotte, on 18 Dec, 2009 - 09:59 PM, said:

Please give us some more details of your problem.
( a ) Does your code compile?
ANSWER - No it doesn't!

( b ) Any errors or warnings? If there are then share them with us.
WAITING for you to provide these.

( c ) Is the program producing any output?
ANSWER - No it doesn't even compile

( d ) How is the actual output different to what you want / expect? Give details and, ideally, examples.
ANSWER - No output. It doesn't even compile

Quote

A= no it dont compile
B= listed below
C= no cant compile
D= I wanted it to display the city number from user input and number of tickets they bought. I also wanted it to show a grand total but I have not got that far in the program. If user inputs an invalid city number it show show error and terminates program, I think I got that part right.
--------------------Configuration: 8A - Win32 Debug--------------------
Compiling...
lab8A.cpp
C:\CIS242\8A\lab8A.cpp(32) : error C2065: 'charge' : undeclared identifier
C:\CIS242\8A\lab8A.cpp(32) : error C2109: subscript requires array or pointer type
Error executing cl.exe.

8A.exe - 2 error(s), 0 warning(s)

This post has been edited by severeduly: 18 December 2009 - 11:31 PM

Was This Post Helpful? 0
  • +
  • -

#4 janotte   User is offline

  • code > sword
  • member icon

Reputation: 991
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: program with arrays

Posted 18 December 2009 - 11:17 PM

So here is your line 32
charge = price * tickets [region - 1];

What does this error tell you?
C:\CIS242\8A\lab8A.cpp(32) : error C2065: 'charge' : undeclared identifier
How might you fix it?

Now this error
C:\CIS242\8A\lab8A.cpp(32) : error C2109: subscript requires array or pointer type
Is "tickets" declared as an array?
No.
So what are the square brackets after it attempting to do?
Talk us through what you want that to achieve with that line of code.
Was This Post Helpful? 0
  • +
  • -

#5 severeduly   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 12
  • Joined: 15-November 09

Re: program with arrays

Posted 18 December 2009 - 11:44 PM

Quote

I understand there is an error in both lines of code but i'm not sure how to fix it. there is no inline or /inline brackets showing on my compiler. I have the price in the array, would have have to do an array for the tickets also?


this is what i want for the program output:

What is the number of your desitination city ? 3
How many tickets do you wish to purchase? 2
The cost is 186.98

Do you wish to purchase more tickets ? Y

What is the number of your desitination city ? 4
How many tickets do you wish to purchase? 1
The cost is 155.99

This post has been edited by severeduly: 18 December 2009 - 11:47 PM

Was This Post Helpful? 0
  • +
  • -

#6 janotte   User is offline

  • code > sword
  • member icon

Reputation: 991
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: program with arrays

Posted 19 December 2009 - 12:12 AM

View Postsevereduly, on 18 Dec, 2009 - 10:44 PM, said:

I understand there is an error in both lines of code but i'm not sure how to fix it.

Seriously?
Really seriously!?
You can't guess how to fix an error that is telling you a variable has not been declared?
Here's a suggestion.
Perhaps you could declare the variable?
Do you think that would help?

So to the details.
The compiler is telling you "charge" has not been declared.
Declare what variable type (int, char, string, <whatever>) you want "charge" to be.
Just like you did for "price".

Give that a try and see if that error goes away or is still there.


View Postsevereduly, on 18 Dec, 2009 - 10:44 PM, said:

there is no inline or /inline brackets showing on my compiler.

Well no there wouldn't be.
Those are things I put into my posting to change how the contents of my posting is displayed in the browser.
Try not to get distracted by irrelevant things.

View Postsevereduly, on 18 Dec, 2009 - 10:44 PM, said:

I have the price in the array, would have have to do an array for the tickets also?

Try to talk in specifics as much as possible.
Instead of just "the array" use its name.
Like this:
"I have the price in the array called rate[]".

I don't think you need an array for the tickets.
But if "tickets" is not an array then you can not use the square brackets after it as you have.

---------

Again I ask you
Talk us through what you want that to achieve with that line of code.
Don't explain what you want the program to do.
Just explain exactly what that you think this line is doing as you wrote it.

"charge = price * tickets [region - 1];"

(I've not used inline tags this time to avoid confusing you.)
Was This Post Helpful? 0
  • +
  • -

#7 severeduly   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 12
  • Joined: 15-November 09

Re: program with arrays

Posted 19 December 2009 - 05:36 AM

View Postjanotte, on 18 Dec, 2009 - 11:12 PM, said:

View Postsevereduly, on 18 Dec, 2009 - 10:44 PM, said:

I understand there is an error in both lines of code but i'm not sure how to fix it.

Seriously?
Really seriously!?
You can't guess how to fix an error that is telling you a variable has not been declared?
Here's a suggestion.
Perhaps you could declare the variable?
Do you think that would help?

So to the details.
The compiler is telling you "charge" has not been declared.
Declare what variable type (int, char, string, <whatever>) you want "charge" to be.
Just like you did for "price".

Give that a try and see if that error goes away or is still there.

#include <iostream>
#include <iomanip>

using namespace std;

int Get_Desitination_City();

int main()
{
	double rate[6] = {56.79, 105.69, 93.49, 155.99, 87.49, 73.99};

	int region,charge;
	double price,
		   tickets,
		   total_price;


	cout << setprecision(2)
		 << setiosflags(ios::fixed)
		 << setiosflags(ios::showpoint);

	cout << "Enter the Desitination City: ";
	cin >> price;
	region = Get_Desitination_City();

	cout << "Enter number of tickets: ";
	cin >> tickets;
	
	charge = price * tickets [region - 1];
	total_price = price + tickets;
	cout << endl << endl;
	cout << " Item Price:	" << setw(9) << price << endl;
	cout << "tickets Charge:   " << setw(9) << charge << endl;
	cout << "Toatl Price:	" << setw(9) << total_price << endl;
	return 0;
} //End of main()

int Get_Desitination_City()
{
	bool invalid_data;
	int region;

	do
	{
		cout << endl;
		cout << "Enter desitination city (1-6):  ";
		cin >> region;
		if (region < 1 || region > 6)
		{
			cerr << endl;
			cerr << "Region in invalid - please reenter. ";
			invalid_data = true;
		}
		else
			invalid_data = false;
	}
	while(invalid_data);
	
	return region;
}




"charge = price * tickets [region - 1];" I have a book but it does not help me to understand it very well, i have had to put together a little bit from different programs in the book. I was thinking this line should return the rate of the ticket? compiler said this:
"C:\CIS242\8A\lab8A.cpp(32) : error C2109: subscript requires array or pointer type"



View Postsevereduly, on 18 Dec, 2009 - 10:44 PM, said:

there is no inline or /inline brackets showing on my compiler.

Well no there wouldn't be.
Those are things I put into my posting to change how the contents of my posting is displayed in the browser.
Try not to get distracted by irrelevant things.

View Postsevereduly, on 18 Dec, 2009 - 10:44 PM, said:

I have the price in the array, would have have to do an array for the tickets also?

Try to talk in specifics as much as possible.
Instead of just "the array" use its name.
Like this:
"I have the price in the array called rate[]".

I don't think you need an array for the tickets.
But if "tickets" is not an array then you can not use the square brackets after it as you have.

---------

Again I ask you
Talk us through what you want that to achieve with that line of code.
Don't explain what you want the program to do.
Just explain exactly what that you think this line is doing as you wrote it.

"charge = price * tickets [region - 1];"

(I've not used inline tags this time to avoid confusing you.)

Was This Post Helpful? 0
  • +
  • -

#8 janotte   User is offline

  • code > sword
  • member icon

Reputation: 991
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: program with arrays

Posted 19 December 2009 - 06:33 AM

Please edit your posting (see the "EDIT" button in lower right).
It's unreadable as it is.
( a ) Delete everything that I wrote and just leave what you want to say.
( b ) Use the "Preview Post" button to check it's all good.
( c ) Use the "Submit Modified Post" button to finish the editing.


The stuff I say is tedious and unpleasant enough for people to have to read once.
Asking them to read it twice is unusual cruelty. :D

Unless you really want to quote what someone else said you can just use the ADDREPLY button (very top and bottom of thread).

Only use the REPLY button if you really think including something someone else said in your post is required or would in someway make your posting better.

=============
Here's the stuff that you wrote with the rubbish removed.

View Postsevereduly, on 19 Dec, 2009 - 04:36 AM, said:

#include <iostream>
#include <iomanip>

using namespace std;

int Get_Desitination_City();

int main()
{
	double rate[6] = {56.79, 105.69, 93.49, 155.99, 87.49, 73.99};

	int region,charge;
	double price,
		   tickets,
		   total_price;


	cout << setprecision(2)
		 << setiosflags(ios::fixed)
		 << setiosflags(ios::showpoint);

	cout << "Enter the Desitination City: ";
	cin >> price;
	region = Get_Desitination_City();

	cout << "Enter number of tickets: ";
	cin >> tickets;
	
	charge = price * tickets [region - 1];
	total_price = price + tickets;
	cout << endl << endl;
	cout << " Item Price:	" << setw(9) << price << endl;
	cout << "tickets Charge:   " << setw(9) << charge << endl;
	cout << "Toatl Price:	" << setw(9) << total_price << endl;
	return 0;
} //End of main()

int Get_Desitination_City()
{
	bool invalid_data;
	int region;

	do
	{
		cout << endl;
		cout << "Enter desitination city (1-6):  ";
		cin >> region;
		if (region < 1 || region > 6)
		{
			cerr << endl;
			cerr << "Region in invalid - please reenter. ";
			invalid_data = true;
		}
		else
			invalid_data = false;
	}
	while(invalid_data);
	
	return region;
}




"charge = price * tickets [region - 1];" I have a book but it does not help me to understand it very well, i have had to put together a little bit from different programs in the book. I was thinking this line should return the rate of the ticket? compiler said this:
"C:\CIS242\8A\lab8A.cpp(32) : error C2109: subscript requires array or pointer type"

This post has been edited by janotte: 19 December 2009 - 06:43 AM

Was This Post Helpful? 0
  • +
  • -

#9 janotte   User is offline

  • code > sword
  • member icon

Reputation: 991
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: program with arrays

Posted 19 December 2009 - 06:58 AM

View Postsevereduly, on 19 Dec, 2009 - 04:36 AM, said:

"charge = price * tickets [region - 1];" I have a book but it does not help me to understand it very well, i have had to put together a little bit from different programs in the book. I was thinking this line should return the rate of the ticket? compiler said this:
"C:\CIS242\8A\lab8A.cpp(32) : error C2109: subscript requires array or pointer type"


OK. You want line 32 to return the "rate of the ticket".

Explain to us what you wrote.
Like this.
"I wanted to work out the area of a rectangle. So I:
- got the length of the rectangle and stored it in a double variable "length"
- got the height of the rectangle and stored it in a double variable "height"
- then i multiplied them together and stored the result in the double variable "area"
This is my code
double area = height + length;


"
Now can you see that there is a difference between what I wanted to achieve and what I wrote in code?
I've made a mistake.
Can you tell me what my mistake is?


So apply the same approach to your code.
1 - Tell us, in words, what the formula is for working out the "rate of the ticket" is.
2 - Tell us step by step exactly what you think each part of the line 32 that you wrote does
charge = price * tickets [region - 1];



Then we can look at your code and see what the difference between what you wanted to achieve and what you actually wrote and why you thought what you wrote would work and we can explain why it doesn't.
Was This Post Helpful? 0
  • +
  • -

#10 severeduly   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 12
  • Joined: 15-November 09

Re: program with arrays

Posted 19 December 2009 - 10:27 PM

View Postjanotte, on 19 Dec, 2009 - 05:58 AM, said:

View Postsevereduly, on 19 Dec, 2009 - 04:36 AM, said:

"charge = price * tickets [region - 1];" I have a book but it does not help me to understand it very well, i have had to put together a little bit from different programs in the book. I was thinking this line should return the rate of the ticket? compiler said this:
"C:\CIS242\8A\lab8A.cpp(32) : error C2109: subscript requires array or pointer type"


OK. You want line 32 to return the "rate of the ticket".

Explain to us what you wrote.
Like this.
"I wanted to work out the area of a rectangle. So I:
- got the length of the rectangle and stored it in a double variable "length"
- got the height of the rectangle and stored it in a double variable "height"
- then i multiplied them together and stored the result in the double variable "area"
This is my code
double area = height + length;


"
Now can you see that there is a difference between what I wanted to achieve and what I wrote in code?
I've made a mistake.
Can you tell me what my mistake is?


So apply the same approach to your code.
1 - Tell us, in words, what the formula is for working out the "rate of the ticket" is.
2 - Tell us step by step exactly what you think each part of the line 32 that you wrote does
charge = price * tickets [region - 1];



Then we can look at your code and see what the difference between what you wanted to achieve and what you actually wrote and why you thought what you wrote would work and we can explain why it doesn't.

Quote

I was trying to get it to charge for number of tickets and the price to the desitnation city, say you pick city 1 and want two tickets for that city. I put the code together from the book but only bits and pieces of codes from the book to make this program, I really have had a hard time with understanding it. I want to learn it but the light bulb is not comming on as of now.

charge = price * tickets [region - 1];


Was This Post Helpful? 0
  • +
  • -

#11 janotte   User is offline

  • code > sword
  • member icon

Reputation: 991
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: program with arrays

Posted 20 December 2009 - 01:45 AM

OK I officially give up.

I have asked you over and over again to explain that line of code.
I have given you examples of what "explaining" means.
You simply can't provide a meaningful explanation.

Does this waffling twaddle from you:

"I was trying to get it to charge for number of tickets and the price to the desitnation city, say you pick city 1 and want two tickets for that city."

Look anything at all like the example I asked you to follow?

"I wanted to work out the area of a rectangle. So I:
- got the length of the rectangle and stored it in a double variable "length"
- got the height of the rectangle and stored it in a double variable "height"
- then i multiplied them together and stored the result in the double variable "area""

I don't know if your English skills are too limited (but the use of idioms like the lightbulb one tell me that you have more than enough English to follow direct instructions) or perhaps there is something else wrong in our dialog but it's going nowhere.

Unless you can follow my repeated requests and example I have nothing more to offer you.

Anyone else want to take up the challenge?

Please come in and help this person because I can't keep bashing my head into this particular brick wall anymore.
Was This Post Helpful? 0
  • +
  • -

#12 severeduly   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 12
  • Joined: 15-November 09

Re: program with arrays

Posted 20 December 2009 - 03:07 AM

Quote

I was looking for some help to understanding this program, I did not ask for you to do the code, so why be RUDE with your words. I would guess you have C++ mastered and could write this program in a short time, but for someone like me, programming is hard and I don't fully understand some of the codes or why it does it. The code is from the book, bits and pieces I have put together to try to get program to work, the program "desitnation city and ticket price" is from my teacher.
I thought why the code was wrote was ok, but I guess I was not clear enough for you, so I'll explain in great detail without being RUDE.

this is the teachers list for program:
1. user picks a desitnation city 1-6, which i think code is right.
1. array for destitnation city with price of the ticket(s), which i have and think it's right.
3. number of tickets for that destitnation city user picked, which i think works right.
4. program is to be used with an array.
5. if user inputs the wrong code for the destitnation city, the program would error.

so this line of code is from the book but not really for this program so it could be wrong, i'm not sure.
I have this line of code in program to charge for destitnation city, thats what the "charge" is for.
price was for the price of the ticket, that's in the array.
then * by the number of tickets the user wanted for the destitnation city.
the region is for the array, for the program to get the price, or should that be something different?
with this line of code I wanted it to total the price of tickets and destitnation city user picks, such as, if user picked city 6 and wants 2 tickets for that city, so you would have $73.99 x2= $147.98, if you look at the array you see that.

i set up the int region, charge; should I change that to something different?



charge = price * tickets [region - 1];
 

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1