954,498 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Help! How to assign value to an array

Hi everyone, Im new to the community and was just wondering if I could get some help :)
I have an assignment where i have to create a program for a shipping company and I was wondering if someone could help me with the coding to assign the sum of a value to my array.
In line 75 after it says Freight Id is: I want to be able to assign the result of case 1 to any number in int freightId and then display it after.

#include <iostream>
#include <iomanip>
#include <ctime> 
#include <cstdlib>
using namespace std;

double shipcharge(double kg,int miles)
{
       
	double sc;
	int p1=(miles/500);
	if (miles%500>0)p1++;
	if(kg<=2)sc=1.1*p1;
	else if(kg<6)sc=2.2*p1;
	else if(kg<10)sc=3.7*p1;
	else if(kg<=20)sc=4.8*p1;
	return sc;
}

int main()
{
   int choice;       // Menu choice

   int freightId [] = { 1721, 1722, 1723, 1724, 1725, 1726, 1727, 1728, 1729, 1730, 1731, 1732, 1733, 1734, 1735, 1736, 1737, 1738, 1739, 1740, 1741, 1742, 1743, 1744, 1746, 1747, 1748, 1749, 1750, 1751, 1752, 1753,1754, 1755, 1756, 1757, 1758, 1759, 1760, 1761, 1762, 1763, 1764, 1765, 1766, 1767, 1768, 1769, 1770, 1771, 1772, 1773, 1774, 1775, 1776, 1777, 1778, 1779, 1780, 1781, 1782, 1783, 1784, 1785, 1786, 1787, 1788, 1789, 1790, 1791, 1792, 1793, 1794, 1795, 1796, 1797, 1798, 1799, 1800, 1801, 1802, 1803, 1804, 1805, 1806, 1807, 1808, 1809, 1810, 1811, 1812, 1813, 1814, 1815, 1816, 1817, 1818, 1819, 1820 };
   

   int d=0,cnt=0 , miles;
	double kg  ;
	double w;


  cout << "\t\tWelcome to FastFreight Company. You can select option from 1-5.\n\n";
   cout << "1. Add Freight Order\n";
   cout << "2. Update Status\n";
   cout << "3. Display Freight Voucher\n";
   cout << "4. Display Freight Order\n";
   cout << "5. Exit the System\n\n";
   cin >> choice;
   
   // Set the numeric ouput formatting.
   cout << fixed << showpoint << setprecision(2);
   
   // Respond to the user's menu selection.
   switch (choice)
   {
      case 1:
         cout << "Weight of Package: ";
         cin >> kg;
         w = kg;
         if (w <=0 )
		{
			cout<<"Please enter weight between 0-20.\n";
            cin>> kg;
		}
		
	else if ( w > 20)
		{
			cout<<"Please enter weight between 0-20.\n";
			cin>> kg;
		}
         cout << "Distance to be sent:";
         cin >> miles;
         d = miles;
         if (d<10)
		{
			cout<<"Please enter distance between 10-3000.\n";
		    cin>> kg;
		}
	else if(d>3000)
		{
			cout<<"Please enter distance between 10-3000.\n";
			cin>> kg;
		}
         cout<<"Cost for "<<w<<" kg for "<<d<<" miles is $"<<fixed<<shipcharge(w,d)<<endl;
         cout<< "Frieght ID is: "<< <<endl;
         
         cnt++;
         break;
LeaguePlayer
Newbie Poster
3 posts since Jan 2012
Reputation Points: 10
Solved Threads: 0
 

why do you need that array? they are all consecutive numbers, so you should be able to just calculate the value of freight id based on something else. I have no idea how you can derive freight id from the rest of the data objects in that program. Are you missing something from the assignment?

Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

Sorry about the following syntax but you can solve it by this way :

int freightId [] = { 1721, 1722, 1723, 1724, 1725, ...} actually means

freightId[0]= 1721
freightId[1]= 1722
freightId[2]= 1723
....

Therefore assign your function results into another array . For ex:
fnc_results[0] = 500
fnc_results[1] = 550
fnc_results[2] = 560
....

Then make them come together like this ;

for(int i= 0 ; i< 10 ;i++ )
cout << "Frieght ID is: " freightId[i] "costs :" + fnc_results[i]

Good luck.

cangan
Newbie Poster
16 posts since Jan 2012
Reputation Points: 11
Solved Threads: 0
 

*Ignore this Post, Look Below*

LeaguePlayer
Newbie Poster
3 posts since Jan 2012
Reputation Points: 10
Solved Threads: 0
 

Well what i need is I need the result of Case 1 to be stored under one of the numbers in int FreightId. I would like it so that each time the program is used it will store the result under different values in FreightId. This is so that in Case 2 i can look up 1722 for example then my program will display the stored value.

#include <iostream>
#include <iomanip>
#include <ctime> 
#include <cstdlib>
using namespace std;

double shipcharge(double kg,int miles)
{
       
	double sc;
	int p1=(miles/500);
	if (miles%500>0)p1++;
	if(kg<=2)sc=1.1*p1;
	else if(kg<6)sc=2.2*p1;
	else if(kg<10)sc=3.7*p1;
	else if(kg<=20)sc=4.8*p1;
	return sc;
}

int main()
{
   int choice;       // Menu choice
   int freightId [] = { 1721, 1722, 1723, 1724, 1725, 1726, 1727, 1728, 1729, 1730, 1731, 1732, 1733, 1734, 1735, 1736, 1737, 1738, 1739, 1740, 1741, 1742, 1743, 1744, 1746, 1747, 1748, 1749, 1750, 1751, 1752, 1753,1754, 1755, 1756, 1757, 1758, 1759, 1760, 1761, 1762, 1763, 1764, 1765, 1766, 1767, 1768, 1769, 1770, 1771, 1772, 1773, 1774, 1775, 1776, 1777, 1778, 1779, 1780, 1781, 1782, 1783, 1784, 1785, 1786, 1787, 1788, 1789, 1790, 1791, 1792, 1793, 1794, 1795, 1796, 1797, 1798, 1799, 1800, 1801, 1802, 1803, 1804, 1805, 1806, 1807, 1808, 1809, 1810, 1811, 1812, 1813, 1814, 1815, 1816, 1817, 1818, 1819, 1820 };
    int d=0,cnt=0 , miles;
	double kg  ;
	double w;


  cout << "\t\tWelcome to FastFreight Company. You can select option from 1-5.\n\n";
   cout << "1. Add Freight Order\n";
   cout << "2. Update Status\n";
   cout << "3. Display Freight Voucher\n";
   cout << "4. Display Freight Order\n";
   cout << "5. Exit the System\n\n";
   cin >> choice;
   
   // Set the numeric ouput formatting.
   cout << fixed << showpoint << setprecision(2);
   
   // Respond to the user's menu selection.
   switch (choice)
   {
      case 1:
         cout << "Weight of Package: ";
         cin >> kg;
         w = kg;
         if (w <=0 )
		{
			cout<<"Please enter weight between 0-20.\n";
            cin>> kg;
		}
		
	else if ( w > 20)
		{
			cout<<"Please enter weight between 0-20.\n";
			cin>> kg;
		}
         cout << "Distance to be sent:";
         cin >> miles;
         d = miles;
         if (d<10)
		{
			cout<<"Please enter distance between 10-3000.\n";
		    cin>> kg;
		}
	else if(d>3000)
		{
			cout<<"Please enter distance between 10-3000.\n";
			cin>> kg;
		}
 
         cout<<"Cost for "<<w<<" kg for "<<d<<" miles is $"<<fixed<<shipcharge(w,d)<<endl;
         cout<< "Frieght ID is: "<< <<endl;
         
         cnt++;
         break;
         
      case 2:
          cout << "Please enter Freight ID: ";
          cin >> freightId [];
           cout << "To update the Package. Enter Weight of Package: ";
         cin >> kg;
         w = kg;
         if (w <=0 )
		{
			cout<<"Please enter weight between 0-20.\n";
            cin>> kg;
		}
		
	else if ( w > 20)
		{
			cout<<"Please enter weight between 0-20.\n";
			cin>> kg;
		}
         cout << "Distance to be sent:";
         cin >> miles;
         d = miles;
         if (d<10)
		{
			cout<<"Please enter distance between 10-3000.\n";
		    cin>> kg;
		}
	else if(d>3000)
		{
			cout<<"Please enter distance between 10-3000.\n";
			cin>> kg;
		}
         cout<<"Updated cost for freightId "<<freightId []<<" is "<<w<<" kg for "<<d<<" miles is $"<<fixed<<shipcharge(w,d)<<endl;
         cnt++;
         break;
         
      case 3:
         cout << "Enter Voucher Number ";
         
        break;
        
      case 4:
          cout << "Enter Frieght ID:.\n";
          cin >>freightId [];
         break;
         
      case 5:
           cout << "The programm will now close.\n";
           exit(1);
           break; 
         
      default:
         cout << "The valid choices are 1 through 5. Run the\n";
         cout << "program again and select one of those.\n";
   }

   system ("pause");
   return 0;
}
LeaguePlayer
Newbie Poster
3 posts since Jan 2012
Reputation Points: 10
Solved Threads: 0
 

Oh no it's not fair... Anyway, try this :

cout<<"Cost for "<If freightId[i] = 1721 assign i= 1721 then create a new array by using i,
freightId_cost[i] = 50 $


cout<<"Updated cost for freightId "<If user inputs freightId [] = 1721 then freightId_cost will become freightId_cost [1721] which equals 50$

cangan
Newbie Poster
16 posts since Jan 2012
Reputation Points: 11
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: