Array problem

This is a discussion on Array problem within the C++ Programming forums, part of the General Programming Boards category; In the following I get a compiler error regarding the scope of x in the int GetInfo(); am I not ...

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

    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
    21,589
    You should use braces.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jan 2011
    Posts
    96
    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
    21,589
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Jan 2011
    Posts
    96
    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
    21,589
    No idea. By the way, you don't need this:
    Code:
    HANDLE hOut;
    hOut=GetStdHandle(STD_OUTPUT_HANDLE);
    so remove it.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,484
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  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
    21,589
    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++.)
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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, 07:50 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21