Thread: Arrays and Functions

  1. #1
    Registered User
    Join Date
    Nov 2009
    Posts
    53

    Arrays and Functions

    Hello,

    I have created a simple program that will be adding data into a file, but I need to get this part going first. I'm having a problem when trying to add the name of the item. It skips that line and jumps to the next one not letting me to enter the name. Any help on this will be appreciated.

    Code:
    #include <iostream>
    #include <iomanip>
    #include <fstream>
    #include <cctype>
    
    #define cMax 100
    
    using namespace std;
    
    void invMenu();
    void crtNewItem(double Qty[], double iPrice[], char iName[], int pNumber[]);
    
    int main()
    {
        int mSelec;
        bool bDone = false;
    
    	do{
    
    cout << "1. Inventory Management" << endl;
    cout << "\nPlease enter your selection [1-5]" << endl;
    cin >> mSelec;
    while (mSelec < 1  || mSelec > 5)
    {
            cout << "Please enter a correct selection between 1-5" << endl;
            cin >> mSelec;
    }
    
    switch(mSelec)
    {
      case 1: invMenu();
      break;
    
       }
    }while (!bDone);
    return 0;
    
    }
    
    void invMenu()
    
     {
        double Qty[cMax];
        double iPrice[cMax];
        char iName[cMax];
        int pNumber[cMax];
    
        int iSelec = 0;
    	system("cls");
    
    cout << "\n1. Create New Item" << endl;
    cout << "\nPlease enter your selection [1-6]" << endl;
    cin >> iSelec;
    while (iSelec < 1  || iSelec > 6)
    {
            cout << "Please enter a correct selection. [1-6]" << endl;
            cin >> iSelec;
    }
    
    switch(iSelec)
    {
        case 1:
    		//This calls out the fuction to create a new item.
    		crtNewItem(Qty,iPrice,iName,pNumber);
        break;
    }
    
     }
    
    void crtNewItem(double Qty[], double iPrice[], char iName[], int pNumber[])
    
    {
    	system("cls");
    	ofstream outputFile;
    
     outputFile.open("InvItems.txt", ios::app);
    
    				cout << "Please enter the quantity of the item:__";
    					cin >> Qty[cMax];
    
    				cout << "Please enter the name of the item:__";
                        cin.getline(iName, cMax);
    
                    cout << "Please enter the part number of the item:__";
                        cin >> pNumber[cMax];
    
    				cout << "Please enter the price of the item:__";
                        cin >> iPrice[cMax];
    
    		outputFile << Qty[cMax] << setw(10) << iName[cMax] << setw(10) << pNumber[cMax] << setw(10) << iPrice[cMax] << endl;
    		outputFile.close();
    
    
                        cout << "\n************************************************";
    				cout << "\nYou have just added a new item to the inventory!";
                        cout << "\n************************************************" << endl << endl;
    }
    Any advice on how to solve this will be appreciated. Thanks!

  2. #2
    Registered User
    Join Date
    Mar 2007
    Posts
    416
    Using cin.getline() after using cin>> will result in cin.getline() taking the return that is left in the input buffer from cin>> since cin>> will only take the value, and not the null terminating character also. To get around this put cin.ignore() after your cin>> calls.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. arrays, functions, HELP
    By beginner1 in forum C Programming
    Replies: 4
    Last Post: 05-20-2009, 03:29 PM
  2. Manipulating Character arrays in functions.
    By kbro3 in forum C++ Programming
    Replies: 11
    Last Post: 08-16-2008, 02:24 AM
  3. functions using arrays
    By trprince in forum C Programming
    Replies: 30
    Last Post: 11-17-2007, 06:10 PM
  4. Arrays and Functions
    By KunoNoOni in forum Game Programming
    Replies: 12
    Last Post: 10-04-2005, 09:41 PM
  5. Arrays out-of-bounds in functions only?
    By KneeLess in forum C Programming
    Replies: 5
    Last Post: 11-03-2004, 06:46 PM