Thread: making selections from a struct

  1. #1
    Registered User
    Join Date
    May 2005
    Posts
    7

    making selections from a struct

    The program has to read a file into an array. after the struct is into an array the user must make up to 8 selections.

    I have a void function to make the sections but I don't know how to print out only the selections the user has chosen via a different void function.

    Code:
    void makeSelection(menuItemType[], int noOfItems)
    {
                    int i = 0;
    	char answer;
    	
    	
    	cout << "You can make up to " << noOfItems << " single order selections." << endl;
    	cout << endl;
    	cout << "Do you want to make a selection? Y/y (yes) or any other key for No: ";
    	cin >> answer;
    	
    	if (answer == 'y' || 'Y') 
    	{
    		if (i == i) 
    	    {
    		cout << "*** This item has already been selected ***" << endl;
    	cout << "Enter item number from Menu: ";
    	cin >> i;
    	cout << "Do you want to make another selection? Y/y (Yes) or any other key for No: ";
    	cin >> answer;
    	cout << "Enter item number from Menu: ";
    	cin >> i;  
    	}
    		
    		}
    		else 
    		{
    		 printCheck (menuList, noOfItems);			}
    	}
    void printCheck (menuItemType menuList[], int noOfItems)
    {
    	cout << fixed << showpoint << setprecision(2);
    	cout << "Welcome to Smitty's Restaurant"<<endl;
    	cout << "The Cheapest Eats in Town!!!" << endl;
    	cout << "=============================" << endl;
    	cout << "****" << setw(15) << "Menu Items Selected: " << "****" << endl;
    	cout << endl;
    }
    Last edited by Npratt23; 05-11-2005 at 12:42 PM. Reason: to add code examples

  2. #2
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    The program has to read a file into an array. after the struct is into an array
    "struct is into the array" ??
    I have a void function
    "void function"??

    Post an example of what you mean--it might be clearer than trying to describe it.
    Last edited by 7stud; 05-11-2005 at 01:46 PM.

  3. #3
    Registered User
    Join Date
    May 2005
    Posts
    7
    I edited my message

  4. #4
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    Hi,

    You can store the user's integer selections in an array, and have your function return an array. So, you might have something like this:

    string menuItems[8]; //contains the items you read in from the file

    int choices[8] = {0}; //use this array to store the user's choices

    Then, in your other function, you can display the strings corresponding to the customer's choices:
    Code:
    if(choices[i] != 0)  
        cout<<menuItems[ choices[i] ]<<endl;
    Note, you might have to subtract 1 from choices[i] to get the right index position in the menuItems array.
    Last edited by 7stud; 05-11-2005 at 02:01 PM.

  5. #5
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    I'd recommend forgetting about writing code for now and try to write out on paper what you want to do with the code line by line:

    0)declare int i and initialize to 0
    1)declare stream to read from file x.
    2)while still data to read into file
    3)read each struct from file x one at a time, placing them into array y using index i to keep track of where each new struct read in should go.
    4)increment i by one each time through loop.
    5)output instructions to user
    6)declare variable to keep track of how many unique items user has selected and initialize to 0.
    7)declare flag to allow user to select less than 8 unique items, if they don't want to select the full 8, and initialize to y/Y.
    8)declare array of z ints to keep track of selections and initialize all elements in array to zero.
    9)Ask user if they want to choose an item.
    10)Enter response into flag.
    11)While response is y/Y and number of unique items selected under 8, then enter loop, otherwise, exit
    12)display menu to select from
    13)ask user for selection
    14)check array to see if value at index corresponding to selection number entered has value of zero, meaning not selected yet.
    15)if item available change value of appropriate element in array to something other than zero, increment number of uinque choices, and ask user if they want to make another selection
    16)else output statement indicating selection already chosen
    17)when loop stops because user entered something other than y/Y or because unique items == 8 use another loop to output selections based on value of elements in array from #8

    Now when you're satisfied you've got all the steps written down on paper, try writing code. Admittedly, this gets easier to do after you've written some successful code, but it's amazing how much it will help get things on the right track.
    You're only born perfect.

  6. #6
    Registered User
    Join Date
    May 2005
    Posts
    7

    still having problems

    I still can't get the choices to accumulate.

  7. #7
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    There's something wrong here:
    Code:
    if (i == i)
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  8. #8
    Registered User
    Join Date
    May 2005
    Posts
    7
    I was trying to check to see if the entry has be entered

  9. #9
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Still, comparing a variable with itself will always yield true.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  10. #10
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > Still, comparing a variable with itself will always yield true.
    Unless it's declared volatile
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  11. #11
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Yeah ... still, I would consider that a bug. A very nasty one, too. What's the chance, after all?
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Looking for constructive criticism
    By wd_kendrick in forum C Programming
    Replies: 16
    Last Post: 05-28-2008, 09:42 AM
  2. Replies: 16
    Last Post: 10-29-2006, 05:04 AM
  3. newbie needs help with code
    By compudude86 in forum C Programming
    Replies: 6
    Last Post: 07-23-2006, 08:54 PM
  4. towers of hanoi problem
    By aik_21 in forum C Programming
    Replies: 1
    Last Post: 10-02-2004, 01:34 PM
  5. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM