csurfer 422 Posting Pro

I think it means, read an integer as input from the user and assign it to the integer variable first_int.

int first_int;
scanf("%d",&first_int);

If you are still getting error, we need code and more detail to answer.

You think Ancient Dragon is so dumb that he couldn't give the simple answer you gave the OP ??? Just have a look at his reputation points and number of solved threads.
Its just the culture here to make people try and learn rather than given them the code well arranged in a golden platter.

I hope you being a part of the DANIWEB family now will help us carry this culture further and help people learn rather than giving them away the codes. :)

Salem commented: *nods* +36
Tom Gunn commented: Can you get any more extreme? It's a trivial 2 line snippet for god sake. No secrets of the programming elite were divulged here. Chill. -1
jephthah commented: lighten up. telling a beginner how to properly use 'slighten up, dude. instructing a beginner on how to use 'scanf' is hardly giving away trade secret. -2
csurfer 422 Posting Pro

Lots of things messed up:

1>

if (numElements <= 0)
return NULL;

NULL is defined as (void*)0 and its not the right place of use.If you want to stop execution then do something meaningful as:

if (numElements <= 0)
{
cout<<"Illegal value for numElements";
return 1; //Signifying improper termination
}

2>

//Return a pointer to the array.
cout << "The array holds the numbers you entered: \n";
for (count = 0; count < numElements; count++)
{
cout << *values << " ";
values++;
}

Why on earth are you manipulating the base address of the array??? And after this executes the value will be pointing to the last element and then after it if you do

delete [] values;
values = 0; //makes elements NULL.

It is bound to give errors.

Instead do this:

//To output the array contents 
cout << "The array holds the numbers you entered: \n";
for (count = 0; count < numElements; count++)
{
cout << values[count] << " ";
}

then after it for deallocation of memory you can do

delete[] values;
values=NULL; //Even though this is not required
tux4life commented: Yes :) +15
csurfer 422 Posting Pro

Why are you creating two threads for the same purpose....?
This thread is just the same...

And with regard to your question you can create a new file say headerfile.h with .h extension and place it in your working directory.Then by usage of #include "headerfile.h" above your main() source code in your cpp file and work.

csurfer 422 Posting Pro

i have a text file abc.text having 32 characters only.
2b7e151628aed2a6abf7158809cf4f3c

i want to load this file in char *key[16] as below

char *key[16] ={"2b", "7e", "15", "16", "28", "ae", "d2", "a6", "ab",
"f7", "15", "88", "09", "cf", "4f", "3c"};
i use the following code

char ch; int i=0;
ifstream F("abc.txt");
while (!F.eof())
{
F.get(ch);
strcpy (key[i++] , ch); // this line is erroneous..have no idea here..
}

1>
Usage of eof() is inviting problems on your own.Have a look at this.
2>
Its better to use pre-allocated memory for your computations.
3>
The usage of get() as you have used will fetch you a single character.But your objective is to fetch 2 characters and store them as a single string or character array.

Instead of using char pointers as strings you can use character arrays for it.More over if you are sure of the length you want to extract into the array then you can simply do something like this:

char arr[16][3]; //Your array
int i=0;
//Other stuff you need for opening of file and all
while(F.good())
{
arr[i][0]=F.get();
arr[i][1]=F.get();
arr[i][2]='\0';
i++;
}
csurfer 422 Posting Pro

Well you wont get complete code here on Daniweb.You need to try it yourself ok.This segment gives you an idea as to how to find the first"[" in the file now develop this idea further.

char c;
ifstream file_ptr;
file_ptr.open (<filename>);
while(file_ptr.good())
{
     c = file_ptr.get(); 
     if (c=='[') break;
}
csurfer 422 Posting Pro

and is there no way out ?
i mean can nothing be done ?

Well you can always find a way out if you want.;).

You can post in the program source code(you program which does the sorting work not the thing getting sorted.)May be we can help you somehow atleast in that way :)

csurfer 422 Posting Pro

Thank you csurfer , i totally agree with you but the problem in my case is that i haven't seen programming ever in my life before, and trying writing the right conditions in order to choose only the useful data that i want (with either way) is very difficult. I dont know how to distinguish only the useful data and put it in an array!!!

Hey you are doing great with your weeks preparation ok.So there is no need to panic.Well all you need to know about reading a file and reading from a particular position is given here. With this information all that you need to know is two things.
1>The place where you have your specific data.
2>The array index in which you want to place it.

As you are the file creator you can find some particular character or a particular place where you can find the data (something like a marker).If no such thing is there then follow the brute force approach and go on reading the file until a particular marker is found which marks the beginning of data.
Then create a for loop for the array input and read the specific data.

Go through the link mentioned earlier.Especially the seekg part in detail.I am sure you'll come up with something.Still if you get some problems then you can always some back to Daniweb for help.

csurfer 422 Posting Pro

do the exe's stop on there own?

They can...In case of signals generated by the operating system to stop them due to extreme conditions. Or due to some of the situations mentioned in my above post.

@csurfer

no there is no such condition in the code so that is certainly not an issue..
butfor the other cases do you have a solution?

Well there is always a possibility for a miss intrepretation in coding or a mistake by the programmer so its better to check once more even if you are sure.Others can be be found out by carefully going through the code.Which is something like we acting as compilers. :)

csurfer 422 Posting Pro

Well it can be due to many reasons:

1>A particular case in a program which comes up only in case of a large number of files.Say something like a condition of counter overflow with regard to an integer variable which is taken up as a counter.
2>Memory leak, which makes the program to crash after some time as the data we are dealing with is quite huge.
3>A mishandelled exit condition or something.
4>May be an operation with the counter variable which results in an illegal memory access which eventually results in a segmentation fault only in case of large values.

Well I pretty much feel its one among reason 1 and 2.

@tux: I too was shocked to hear about a program which executed for sometime and then aborted and then refused to work.Its kind of AI implementation in C lplz ;)

csurfer 422 Posting Pro

Well this is what I understand :

(LHS=Left Hand Side,similarly RHS=Right Hand Side)

1>

int** a = new int*[col];

Here the LHS tells us that a is a 2d pointer variable i.e a pointer to a pointer type.
In RHS we ask the compiler to allocate memory for "col" number of int* type pointers and return the starting address of that block allocated.
As RHS is returning a pointer to a block of int* type pointers a takes up the value as it is int** type.Hence this is correct.

2>

int** b = new int[][col];

Here the LHS is same as the first case.
Here in RHS we are asking the compiler to allocate memory for "col" number of elements to each one of the array pointers shown by int[] where we are assuming that such an array of pointers already exists which conflicts with the actual case.Hence to avoid the confusion the error.The same error persists even if you try something like this with defined limits as:

int ** b = new int[10][col];

3>

int* c[] = new int[][col];

Here the RHS is same but the LHS requires the compiler to return a pointer to the memory allocated for an array of int* type pointers.Hence the conflict which results in an error.

Basically what we clearly need to understand is that we need the array elements to be in contiguous memory locations and arrays are not just contiguous …

VernonDozier commented: Nice explanation. +18
csurfer 422 Posting Pro

Well only thing I can think of is there might be some problem with your repositories.So may be you can install them again.Because some problems can arise because they are of same origin.
gcc is GNU Compiler Collection which has specific packages for compilation of C++ files which we call g++.

csurfer 422 Posting Pro

Both of your thoughts are useful.
1>If you are using this data as independent units i,e when you are using the data set by set and not as a dependent quantity then you can use the single dimension arrays for your work.
2>If in case you are using the data as dependent units, i.e if X1 and Y1 go together then you can use them in a 2d array for easy usage.

csurfer 422 Posting Pro

Well Vernon has given quite a good explanation but if you still didn't get it its ok.

As the name goes "new" is to create something new and "delete" is to delete something which is already created.

Here in C++ if we don't exactly know how much memory is needed for something while writing the source code then we can make the memory allocation dynamic and hence use new to allocate memory.Usage of delete is not compulsory i,e it not being used doesn't create an error but in the long time can create a much more dreadful failures. delete is just a way to return the memory so that it can be reused.

As every call of new allocates memory in the heap area its usage within a function cannot be regarded as beneficial. If it were to locate memory on the runtime stack then we could say that its call within a function is beneficial because as soon as the function returns the memory will be returned.

I'm till confused to find exact position to utilize 'new' & 'delete'

This statement itself is wrong because the concept of dynamic memory was introduced to give user the freedom to allocate memory wherever he wants. By limiting the positions of usage of new we are going against this concept of dynamic memory.

After all this where exactly should we use new and delete can be answered as below:
Its up to the programmer to decide …

csurfer 422 Posting Pro

You mean to say it worked for a while and not working now....?

csurfer 422 Posting Pro

We wouldn't like to collaborate with someone who is not willing to put in a bit of effort and want someone to write the code for him !!! :angry:

csurfer 422 Posting Pro

excuse me i have a problem in writting above code i use arr[][] in func():
void func()
{
arr[0][1] = &c; // compiler error to this line arr no declarade
}

Ya thats what I mentioned.You use arr[][] in function and not a[][].For making it work write the code as follows:

//Definition of class X
X *arr[10][10];
//Definition of class Y with public X inheritence
//Function definition of Y::fun()

To declare the variable before it is being used or you can even do what Ancient Dragon has stated in post #5.

csurfer 422 Posting Pro

Nicely pointed by Siddhant3s.Whenever you pass an array to a function then its bounds should be clearly defined.The index indicating a pointer may be left as such but the array bounds should be specified as:

float mp[][3];
csurfer 422 Posting Pro

@thr:
A function in class y can work on variables declared in class y or the public variables in class x or the global variables(Usage of global variables is not recommended.).But I think you want to use the pointer to objects of class x which you have declared as X *arr[10][10] But you are using a[][] instead of arr[][] inside the function

void y::func()
{
a[1][0] = &c; //compiler error: a[1][0] undeclared identifier
}
csurfer 422 Posting Pro

I'm guessing you actually have ... in your source instead of actual code. a and c are not anywhere to be found. Provide the full source code and we can help you. Also, use code tags and void main is now int main.

The problem is quite clear.And there is no need for the source code for solving this problem.Even though the source code may be needed in the near future for other problems which may rise. ;)

csurfer 422 Posting Pro

That is right only one argument suffices when you are providing the position in the stream you want to access directly.

And the directions is for the seek call to count.
ios::beg with this the file pointer starts to calculate offset from the beginning of the file(i,e to say it starts counting the number mentioned as offset from the file start.)
and so on with the other flags.

I highlighted that as the mistake because with respect to g++ I think the flag field is needed.

csurfer 422 Posting Pro

Well string class is defined so that it stops on reading a space or newline so alternatively you can do this :

char s[20];
gets(s);
cout<<"\n"<<s;

This will read until it finds a \n' character and this will suffice your needs.

csurfer 422 Posting Pro

Major mistakes:

1>Using conio.h its an extinct header man.
2>Using clrscr() its also an extinct call.
3>Problem here is with :

printf("\n %d",temp);

You are printing the long int temp as a signed integer so after it crosses the limit of 32767 it goes on to the negative side.So use use it as:

printf("\n %ld",temp);
csurfer 422 Posting Pro

tempIn.seekg (tempOut.tellp ());

Problem with this line is due to seekg function. seekg and seekp calls take the position from which they need to start seeking as flags as: file_ptr.seekg(<offset>,flag); Offset is given by tempOut.tellp() but the flag should be one among :
ios::beg to start counting from beginning.
ios::cur to start counting from current file pointer position.
ios::end to start counting from the end of the file.

According to me here you need ios::beg flag so replace that statement with:

tempIn.seekg (tempOut.tellp (),ios::beg);
csurfer 422 Posting Pro

Alright, I have read most of that post...I believe that the values need to be passed by reference...which would change the values...but does that mean that that is the only thing that I can return? When the program runs, it just completely skips the whole...display the results thing. Does it need another pause maybe? Let me go mess with it a bit more and I'll report back. Thank you so much for your help! This is really helping me learn, not just get it done, and I'm grateful for that =)

Hey buddy I wasn't boasting about my post.There is something like <link> in that post (#5) click that and you will get a page which contains all the information you need for this problem.Have a good scan through it.You will learn a lot.:)

csurfer 422 Posting Pro

I don't see a hash table to read from... ;)

Create a hash function according to your needs and have a look at some standard hashing functions.Once you have a hashing function pass the key parameter of the data you want to write to hashing function and get the hash value corresponding to it.And you can open the file and move the filepointer to the location of your hash value and write the data there. Thats it. Later when you want to look for the data you just need to generate the hash value and corresponding position of data.

This ofcourse is only one among the several ways you can use a hashing.

greg29 commented: I have the same coursework to do.can you do an example for us please sir. I would be very grateful +0
csurfer 422 Posting Pro

Look you advanced so much. Read the link in that post its quite informative for beginners.Go on you are doing great. :)

csurfer 422 Posting Pro

You can use strchr function with space as the delimiter to mark the boundary of the first token and then read it accordingly.

csurfer 422 Posting Pro

Yeah, that's the error I'm getting, I know...but I'm not sure how to fix that...I'm totally new to C++ and I really don't understand it... How do I pass something to the function or from it?

1>First decide what the Calculate function needs to do.
2>Now decide whether you want any inputs from main to calculate within the calculate function.
3>If so pass those variables as parameters properly.
4>If not make both the functions as returning void.Simple right ??? :)
5>Now try it.Don't worry we are here to help.

Here is something you can read from. <link>

csurfer 422 Posting Pro

You are not passing any thing to the calculate function within main.So few parameters error.

void Calculate( double )

Don't know what exactly you want to do with this really :?: If you want to pass any parameters then pass it else make the function as

void Calculate()

in both the initialization and declaration.

csurfer 422 Posting Pro

You should have posted this under the same thread buddy.Because we need to start from the scratch in understanding if you start a new thread.Topic under same thread will let us know that its a part of it only.

You still have a mistake left :

rss.open("studentScoreData.txt",ios::in,ios::binary);

Its not "," that you should use in between the flags.you should use "|" operator as below:

rss.open("studentScoreData.txt",ios::in|ios::binary);

Other things are running fine i suppose :).Enjoy.

csurfer 422 Posting Pro

kindly do it for me and please give it to me these are the two things we hate. We will neither do both.You do it and we will help.

And why the conversion from post #1 to post #4 ??? you had it in the form of a class only then.You messed it up later...Go on with the thing you have written in post #1 and continue and we are here to help you if you get any errors.

csurfer 422 Posting Pro

Its you who needs to decide which application you need to develop and on which platform.You have specific tools for both.So its your decision.

csurfer 422 Posting Pro

Thanks a lot. that makes sense now that I look at it. correct me if I'm wrong here.

i%count == 0 endl... translates into:

if the remainder of i divided by the count is zero then skip a line. just trying to re-enforce what i've learned here.

Ya if remainder is zero then "skip the line" or "print the next number from next line" or "go to next line" which ever way you wanna say.By doing that you get out of trouble of dividing the numbers into groups(all the multiple of n issues) and will get a perfectly formatted output.

And ya once you are done mark the thread solved.If not satisfied you can continue posting in your queries.

one other thing to make sure I'm not gaffing this up. but I can loop the random numbers into an array to do other math functions later right?

Ya you can push those random numbers generated into an array for future use.But I don't see any sense in doing that because if you want random numbers in future then you would generate them then and there itself,no need to pre compute them and store them.However if you want to do so and your code needs it you can do so.

csurfer 422 Posting Pro

Please Note:

You are using a completely outdated compiler because it is letting you use "conio" "clrscr" and all the ".h" extensions. Get a standard compiler.

You have just posted the code and the line "How to convert Structure in class" that too with a spelling mistake and that doesn't explain your needs at all because I see no structure.

If you want help please write in what exact help you want from us and do it in a way people can understand.

csurfer 422 Posting Pro
int main()
{
double randValue;
int a,b,i, n, entry, y, count;
char again = 'y';

while (again == 'y')
{
  cout<<"Please enter the number of random numbers you want(1-100):"<<endl;
  cin>>n;
  cout<<endl;


  if (n <= 100)
  {
     srand(time(NULL));  // this generates the first "seed" value
     cout << "Please enter the range you want (lowest to highest) : " <<endl;
     cin >> a;
     cin >> b;
     cout<< "Please enter the number of elements you want in a group "<<endl;
     int cnt;
     cin >> cnt;

  if(a<b)
  {
     for (i = 1; i <= n; i++)
     {
         randValue = a + rand() % (b+1-a);
         cout << randValue << "  ";
         if(i%cnt==0) cout<<endl;
     }
     return 0;
  }
  else
  {
    cout << "Enter the range from lowest to highest only!" <<endl;
    cout << "Do you wan to continue? y or n?" <<endl;
    cin >> again;
  }
}

else
{
     cout << "Enter a value between 1-250 only!" << endl;
     cout << "Do you want to continue? y or n??" << endl;
     cin >> again;
}
}
}

The additions made in red are the only things you need to change to get your desired output.

csurfer 422 Posting Pro

@zeus1216gw:

No need to flame up ok.Read your post (#5) k.It reads as below :

sorry I forgot to add the groups need to be on the same line separated by a blank line. would it be sometime similar? I keep getting errors. thanks

I called the phrase idiotic not you.And we before helping have every right to ask you to show your effort(Sometimes the codes).And that was asked to help you out.So keep your calm and read what people have posted and what you have posted before flaming !!!

csurfer 422 Posting Pro

sorry I forgot to add the groups need to be on the same line separated by a blank line

That's idiotic I think you want to say blank space.
Hey you really cant expect us to give every detail.You need to work yourself.If you need the groups to be on separate lines you used to insert a "\n" or an endl in cout now when you want them to be separated by a space for the same code insert " " (space) rather than "\n".

Thats what Tom Gun said,you have your code right post it in.

csurfer 422 Posting Pro

For reading: Link
For comparison: Link
And you have the information given by Ancient Dragon.Use "word" in his example as a string class object.

csurfer 422 Posting Pro

Well if you want appending of information follow post #4
if overwriting you don't need to use any flag as such for more info about this refer post # 5 and # 6

csurfer 422 Posting Pro

@Ancient Dragon :

Of course that can be done -- if you want the old information deleted when you open the file just specify ios::trunc as the second argument in the open statement. ofstream out("file.txt", ios::trunc); Look here for complete reference.

[edit]Maybe I misunderstood the problem. See ^^^ post.[/edit]

On opening a file as :

ofstream file;
file.open(<filename>);

Default action is truncation so no need of ios::trunc flag but ya for appending we do need ios::app flag.

csurfer 422 Posting Pro

After Ancient Dragons post the only thing I could do was to lead you to that a proper source so that you can read from the standard ones rather than some link that pops up :) !!! So enjoy reading this.

ofstream is for output particularly so just use flags as:

ofstream file;
file.open(<filename>,ios::app);

Or fstream with file opened as

fstream file;
file.open(<filename>,ios::out|ios::app);

for appending rather than over writing.

Actually the post is confusing because the above flag is for appending(not deleting the old contents).

csurfer 422 Posting Pro

3 things before I answer your questions :
1>You are heartily welcome here until you keep showing effort.
2>Use pure English rather than message language.
3>Get yourself a decent compiler.Because Turbo C is the worst.Use Code::Blocks if you can.Or MSVisual C++ 2008.

Now for your program :
1>Hold the DOS commands list with you.
2>Now you just need your creativity.
3>Know the procedure as to how to invoke the system commands from C language,know about the "system" call.
4>Organise your stuff correctly and go on with your coding part.
5>Example :
Say you want to execute md or mkdir command :
* Scan the input string,the first should be the command followed by the options followed by the arguments.
* Now try to implement one after the other.

csurfer 422 Posting Pro

Ya you can do that or device your own method to convert it into an integer.

csurfer 422 Posting Pro

@skydiploma :

Use the function strcmp(char ,char)

Missing out on several things today ha buddy ???
Its strcmp(char *,char *) for strings. ;)

csurfer 422 Posting Pro

@skydiploma:

The "==" comparision for strings given in namespace std is given only for strings of class string.

@KangarooBlood

Make the changes given in post #3 it will work.

And ya don't use abusive words on the forum please.

Sky Diploma commented: I almost forgot that It was a c-styled string. +5
csurfer 422 Posting Pro

Three small changes :

1>Use #include<string> in the beginning
2>Take name as

string name;

rather than char name.
3>Use "kevin" rather than 'kevin' in the check.

csurfer 422 Posting Pro

Why calling the function moves.size() again and again for every loop ??? Do it once,else its just a waste of computation time.

Do this :

int max=moves.size()-1;
for(i=0;i<max;++i)
{
	if(moves[i]==moves[i+1])       
		k++;
	else
		cc.push_back(k);
}

And with respect to the segmentation fault I am not finding any problem with the code.Well may be its not because of the statement you said.

csurfer 422 Posting Pro

Ya I agree with jephtah . Start up with some easy book then when you think you know C to a certain extent you can come to K&R.

Or you can even come to Daniweb scan through the C forum. Because here you will get more than one ways to solve a particular problem and also the credentials of a particular way and its defects.And finally the best way to solve that problem.

Trust me just by reading the threads here you'll get to know a lot off stuff.

csurfer 422 Posting Pro

Image Compression,Audio Compression .You couldn't even find this.....?

csurfer 422 Posting Pro

I really don't get why MrNoob is so obsessed with fflush(stdin); and also with fputs .And ya one more thing "Program Keeps reading wrong input".

It always just does what you have programmed it for.So the program is not wrong you are !!!

Rats! I looked right at that and skipped over it! Good catch CSurfer!

Don't let your eyes run ahead of your brains!!!:)

Salem commented: Solid contribution in the whole thread +36