2 Replies - 183 Views - Last Post: 08 December 2012 - 08:09 PM Rate Topic: -----

#1 Speakeasys  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 35
  • Joined: 19-September 12

Char array + Pointers* help

Posted 08 December 2012 - 04:57 PM

Ok so i'm learning to work with char arrays. But i'm confused how to make my functions communicate properly in my main.
how do i fill my char array and use pointers to move my data about?

#include <iostream>
#include <string>
#include <sstream>

using namespace std;

const int LABS_SIZE = 4;


void admin_menu( int, int, int );
void fill_array();
void print2screen();

//for(int i=0; i<LABS_SIZE; i++) { totlab[i] = new int[LABS_SIZE[i]]; }

void fill_array();
{

}
int main()
{
    char lab1[];
    cout << "Login (lab, station, student ID) or (admin): ";
    char lab, comp, *id = new char [5], comma;
    cout << "input";
    cin >> lab;
    if ( lab  == 'a' )
    {
      admin_menu();
      cin.ignore('\n',1000);
    }
    else
      cin >> comma >> comp >> comma >> id;
      fill_array(lab, comp, id);
      print2screen(lab, comp, id);
}
void admin_menu( int a, int b, int c )
{
    char response;
    int lab, comp, id;
    cout << "You are now logged in as the admin";
    cout << "Would you like to search for user?";
    cin >> response;
    if (response == 'Y' || response == 'y')
        for (int i=0; i<lab; i++)
            cin >> lab;
                if (lab == 1 || lab == 2 )
                    cout << "Lab 1";
                else (lab == 2 || lab == 2);
                    cout << "Lab 2";
}



Is This A Good Question/Topic? 0
  • +

Replies To: Char array + Pointers* help

#2 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1072
  • View blog
  • Posts: 4,532
  • Joined: 09-June 09

Re: Char array + Pointers* help

Posted 08 December 2012 - 05:04 PM

An array is a continuous chunk of memory. When you pass an array to a function via pointer, you are passing a pointer to the first element in the array - since all other elements will follow sequentially. There are two types of syntax for passing an array (pointer syntax vs array syntax) - both of which are equivalent.

i.e.
(pointer syntax)
void fillArray(char *arr, int size) {
//code
}


(array syntax)
void fillArray(char arr[], int size) {
//code
}



Don't be fooled by the array syntax, you never pass an array by value. Both notations correspond to a pointer to the first element of the array.

This post has been edited by jjl: 08 December 2012 - 05:05 PM

Was This Post Helpful? 0
  • +
  • -

#3 Speakeasys  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 35
  • Joined: 19-September 12

Re: Char array + Pointers* help

Posted 08 December 2012 - 08:09 PM

I'm stuck on the admin log in and search function. How would I go about this?
Create a menu that allows the administrator to simulate the transmission of information by manually typing in the login
or logoff data. Whenever someone logs in or out, the display should be updated. Also write a search option so that the administrator can type
in a user ID and the system will output what lab and station number that user is logged into, or "None" if the user ID is not logged into any computer station.


#include <iostream>

using namespace std;

const int LABS_SIZE = 4;
//void admin_menu ( int, int, int );
void fill_array ( char ***tot, char lab, char comp, char *id );
void print2screen ( char ***tot );
void settozero ( char ***tot );
int main()
{
    char **tot[LABS_SIZE];
    tot [0] = new char*[5]; tot [1] = new char*[6]; tot [2] = new char*[4]; tot [3] = new char*[3];
    settozero ( tot );
    cout << "Login (lab, station, student ID) or (admin): ";
    char lab, comp, *id = new char [5], comma;
    cin >> lab;
    if ( lab  == 'a' )
    {
        admin_menu();
        cin.ignore('\n',1000);
    }
    else
    {
        cin >> comma >> comp >> comma >> id;
        fill_array(tot, lab, comp, id);
        print2screen( tot );
    }

}
void admin_menu( )
{
    char response;
    cout << "You are now logged in as the admin";
    cout << "Would you like to search for user?";
    cin >> response;
    if (response == 'Y' || response == 'y')
        for (int i=0; i<LABS_SIZE; i++)
            cin >> ;
                if (lab == 1 || lab == 1 )
                    cout << "Lab 1";
                else (lab == 2 || lab == 2);
                    cout << "Lab 2";
}

void fill_array( char ***tot, char lab, char comp, char *id )
{
    int i, j;
    if ( lab == '1' )
        i = 0;
    if ( lab == '2' )
        i = 1;
    if ( lab == '3' )
        i = 2;
    if ( lab == '4' )
        i = 3;
    if ( comp == '1' )
        j = 0;
    if ( comp == '2' )
        j = 1;
    if ( comp == '3' )
        j = 2;
    if ( comp == '4' )
        j = 3;
    if ( comp == '5' )
        j = 4;
    if ( comp == '6' )
        j = 5;
    tot[i][j] = id;
        cout << tot[i][j];
}

void print2screen ( char ***tot )
{
    cout.setf(ios::left);
    cout << endl << "Lab Number     Computer Station" << endl;
    cout.width(20);
    cout << "Lab 1:";
    for(int i = 0; i < 5; i++)
    {
        cout << i + 1 << ": ";
        if(tot[0][i] == '\0')
            cout << "No User";
        else
            cout << tot[0][i] << " ";
    }
    cout << endl;
    cout.width(20);
    cout << "Lab 2:";
    for(int i=0; i < 6; i++)
    {
        cout << i + 1 << ": ";
        if(tot[1][i] == '\0' )
            cout << "No User";
        else
            cout << tot[2][i] << " ";
    }
    cout << endl;
    cout.width(20);
    cout << "Lab 3: ";
    for(int i = 0; i < 4; i++)
    {
        cout << i + 1 << ": ";
        if(tot[2][i] == '\0' )
            cout << "No User";
        else
            cout << tot[3][i] << " ";
    }
    cout << endl;
    cout.width(20);
    cout << "Lab 4: ";
    for(int i=0; i < 3; i++)
    {
        cout << i + 1 << ": ";
        if(tot[3][i] == '\0' )
            cout << "No User";
        else
            cout << tot[4][i] << " ";
    }
}

void settozero( char ***tot )
{
    for(int i=0; i < 5; i++)
        tot[0][i] = '\0';
    for(int i=0; i < 6; i++)
        tot[1][i] = '\0';
    for(int i=0; i < 4; i++)
        tot[2][i] = '\0';
    for(int i=0; i < 3; i++)
        tot[3][i] = '\0';
}


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1