Rank one and two arrays for c++ project

I am having trouble comprehending rank one and rank two arrays in my online programming c++ course. I have so far my code for the assignment and the assignment below, even though I know it is incorrect. Any suggestions, hints, or ways I can correct the program would be awesome.
THANKS!



// This program allows the user to select items to purchase
// It will show the user the shopping cart and total of the purchase
//Programmers: Bryce Acton
#include <cstdlib>
#include <iostream>
#include <string> // for string
#include <cmath> // for math computations
#include <iomanip> // required for setw()
using namespace std;

void displayMenu(char itemname[] [22] , float* price, int* numberAvailable, int* numberOfitems);
void displayshoppingcart (char itemname[] [22] , float* price, int* numberAvailable, int* numberOfitems);

int main(int argc, char *argv[])
{
char couch, cinderb, best, truck, cart, done, maxitems, namelength;
double c, cb, b, t, select;
float price;
int numberAvailable, numberOfitems;

char itemname [maxitems] [namelength];// rank two array
displayMenu( price, numberAvailable, numberOfitems);// calling out function
displayshoppingcart (char itemname[] [namelength] , float* price, int* numberAvailable, int* numberOfitems); //// calling out function


float x[4];
x[0] = 10.29;
x[1] = 4.88;
x[2] = 5.00;
x[3] = 84.99;

// Displays menu
cout << "Please make a selection <1 to 6>: " ; // user input of selection
cin >> select;
double yc, acb, mbf;
if ( select==1)
{cout << "How many yard couches would you like? " ;
cin >> yc;
cout << "Your order will be processed." << endl;
}
if ( select==2)
{cout << "How many two auto cinder blocks would you like? " ;
cin >> acb;
cout << "Your order will be processed." << endl;
}
if ( select==3)
{cout << "How many of Man's best friend would you like? " ;
cin >> mbf;
cout << "Your order will be processed."<< endl;
}
if ( select ==4)
{cout << "You have purchased a work truck. No need to ask for how many." << endl;
cout << "Your order will be processed." << endl;
}
if ( select==5)
{cout << "Your shopping cart!" << endl;
}
if (select ==6)
{cout << "Done!" << endl;
}




system("PAUSE");
return EXIT_SUCCESS;
}

void displayMenu(char itemname[] [namelength] , float* price, int* numberAvailable, int* numberOfitems);
void displayshoppingcart (char itemname[] [namelength] , float* price, int* numberAvailable, int* numberOfitems);

{
double c, cb, b, t, select;
c = 10.29;
cb = 4.88;
b = 5.00;
t = 84.99; // define prices of items on menu

cout << "Items you may purchase from Cletus!" << endl;
cout << " " << "#" << " " << "Name" << setw(30) << "Price " << setw(15) << "Availabull" << setw(4) << endl;
cout << "1" << " " << "Yard couch" << setw(20) << c << setw(10) << "3" << setw(4) << endl;
cout << "2" << " " << "Two Auto cinder blocks" << setw(8) << cb << setw(10) << "45" << setw(4) << endl;
cout << "3" << " " << "Man's best friend" << setw(13) << b << setw(10) << "8" << setw(4) << endl;
cout << "4" << " " << "Work Truck" << setw(20) << t << setw(10) << "1" << setw(4) << endl;
cout << "5" << " " << "Show Shopping Cart" << endl;
cout << " " << "6" << " " << "Done" << endl;
cout << " " << endl;

char couch, cinderb, best, truck, cart, done;

Name[0] = c;
Name[1] = cb;
Name[2] = b;
Name[3] = t;
Name[4] = cart;
Name[5] = done;

itemname[0]= yard couch
itemname[1]= two Auto cinder blocks
itemname[2]= man's best friend
itemname[4]= work truck

namelength[0] = 10
namelength[1]= 22
namelength[2] = 17
namelength[3] = 10


price[0] = 10.29;
price[1] = 4.88;
price[2] = 5.00;
price[3] = 84.99;

numberAvailable[0] = 3;
numberAvailable[1] = 45;
numberAvailable[2] = 8;
numberAvailable[3] = 1;


}


Chapter 6 and 7 Homework

ENGR 104

Background: It takes careful consideration to shop on-line. Cletus, a slack-jawed yokel, wants to hire you to build software for his on-line store. It's his first venture into entre-panoor-ship, so his selection of merchandise is rather limited.

Specifications: Your program is to present the user with a main menu that allows the user to select which items they wish to purchase. The user will select an option by entering an integer value. After the user selects an option, the computer will prompt the user to enter the number of those items he or she wishes to order. If the user picks the option to show the shopping cart, the screen should display the user’s choices and the current cost of the selected items. If the user picks the same item twice, the new number of items will simply replace the old number of items rather than adding to the total.

After an option is chosen and the user has been prompted for the quantity of that item, the main menu should be displayed again.

Once the user selects “Dun”, then the program will display the shopping cart and end. Here is the information in the main menu:



MAIN MENU
1.) Yard couch $10.29
2.) Two Auto cinder blocks $ 4.88
3.) Man's best friend $ 5.00
4.) Good Truck $84.99
5.) Show shopping cart
6.) Dun (got-r-done)
Specs on the Specifications:

You should range-check all inputs for reasonablenessivity. This begs the question, "What's a reasonable number of porch couches, say, a person would want to buy?" Well, you decide. But be sure to use constants where appropriate. If input is invalid, return to the main menu.

You must use functions and arrays in this program. I require a function to display the menu and another function to display the shopping cart. Use these prototypes:



void displayMenu(char itemName[][nameLength], float* price, int* numberAvailable, int numberOfItems);



void displayShoppingCart(char itemName[][nameLength],float* price,int* numberOrdered, int numberOfItems);



You will need three rank-one arrays: price, number available and number ordered of each item. The main function will include a loop that calls the displayMenu and displayShoppingCart functions as needed. Set the maximum number of items to be 10 and use that when declaring your arrays.



The item names should be stored in a rank 2 array. To declare that array, use this command:



char itemName[maxItems][nameLength];



You do not need to use the input file for the normal assignment. You may instead code the data from that file directly into your cpp file. You must still use arrays and functions, however.

Further note: Don't underestimate the time that it will take to write this program!


I don't know where to begin exactly, as an experienced programmer I would approach the problem differently given the assignment.

Besides the given functions in the assignment I would have the following.

All arrays would be defined before your main line in the global space.
void InitArrays(); // this would initialize your arrays, you have the code in a bad spot. This would be called at the start of your main line to set them up.

This code sample is how I would do the mess in the main line that you have with the if's
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
int nSelection;
int nCount;

DisplayMenu();

cout << "Make A Selection :";
cin >> nSelection;

if(nSelection < 6)
{
      cout << "How many woud you like of ";
      cout << name[nSelection -1] << ": "  << itemName[nSelection-1] << "?";
      cin >> nCount;
      // a test to see if we have that many in stock....
      // if valid we display the correct info...
      // if we don't we report that.
}
else
{
     // print my goodbye....
}



As for sorting, it would be which array I wanted to sort,and an array to store the sorted info.

It comes down to the array I set up for Orders which is the nSelection and nCount. This is how I would attack the given information. I would also have a loop in the main line so I could put in multiple pieces of one order.


Topic archived. No new replies allowed.