tux4life 2,072 Postaholic

A roman to decimal converter, no validity checking, so inputting an invalid roman number will certainly just yield a wrong result.

tux4life 2,072 Postaholic

First thanks to add code tags to your post.
Second, (and I know this may sound harsh): your code is the ideal example of how one shouldn't program it, but no fear, we're here to help you and give you a better insight in how you should do it.

To begin I'll start to comment on some bad things in your code:

  • You've multiple include directives in your code, but they aren't all necessary (you can leave out half of it).
  • You make use of deprecated header files, are you maybe using an ancient compiler? (anyway, here's a link which will get you started getting a decent compiler and migrating your code to standard C++: http://siddhant3s.googlepages.com/how_to_tell_rusted_cpp.html)
  • You use void main(), per standard you should use int main() (also check out the link in my previous post).
  • You're also making use of global variables, agreed they can be useful in very rare circumstances, but for a simple program like this you better avoid using them, not only because your functions are making use of variables which are defined outside them, but also because those variables' values could be accidentally changed, causing your program to fail.

Could you maybe first of all improve your code, keeping the above list in the back of your mind?
(a little try, and even the smallest effort is okay for me, but if you want to impress me (and others), and are willing to improve yourself, then just …

tux4life 2,072 Postaholic

After 25 posts it's certainly time to become familiar with something which is utterly important on this forum.
Most people call it "code tags".

Even with all the help available on code tags, you don't seem to know about them (which unfortunately is a bad thing).
Just a small list on what places you were encouraged to find out what code tags are:

  1. in the Rules you were asked to read when you registered.
  2. in the text at the top of this forum.
  3. in the announcement at the top of this forum titled Please use BB Code and Inlinecode tags.
  4. in the sticky post above titled Read Me: Read This Before Posting
  5. any place CODE tags were used.
  6. Even on the background of the box you actually typed your message in.
tux4life 2,072 Postaholic

Ouch, my eyes!!

Please use BB Code and Inlinecode tags
What's the deal with void main()?

Only to shut about the use of deprecated header files and the unportable conio library, and several other includes which you better don't mix or just aren't making use of.

tux4life 2,072 Postaholic

Don't use l33t speak
Post your query (together with your code) into the appropriate forum (don't forget to use code tags).

tux4life 2,072 Postaholic

>any idea?
I actually don't quite get the question of your assignment.
Also I see no code I can help you with.

But I know some interesting code which can help you to get help, here on this forum:

Effort opEffort;

if(opEffort == null) {
    System.out.println("You should urgently stop right here and think about your assignment.");
    System.out.println("Give it a try before moving on to a forum to ask for help.");
    System.out.println("When you're finished writing some code, and then get stuck, move on to a forum,");
    System.out.println("post down your code, and ask for help.");
}

(The pseudo-code can be found here).

tux4life 2,072 Postaholic

a way you can do this would be to have a void check function inside a while loop in your main function.

void CheckInput(char[80], bool*);

int main()
{
       char[80] cake;
       bool good = false;
       while (!good)
       {
              cout << "please enter a cake: "
              cin >> cake;
              CheckInput(cake, &good);
       }
       //...
}

void CkeckInput(char[80] cake, bool* good)
{
       //  run a switch checking cake agiants the valid choices
       // if the check succeds then set good to true otherwies do nothing
}

im not sure if you are using pointers though so if you are not this solution wont work.

Since cake is a character array, the following instruction will allow an array overflow (in case there are more than 80 characters entered): cin >> cake; ,
you can prevent this by for example using cin.getline(cake, 80); or cin >> setw(80) >> cake; (but for this one you'll need to include the iomanip-header)

tux4life 2,072 Postaholic

b. The program returns 0xFFFFFFFF as -1 in decimal. However when I use a Hex-Decimal converter I get 0xFF as 255 and 0xFFFFFFFF as 4294967295?

In my previous post I forgot to point out that 0xFFFFFFFF has a binary representation wherein every bit is one, if such a binary value goes into a signed integer variable, then the variable contains a value equivalent to -1 in decimal.
If you read about the two's complement (A must read! Google it up), you'll be able to understand why this is the case :)

Also thanks to Tom Gunn for pointing out a mistake in my post and providing an excellent correction :)

tux4life 2,072 Postaholic

But, what about my other question. Why do I get 0xFFFFFFF and -1 as the decimal?

Oh sorry, I forgot to answer that one :P
Just as a convenience for anyone who's reading the thread:

b. The program returns 0xFFFFFFFF as -1 in decimal. However when I use a Hex-Decimal converter I get 0xFF as 255 and 0xFFFFFFFF as 4294967295?

Well, this is because in your code you use signed ints (that is what you get when you declare a variable by just using [B]int[/B] [I]a_variable_name[/I] ).
As int exists in both signed and unsigned flavors, your hex-converter was using an unsigned int to do the conversion, while in your code you have a signed int, as the name tells you it's signed, which means that it can have a sign, this isn't the case with unsigned integers which cannot have a sign.
A very non-technical explanation (but maybe less correct) is: with signed integers the high-order bit signifies whether the number is negative (high-order bit is one), or positive (high-order bit is zero).
With unsigned integers, the high-order bit is used to store another range of possible numbers.
Important to mention is that both signed and unsigned types do consume the same amount of bytes, this is also why an unsigned integer variable can store twice as huge positive values as a signed one, while a signed one can also store negative numbers.
Maybe something interesting for you to google up is: two's …

tux4life 2,072 Postaholic

Question:
a. Why does the program deliver a 8-bit hex number (0xFFFFFFFF) when the input is 0xFF and when the sizeof() delivers char as 4-bit ?

This code is wrong when you want to display the size of a char (in bits):

printf("Size of char: %d-bits\n", sizeof(char)*4);

Sure it will report that a char variable consists out of 4 bits, but there's something wrong with your code, for example: how did you come up with this magic 4? For what does it stand? Second: the sizeof operator will always (under all circumstances, on all platforms return 1 if you invoke: sizeof(char) ), so this multiplied by 4 will always give 4 as the result.
The correct way to get the correct size in bits per byte (a char is always 1 byte) is by using the CHAR_BIT macro as defined in the limits.h header (you have to include this file if you want to use the CHAR_BIT macro, you can do this by adding this directive to the top of your program: #include <limits.h> ).

The correct way of getting the size (in bits) of a variable of type char and or integer in C:

char:

printf("Size of a variable of type char (in bits): %lu", CHAR_BIT);

integer:

printf("Size of a variable of type integer (in bits): %lu", CHAR_BIT*sizeof(int));

What exactly does CHAR_BIT represent?
The CHAR_BIT macro represents the number of bits in a byte (a variable of type char does always …

Tom Gunn commented: Good. +13
kvprajapati commented: Excellent! +17
tux4life 2,072 Postaholic

In your previous thread you've already been suggested to use code tags, perhaps you missed it?
Anyway here's the link which will directly bring you to the page where the use of code tags is explained (read it, I'm sure that when you've read it you'll say: "Ohhh, was it that easy?")

P.S.: Instead of manually entering the bb-tags to include code, you could also just simply use the button labeled: (code).

tux4life 2,072 Postaholic

I already know this kind of game: we make suggestions and never it's a good suggestion because either it's too difficult, or too easy, or too boring, or whatever else.

You know what you can and can't, you also know what interests you, combine your creativity with these two elements and I'm sure you'll find something.

tux4life 2,072 Postaholic

Great, you've got some code, and what exactly is your question?

tux4life 2,072 Postaholic

In addition (personally one of my favourite links):
http://cboard.cprogramming.com/c-programming/88495-development-process.html

yellowSnow commented: Good link. That methodology should be smacked into the heads of every newbie would be programmer. +8
tux4life 2,072 Postaholic

Post down your code please.

iamthwee commented: nice signature. +22
tux4life 2,072 Postaholic

>I use only linux and I am unable find the compiler.
>Any help will be much appreciated as I am stuck.

AFAIK it isn't available for Linux, however you might be able to get it work if you use Wine, or a very outdated Turbo C++ compiler under a DOS emulator.
I personally wouldn't recommend any of the above, but you said that other compilers aren't allowed for this project, could you maybe explain why?
Another option is maybe to run Windows into a Virtual Machine under linux, and then run the Windows version of the Free Borland Compiler on top of that VM (this guide might be helpful then, while installing the compiler) :)

tux4life 2,072 Postaholic

After some research,I decided to go with "Head First Java,2nd Edition".
It's not as the book I was looking for,but it seems it is one of the best books for learning Java.
Thank you everyone.

I also own a copy of that book and I have to say one thing: It's a great book!

tux4life 2,072 Postaholic

After some time searching around the forums for a thread about an e-book for crash course from C++ to Java and no luck I decided to post a thread about it.

Seems like I have a lot more luck than you:
http://www.janiry.com/bruce-eckel/

(the Java e-book is for and older Java version, 1.4 I believe, but is still useful)
Have success reading them :P

kvprajapati commented: Great choice. It's a really a good book. +16
tux4life 2,072 Postaholic

This won't fix your problem, but it will fix a memory leak:
if you write this in your code: char *cool=new char[20];//the name pointer then at some point in future your program doesn't need the memory anymore (for example just before your program terminates, is the memory still needed then? I guess not).
So you'll want to add this line at the end of your main() function: delete[] cool; , this line will free up the previously allocated memory :)

(And yes, I know: some operating systems free up/deallocate all the memory which was associated with the program, at the time when the program terminates, but it doesn't make the step of cleaning up the manually allocated resources unnessesary: not all OSes do offer you this feature, and not manually deallocating your allocated resources can result in a memory leak either when your program is running or even at the moment when your program isn't running anymore (in case your OS doesn't provide some kind of garbage collection))

So, moral of the story: when you manually allocate memory using new then always also manually deallocate it using delete (this will also ensure portability).

tux4life 2,072 Postaholic

Could you provide us with the file you tried to encrypt/decrypt using your program?

tux4life 2,072 Postaholic

Dear Sir,
I am facing packet loss, in my connection, when i press the post quick reply button, the page didnt downloaded properly, so i refreshed it, i.e why it was posted double.

Oh yes, and how do you explain the several minutes difference between all those posts then?
The post above this one (if not already removed by a moderator) is just the evidence that it hasn't something to do with your connection (it just again demonstrates you haven't read my previous post(s), otherwise you should have known you should use code tags, but no, copy-paste seems easier to you).

tux4life 2,072 Postaholic

Okay, I really have enough of your double postings in your thread:

a) you won't get help faster by doing this
b) you just make your own thread unreadable
c) you waste your time, use that time to try figuring out the solution yourself
d) code tags seem useless to you, how did you miss using them?
Information about code tags is found all over Daniweb:

1) in the Rules you were asked to read when you registered
2) in the text at the top of this forum
3) in the announcement at the top of this forum titled Please use BB Code and Inlinecode tags
4) in the sticky post above titled Read Me: Read This Before Posting
5) any place CODE tags were used
6) Even on the background of the box you actually typed your message in!

e) several other reasons: like asking to create the code for you

I don't see any reason why I should help you, the reasons why I shouldn't help you are denoted above.

I'm going to quote what I already have said before in this thread:

First of all I want you to NOT PM me with your questions to do it for you, it won't work, I'm a free person and I'm free to do what I want, and not to do what I don't want (let that be clear).
What …

VernonDozier commented: Good comments, good links. +25
tux4life 2,072 Postaholic
if(currentChar<(char)'0' || currentChar>(char)'9'){
   cout << "Invalid Pin" << endl;
}

Explicitly casting to a char isn't even needed here, the following code is equivalent:

if(currentChar<'0' || currentChar>'9'){
   cout << "Invalid Pin" << endl;
}
tux4life 2,072 Postaholic

How do you make an array dynamically expandable?

Using a 'normal' array this isn't possible in C, you could maybe try your hands on a linked list?

kvprajapati commented: Good suggestion. +15
tux4life 2,072 Postaholic

Sorry, here it is

int frameScores[];

frameScores[0]=1;
frameScores[1]=2;
for (int j=0;j<(sizeof(frameScores)/sizeof(frameScores[0]));j++) {
	// this wont execute since j =0 and its size is =0    
}

But how can you compile that? It isn't possible, my compiler even produces error messages, which is logical in such a case:

Borland C++ 5.5.1 for Win32 Copyright (c) 1993, 2000 Borland
tttt.cpp:
Error E2449 tttt.cpp 6: Size of 'frameScores' is unknown or zero in function main()
Error E2449 tttt.cpp 6: Size of 'frameScores' is unknown or zero in function main()
Error E2109 tttt.cpp 11: Not an allowed type in function main()
*** 3 errors in Compile ***

You should rather do it like this:

int frameScores[] = {1, 2};

for (int j=0;j<(sizeof(frameScores)/sizeof(frameScores[0]));j++) {
	// this wont execute since j =0 and its size is =0    
}
tux4life 2,072 Postaholic

i have posted above the code itself.

I don't believe that's the smallest compilable solution to reproduce the problem.

tux4life 2,072 Postaholic

I guess you mean all characters other than 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 when you say: any non-numeric character.

Why not just get your number like this:

int num;
cout << "Enter a number: ";

if( !(cin >> num) )
{
    // Reading a number failed, most likely due the fact the user
    // didn't enter a valid number.
    
    /*
         Put your code to handle such a situation here
    */
}

or use a stringstream:

stringstream ss;
char cnum[] = "1234";
int inum

ss << cnum;

if( !(ss >> inum) )
{
    // Again: extracting the number failed
    // Most likely due the fact that you're trying to read
    // something else than a number.
}

You need to include the sstream header in order to make the above snippet compile, you can do this by adding the following include directive to your program: #include <sstream> .

tux4life 2,072 Postaholic

No,

how do i make it expand without exact array length?

Do I understand this correctly? You want to create an array which' size can increase dynamically as elements are added or removed?

Again: please post down the smallest compilable code which I can use to reproduce your problem.

tux4life 2,072 Postaholic

Did you declare the array like this:

int x[] = {5, 7, 9, 2}; // put some values in it

?

[edit]
Could you provide me with the smallest compilable solution which doesn't work correctly on your system?
[/edit]

tux4life 2,072 Postaholic

hi,

i know this is a basic question and i have tried searching GOOGLE but it does not solve my problem.

I have a array which is declared as

int frameScores[];

but when i tried to get its size after adding elements by using sizeof, i get a 0.

I'm sure that the array has values but i just can't get its length which i normally get by using the sizeof method.

Any possible solution?

You can only declare an array like this:

int frameScores[];

if you directly initialize it, otherwise that declaration is impossible (there's no way for the compiler to find out the length of the array you want to make).
So assuming you did it the correct way, you can get it's size like this:

sizeof frameScores / sizeof *frameScores;
tux4life 2,072 Postaholic

^^ Good point :D

tux4life 2,072 Postaholic

Exactly. One of the other typos I still make now and then is : if (a=1) versus if (a==1) Which can also give you a headache..

Yea, but if you write it like this: if(1 = a) your compiler will report it as an error, saving you much headache :P

tux4life 2,072 Postaholic

How I would go trough the process of designing such a program:
Create a simple menu, which will allow the user to choose whether he wants to encrypt or decrypt. (you've done this)

Create two separate functions: one to encrypt and one to decrypt.
(you've done this)

But here's where my approach starts to differ from yours:
As I am, I first of all test it without all the file handling, this means that I would let the user enter a line of text on the keyboard, and print out the encrypted/decrypted sentence.
When this works correctly, I move on to writing/changing code to be able to operate (encrypt / decrypt) successfully on files.

tux4life 2,072 Postaholic

You mean that the output on your screen has to look like this: \n ?

In that case, you just print the following string to your screen: "\\n" :)

tux4life 2,072 Postaholic

Hi johnab2112,

Welcome to Daniweb I hope you're liking your stay here as much as I do.
I can assure you that you can learn loads of things here from very knowledgeable people (only a minor effort is required).

Looking forward to see you participating in the discussions here :)

P.S.: Have you already read the forum rules? If not, I suggest you to have a look at them (this will most likely prevent you from running into trouble with a forum moderator).
It's also recommended to have a look at the forum announcements and the sticky threads (sticky threads are the uppermost threads in a particular forum).

tux4life 2,072 Postaholic

very right sir finally i will be running this program in linux

Is that the only reason you want to undertake all the work to write an encryption program?
Even if TrueCrypt is available for Linux as well ?

tux4life 2,072 Postaholic

but the problem is that u r using oops object oriented programming and i told u sir that i m new to c basically a biginer.

As far as I know I only used cout, which you also did (you even mixed C with C++ I/O (which is also OOP), so I don't see where the problem is :)

Do you want to rewrite the whole program in C or what (as your thread seems to be in the C++ forum) ?

And I don't care you're a beginner, as long as you're willing to put effort in it I'm happy :)

[edit]
Just out of curiosity, you're a Network/System Administrator, but why is there need to write your own encryption program then?
Can't you just use something which you can safely rely on?
Maybe something like TrueCrypt ?
[/edit]

tux4life 2,072 Postaholic

I have to agree with the person who said that you actually can only use one computer at a time: though you might be using VNC or RDP the remotely connect to a computer, you'll most likely be focused more on computer A than on computer B.
I can't imagine how clunky it would be to actually use two computers at the same time (and I mean that you use them both exactly the same amount of time, to really perform tasks, nah that would be far too difficult for me).

tux4life 2,072 Postaholic

Dear Sir,
I want to get plain text from a file
then
encrypt it and save it in a file

and then
decrypt it and save in the file
and compare the decrypted text with the orignal plain text

so that i can check wether the program is working fine or not

please help me out of this big problem

First of all I want you to NOT PM me with your questions to do it for you, it won't work, I'm a free person and I'm free to do what I want, and not to do what I don't want (let that be clear).
What I want is to help you, what I don't want is spend my time to create a full-fledged encryption solution for you (or in other words: I rather provide paths to the solution, not the entire solution itself), so that you can turn it in, and pass (or fail :P) using my work, that wouldn't be fair, not? The point of the assignments you get is to show off your knowledge, show what you can. This means that you have to do the work, passing using someone else's work won't get you far in real life. The point is that you do the entire thing yourself (with some small help and hints from the forum members (the so-called "gurus") here), and that you finally (when you finished the program) can say: I did it!

[edit]
So …

kvprajapati commented: Well said! +14
tux4life 2,072 Postaholic

example:

cin >> KeysPressed; 
if (KeysPressed=="K");
cout << "you pressed K";

it print "you pressed K" no matter what you you just pressed

Right what I was thinking, you need to tell your compiler what code you want to associate with the if-statement, so this is incorrect

cin >> KeysPressed; 
if (KeysPressed=="K");
cout << "you pressed K";

if you want to associate the line cout << "you pressed K"; with the if-statement.
You can easily fix it, by using an if as follows:

cin >> KeysPressed; 
if (KeysPressed=="K")  // semicolon removed
   cout << "you pressed K"; // this statement is now associated with the if
// all code below is NOT associated with the if

As you see in the above code example, you can only associate one line with the if-statement, however there's another approach: associating a block of code with an if-statement (a block of code starts with a { and ends with a }), example:

cin >> KeysPressed; 
if (KeysPressed=="K")
{
   cout << "you pressed K";
   // all the other code you want to associate with
   // this if-statement must be put between the curly brackets
}
// all the code here doesn't belong to the if anymore
tux4life 2,072 Postaholic

but somehow it tends to do stuff no matter if the string value is K or something else..

I don't get that, what do you mean by this?

[edit]
I think I got it :)
Could you otherwise post the code of your whole program?
For now I can only guess that it has something to do with the semicolon at the end of the if-statement: if (KeysPressed="K")[B];[/B] :)
[/edit]

tux4life 2,072 Postaholic

how to make something like this work?
if (KeysPressed="K");

The part on how to fix it:
I see you want to compare a string-variable with a string literal, but you're doing something wrong: you're using the = -operator, this operator is also called the assignment operator, and is used if you want to assign a value, but in this case you want to compare two values, and not assign a value right? So how do we fix it? Simple, to compare two values, you use the == -operator.
So, in order to make your code work (in a more logical way, as your code is syntactically correct), you must change if (KeysPressed="K"); to if (KeysPressed=="K"); .

The more in-depth coverage:
The reason why = doesn't work is simple: = is an assignment operator, which you can't use to compare two variables (at least in C, C++, and some other languages; yes I know, in BASIC-like languages, you can compare using = , but as of C/C++ this isn't the case).
If you write if (KeysPressed="K"); , you assign the string "K" to the string variable KeysPressed, that is: variable KeysPressed now contains the string "K" , as of the assignment operator, it always returns the assigned value, this is why your program did compile, but did not work correctly, in this line of code: if (KeysPressed="K"); , "K" will be used to control the if-statement, "K" is a non-zero value, which means: true, so …

ddanbe commented: marvelous explanation! +12
tux4life 2,072 Postaholic

Tux4life: im sorry, what did you want me to do with the text editor?

Well, you know the text editor? I meant: the program where you write your code in, launch that program, open the file containing the code of your program, copy that code, and paste it in a new post in this thread :).

tux4life 2,072 Postaholic

To the OP:
No offense, but I can't trust the code you posted down.
Wait, let me explain, I came across a line which looked quite strange to me, and which will never let me compile this program: skrivtraed << placering[B],[/B] "\n"; .
Could you otherwise just copy and paste your code directly from your text editor?
(Try to compile it first, only when it compiles, you copy and paste it again, otherwise you post down the error messages together with the not-compiling code).

As it seems like you're able to compile that:
Are you sure that the file Seek and Hide DB.ini exists in the same directory where you run your program from (at the moment when you choose option 1)?
Note that you're opening the file in appending mode, which means that the file has to exist already before you can add any other stuff to it.

Also, you don't seem to check whether the file indeed could be opened, what if you try to read/write from a file which couldn't be opened?
You can easily add a check, like I'll demonstrate in the following example:

ofstream fout("test.txt");

// Check whether the file could be opened succesfully
if( !fout.is_open() )
{
   // The file couldn't be opened
   // Put your code to handle such a situation here
}

When using a check like shown above, then (and I can never say it enough times) make sure that you …

Salem commented: It's all good :) +36
tux4life 2,072 Postaholic
tux4life 2,072 Postaholic

Could you maybe explain us first what the goal of the program you posted is? (I mean: what output do you expect, etc.)

To answer the question in your signature:

Yo god, where do i report a bug?

Start a new thread about the bug in the DaniWeb Community Feedback forum (check first whether the bug you're going to report isn't already reported!).

To finish this post:

Help me fail searching (if statement - ****** up)

Sorry, but I'm unable to see the 'link' between this and your code, could you maybe explain this, or choose a more appropriate thread title in future?

tux4life 2,072 Postaholic

Damned, I also had that impression, but it could just be that I (wrongly) assume that newer is always better/faster :P
But in this case I really have to admit that I've the feeling that it's working a lot faster.

tux4life 2,072 Postaholic

Bug Fix!
To begin: the mystrstr function takes two pointers as its arguments, one to the string where it has to search in, and one to the substring where it has to search for.
But what if the substring is: "" ?
In that case the function won't return a null-pointer, instead it will return the same pointer as it received as its first argument.

So, how to fix this nasty bug?
Change this if: if( !*q_tmp ) to if( *p == *q && !*q_tmp ) :)

tux4life 2,072 Postaholic

This is how I would write the standard library function strstr if I had to implement it from scratch.

tux4life 2,072 Postaholic

>Wait. bitset has an OR operator? Didn't realize that.
Yes sure it has one, all the operators it supports are listed here:
http://www.cplusplus.com/reference/stl/bitset/operators/
(and bitwise OR-assignment is one of them :) )