Thread: program working perfectly, but array is giving me a little problem

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    6

    program working perfectly, but array is giving me a little problem

    Code:
    #include <iostream>
    using namespace std;
    
    int main()
    {
    	const int nos_emp=5;
    	double hours1[nos_emp];
    	double payrate1[nos_emp];
    	int budget1;
    	double result1;
    	//const double payrate1[3] = {25.50, 35.75, 50.00};
    
    	cout<<"Please Enter Your Budget:";
    	cin>>budget1;
    	result1=0;
    	for(int i=1;i<nos_emp;i++)
    	{
    		cout<<"Please Enter Hours Worked By Employee :"<<i<<" : ";
    		cin>>hours1[i];
    		cout<<"Please Enter Hourly Pay Rate For Employee :"<<i<<" : ";
    		cin>>payrate1[i];
    		result1=result1+(hours1[i]*payrate1[i]);
    	}
    	cout<<"Result is "<<result1;
    	if (result1>budget1)
    		cout<<"\nYou are over budget By "<<result1-budget1;
    	else if (result1==budget1)
    			cout<<"\nYoy are exact at budget";
    	else if (result1<budget1)
    			cout<<"\nYou are under budget by "<<budget1-result1<<endl;
    	return 0;
    }
    //const double payrate1[3] = {25.50, 35.75, 50.00};
    just trying to make that work

    please help me out
    thank you

  2. #2
    Registered User
    Join Date
    Nov 2005
    Posts
    673
    You cannot not initialize arrays at runtime, unless they are dynamic arrays.
    Code:
    int main()
    {
       int x = 3;
       int y[x]; //Will not work
    }
    
    int main()
    {
       int x = 3;
       int* y = new int[x]; //Will work, but DO NOT forget to free the memory
       delete [] y; //free the memory in the y array
    }
    Hope that helps

  3. #3
    Captain - Lover of the C
    Join Date
    May 2005
    Posts
    341
    Where do you see him initializing an array at runtime?
    Don't quote me on that... ...seriously

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    Here is some code you can study to figure out your problem.
    Code:
    #include <iostream>
    
    int main()
    {
        int i=0;
        const int n=4;
        int arr[n] = { 12, 34, 23, 123 };
    
        for (i=0; i<n; i++)
        {
            std::cout << arr[i] << ' ' << i << std::endl;
        }
    
        for (i=1; i<n; i++)
        {
            std::cout << arr[i] << ' ' << i << std::endl;
        }
    
        return 0;
    }
    Last edited by robwhit; 12-10-2007 at 11:46 PM. Reason: Added iterator printouts

  5. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by arps789 View Post
    program working perfectly, but array is giving me a little problem
    Don't contradict yourself. Your program isn't working perfectly. If it were, you wouldn't have any array problem, and you probably wouldn't be here asking for help.

    Now, you can start by asking a question. What's the problem?
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    For your arrays, try std::vector.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Array Program
    By alex1067 in forum C Programming
    Replies: 5
    Last Post: 04-15-2008, 06:26 AM
  2. Replies: 4
    Last Post: 03-26-2008, 08:48 AM
  3. Need some help with a C program...
    By pityocamptes in forum C Programming
    Replies: 1
    Last Post: 03-25-2005, 06:43 PM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. Array Program
    By emmx in forum C Programming
    Replies: 3
    Last Post: 08-31-2003, 12:44 AM