Thread: Array problem

  1. #1
    Registered User
    Join Date
    Jan 2011
    Posts
    101

    Array problem

    In the following I get a compiler error regarding the scope of x in the int GetInfo(); am I not using the array properly?

    Code:
    #include <windows.h>
    #include <iostream>
    #include <istream>
    #include <ostream>
    #include <string>
    #include <vector>
    #include "ShortColours.h"
    
    using namespace std;
    
    int GetInfo();
    int Change();
    int WriteInfo();
    
    vector<string> employee;
    vector<int> payrate;
    vector<int> hoursworked;
    vector<int> totalpay;
    vector<int> fiftypounds;
    vector<int> twentypounds;
    vector<int> tenpounds;
    vector<int> fivepounds;
    vector<int> twopounds;
    vector<int> onepound;
    vector<int> fiftypence;
    vector<int> twentypence;
    vector<int> tenpence;
    vector<int> fivepence;
    vector<int> twopence;
    vector<int> onepenny;
    
    string name;
    int rate;
    int hours;
    
    
    int main()
    {
        HANDLE hOut;
        hOut=GetStdHandle(STD_OUTPUT_HANDLE);
    
        int GetInfo();
        int Change();
        int WriteInfo();
    
        return 0;
    }
    
    // Get employees, pay rates and hours
    int GetInfo()
    {
        for (int x(0); x<6; x++)
            cout<<"Employees name : ";
            cin>>name;
            employee.at(x)=name;
            cout<<"Payrate: ";
            cin>>rate;
            payrate.at(x)=rate;
            cout<<"Hours worked : ";
            cin>>hours;
            hoursworked.at(x)=hours;
            totalpay.at(x)=rate * hours;
    }
    
    //Work out change required
    int Change()
    {
    cout<<"Work out change"<<'\n';
    }
    
    //OutputInfo
    int WriteInfo()
    {
    cout<<"Write results"<<'\n';
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    You should use braces.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jan 2011
    Posts
    101
    Quote Originally Posted by laserlight View Post
    You should use braces.
    Thanks I did put braces in and the compiler error does go away but the program doesn't actually do anything while I am expecting it to ask for the first name.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    Eh, I was too quick. What's with all those global variables?

    At the moment, what you need to do is to change them into local variables. You don't need most of them right now, so erase them. Yes, you will need to add them back in later, but later is later. Do the simplest thing that could possibly work now.

    Now, when you want to append stuff into a vector, append with push_back or something like that. At the moment you are accessing beyond the bounds of the vector, and I am surprised that at() did not throw an exception.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Jan 2011
    Posts
    101
    Quote Originally Posted by laserlight View Post
    Eh, I was too quick. What's with all those global variables?

    At the moment, what you need to do is to change them into local variables. You don't need most of them right now, so erase them. Yes, you will need to add them back in later, but later is later. Do the simplest thing that could possibly work now.

    Now, when you want to append stuff into a vector, append with push_back or something like that. At the moment you are accessing beyond the bounds of the vector, and I am surprised that at() did not throw an exception.
    Ok will do as you suggest, will have to read up more on vectors I was imagining they are like using arrays in BASIC but obviously not. I declared all the variables as I have planned out what I want to do and what variables I need to do it, maybe my programming technique is not as it should be either, I tend to plan the whole thing on paper before I even start to code anything. What I was trying to do here was set myself a task to see if I have the basics of C++ in my head properly.

    But I still wonder why the first "cout<<"Employees name : ";" doesn't even appear even though the program has completed?

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    No idea. By the way, you don't need this:
    Code:
    HANDLE hOut;
    hOut=GetStdHandle(STD_OUTPUT_HANDLE);
    so remove it.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    Well lines 42 to 44 in your main() are still only function prototypes, not function calls.

    int foo();
    Is a prototype.

    int result = foo();
    is a function call.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  8. #8
    Here we go again...
    Join Date
    Sep 2011
    Location
    San Diego
    Posts
    102
    Fix this function:

    Code:
    int GetInfo()
    {
        for (int x(0); x<6; x++)
        {
            cout<<"Employees name : ";
            cin>>name;
            employee.at(x)=name;
    
            cout<<"Payrate: ";
            cin>>rate;
            payrate.at(x)=rate;
            cout<<"Hours worked : ";
            cin>>hours;
            hoursworked.at(x)=hours;
            totalpay.at(x)=rate * hours;
        }
    }
    For loops needs curly brackets, and int(x) should be int = x.

    Code:
    employee.at(x)=name
    should be

    Code:
    employee.at[x]=name;

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    Quote Originally Posted by rmatze
    int(x) should be int = x
    Actually, JayCee++ wrote:
    Code:
    int x(0);
    which is correct, though uncommon for an integer type.

    Quote Originally Posted by rmatze
    Code:
    employee.at(x)=name
    should be
    Code:
    employee.at[x]=name;
    No, JayCee++'s use of the at member function is correct. (The missing semi-colon is from your quote, not the code posted by JayCee++.)
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. problem initializing a double array for large array
    By gkkmath in forum C Programming
    Replies: 4
    Last Post: 08-25-2010, 08:26 PM
  2. Problem converting from char array to int array.
    By TheUmer in forum C Programming
    Replies: 11
    Last Post: 03-26-2010, 11:48 AM
  3. array problem.
    By pessi in forum C Programming
    Replies: 6
    Last Post: 08-21-2007, 07:00 AM
  4. simple array of char array problem
    By cloudy in forum C++ Programming
    Replies: 5
    Last Post: 09-10-2006, 12:04 PM
  5. need help on array problem
    By scheon83 in forum C++ Programming
    Replies: 1
    Last Post: 02-12-2002, 08:50 AM