C++
Page 1 of 4 123 ... LastLast
Results 1 to 10 of 32

Thread: C++

  1. #1
    Banned
    Join Date
    Oct 2001
    Posts
    263

    Question C++

    ok im working on upgrading a simple encryption program that i had running then lost due to HDD failure. ok but im comming up with a few questions about C++..... well more like how-do-i's really:

    how do i make the program clear the screen fully on its own like i would if i gave the command 'clear' at the prompt?

    how do i get a key from a file then pass it along into a string for use in the encryption, just a char string nothing fancy.

    and im also not fully understanding passing things to functions...... i can use functions and normaly if i need something passed back and forth ill just use global varriables (i know this isnt the best way, thats why im asking) ok you have the protoytpe at top: "int area(int length, int width);" then below the main() you have it again but it can be different?!?: "int area(int yardLength, int yardWidth){"????? im missing something here arent i? thats what it says in my (i know, i know) TY21 book as an example........ how can they be different if its the same thing that your defining insted of declaring......???? im really missing something arent i?

    and i just wanna make sure that i have one little thing strait in my mind also before i go on. when you call a function in the code, then it return a value, the main() (or what ever function called it) treats it like its just what got returned...... right? did i explain that right? i hope so.

    well i suck at coding just yet so ill probly have more questions soon. thanks

  2. #2
    Junior Member
    Join Date
    Apr 2002
    Posts
    5
    hi,
    the answer of your first question is simple, that is use the library function clrscr() at the beginning of your program.

    and answer of your second question is also simple, you can use file pointer ( FILE * fp) to open your desired file in which you have embedded the key and to read the characters from file use fread.
    collect the key in buffer and then use it from there as you like i.e you manipulate it or pass it your func.

  3. #3
    Junior Member
    Join Date
    Jul 2002
    Posts
    10
    Clear Screen: clrscr() with Borland, only way I found with VC++ was system("cls") or one of the Console functions. For Unix, look at curses.

    File: I would suggest using the file streams with C++, they're a lot easier to use (and more stable):
    Code:
    #include <iostream>
    #include <fstream>
    
    using namespace std;
    
    void somecode() {
        ifstream input("myfile");
    
        if(!input) {
            // something went wrong, bail out here
            return;
        }
    
        char key[64]; // or however long your key is
        input.read(key, 64);
    }
    If you don't know how long the key is, use a std::string object.

    Prototype: It's only concerned by the types you use, in this case you pass two ints to it, which is fine (it only uses the names within the function -- in fact you can supply only the type to the prototype and the definition of the function, you just can't reference it like that).

    Return values: The function return value can be used almost anywhere:
    Code:
    #include <iostream>
    
    using namespace std;
    
    int function(int, int);
    
    int main() {
        int i = 5;
        int j = 4;
    
        cout << function(i, function(j, j)) << endl;
    }
    
    int function(int thing, int that) {
        return thing + that;
    }
    Byte Me

  4. #4
    Banned
    Join Date
    Oct 2001
    Posts
    263
    ok, clrscr(); comes up with an error "implicit declaration of function `int clrscr(...)'"

    is it in a different libraby than iostream.h?

  5. #5
    Antionline Herpetologist
    Join Date
    Aug 2001
    Posts
    1,165
    For your first question.
    #include <stdlib.h>

    int main()
    {
    system("cls"); //system("clear"); if you use UNIX/Linux
    }


    For the second one, you should use the classes ofstream and ifstream for file IO in C++. The code that you want would run something like this:

    #include <iostream>

    using namespace std;

    int main()
    {
    char chararray[10];
    ifstream in("Filename");
    for(int i=0;i<10;i++)
    chararray[i]=in.read();
    }

    You prototype or declare a function to let the compiler know that it exists (kinda redundant I know but it has to be done). You define a function when you write the function body later on in the program.

    As for your last question, you've pretty much got it right.

    Hope that cleared some stuff up.
    Cheers,
    cgkanchi
    Buy the Snakes of India book, support research and education (sorry the website has been discontinued)
    My blog: http://biology000.blogspot.com

  6. #6
    Junior Member
    Join Date
    Jul 2002
    Posts
    10
    Originally posted here by LoggOff
    ok, clrscr(); comes up with an error "implicit declaration of function `int clrscr(...)'"

    is it in a different libraby than iostream.h?
    It should be in conio.h for the Borland compiler. There's no mention of it in Visual C++.

    PS: Always use <iostream> not <iostream.h> which is the older, deprecated style.

    PPS: cgkanchi -- why use a loop? Why not just read all 10 at once?
    Byte Me

  7. #7
    Banned
    Join Date
    Oct 2001
    Posts
    263
    hooah! that system stuff is right on. can you pass any commands to the shell thru it?

    i dont use borland, i use G++.....i originaly used borland but then i went to linux and borland became pointless to use when ive got the defacto linux compiler already there.

    also whats this using namespace std? is that just like a note or is that something that i should take note of and start using....... whats it do why should i use it?

  8. #8
    Member
    Join Date
    Jul 2002
    Posts
    65
    Originally posted here by LoggOff
    hooah! that system stuff is right on. can you pass any commands to the shell thru it?
    Of course, that's what it's there for.

    i dont use borland, i use G++.....i originaly used borland but then i went to linux and borland became pointless to use when ive got the defacto linux compiler already there.
    For sure! Good choice.

    also whats this using namespace std? is that just like a note or is that something that i should take note of and start using....... whats it do why should i use it?
    The answer to that question lies here: http://www.cplusplus.com/doc/tutorial/tut5-2.html

    Originally posted here by cgkanchi

    #include <iostream>

    using namespace std;

    int main()
    {
    char chararray[10];
    ifstream in("Filename");
    for(int i=0;i<10;i++)
    chararray[i]=in.read();
    }
    Dont forget about fstream, and you must return a value.

    --Sudo

  9. #9
    Junior Member
    Join Date
    Jul 2002
    Posts
    10
    Not from main() you don't

    Somewhere in the standard, it says that if you don't have an explicit return statement, the compiler should substitute code for "return 0" at the end

    PS: G++, good one, use it frequently
    Byte Me

  10. #10
    Member
    Join Date
    Jul 2002
    Posts
    65
    Originally posted here by parksie
    Not from main() you don't

    Somewhere in the standard, it says that if you don't have an explicit return statement, the compiler should substitute code for "return 0" at the end
    Awesome, I didn't know that. I figured that it would only do that if it was void main()

    Good call Parkside.

    --Sudo

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •