Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Apr 8th, 2006, 11:13 PM   #21
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
It doesn't hurt to do some limited research outside the forum. The documentation covers constructors....
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Apr 9th, 2006, 12:04 AM   #22
codylee270
Unverified User
 
Join Date: Sep 2005
Posts: 209
Rep Power: 0 codylee270 is an unknown quantity at this point
I tried my main like this:

for(i=0; i<10; i++)
{
	cout << "Please enter the following Employee information:" << endl;
	cout << "\nEmployee ID No: ";
	cin >> id;
	cout << "\nEmployee Sex: ";
	cin >> sex;
	cout << "\nEmployee Wage: ";
	cin >> wage;
	cout << "\nYears with Company: ";
	cin >> years;
	Employee emp[i+1]=Employee(id,sex,wage,years);
}

I get errors with the:
Employee emp[i+1]=Employee(id,sex,wage,years);

Just as an experiment, I took out the [i], and put a 1, just as if were creating one instance, and all I got was the Warning i had gotten a little while earlier, and it actually compiled. So the main problem is trying to create an instance for each of the 10 employees, and I really don't know how else to cycle through all of the inputs unless i actually hardcode for each instance, which seems unecessary, There has to be a way to loop through for each instance, and I know someone has to know how. And if anyone can think of a better way to go about this problem as a whole, i'm all for it. But can someone tell me, if we were to get the above to work, and each employee's info was stored in some sort of array or as an object, how could we sort just the years worked, and then get all of the corresponding info to sort with it?
codylee270 is offline   Reply With Quote
Old Apr 9th, 2006, 12:30 AM   #23
Jimbo
Battle Programmer
 
Jimbo's Avatar
 
Join Date: Feb 2006
Location: Bellevue, WA, USA
Posts: 754
Rep Power: 3 Jimbo is on a distinguished road
if its done as an object, you could either have the year be public or make set/get methods for it, and then when you sort the objects based on the year value, they will be sorted correctly.

And for the indexing, use [i] instead of [i+1] or else you'll be reading into memory that most likey isn't yours. Also, dont put the type there, because you're not declaring a new variable (it needs to be declared outside of the loop, like before).
Jimbo is offline   Reply With Quote
Old Apr 9th, 2006, 12:48 AM   #24
codylee270
Unverified User
 
Join Date: Sep 2005
Posts: 209
Rep Power: 0 codylee270 is an unknown quantity at this point
Jimbo. Sounds like you are onto something. for the[i+1], i was using that as a naming for an instance of emp, it has nothing to do with the creation of an array. so instead of
Employee emp1 = employee(blah,blah,blah.blah) x 10
I was hoping to use the [i+1] so that emp[i+1], where i was at 0, would give me employee 1 with all the info sent to the constructor under emp1, then after i is incremented, emp2 would be created with all of the info passed. I don't know if that was confusing people or not since everyone wanted to change it..lol... b/c if you were going to use a loop to send info to the constructor on each iteration, you can't very well create emp1 everytime, the name has to change, hence the emp[i+1]. It could have very well been emp[i], but it would have been more intuitively correct and easier to deal w/ (IMO) had emp1 have employee 1's info, and so on. Wishful thinking i guess you could say. so JIMBO, if you could give me a visual of what you mean from you last post, i would certainly appreciate it. I'm very visual when it comes to programming, b/c when i go from conceptual to actual code, I always do better with something to look at and learn from.

and one more quick issue w/ this:
	cout << "\nEmployee Sex: ";
	cin >> sex[i];
	while(sex[i] != 'M' || sex[i]!='m' || sex[i]!='F' || sex[i]!='f')
	{
                    cout << "Must be M or F. Please retry: ";
                    cin >> sex[i];
    }


I didn't really think about it at first since getting the crap to initialize was a little more important, BUT, when it comes to sex, it can only be M, m, F, or f. My while() doens't work, it keeps asking for correct input regardless. Even if you input a correct character. What's wrong now??? C++ can't give me a break!!!

Just to let everyone know though, i'm very gracious for everyone's input and opinions. It's amazing to know that there is someplace for myself to go with my problems in an area of study that i'm hoping to someday make a career out of. I love how much i can learn from other people, and in most cases, i get more from you guys and your experience than class itself..lol... Keep on truckin'!
codylee270 is offline   Reply With Quote
Old Apr 9th, 2006, 2:32 AM   #25
Jimbo
Battle Programmer
 
Jimbo's Avatar
 
Join Date: Feb 2006
Location: Bellevue, WA, USA
Posts: 754
Rep Power: 3 Jimbo is on a distinguished road
Since you changed sex to a string, you have to use double quotes to compare it. IMHO, you would do just as well to leave it as a char, but that really comes down to your choice. (I did it here as a char for the first example, string for the second.)
int main() 
{
    int i,id, years;
    char sex;
    float wage;
    Employee emp[10];
    for(i=0; i<10; i++)
    {
             cout << "Please enter the following Employess information:" << endl;
             cout << "\nEmployee ID No: ";
             cin >> id;
             do
             {
                 cout << "\nEmployee Sex: ";
                 cin >> sex;
                 cin.clear();
                 sex = toupper(sex);
             } while(sex != 'M' && sex != 'F');
             cout << "\nEmployee Wage: ";
             cin >> wage;
             cout << "\nYears with Company: ";
             cin >> years;
             emp[i] = Employee(id, sex, wage, years);
    }
    system("PAUSE");
    return 0;
}
or with std::vector (and strings):
#include <vector>

int main() 
{
    int i,id, years;
    string sex;
    float wage;
    std::vector<Employee> emp;
    for(i=0; i<10; i++)
    {
        cout << "Please enter the following Employess information:" << endl;
        cout << "\nEmployee ID No: ";
        cin >> id;
        do
        {
            cout << "\nEmployee Sex: ";
            cin >> sex;
        } while(sex != "M" && sex != "m" && sex != "F" && sex != "f");
        cout << "\nEmployee Wage: ";
        cin >> wage;
        cout << "\nYears with Company: ";
        cin >> years;
        emp.push_back(Employee(id, sex, wage, years));
    }
    system("PAUSE");
    return 0;
}

[edit:] also, you dont want to be indexing the string (if you choose that route) when you compare it (see your code: sex[i]).
Jimbo is offline   Reply With Quote
Old Apr 9th, 2006, 10:35 AM   #26
codylee270
Unverified User
 
Join Date: Sep 2005
Posts: 209
Rep Power: 0 codylee270 is an unknown quantity at this point
Hi again,

I tried your main() and I got the following errors:

invalid conversion from `char' to `const char*
initializing argument 1 of `std::basic_string<_CharT, _Traits, _Alloc>::basic_string(const _CharT*, const _Alloc&) [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>]'

These are associated with the line;
 emp[i] = Employee(id, sex, wage, years);

But at lease now i see what you are trying to do.
AND, on another note, When i was doing the comparision earlier to catch input for M or F, i had already changed it to char, and i was using the single quotes, i.e. 'M', but it still made me repeat the input over and over regardless had I input a legitimate value.
codylee270 is offline   Reply With Quote
Old Apr 9th, 2006, 10:41 AM   #27
jayme
Professional Programmer
 
jayme's Avatar
 
Join Date: Nov 2005
Location: Canada
Posts: 495
Rep Power: 0 jayme is an unknown quantity at this point
Send a message via MSN to jayme
Were you using the || operator? That would send you back to the loop every time because you can't select M and F. || checks to make sure the string is == to M AND F, use &&.
__________________

Quote:
Originally Posted by Mohamed Jihad
Durka durka!
Due to incorrect calculations during the middle ages, our calendar actually begins a few years after Jesus' birth. Thus the real 6/6/6 happened a few years back. The world already ended and you missed it.

Download Code::Blocks now!
jayme is offline   Reply With Quote
Old Apr 9th, 2006, 11:02 AM   #28
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
I really think there are too many cooks in the kitchen, but I'm compelled to ask a simple question. Tell me what happens if the user inputs "Big Bucks" in answer to the wage question.
             cout << "\nEmployee Wage: ";
             cin >> wage;
             cout << "\nYears with Company: ";
             cin >> years;
             emp[i] = Employee(id, sex, wage, years);
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Apr 9th, 2006, 12:11 PM   #29
codylee270
Unverified User
 
Join Date: Sep 2005
Posts: 209
Rep Power: 0 codylee270 is an unknown quantity at this point
big bucks into wage causes an explosion! But the && operator fixes the do while!
I guess i should do some check on wage to make sure it's numeric, eh. But I reazlly wish toupper() would work to make the code slightly more efficient. i'm looking at an example in my book and what i tried was the exact same thing the book has done. Weird..
codylee270 is offline   Reply With Quote
Old Apr 9th, 2006, 1:50 PM   #30
codylee270
Unverified User
 
Join Date: Sep 2005
Posts: 209
Rep Power: 0 codylee270 is an unknown quantity at this point
Please forgive me for posting such a massive amount of code, but i'm in a horrible time crunch, and i tried to quickly come up with something that doesn't do the sort of years w/ company. This is supposed to write to a file. Except after you enter the info, instead of giving you the option to write to a file, the program stalls and fails. I'm clueless, it could be parentheses.

#include <iostream>
#include <fstream>
#include <string>
#include <cctype>
#include <iomanip>
#include <vector> 
using namespace std;

void descend_Sort(vector<int>, vector<int>);


int main() 
{
    int i,j,id[10];
    vector<int> years;
    char sex[10];
    float wage[10];
    
    
for(i=0; i<1; i++)
{
	cout << "Please enter the following Employee information:" << endl;
	cout << "\nEmployee ID No: ";
	cin >> id[i];
	cout << "\nEmployee Sex (M or F): ";
	cin >> sex[i];
	while(sex[i] != 'M' && sex[i]!='m' && sex[i]!='F' && sex[i]!='f')
	{
	
                    cout << "Must be M or F. Please retry: ";
                    cin >> sex[i];
    }
                    
	cout << "\nEmployee Numeric Wage: ";
	cin >> wage[i];
	cout << "\nYears with Company: ";
	cin >> years[i];
	cout << endl;
	
}
//vector<int> yearscopy(years);
//descend_Sort(yearscopy, years);

cout << "You have entered:" <<endl;
cout <<"Employee ID       " << "Employee Sex         " << "Numeric Wage        " 
     << "Years with Company" << endl;
cout << "-----------       " << "------------         " << "------------        "
     <<"------------------" << endl;
     
     for(j=0; j<1; j++)
     {
              cout << setw(11) << right << id[j] << "       "
              << setw(12) << char(toupper(sex[j])) << "         " << "$  " << setw(9) << fixed <<
              setprecision(2) << right << wage[j] << "        " << setw(18) << 
              right << years[j] << endl;
     }
     
     cout << "Would you like to write the information to a file? This will overwrite the file."
     << "Would you like to continue? (y/n) : " ;
    char response;
    cin >> response;
	while(response !='N' && response!='n' && response!='Y' && response!='y')
	{
	               cout << "Must enter Y or N: ";
                   cin >> response;
    }
    if( response == 'Y' || response == 'y')
    {
        ofstream file;
        file.open("testfile.txt");
        if(file.fail())
        {
                       cout << "The file was not succesfully opened." << endl;
        }
        else
        {
           file <<"Employee ID       " << "Employee Sex         " << "Numeric Wage        " 
           << "Years with Company" << endl;
           file << "-----------       " << "------------         " << "------------        "
           <<"------------------" << endl;
     
     for(j=0; j<1; j++)
     {
              file << setw(11) << right << id[j] << "       "
              << setw(12) << char(toupper(sex[j])) << "         " << "$  " << setw(9) << fixed <<
              setprecision(2) << right << wage[j] << "        " << setw(18) << 
              right << years[j] << endl;
     }
     file.close();
     }
     
     cout << "The file has been written successfully." << endl;
     }
     else
     {
         cout << "The file has not been written." << endl;
     }       
     
    
    system("PAUSE");
    return 0;
}

PLEASE FORGIVE ME FOR THE AMOUNT. It's the best i could do.
codylee270 is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 12:50 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC