student question

This is a discussion on student question within the C++ Programming forums, part of the General Programming Boards category; I've got a program that has a main menu for the user to choose from in main. each choice calls ...

  1. #1
    Registered User
    Join Date
    Oct 2003
    Posts
    27

    student question

    I've got a program that has a main menu for the user to choose from in main. each choice calls a different function. the first choice in main is to enter information, it calls a function getinfo. It works but how do I go back to main everytime I finish a function so the user can pick another.
    choices (and functions)
    1. enter info
    2. update info
    3. calculate
    4. display
    5. exit

    any help is appreciated....

  2. #2
    Pursuing knowledge confuted's Avatar
    Join Date
    Jun 2002
    Posts
    1,916
    Use a while loop in main. For details, use a board search.

    Next question please.
    Away.

  3. #3
    Registered User
    Join Date
    Oct 2003
    Posts
    27
    Thank you...it works....

    Ok heres another....

    How do I display the information, entered in the function getinfo, in the other functions

  4. #4
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,088
    Usually you would use a class to store related information and pass in whatever was necessary as a parameter to the function. In a small student program, though, you could also declare the important variables globally (meaning you declare them outside of any function), and then they will be available to all functions of your program.

    Also, if your program is small, posting it (with [code] [ /code] tags) would help people give more specific answers.

  5. #5
    Registered User
    Join Date
    Oct 2003
    Posts
    27
    the problem is that when i choose 1 and enter the information when I click enter after price it will show the calculate function information..
    quantity = 0 & price = 0

    Code:
    #include "stdafx.h"
    #include <iostream>
    
    #using <mscorlib.dll>
    
    using namespace std;
    
    struct point
    {
    public:
    	float quantity;
    	float price;
    };
    
    
    void get_input(char ,int, float);
    float calculate(char, int, float);
    
    int main()
    {
    	int numbinput, quantity;//, calculate;
    	float price, total;
    	char product;
    do
    {
    	cout << endl << "Enter a number: " << endl;
    
    	cout << "1.  Enter Information" << endl;
    	cout << "2.  Calculate Sales" << endl;
    	cout << "3.  Update Informaton" << endl;
    	cout << "4.  Exit" << endl <<endl;
    	cin >> numbinput;
    
    	switch (numbinput)
    	{
    	case 1:
    		{
    			get_input(product, quantity, price);
            }
    
    	case 2:
    		{
    			total = calculate(product, quantity, price);
    			cout << "The total is: " << total;
    		}
    	}
    }
    while (numbinput != 4);
    	return 0;
    }
     
    
    void get_input(char product, int quantity, float price)
    {
    		//char product;
    		//int quantity;
            cout << "Product Name: ";
    		cin >> product;
    		cout << "Quantity: ";
    		cin >> quantity;
    		cout << "Price: ";
    		cin >> price;
    		//cout << "Press the spacebar to continue...";
    		//getch();
    }
    
    float calculate (char product, int quantity, float price)
    {
    	//char prod;
    	//int quant;
    	float total;
    
    	cout << "Product: " << product;
    	cout << "Quantity: " << quantity;
    	cout << "Price: " << price;
    	total = price * quantity;
    
    	return total;
    }

  6. #6
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,088
    Ahhh, look at your function call to get_input. You have three variables declared in main() - product, quantity, and price - that you pass to get_input.

    The function get_input gets a copy of these values to use. So when it does its work by filling the data with user input, it does the work on the copies. The original variables in the main() method don't get changed.

    To fix this, I would suggest passing a reference to these variables to your method. All you have to do is change your declaration and definition of get_input:
    Code:
    void get_input(char& product, int& quantity, float& price)

  7. #7
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146
    Another problem is that you don't have breaks in your switch statement. Usually switch statements go like this:
    Code:
    switch(variable)
    {
      case something: expr1;
                                 expr2;
                                 break;
      case something_else: expr3;
                                         break;
      default: expr4;
                   break;
    }
    If you omit those breaks, then control will flow to the other cases. Sometimes you want that, but it doesn't sound like you do.
    FAQ

    "The computer programmer is a creator of universes for which he alone is responsible. Universes of virtually unlimited complexity can be created in the form of computer programs." -- Joseph Weizenbaum.

    "If you cannot grok the overall structure of a program while taking a shower, you are not ready to code it." -- Richard Pattis.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Suggestions for this code? and 1 question....
    By Huskar in forum C Programming
    Replies: 6
    Last Post: 03-31-2009, 10:24 AM
  2. another do while question
    By kbpsu in forum C++ Programming
    Replies: 3
    Last Post: 03-23-2009, 01:14 PM
  3. sequential file program
    By needhelpbad in forum C Programming
    Replies: 80
    Last Post: 06-08-2008, 02:04 PM
  4. a question on pointers, structs and arrays
    By onefootswill in forum C Programming
    Replies: 3
    Last Post: 12-06-2007, 01:27 AM
  5. what does this warningmean???
    By kreyes in forum C Programming
    Replies: 5
    Last Post: 03-04-2002, 07:53 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21