3

I was reading the chapter on structures in my book, and it got me re-modifying a program I already made, but this time using structures which I have never used before; however, after finishing the program, there's one issue I'm not understanding. The output of the program only displays once. It's in a for loop, and yet even though it asks me to input my information three times, it only outputs the first information.

I'm probably just not understanding how arrays in structures work. An example of my issue is the following. I have my output on the following loop

for(int counter = 0; counter <size; counter++)

The size is 3, which would mean I'll get the output printed three times; however the answer I'm getting is the same as if I was asking for the following.

Listofnames[0].F_name

When what I actually want is

Listofnames[0].F_name Listofnames[1].F_name Listofnames[2].F_name

However, I don't want to have to write it three times, I did to test it and it actually worked, but is that the only way to do it? Or did I miss something in my program?

#include <iostream>
#include <string>
#include <iomanip>
using namespace std;

    struct Names
    {
        string F_name;          //Creating structure called Names.
        string L_name;
        char Mi;
    };
    struct Payrate
    {
        double rate;
    double hoursworked; //Creating structure called Payrate.
    double gross; 
    double net;
};
int main()
{

    double stateTax = 0, federalTax = 0, unionFees = 0, timeHalf = 1.5;         //Initializing variables.
    const int size = 2;         //Array size.
    Payrate employee[size];             //Structure variables
    Names Listofnames[size];
    for (int counter = 0; counter < size; counter++)            //Initializing for loop.
    {
        cout << "What's your first name?: " << endl;
        cin >> Listofnames[counter].F_name;
        cout << "What's your last name?: " << endl;                     //Displaying names, and hours worked, rate.
        cin >> Listofnames[counter].L_name;
        cout << "What is your middle initial?: " << endl;
        cin >> Listofnames[counter].Mi;
        cout << "How many hours did you work? Please enter a number between 1-50: " << endl;
        cin >> employee[counter].hoursworked;
        cout << "What is your hourly rate? Please enter a number between 1-50: " << endl;
        cin >> employee[counter].rate;

        if (employee[counter].hoursworked < 0 || employee[counter].hoursworked >50)                 //Initializing conditional statements.
        {
            cout << "Sorry you entered a erong entry. Pc shutting off " << endl;                        //Displays what happens is user inputs a number under 0 or over 50.
        }
        if (employee[counter].rate < 0 || employee[counter].rate > 50)                                              //Initializing conditional statements.
        {       
            cout << "Sorry you entered a erong entry. Pc shutting off " << endl;                                //Displays what happens is user inputs a number under 0 or over 50.         
        }
        if (employee[counter].hoursworked <= 40)                                                                                //Initializing conditional statements.
        {                                                                                                       
            employee[counter].gross = employee[counter].hoursworked * employee[counter].rate;               //Calculating gross.
        }
        else if (employee[counter].hoursworked > 40)                                                                                //Initializing conditional statements.
        {
            employee[counter].gross = employee[counter].hoursworked * (employee[counter].rate * timeHalf);  //Calculating gross.
        }
        stateTax = employee[counter].gross * 0.06;
        federalTax = employee[counter].gross * 0.12;                                                                            //Calculates all the tax fees, and net.
        unionFees = employee[counter].gross * 0.02;
        employee[counter].net = employee[counter].gross - (stateTax + federalTax + unionFees);
    }
    cout << "FirstN " << "MI " << "LastName " << "\t" << "Rate " << "HoursWorked " << "TimeHalf " << "StateTax " << "FederalTax " << "UnionFees " << "Gross " << "  " << "Net " << endl;            //Displays header of output.
    cout << "==================================================================================================================" << endl;
    for (int counter = 0; counter <= size; counter++)
    {
        //Output.
        cout << Listofnames[counter].F_name << "\t" << fixed << setprecision(2) << Listofnames[counter].Mi << " " << Listofnames[counter].L_name << "\t" << employee[counter].rate << "\t" << employee[counter].hoursworked << "\t" << setw(7) << timeHalf << "\t" << setw(8) << stateTax << setw(12) << federalTax << "\t" << unionFees << "\t" << employee[counter].gross << "\t" << employee[counter].net << endl;
        system("pause");
    }
}

P.s If you had to re modify this program again, what would you use to simplify it. Asking so I can keep re-modifying, and learn more advanced stuff. Vectors, pointers? Thanks in advance.

ReMaKe
  • 37
  • 1
  • 9
  • Can you provide inputs where this fails? Since you do not validate input issues you are seeing are likely due to your parsing misbehaving. – Benjamin Bannier Mar 10 '17 at 07:00
  • I Just tested the code on my maschine and it printed the output twice - since the size is set to 2 right here `const int size = 2;` not 3 as you said. Also did you pressed a button after calling `system("pause")`? Otherwise the program will pause after outputting just the first line. – muXXmit2X Mar 10 '17 at 07:00
  • Don't use arrays, you should almost always use std::vector in c++. – Lord_Curdin Mar 10 '17 at 07:01
  • if your arrays are of size 3 you should run the loop `i < size` (because 3 elements use index 0 1 2).. anyway your code works it outputs more then 1 line if you press a key because of the `system("pause");` – xander Mar 10 '17 at 07:08
  • @muXXmit2X it was the silliest thing. I had to go to build, and press clean solution and it started working. I guess it never updated the program. Does anyone know why it does that? When I use to code on xcode, that never happened to me. It makes you think your code is completely wrong when in actuality it's correct. – ReMaKe Mar 10 '17 at 07:09
  • 1
    @Android400 So you recommend always using vectors? I'll start reading about vectors. Is the concept similar to arrays? – ReMaKe Mar 10 '17 at 07:10
  • @ReMaKe welcome to the wonderful world of compiler caching, sometimes you just have to clean your build to get it compiled correctly, happens to most compiled languages sometimes :D – xander Mar 10 '17 at 07:11
  • @ReMaKe Vectors are similar to arrays but they are better as you can increase their size as you like(dynamic). – Ayush Mar 10 '17 at 07:12
  • @ReMaKe in general, would recommend vector because you don't have to manuel manage your memory and you have standart functions implemented on vectors. – Lord_Curdin Mar 10 '17 at 07:13
  • @xander haha, yeah, it's just frustrating at times since you're looking at your program over and over like what's wrong!! But I'm very new to this so now I know next time to always clean. – ReMaKe Mar 10 '17 at 07:14
  • @ReMaKe Read about arrays vs vectors in this link http://stackoverflow.com/questions/15079057/arrays-vs-vectors-introductory-similarities-and-differences. – Ayush Mar 10 '17 at 07:17
  • @Android400 okay, i'll definitely start learning how to use vectors. I'm just starting to get into structures, and classes, next month due to school I'll be getting really into sorting files. For someone who's where I'm at, apart from vectors, is there anything else you feel is essential to learn, and will simplify my coding? Is pointers essential? appending? – ReMaKe Mar 10 '17 at 07:18
  • @Ayush thank you for the link, will read it. – ReMaKe Mar 10 '17 at 07:19

1 Answers1

2

You have an array with 3 indexes but your loop is only going upto 2 indexes. Change your for loop to this.

for (int counter = 0; counter <= size; counter++) 

Now, this loop will print the all the indexes.

Instead of using a static value you can also use this.

for (int counter = 0; counter < sizeof(Listofnames)/sizeof(Listofnames[0]); counter++)

sizeof(Listofnames)/sizeof(Listofnames[0]) This will give you the total size of your array.

Ideone Link

Ayush
  • 741
  • 9
  • 19
  • Sorry, edited program, I mistakenly put the things I was trying to do to fix it. The program is now exactly how I have it, still only displaying once. – ReMaKe Mar 10 '17 at 07:06
  • You could otherwise increase size to +1 instead of changing conditional operation to evil one `<=` inside the for parameter – knoxgon Mar 10 '17 at 07:08
  • @ReMaKe It is working fine for me. I used the same code. Check this link. http://ideone.com/uGeQEQ – Ayush Mar 10 '17 at 07:09
  • @Ayush yeah it's now working. I commented on another persons comment, but i'll say the same thing here. it was the silliest thing. I had to go to build, and press clean solution and it started working. I guess it never updated the program. Does anyone know why it does that in visual studios? When I use to code on xcode, that never happened to me. It makes you think your code is completely wrong when in actuality it's correct. Just pressing run, doesn't update at times. – ReMaKe Mar 10 '17 at 07:12