Thread: C++
-
July 21st, 2002, 02:24 PM
#1
Banned
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
-
July 21st, 2002, 05:03 PM
#2
Junior Member
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.
-
July 21st, 2002, 05:19 PM
#3
Junior Member
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 
-
July 21st, 2002, 05:22 PM
#4
Banned
ok, clrscr(); comes up with an error "implicit declaration of function `int clrscr(...)'"
is it in a different libraby than iostream.h?
-
July 21st, 2002, 05:53 PM
#5
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
-
July 21st, 2002, 06:31 PM
#6
Junior Member
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 
-
July 21st, 2002, 07:42 PM
#7
Banned
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?
-
July 21st, 2002, 08:12 PM
#8
Member
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
-
July 21st, 2002, 08:26 PM
#9
Junior Member
-
July 21st, 2002, 08:34 PM
#10
Member
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
-
Forum Rules
|
|