tux4life 2,072 Postaholic

Ya I don't know why he wants us to ignore the null terminator but he does. So I am at a standstill with this for now I guess. any idea how I could code this for loop? I cant really go any farther without it.

Well, first you need to create a good skeleton for your for-loop:

for(size_t i = 0; i < sz; ++i) {
   // Other code here        
}

This for-loop will run the same amount of times, as the amount of characters there are in the original string (null terminator not included).
This is exactly what we need.

Now we have the for-loop skeleton, we can start adding features to copy the whole thing to a non null-terminated character array inside the class:

// Our copy routine:
for(size_t i = 0; i < sz; ++i) {
   p[i] = original[i];
}

In the above example I assume that p is a pointer to the newly allocated memory to hold the non-null-terminated copy of the original string.
original in my example is the string you pass via the class' constructor.
sz is the value you get by using the strlen-function like this: sz = strlen(original); (I want to note that sz has size_t as it's type).

tux4life 2,072 Postaholic
tux4life 2,072 Postaholic

Wouldn't you just use a "include" statement and include the second file in the first, then it's just a normal compile and the compiler will handle what files need to be compiled due to direct dependencies, or am I just not understanding the question????

I've to admit that I've never seen this before.
But I have a strange feeling that this is an inappropriate way, not sure though why, I think it can cause naming conflicts.

tux4life 2,072 Postaholic

Well, as long as you hold the correct number of characters in a variable, this would be no problem, but a null-terminator takes 1 byte extra space, and you won't have to add several other routines to make the string display correctly on the screen.

But, of course, if his assignment is to do it without a null-terminator, then my advise probably doesn't make much sense :)

>The only reason to do this that I can think of is to make the STL string object independent of any C coding requirements.
Yeah, probably his teacher wants that he avoids functions from the standard function library.

tux4life 2,072 Postaholic

>how to compile c++ program when we r using two different functions which are defined in different file(s) under same directory in linux gcc???

First look at Sky Diploma's post.

Second:
Well, I would type: [B]g++[/B] -o [I]outputfile[/I] [I]file1.cpp[/I] [ [I]file2.cpp[/I] ] [ file[I]x[/I].cpp ] The square brackets indicate an optional element and outputfile is the name you want to give to your executable file.
After compiling you can run your program by entering: ./outputfile on your keyboard.

@Sky Diploma:
The gcc command invokes the C compiler, not the C++ compiler.

tux4life 2,072 Postaholic

>It should set the size of the string to the number of characters in the C-style string (not including the null character), allocate a string text array of that size, and then copy the characters of the C-style string into the string text array.

Why would someone want to do that?
You have to allocate space for the null terminator as well.
Otherwise you'll run into problems while displaying the string using cout for example.

tux4life 2,072 Postaholic
tux4life 2,072 Postaholic

My problem is to output the array single time. If i use a for or a while loop ,each array element is outputted sperately. I want to output all the necessary elements together.

Oh sorry, I guess I misunderstood your question.
I understand it now, you could for example use a string and a stringstream to achieve that.

tux4life 2,072 Postaholic

i just edited my post. The program is working properly but i want to output all the values together and not using for statement.
I am submitting to a online judge so should ouput it in one go

Well, use a while loop then :)
You can let your while loop do the same as a for loop:

A for-loop in general looks like this:

for( [B]PART A[/B]; [B]PART B[/B]; [B]PART C[/B] )
{
    // Your code
}

You can easily make your while loop behaving in the same way by putting the parts in another order:

[B]PART A[/B]
while( [B]PART B[/B] )
{
    // Your code
    [B]PART C[/B]
}

:)

tux4life 2,072 Postaholic

>I wrote a code to get factorial for a number uptil 100.
If you even can't print out the result, how can you be sure then that your code is working?

tux4life 2,072 Postaholic

Hey thanks a lot for all your comments! Really helpful.
one thing that i would like to mention

Edit:: Another thing that is annoying me is that you included a lot of useless comments into your code:

actually i commented out the statements which i was previously using in the program that I made.

Yes, yes, I know.
I wasn't complaining about the the out-commented statements, I only wanted to notify you that comments in your code like: // end of main are just unnecessary clutter in your code, clutter which serves no useful purpose :)
But of course, when your code reaches its final stage, I'd recommend you to remove the unneeded out-commented statements from your code, but while developing, out-commenting a/multiple statement(s) is always useful.

Edit:: I exaggerated maybe a bit when I said: "a lot of useless comments" :P

tux4life 2,072 Postaholic

WTF?
Seems like you're even not using a compiler, but an interpreter...

tux4life 2,072 Postaholic

Read the information on the web page below very carefully, and then you can get started by upgrading your code to more decent C++ code.

http://siddhant3s.googlepages.com/how_to_tell_rusted_cpp.html

tux4life 2,072 Postaholic

http://www.geekpedia.com/KB30_How-can-I-pass-command-line-arguments-while-debugging-within-Visual-Studio-.NET.html
(The steps are the same for your version of Visual Studio)

Edit:: This link is not to demotivate you, it doesn't require much reading (only one line of text) and is very straightforward :P

tux4life 2,072 Postaholic

problem solved!!!

Maybe in future, you could mention how you solved your problem, so others, who get to this page (for example by using Google (everyone's friend :P)), having the same problem can see how to fix their problem.

tux4life 2,072 Postaholic

>I also want to read from the passwords file and check if the password has been already generated...... If yes, then generate a new password .....
Well, you could for example at the start of the program, read out all the previously generated passwords from the file, and store them into a vector.
When generating a new password, you should loop through each element of the vector (containing all the previously generated passwords), to check whether the newly generated password wasn't already generated before.
If so, then you generate a new password, and repeat the above process, until you have a new 'unique' password.
When you've obtained such a new 'unique' password, you can write it to the file.

Note:: When your program doesn't directly exit after generating passwords, and for example asks the user to generate a new one, then be sure to every time add the newly generated pass to the vector, to avoid to always have to reread the the entire passwords file's contents.

Edit:: Maybe a tip to make the generated passwords more random:
You could maybe let the user enter a blurb of text on his keyboard (no matter what), and let an algorithm operate on this "blurb of text".

Edit:: Your code is using a lot of deprecated header files, maybe consider migrating your code to standard C++ ?
Here's a link to a nice guide: http://siddhant3s.googlepages.com/how_to_tell_rusted_cpp.html

Edit:: Deprecated headers in your …

kingben commented: awesome!!! +1
Salem commented: Nicely done +36
tux4life 2,072 Postaholic

>The type of the array is int, so the size of the array is 10 * 4.

To the OP:
Yes, on most implementations, this is the case, but certainly not on all.
In that example you assume that an integer is 4 bytes.
You can get the exact size, by multiplying the number of elements in the array by sizeof(int) (or you can just put the following instruction in your program: [B]sizeof(array)[/B] , but this will only work with arrays which aren't on the heap of course)

(Tom, I'm sure you know that, but maybe the OP doesn't, though on his implementation it seems to be that integers are 4 bytes)

tux4life 2,072 Postaholic

Buddy, if he start from 30 in reverse order, then also he need to check if the value is between 3 and 30 or not. This will only increase the confusion..

Depends on the way how you implement it, if you each time decrease with 3, then you only have to check whether it's higher or equal to 3.

tux4life 2,072 Postaholic

>i,ll b thankful to you
Oh yes??
http://www.daniweb.com/forums/post912553.html

tux4life 2,072 Postaholic

i very well knw tht..
n first plz if u cn stp being tht rude..

i hv been successful in making the code tht helps me do the complete task bt for a single file...
nw hw do i manage to gt all the files n do the searches..??

He isn't rude, he is only trying to help you, if you don't want that then you shouldn't waste your time here.

even after posting the code if none can help..well fine then..

anyways thanks...

Yes, you have posted your code, but we need to be able to do exact the same things as you do, to get a better understanding of the whole problem, if you'd for example posted the .CAP file, we can see exactly how your program is dealing with it.
BTW, Have some patience, we're not mind readers, we're not clair-voyant, instead we're just normal humans (I hope so :P), like every other human (with minor exceptions :P).

Anyway, I suggest you to read this: http://catb.org/esr/faqs/smart-questions.html

tux4life 2,072 Postaholic

And... what's the point of having more than one thread about the same thing?
http://www.daniweb.com/forums/post911481.html

If your problem is so urgent, then you better spend your time to create your program, instead of double posting.

tux4life 2,072 Postaholic

Probably there are some additional files which belong to the .exe as well (images, etc.), put them all together in a .zip file and upload the zip-file to Rapidshare :)

tux4life 2,072 Postaholic

How could one share a .exe that loads textures. I am using
c++ and openGL. I want to share the .exe that I made using
rapidshare.

Well, isn't the homepage informative enough?
It describes you what to do:
1. Click the browse button: browse for your .EXE file you want to share
2. Click the upload button
3. Wait
4. Give the rapidshare link to the person with who you want to share this exe.

tux4life 2,072 Postaholic

>For my stringSize I have the type declared as a char* in my header file so I would use that instead of size_t correct?
Nope, to store a size, you don't use a char pointer, instead you use size_t (you can compare it to an unsigned int).

tux4life 2,072 Postaholic

Corrections:

MyString::MyString(const char* s)
{
    [B]// don't forget the type:[/B]
    [B]size_t[/B] stringSize = strlen(s);

    [B]// you need a pointer to the newly allocated memory:[/B]
    [B]// don't forget to allocate memory for the null-terminator as well:[/B]
    [B]char *p[/B] = new char[stringSize[B]+1[/B]];

    strcpy ([B]p[/B], s);
}
tux4life 2,072 Postaholic

For getting the size, you can already use the strlen function:

size_t sz;
sz = strlen(s);

You can use new[] and delete[] for the memory allocation to hold the desired string.

After that, you copy the string passed via the argument to the newly allocated memory, use strcpy.

Note: Within your object you'll do some dynamic memory allocation.
You'll probably want to implement a destructor, a copy constructor, and overload the assignment operator as well.

tux4life 2,072 Postaholic

Unfortunately my instructor wants the destructor included in the program, so I have to figure out why it's not working.

Sorry, I had edited my post, but probably you haven't seen my edit, so I'm including it here:

Edit:: ^^
Actually this is just giving a compiler error because you don't call a destructor like that, you have to specify the object as well, for example: a.~Question(); , but remember that in general, calling a destructor manually is not recommended, and practically never needed.

tux4life 2,072 Postaholic

Well, my first remark is that you don't have to call a destructor explicitly, it will be automatically called when the object goes out-of-scope (this is for example when you shut down your program), so don't write: ~Question(); anywhere in your code.
Edit:: ^^
Actually this is just giving a compiler error because you don't call a destructor like that, you have to specify the object as well, for example: a.~Question(); , but remember that in general, calling a destructor manually is not recommended, and practically never needed.

Also, don't use system("pause"); , it's quite heavy on system resources, it's better to use cin.get() instead of it.
(For a complete overview about the bad things of system("pause"); , I suggest you to read the following information: http://www.gidnetwork.com/b-61.html).

These instructions aren't valid in the code of your destructor, because the arrays aren't dynamically allocated in memory:

delete[]correct;                 
delete[]answers;

Just remove these statements from your destructor.
The arrays will be cleaned up automatically (because they aren't dynamically allocated).

tux4life 2,072 Postaholic

What??! Already the 5th post and nobody has told the OP to use code tags?

To the OP:
http://www.daniweb.com/forums/announcement8-3.html
(read that information please, before making any other post, containing code snippets)

tux4life 2,072 Postaholic

Hi linkingeek, welcome to this forum.
Could you please post using code tags?
This makes the code much more readable.

tux4life 2,072 Postaholic

Well, with me, your code correctly writes to the log file (I'm using MinGW as compiler), though I couldn't fully test the code because I don't have the input file: Square_Equals_Task.tex.

The log-file created on my computer looks like:

The begining.
Before while.

What you could try is: create a new source file, copy all the code from your current file into it, compile the new file, and see whether the newly generated executable runs fine.
This may sound ridiculous, but I've seen such a problem before, and guess: it worked!

tux4life 2,072 Postaholic

You might want to try recompiling your source.
In case recompiling doesn't solve the problem: Could you post your whole code, so I can check on my implementation?

tux4life 2,072 Postaholic

Does this simple program run correctly on your computer?

#include <fstream>

int main()
{
    std::ofstream fout("test.txt");
    fout << "Hello World!\n";
}

Check if the program creates a file called test.txt, and check whether it contains: Hello World!.
If this program runs fine, then try and check whether the following runs correctly:

#include <fstream>

int main()
{
    std::ofstream fout("C:\\Users\\DimOK\\Desktop\\Lat\\TEST1\\test.txt");
    fout << "Hello World!\n";
}

Edit:: Depending on the location were you run my first example from, it might be good to delete test.txt before running the second test program.

tux4life 2,072 Postaholic
#include "fstream"
#include "iostream"

has to be:

#include <fstream>
#include <iostream>

Edit::
Why are you using a preprocessor macro? typedef would be a more accepted way :)

Edit:: OP edited/clarified his post, so I removed some things from mine as well :P

tux4life 2,072 Postaholic

To the OP:
Don't write your code like this: while(os.eof()) , instead write it like jencas did.
Want to know why? Do a forum search on: feof or eof :)

Edit:: I thought it was safely to assume that the 'oef' in 'os.oef()' is a typo he made.
(Because AFAIK there is no such data member in the iostream class library)

tux4life 2,072 Postaholic

>I dug in and saw that mbstowcs_s that attempts convert multi bite chars to wide fails.
Why this conversion is needed I do not know... And I would be happy to avoid it...

What is your code intended to do?

tux4life 2,072 Postaholic

>Any ideas?
Remove ios::in :)

EDIT::
I usually open files like this if I want to open them in text mode:

string fn = "test.txt";
ofstream fout;
fout.open( fn.c_str() );

(I also used a string like you did)

tux4life 2,072 Postaholic

>what should i do first plz help me
Begin creating one of the classes, no matter which one you start with.
Do you know how a class skeleton looks like?
If no, then I would like to suggest you to read your text book first.
Otherwise, give it a shot and post what you've come up with.

tux4life 2,072 Postaholic

Use getline:

string s;
cout << "Enter some text: ";
getline(cin, s);
cout << "You typed: " << s << endl;

:)

tux4life 2,072 Postaholic

You'll find this code snippet helpful :)
(If you're not allowed to use vectors, you can always adapt it to use an array)

Or use strtok.

tux4life 2,072 Postaholic

Well, your code can be easily fixed, change:

for (int x = 1; x <= numExs; x++)
{
total += x;
totalxs = total * 'X'; [B]// (1)[/B]
}

to:

for (int x = 1; x <= numExs; x++)
{
totalxs += 'X';
}

(1): Wrong steps:

  1. You multiply an ASCII value, first the character will be implicitly converted to an integer (representing it's ASCII value), that integer will be multiplied by the value in variable total, the result of the multiplication will be implicitly converted back to a character, and then that character will be put into the string (overwriting the previous value).
  2. Each time you overwrite the existing value in the string

:)

tux4life 2,072 Postaholic

Another remark: in nearly all benchmarks, outputting an integer takes longer than a string.

tux4life 2,072 Postaholic

MinGW (Code::Blocks), on Windows Vista Home Premium SP1, 2GB RAM and Dual Core (Intel Pentium D) (Clock Speed per Core = 1.6GHz):

cout string : 12215 ms
printf string : 7020 ms
cout int : 12230 ms
printf int : 7504 ms

cout string : 12137 ms
printf string : 6833 ms
cout int : 11934 ms
printf int : 7238 ms

tux4life 2,072 Postaholic

I don't mean to compare input and output, I mean to compare cin,cout,ifstream,ofstream with scanf, printf, fscanf, fprintf, respectively.

Did you read post #4 of this thread ?

tux4life 2,072 Postaholic

Sorry I should have been more specific. I mean cin, cout and also fstream (ifstream and ofstream).

How can you compare output performance to input performance ?
(for example: cin versus printf, what's the point of this ?)

tux4life 2,072 Postaholic

It is said so, but could you tell me more about the technical details of why iostream is slower?

Can you be more detailed in what you mean with iostream ?
As I said in my previous post: it's a class library for I/O.
Maybe you meant cout versus printf ?

Edit:: http://programming-designs.com/2009/02/c-speed-test-part-2-printf-vs-cout/ (here they measure the performance between both: cout and printf), however I don't know whether you should take this seriously (for example you'll have to take a look at the source to check how the performance tests are performed).

Edit:: But don't follow the advice to use printf instead of cout, instead follow siddhant3s' advice.
(If we have to believe the test, it's only about a few milliseconds, you aren't going to notice the difference, if you can, please tell me)

tux4life 2,072 Postaholic

iostream objects are usually slower than printf, but the little efficiency you gain by using printf is not worth avoiding the flexibility of the iostream classes.
(remember: iostream is not only cout, cin.)

tux4life 2,072 Postaholic

from which value ur loop is going to start?

Well, I would be glad if you had read my post:

Why starting with 30 if you can easily start with 3 and each time add 3 ?
Then the output will be in a more logical order, right?

:)

tux4life 2,072 Postaholic

If you specify %d in the format string of the printf() function, you also have to specify a corresponding integer value to print.

General rule: For each format specifier you include in the format string, you also have to supply a corresponding value (of the same type as the format specifier dictates).

And if I'm not wrong, if you use the printf-function like this: [B]printf("%d");[/B] , then this will lead to undefined behavior.

Salem commented: Yes, very undefined behaviour! +36
tux4life 2,072 Postaholic

but starting from 3 will add unnecessary numbers in our answer...u have to check whether ur number is not less than ur input number and should not greater than 30...and also u have to reject sum ur calculated numbers

in my each calculated number will be our answer..and we do only compare with input number...

You don't understand what I'm saying, right?