Trouble With Arrays and For Loops

Generating and Displaying an Array

  • (2 Pages)
  • +
  • 1
  • 2

16 Replies - 1111 Views - Last Post: 12 December 2008 - 03:49 PM Rate Topic: -----

#1 jasimare01  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 28
  • Joined: 11-December 08

Trouble With Arrays and For Loops

Post icon  Posted 11 December 2008 - 08:11 PM

I am working on my last assignment for the year and I am completely stuck.

I am not sure what I am doing wrong, at all. I am going to attach the assingment.

and here is my code

#include "stdafx.h"
#include <string>
#include <cmath>
#include <iomanip>
#include <cstdlib>


using namespace std;

void  HeaderDisplay (); 	//function prototype
void	  DisplayFee();	//function prototype
double  WeightZone [6] [3];	// Array that containing the costs
char	  WghtRng [6][12] = {	"  0 to 5", 
	" >5 to 10  ",
	">10 to 15  ",
	">15 to 20  ",
	">20 to 25  ",
	">25"};	// weight labels
const double BasicOvrChg = 2.27;	// the basic charge for all deliveries

int _tmain(int argc, _TCHAR* argv[])
{
HeaderDisplay(); // call function to generate the headers
	
	return 0; //terminate successfully

} // main

/********************************************************************	*/
/*	 Prints out the Header information						*/
/*******************************************************************	*/

void HeaderDisplay ()
{
system("cls");
cout << "						Excellent Parcel Service Company" << endl;
cout << "						Package Fee Calculation" << endl;
cout << endl;



} // HeaderDisplay

/*********************************************************************	*/
/*  Calculates the various elements of the table		*/
/*********************************************************************	*/

void DisplayFee ()
{
	  cout << “Weight Range,” “Zone 1,” “Zone 2,” and “Zone 3.”

	int lbs, zone; // descriptive counters for the for loop
	
	cout << setw(32) << "Zones";
	cout << setw(10) << "Zone 1";
	cout << setw(10) << "Zone 2";
	cout << setw(10) << "Zone 3";
	cout << endl << endl;
	
	for ( lbs = 0; lbs < 6; lbs = lbs + 1)
	{
		cout << setw(32) << WghtRng[lbs];
		for ( zone = 0; zone < 3; zone = zone + 1 )
		{
			WeightZone[6][3] =(BasicOvrChg+2*lbs)(1.62*zone);
			cout << setw(10) << WeightZone[lbs][zone];
		} // inner for loop

	 } // outer for loop

} // DisplayFee



I just do not have a clue what I am missing, and I am so frustrated that I am at the point of ripping my hair out.

Any assistance would be more appreciated that you could possibly know.

Thank you in advance and I apologize for this being my first post, I just didn't know what else to do.

Attached File(s)


This post has been edited by jasimare01: 12 December 2008 - 12:50 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Trouble With Arrays and For Loops

#2 bodom658  Icon User is offline

  • Villiage Idiom
  • member icon

Reputation: 113
  • View blog
  • Posts: 1,123
  • Joined: 22-February 08

Re: Trouble With Arrays and For Loops

Posted 11 December 2008 - 08:16 PM

this line:

WeightZone[lbs][zone] = [BasicOvrChg+2*lbs][1.62*zone]

is not valid code.

First off, no semicolon. Second off, you tried to use the access operator[] without naming an array or vector or nething else to access from. You either forgot to name it or you meant to use ()'s.

Hopefully that clears it up.
Cheers,

Bodom

This post has been edited by bodom658: 11 December 2008 - 08:18 PM

Was This Post Helpful? 0
  • +
  • -

#3 janotte  Icon User is offline

  • code > sword
  • member icon

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

Re: Trouble With Arrays and For Loops

Posted 11 December 2008 - 08:25 PM

Please describe any compiler errors or warnings you are getting or describe what is wrong with the program outcome / output.

Help us help you.
Was This Post Helpful? 0
  • +
  • -

#4 jasimare01  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 28
  • Joined: 11-December 08

Re: Trouble With Arrays and For Loops

Posted 11 December 2008 - 08:35 PM

View Postjanotte, on 11 Dec, 2008 - 07:25 PM, said:

Please describe any compiler errors or warnings you are getting or describe what is wrong with the program outcome / output.

Help us help you.


These are the errors that I am getting from my compiler :


(38) : error C2065: 'cout' : undeclared identifier
(39) : error C2065: 'cout' : undeclared identifier
(40) : error C2065: 'cout' : undeclared identifier
(40) : error C2563: mismatch in formal parameter list
(40) : error C2568: '<<' : unable to resolve function overload

c:\program files\microsoft visual studio 9.0\vc\include\ostream(974): could be 'std::basic_ostream<_Elem,_Traits> &std::endl(std::basic_ostream<_Elem,_Traits> &)'
1> with
1> [
1> _Elem=wchar_t,
1> _Traits=std::char_traits<wchar_t>
1> ]
c:\program files\microsoft visual studio 9.0\vc\include\ostream(966): or 'std::basic_ostream<_Elem,_Traits> &std::endl(std::basic_ostream<_Elem,_Traits> &)'
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]
1> c:\program files\microsoft visual studio 9.0\vc\include\ostream(940): or 'std::basic_ostream<_Elem,_Traits> &std::endl(std::basic_ostream<_Elem,_Traits> &)'
(52) : error C2065: 'cout' : undeclared identifier
(56) : error C2065: 'cout' : undeclared identifier
(57) : error C2065: 'cout' : undeclared identifier
(58) : error C2065: 'cout' : undeclared identifier
(59) : error C2065: 'cout' : undeclared identifier
(60) : error C2065: 'cout' : undeclared identifier
(60) : error C2563: mismatch in formal parameter list
60) : error C2568: '<<' : unable to resolve function overload
(64) : error C2065: 'cout' : undeclared identifier
(67) : error C2064: term does not evaluate to a function taking 1 arguments
(68) : error C2065: 'cout' : undeclared identifier

Thank you so much, words do not exist in the english language for how thankful I am for the help.
Was This Post Helpful? 0
  • +
  • -

#5 janotte  Icon User is offline

  • code > sword
  • member icon

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

Re: Trouble With Arrays and For Loops

Posted 11 December 2008 - 08:43 PM

For all these 'cout' errors try adding
#include <iostream>
at the head of the file
(38) : error C2065: 'cout' : undeclared identifier

Try that and report back with the errors / warnings remaining.

This post has been edited by janotte: 11 December 2008 - 08:44 PM

Was This Post Helpful? 1
  • +
  • -

#6 jasimare01  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 28
  • Joined: 11-December 08

Re: Trouble With Arrays and For Loops

Posted 11 December 2008 - 08:49 PM

View Postjanotte, on 11 Dec, 2008 - 07:43 PM, said:

For all these 'cout' errors try adding
#include <iostream>
at the head of the file
(38) : error C2065: 'cout' : undeclared identifier

Try that and report back with the errors / warnings remaining.



Oh my goodness, you are my hero.

I only have one error now.

(68) : error C2064: term does not evaluate to a function taking 1 arguments

I don't know how to fix it, but I am thrilled and completely amazed that all of the other errors vanished. :D

I don't suppose you could assist with that last error?

Thank you soooo much, you saved me.
Was This Post Helpful? 0
  • +
  • -

#7 janotte  Icon User is offline

  • code > sword
  • member icon

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

Re: Trouble With Arrays and For Loops

Posted 11 December 2008 - 09:00 PM

Like the old Cornflakes ad used to say "Sometimes the simple things are the best".

Glad to be helpful.

I'm pretty sure your last error is this line:
system("cls");


Am I right?

This is going to be harder for me because I'm not on Windows.

My first guess is to try removing the quotes leaving:
system(cls);


But it's a big guess.

What IDE are you using?
We really need someone who knows that IDE on Windows to help us now.
Was This Post Helpful? 0
  • +
  • -

#8 janotte  Icon User is offline

  • code > sword
  • member icon

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

Re: Trouble With Arrays and For Loops

Posted 11 December 2008 - 09:06 PM

Or is this the bad line?
for ( zone = 0; zone < 3; zone = zone + 1 )



That should really be:
for ( zone = 0; zone < 3; zone++ )


even if it the compiler isn't complaining.
Was This Post Helpful? 0
  • +
  • -

#9 jasimare01  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 28
  • Joined: 11-December 08

Re: Trouble With Arrays and For Loops

Posted 11 December 2008 - 09:07 PM

View Postjanotte, on 11 Dec, 2008 - 08:00 PM, said:

Like the old Cornflakes ad used to say "Sometimes the simple things are the best".

Glad to be helpful.

I'm pretty sure your last error is this line:
system("cls");


Am I right?

This is going to be harder for me because I'm not on Windows.

My first guess is to try removing the quotes leaving:
system(cls);


But it's a big guess.

What IDE are you using?
We really need someone who knows that IDE on Windows to help us now.



Microsoft Visual C++ 2008 Express Edition

The corresponding number for the error line of code that the compiler is returning is
WeightZone[6][3] =(BasicOvrChg)+(2*lbs)(1.62*zone);



You have been so very helpful. Thank you so very much!! I don't think I will be able to thank you enough for the help that you have provided.
Was This Post Helpful? 0
  • +
  • -

#10 janotte  Icon User is offline

  • code > sword
  • member icon

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

Re: Trouble With Arrays and For Loops

Posted 11 December 2008 - 09:15 PM

Ah ha - this makes more sense than my wild guesses.

The corresponding number for the error line of code that the compiler is returning is
WeightZone[6][3] =(BasicOvrChg)+(2*lbs)(1.62*zone);


OK the problem is here:
(2*lbs)(1.62*zone)

did you mean to have a +, -, * or / between those two parens?

You are not allowed to just but two parens up against one another like that.

If you can't see the answer try telling us what that formula is supposed to be in 'standard maths' script or in words and we can try and make the code match what you need it to be.
Was This Post Helpful? 1
  • +
  • -

#11 jasimare01  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 28
  • Joined: 11-December 08

Re: Trouble With Arrays and For Loops

Posted 11 December 2008 - 09:32 PM

I can't believe I missed that one. Thank you sooo much! That got rid of all of the errors and the program compiled, but the table is not lining up when I run the program it is all scattered on the screen. it should be in rows and columns. I hate to keep bugging you with all of the questions, but you are SO incredibly helpful.

Thank you, thank you again.
Was This Post Helpful? 0
  • +
  • -

#12 janotte  Icon User is offline

  • code > sword
  • member icon

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

Re: Trouble With Arrays and For Loops

Posted 11 December 2008 - 09:47 PM

Giving you another pair of eyes to see things that you can't because you are tired, or frustrated or whatever is what this forum is about.

You've done the work in writing the code.

Polishing it is something we are always prepared to help with.

Please post your code as it is now and let's see if we can work on this output problem.

I may not be able to help as we are moving back into "Windows only" territory so if anyone else is here with Windows knowledge / skills please jump in!!

This post has been edited by janotte: 11 December 2008 - 09:48 PM

Was This Post Helpful? 0
  • +
  • -

#13 jasimare01  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 28
  • Joined: 11-December 08

Re: Trouble With Arrays and For Loops

Posted 11 December 2008 - 09:52 PM

You have really made me feel welcome and have made it so that I do not have to purchase a wig as a result of me pulling it all out in frustration. This is a fabulous forum and I will be coming back for certain. Thank you for all you have done so far, and for still willing to help me. It is not always easy to ask for help, but you have made it a pleasure.

The code I have now is

#include "stdafx.h"
#include <string>
#include <cmath>
#include <iomanip>
#include <cstdlib>
#include <iostream>


using namespace std;

void  HeaderDisplay (); 	//function prototype
void	  DisplayFee();	//function prototype
double  WeightZone [6] [3];	// Array that containing the costs
char	  WghtRng [6][12] = {	"  0 to 5", 
	" >5 to 10  ",
	">10 to 15  ",
	">15 to 20  ",
	">20 to 25  ",
	">25"};	// weight labels
const double BasicOvrChg = 2.27;	// the basic charge for all deliveries

int _tmain(int argc, _TCHAR* argv[])
{
HeaderDisplay(); // call function to generate the headers
DisplayFee(); // call function to generate fee
	
	return 0; //terminate successfully

} // main

/********************************************************************	*/
/*	 Prints out the Header information						*/
/*******************************************************************	*/

void HeaderDisplay ()
{
system("cls");
cout << "						Excellent Parcel Service Company" << endl;
cout << "							 Package Fee Calculation" << endl;
cout << endl;



} // HeaderDisplay

/*********************************************************************	*/
/*  Calculates the various elements of the table		*/
/*********************************************************************	*/

void DisplayFee ()
{
			

	int lbs, zone; // descriptive counters for the for loop
	
	cout << "WeightRange";
	cout << setw(32) << "Zones";
	cout << setw(10) << "Zone 1";
	cout << setw(10) << "Zone 2";
	cout << setw(10) << "Zone 3";
	cout << endl << endl;
	
	for ( lbs = 0; lbs < 6; lbs = lbs + 1)
	{
		cout << setw(32) << WghtRng[lbs];
		for ( zone = 0; zone < 3; zone++ )
		{
			WeightZone[6][3] =(BasicOvrChg)+((2*lbs)*(1.62*zone));
			cout << setw(10) << WeightZone[lbs][zone];
		} // inner for loop

	 } // outer for loop

} // DisplayFee


:D
Was This Post Helpful? 0
  • +
  • -

#14 janotte  Icon User is offline

  • code > sword
  • member icon

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

Re: Trouble With Arrays and For Loops

Posted 11 December 2008 - 10:42 PM

OK
With some fiddling I got your code to compile on my Mac and after some more fiddling got the output to look like this:
----
						Excellent Parcel Service Company
							 Package Fee Calculation

WeightRange						   Zones	Zone 1	Zone 2	Zone 3

									 0 to 5		 0		 0		 0
								   >5 to 10		 0		 0		 0
								  >10 to 15		 0		 0		 0
								  >15 to 20		 0		 0		 0
								  >20 to 25		 0		 0		 0
										>25		 0		 0		 0



You just need to look at some detail areas:
Point 1
------
Here you need to write your labels so they are consistent.
char	  WghtRng [6][12] = {	"  0 to 5", 
	" >5 to 10  ",
	">10 to 15  ",
	">15 to 20  ",
	">20 to 25  ",
	">25"};	// weight labels


Two of your labels have no spaces between the last character and four of them do have spaces.
Make them all the same.
My suggestion is have no trailing spaces for all of them.
------

Point 2
------
Here:
		cout << setw(32) << WghtRng[lbs];


you need to fiddle with the value which is now "32" until it lines your weight range labels up where you want them on the page.
Just keep trying with different values until you are happy.

Point 3
------
Here:
		for ( zone = 0; zone < 3; zone++ )
		{
			WeightZone[6][3] =(BasicOvrChg)+((2*lbs)*(1.62*zone));
			cout << setw(10) << WeightZone[lbs][zone];
		} // inner for loop

	 } // outer for loop


You need to ensure that there is a line break after you have printed out the 3 values.
The code you need to add is
cout << endl;
I am sure you can work out where to add it if you think about it for a bit.
------

Point 4
------
The above will fix all the formatting and you should get that sorted out now.
However, once that is done, you will see I get all zeros as the table contents.
Do you get all zeros or do you get the right values?
------
Was This Post Helpful? 0
  • +
  • -

#15 jasimare01  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 28
  • Joined: 11-December 08

Re: Trouble With Arrays and For Loops

Posted 12 December 2008 - 07:59 AM

Point 4
------
The above will fix all the formatting and you should get that sorted out now.
However, once that is done, you will see I get all zeros as the table contents.
Do you get all zeros or do you get the right values?
------
[/quote]

I am highly impressed and very thankful!!!

I also get all zeroes in my code. That was actually going to be my very next question as I am not completely sure what I did wrong. There is supposed to be one calculation that populates the fees for the different weights and zones.

Thank you again for all of your valuable help. I would be rocking in some corner somewhere without it.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2