Thread: hai guys help on 2-dim arrays

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    12

    hai guys help on 2-dim arrays

    so i do loop for ex:
    Code:
    #include <iostream>
    #include <cmath>
    #include <iomanip>
    #include <string>
    
    using namespace std;
    const int number_of_rows=40;
    
    void empinfo(char empname[][30],unsigned int hours[][30],float moneydata[][6],int x, int counter);
    int main()
    {
    	char empname[30][30];
    	unsigned int hours[30][30];
    	float moneydata[30][6];
    	int x= 30;
    	int counter = 0;
    	char choice;
    	char answer;
    
    
    	cout<<"Welcome to the Payroll Report for the week! \n";
    	cout<<"Please be sure to enter the information accurately. \n";
    	cout<<" You will be promted with the following \n";
    			
    	cout<<"\nEmployee's Name: \n";
    	cout<<"Amount of Hours of each workday: \n" ;
    	cout<<"Emplyee's Rate(wage per hour): \n";
    	cout<<"Amount of Dependents: \n";
    	cout<<"Healthplan( Y or N): \n";
    	cout<<"Are there more employees(Y or N)?\n";
    
    	cout<<"Would you like to continue?( Y or N)\n";
    	cin>>choice;
    
    	switch(choice)
    	{
    		case 'Y':
    		case 'y':
    			do
    			{
    				counter++;
    				empinfo(empname,hours,moneydata,x, counter);
    				cout<<"Are there more employees?(Y or N) \n";
    				cin>>answer;
    			}while(answer='Y');
    
    
    	
    	return 0;
    	}
    }
    void empinfo(char empname[][30],unsigned int hours[][30],float moneydata[][6],int x, int counter)
    {
    	
    	cout<<"Employee's Name: \n";
    	cin.clear();
    	int stack;
    	cout<<stack + 1<<endl;
    	
    	do
    	{
    	for(int j=0;j<stack;j++)
    	{
    		cin.get(empname[counter][j]);
    		cin.ignore();
    		cin.clear();
    	}
    	}while(stack!=0);
    	cout<<"Amount of Hours Each Day: \n";
    		for(int h=0;h<7;h++)
    		{
    			if(h>7)
    			{ 
    				cout<<"Please only enter a 7 day schedule \n";
    				cin>>hours[h][30];
    			}
    		}
    	cout<<"Employee's Rate: \n";
    		for(int r=0;r<counter;r++)
    		{
    			cin>>moneydata[r][0];
    		}
    }
    the program works but it will only stores 1 char of the name...
    so if the name is Zerlok Zerlok..only the z would be stored if i put a whole name it skips my whole program i'm not sure what i'm doing wrong..=\

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Code:
    cin.get(empname[counter][j]);
    You ask for one character, that's all you get. If you want to get a string, you need to pass a string into cin.get. You're including <string>, why not use std::strings?

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Code:
    cin>>hours[h][30];
    That's not the right way to get a string from the user. You're reading a single character into hours[h][30] (which is, incidentally, beyond the end of the array). What you want is more like this:
    Code:
    cin>>hours[h];
    Code:
    }while(answer='Y');
    That's always true. You likely want
    Code:
    } while(answer == 'Y');
    If you want to check for lowercase too, consider "answer == 'Y' || answer == 'y'". Or use tolower()/toupper() from <cctype>.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  4. #4
    Registered User
    Join Date
    Feb 2008
    Posts
    12
    ahh yes i can't use string because i'm not allowed to for this assignment...
    cin.getline(empname) would work?

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Maybe
    Code:
    cin.getline(empname[counter], 30);
    http://faq.cprogramming.com/cgi-bin/...&id=1043284385
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Vertex Arrays
    By Shamino in forum Game Programming
    Replies: 2
    Last Post: 01-08-2006, 01:24 AM
  2. Need Help With 3 Parallel Arrays Selction Sort
    By slickwilly440 in forum C++ Programming
    Replies: 4
    Last Post: 11-19-2005, 10:47 PM
  3. Building B-Tree from Arrays
    By 0rion in forum C Programming
    Replies: 1
    Last Post: 04-09-2005, 02:34 AM
  4. Help with arrays and pointers please...
    By crazyeyesz28 in forum C++ Programming
    Replies: 8
    Last Post: 03-17-2005, 01:48 PM
  5. Merging two arrays.
    By Roaring_Tiger in forum C Programming
    Replies: 2
    Last Post: 08-21-2004, 07:00 AM