function and input problems

This is a discussion on function and input problems within the C++ Programming forums, part of the General Programming Boards category; I am using a g++ compiler. The program will read from keyboard input or a given file. It will read ...

  1. #1
    Registered User
    Join Date
    Sep 2001
    Posts
    15

    Talking function and input problems

    I am using a g++ compiler. The program will read from keyboard input or a given file. It will read in numbers and put them in the array for sorting later.

    Here is part of my int main (argc, argv[])

    int useFile = 0; // the state of using a given file
    double array[numEle};
    iftream in;
    ...

    if (usefile)
    read_file (in, array, numEle, act_read);
    else
    read_keyboard (array, numEle, act_read);

    -------------------------------------
    function prototype

    void read_file (ifstream& infile, double hold[], numEle, num_read)
    {
    for (int i=0; i <numEle, i++)
    {infile >> hold[i];
    num_read++
    }

    void read_keyboard (double hold[], numEle, num_read)
    { ...cin >>...
    }

    --------------------------------------
    Questions:
    Do I need 2 functions? one for reading files, one for reading keyboard input? I am struggling between ifstream and istream...

    Can two different functions in the same file share the same variable name?

    If I define numEle in main.h as a constant (to regulate the maximam numbers of array items), can I use it in other source files? Is this a Global varialbe? If Global variable is not allowed, how can I pass the array to do input and output in other source files?

    Thank you for your time and helping me through these questions!

  2. #2
    zen
    zen is offline
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    Do I need 2 functions? one for reading files, one for reading keyboard input? I am struggling between ifstream and istream...
    No, you could do something like this

    istream* in;

    if(usefile)
    in=new ifstream("filename");
    else
    in=&cin;

    and then you could use 'in' for either file or keyboard input. When the read was finished you'd have to check what in pointed to and if it's the ifstream then delete it.

    If I define numEle in main.h as a constant (to regulate the maximam numbers of array items), can I use it in other source files? Is this a Global varialbe? If Global variable is not allowed, how can I pass the array to do input and output in other source files?
    I think consts default to static in c++, so you'd have to explicitly declare it as extern in both files it's used in.

  3. #3
    zen
    zen is offline
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    Oh yeah, forgot to mention that if you want to use this method for establishing the input type you'll have to de-reference 'in' before using it -

    *in >>yourVar;

    in->getline();

    etc.

  4. #4
    Registered User
    Join Date
    Sep 2001
    Posts
    15

    Wink input / array size

    Thank you for your help. I have not used pointer in my programs, and I will learn it soon. I still came out with two functions to do the input. So far, it seems to be working at the point.

    Since I gave the size of the array, say 5, if I entered three numbers, the other two spots will be given as "0". Is there anyway I can ask the compiler to just proceed what it got?
    Do I need something like dynamic allocation (which is another new topic)? or I missed some very basic concepts of array processing?

    Have users tell how many items they have may be an idea, but how about in the case that no prompt is given?

    Thank you.

  5. #5
    zen
    zen is offline
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    If your reading from a file you can test for the end of the file and ilmit the input this way with something like

    int i=0;
    while(in>>hold[i++]&& i<numEle)
    num_read++ ;

    but with keyboard input you can test like this but you could take a specific value entered by the user to mean end of input.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. can someone help me with these errors please code included
    By geekrockergal in forum C Programming
    Replies: 7
    Last Post: 02-10-2009, 01:20 PM
  2. still problems with ceasar shift
    By trevordunstan in forum C Programming
    Replies: 2
    Last Post: 09-14-2008, 01:49 AM
  3. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  4. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM
  5. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM

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