tux4life 2,072 Postaholic

Should the program prevent input like: A3--5,9-01 ??
Is it allowed that the problemset's name contains more than 1 character ?
What to do with things like: A35,9-01,A-Z-9 ??

>And then convert it back to char with itoa()
itoa() is non-standard.

tux4life 2,072 Postaholic

I am so sorry dude but i dont understand that guide :( I am so sad :( Can you explain me with some easyer guide ?

In fact, that link describes everything step by step, so I don't know whether easier is possible.

tux4life 2,072 Postaholic

>Any clue to the solution???
Yes, start with the structure.

tux4life 2,072 Postaholic

I ran a memory test (memtest86) on my RAM, and it didn't give errors.
Maybe the problem is the CPU then (though, after one day staying powered on and being used (when the pc can boot up, you can reboot without any problems, but when you turn off the pc, it doesn't boot up anymore) intensively the CPU isn't very warm/hot at all) ?

tux4life 2,072 Postaholic

>"->" is used to access data members on the heap

The arrow operator -> is used to access data members (of a class) via a pointer, these data members do not necessarily have to be on the heap, consider the following example:

// declaration of the 'counter' structure:
struct counter { int count; };

// declare pointer:
counter *p;

// declare structure variable:
counter c;

// put some value in it:
c.count = 5;

// Let the pointer point to an instance of 'counter':
p = &c;

.
.

Well, is c on the heap here?
I think it's in the automatic memory.
(sorry for the boring example, but I really had no inspiration :P)

The arrow operator ( -> ) is often used to replace things like this in your code: (*[I]pointer_to_structure[/I]).[I]data_member[/I] .
There have to be parentheses around pointer_to_structure because of the rules of precedence, if you don't place them, then you are referring to a data member of a structure (and in this case: a pointer which is a data member of a structure).

Yes, the "." works, but why?

Because the arrow operator ( -> ) is used in combination with pointers.
(Take a look at Tom Gunn's post, he has already explained it to you)

tux4life 2,072 Postaholic

c program to find factorial series for n values

First:
Why do you post two times the same question?
(your problem won't be answered quicker or so)

Second:
We are not to make you such a program, you can easily put it together yourself by using a Big Number library such as GNU MP Bignum Library, which can be found here:
http://gmplib.org/

:)

tux4life 2,072 Postaholic

From Wikipedia:

Visual C++ Express

Visual C++ 2008 Express can build both native and managed applications.
...

(Source: http://en.wikipedia.org/wiki/Microsoft_Visual_Studio_Express)

tux4life 2,072 Postaholic

The only problem with Visual 2008's compiled files is that you cannot run it on other machines without the .NET framework. I recommend finding a way to get it to work without the .NET framework or get DevC++.

If I'm not wrong, you can also compile your source code as a native Windows application, which doesn't need .NET to run.
Though I'm not so sure about this.

Edit::
http://msdn.microsoft.com/en-us/library/60k1461a.aspx
http://en.wikipedia.org/wiki/Microsoft_Visual_Studio_Express

tux4life 2,072 Postaholic

>Do you think this would cause memory problem?
Well, that actually depends on how much RAM there's inside your computer, whether you're planning to create arrays of structures, etc. ...

You can quickly check how much bytes a structure occupies in your computer's memory by using the sizeof operator.

You could use the following program to check the memory occupied by the structures:

#include <stdio.h>

// Your structures are declared here
struct table{
  int id, rank, hop_sup;
  float dist,bs_dist, res_enrg, total; 
};

struct nd{
  float x,y, enrg, bsdist;
  int id,my_rank, hop;
  int neighbor[50], prfrdnbor;
  float nbordist[50];
  struct table nbr[25];       
} node[127];

int main()
{
  // Store the size (in bytes)
  size_t sz_nd = sizeof(struct nd);
  size_t sz_table = sizeof(struct table);
  size_t sz_node127 = sizeof(node);
  
  // Output the size to the screen
  printf("%s%u%s\n", "Size of a \'nd\' structure: ", sz_nd, " bytes.");
  printf("%s%u%s\n", "Size of a \'table\' structure: ", sz_table, " bytes.");
  printf("%s%u%s\n", "Size of \'node[127]\': ", sz_node127, " bytes.");
  
  return 0;
}

/* My output:
Size of a 'nd' structure: 1132 bytes.
Size of a 'table' structure: 28 bytes.
Size of 'node[127]': 143764 bytes.
*/

Please note that the output can differ from implementation to implementation.
Well, you can decide for yourself whether this occupies much memory or not, you'll have to compare it with the amount of RAM inside the computer where you're planning to run your program on.

tux4life 2,072 Postaholic

>First make sure in your code every time you open a file you close it.

Any fstream (and thus also ifstream / ofstream) object has what's called a destructor, this destructor will close the file automatically when the file-object goes out-of-scope.
So actually there's no need to explicitly close a file as it will be closed automatically (as the object's destructor will be called then; this is only the case when the object goes out-of-scope).
But when your program just needs to read/write some data from/to a file, and then still has to execute loads of instructions (within the same scope as the file-object), then I would advise you to close the file manually using the close() method.

tux4life 2,072 Postaholic

When the operand for sizeof is a type, you have to use parentheses because types can be made up of more than one token and the parentheses make it easier for the compiler to parse.

the parentheses make it easier for the compiler to parse.

Who cares about the fact that the compiler can easier parse your code?
That's not the real reason.
The reason for using parentheses is because it's defined like that in the standard:

The sizeof operator yields the number of bytes in the object representation of its operand. The operand is either an expression, which is an unevaluated operand (Clause 5), or a parenthesized type-id.

tux4life 2,072 Postaholic

Visual Studio does not have a C99 mode.

http://stackoverflow.com/questions/146381/visual-studio-support-for-new-c-c-standards

I didn't know that, as I'm not a Visual Studio user, in that case I would suggest the OP to get a compiler/IDE which features C99, like:
Pelles C
(Or MinGW's gcc)

tux4life 2,072 Postaholic

i cant understand what does ++*++p means???
can u help me

Consider the following code:

#include <iostream>

int main()
{
  int a[5] = {0, 5, 2, 3, 4};
  int *p = a;
  
  ++*++p;
  std::cout << a[1] << std::endl;
  
  return 0;
}
/* Output:
6
*/

Explanation:
The first ++ will increase the value on its right side (*++p).
According to the rules of precedence, ++p will be executed before the pointer is dereferenced, so the pointer (which was pointing to the first element (element 0) of the array) now points to the second element (element 1) of the array, then the pointer is dereferenced.
The value (that we got by dereferencing that pointer) will now be increased by one.

shadwickman commented: Good explanation of ++*++p - I learned something new too :P +3
tux4life 2,072 Postaholic

>Is there any way to build the program, so that it is an .exe file that can be used independently?
Yes, sure there is!

I assume you can run the program from within Visual Studio?
In that case you just browse to the folder where your project is saved in, there will be a folder called "Release" or something, and in that folder you'll probably find your executable file.
If your program is fully finished, I would also like to advise you to compile your code in "Release" mode, instead of the default "Debug" mode.

On MSDN you can also find a video to learn become familiar with the IDE, check it out here.

tux4life 2,072 Postaholic

this is of topic but, how come the size of thing is called an operator if it takes a parameter?

Because it's defined like that in the standard :P
BTW, It's not a parameter, it's called an operand.
Here the operands are: (double) and (float) .
With variables parentheses are optional, with type names (e.g: double, int, float, etc.) parentheses are needed.

Also check out this :)

tux4life 2,072 Postaholic

That's because for each integer printed, all operations will be executed.
For example, with me this would give the following output: 4 5 4 5 4 5 The first integer printed, is the result of all the following operations, executed directly after each other: i,i--,--i,i++,--i,i++
Let's step through this: At the beginning of the program, integer variable i is set to value 5, then after executing i, it's value won't change, then we encounter i--, i's value won't change directly here (only at the end of the expression, because we used it as a postfix operator), the value is still 5 at this point, then we come across --i, because we used the prefix operator i's value will be decreased by 1, becoming 4 now, after that we see: i++, this won't change i's value directly (only at the end of the expression), making the first integer printed 4.
The same applies to the other integers printed.

Edit::
This may not be the case on all compilers, but your compiler should give you a warning about this, when compiling with all warnings on, I get the following warnings, which directly explain what the problem is:

t.c: In function ‘main’:
t.c:6: warning: operation on ‘i’ may be undefined
t.c:6: warning: operation on ‘i’ may be undefined
t.c:6: warning: operation on ‘i’ may be undefined
t.c:6: warning: operation on ‘i’ may be undefined
t.c:6: warning: operation on ‘i’ may be undefined
tux4life 2,072 Postaholic
tux4life 2,072 Postaholic

It is not working in visual studio?

You'll probably have to explicitly turn on the C99 mode.

tux4life 2,072 Postaholic

Could you please post using code tags?
It looks better to me if you'd migrate your code to be more standard, this guide will help you to accomplish the task :)

And also: what problem are you having with this code?
(you only posted your code, but you didn't ask a question, please keep in mind that we're not clairvoyant)

tux4life 2,072 Postaholic
tux4life 2,072 Postaholic

And a little addition to niek_e 's post:
To get the amount of bytes a float and a double occupy in memory (on your implementation), you can use the sizeof operator:

cout << "Float: " << sizeof(float) << endl;
cout << "Double: " << sizeof(double) << endl;
tux4life 2,072 Postaholic

Hey, why not telling him to post using code tags?

To the OP:
Information about code tags is spread over whole 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

tux4life 2,072 Postaholic
tux4life 2,072 Postaholic

Debug -> Start Debugging

Or just press F5

tux4life 2,072 Postaholic

What OS are you on?
If you're on Windows, then you choose wxMSW :)

tux4life 2,072 Postaholic

Quote:

What is your C++ question?

And let it say me again:
What is actually your C++ question?
It's pretty much difficult to solve a C++ problem, if we even don't know the problem :P

tux4life 2,072 Postaholic

hello thanks for the reply,hmm..could u please change the logic of the program coz i want that the user wil input the value to be added in the set. PLease and kindly correct it.

I have put much effort in writing such an extensive post, so could you please try first? (look at my previous post)

Edit::
If I didn't misunderstand you, your assignment was: Get some values from the user, store them in an array, get another value from the user, and increase each value in the array with that value.
Am I right on this?

tux4life 2,072 Postaholic

This is already a big error: int result[3],item,[B]*count=0;[/B] .

You declare a pointer to an integer, correct till here, but what do you do then?
Well, you assign a value to it, hmm, what could be wrong here?
Remember this for ever: do not assign a value to a pointer which you didn't explicitly assign a memory address to.
In your program you're just writing the value zero to a random location in your computer's memory, but that's not what you want, am I right?
So, how to fix this?
Well, before assigning the value zero to that pointer, you let the pointer point to the desired memory address first.

Why int result[3] ?? At the top of your program's code you defined a constant for it: SIZE, this isn't a bug or something, but to prevent potential bugs it would be better to change it to: int result[SIZE] Integer variable item will have a random value as well, because you didn't initialize this variable.

You probably meant this whole statement: int result[3],item,*count=0; like this: int result[3],item[B]=0[/B],*count[B]=result[/B]; What's the point of that if-statement and that scanf function in this loop?

for(i=0; i<SIZE; i++)
{
  scanf("%d",&result[i]);
[B]  if (result[i]==item) // (1)
  {[/B]
    printf("&d",result[i]);
  [B]  (*count)++; // (2)[/B]
[B]  }[/B]
}

(1) why this if??
(2) increasing the value where the pointer points to with one??
(this isn't the same as moving the pointer to the next element in the array, if …

tux4life 2,072 Postaholic

I need to create a program that adds two sets,
hint: the set is composed of distinct (integer)


How to do this using this Function:
void add(int result[],int item,int *count);
add result the value of the item.

Please i really need ur immediate help...pls600x

Well, here's my immediate help:
Can you show us the code you already have?

tux4life 2,072 Postaholic

>Why would we need to make something private within a program?

Well, that's just the principal of data encapsulation: It's better that you let the object manage his own data members (in this context: variables), object oriented programming follows the concept of: "divide and conquer", you don't have to remember what all variables in a class stand for, you don't have to remember what all those help-functions do, you only have to remember the interface of your object.
It's better that all those complex things are out of your eyesight, but declaring a data member inside the private section of your object also ensures that your program (or a function) for example cannot accidentally change a variable's value. (It helps you to introduce less bugs in your program)

The interface of an object should define an easy way to let your program communicate with the object.

One important thing you should keep in mind while designing a class is: when there's no need to be able to access the data members of an object directly, you should make them private, in that case you're sure that the data can only be changed by the object's methods itself, and not from anywhere else in your program.

An example:

#include<iostream>
using namespace std;

class bird
{
private:
    int count;

public:
    void seen() { count++; }
    int get_count() { return count; }
    
    bird()
    {
        // constructor
        count = 0;
    }
};

int main()
{
    bird starling;
    
    cout …
Nick Evan commented: for the effort put in this thread +19
tux4life 2,072 Postaholic

Well, it sounds like a bin packing problem, which is NP-hard so there's no way to always get the optimal solution. I've done some google search to find any algorithm solving it (I really cant do it on my own, I'm a freshman in C) Here it is a useful link but still I have difficulties understanding it...http://www.cs.arizona.edu/icon/oddsends/bpack/bpack.htm

If you're in a class, then you should be able to do that at this stage, a teacher won't give you an assignment, you won't be able to complete successfully.

tux4life 2,072 Postaholic

My compiler is Dev-C++ and I have Ubuntu 8.04. Thanks everyone. LOL um siddhant3s I am not asking anymore questions, I don't want to ask a stupid question or irrelevant question, or any of that (read Cheesey's thread and what happened there.) Haha gives me even more motivation to learn how to research better.
Switching to Ubuntu now, I got a portable hard drive (and I installed Ubuntu on that so I wouldn't have to get rid of Windows, yet ;)..)

Not fully agreed, but then in the sense that: you should ask questions if you don't understand something, could you imagine something worse than always having to doubt whether you're correct or not?
Of course I agree that you should search for your answers first, but that seems pretty much logical.
About Cheesey's thread: It's good that it motivates you to do better research before asking any question here, but you've to know one thing: if someone doesn't want to use code tags when posting code, and he explicitly tries to find reasons why he might be right for not using code tags, then he's just asking for problems, it's clearly mentioned anywhere on this forum, he has no excuse!

tux4life 2,072 Postaholic

on,i have heard of API and GUI what is the diference ?

Most of the time, the functions to create a GUI are part of an API, but the difference lies in the fact that an API doesn't necessarily have to feature a GUI, we could for example talk about a network API, which enables you to let your program communicate over a network.

(Possible) Definitions:

An application programming interface (API) is a set of routines, data structures, object classes and/or protocols provided by libraries and/or operating system services in order to support the building of applications.

A graphical user interface (GUI, ) is a type of user interface which allows people to interact with electronic devices such as computers.
(Most of the time you can also make use of a mouse pointer within a GUI, but this isn't necessarily always the case, you can also have GUI's which only interact via a keyboard for example).
You actually already know what a GUI is: you probably use it all the time when you're working on your computer, you can control it with mouse and keyboard...

Oh, yes: If I had to choose between GTK+ and wxWidgets, I would choose wxWidgets.
Though this is just my opinion, the only way to find out what the best for you is, is by trying them out both, and stay with the one which fits best your needs and which you do like the most.

:)

Salem commented: Nice post +36
tux4life 2,072 Postaholic

>I was trying to explaining op the purposes of using a copy constructor with that,thats it,I know the way I took is hopeless.

Why not just providing some links?
http://www.fredosaurus.com/notes-cpp/oop-condestructors/copyconstructors.html

http://www.learncpp.com/cpp-tutorial/911-the-copy-constructor-and-overloading-the-assignment-operator/
http://www.learncpp.com/cpp-tutorial/912-shallow-vs-deep-copying/

:P

csurfer commented: Good work tuxxy !!! ;) +2
tux4life 2,072 Postaholic

Answers:

1>Assume that there is a class as:

class check{

int a;

public:

check()
{ 
cout<<"Constructor"; 
}

check(int i)
{ 
//copy constructor code 
}
.
.

Look at this:

check(int i)
{ 
//copy constructor code 
}

Why would this be a copy constructor?
It's just an overloaded constructor, it doesn't make a copy.

tux4life 2,072 Postaholic

>I made a game, and my friend told me: It doesn't work on linux.
Because it's a Windows API function.
That's why :)

wxWidgets probably features a comparable function.

tux4life 2,072 Postaholic

You haven't defined the bodies for the constructors: manager(); etc...

Edit::

[B]/*** Fixes to make it compile are made in Green ***/[/B]
#include<iostream>
#include<string>

using namespace std;

class base_employ
{
      protected:
                string First_Name;
                string Last_Name;
                double Salery;
      public:
             base_employ() [B]{}[/B]
             base_employ( string &FN, string &LN, double S)
             { First_Name=FN;
               Last_Name=LN;
               Salery=S;
             }
             void get_FN(string FN)
             { getline(cin,FN);}
             string set_FN()
             {return First_Name;}
             void get_LN(string LN)
             { getline(cin,LN);}
             string set_LN()
             {return Last_Name;}
             void get_S(double S)
             {cin>>S;}
             double set_S()
             {return Salery;}
             void Print()
             { cout<<"First Name:"<<" "<<First_Name<<endl;
               cout<<"Last Name:"<<" "<<Last_Name<<endl;
               cout<<"Salery:"<<" "<<Salery<<endl;
             }
             };
             

class manager : public base_employ
{ private:
          int Meet_per_week;
          int Vacation_days_pyear;
  public:
         manager() [B]{}[/B]
         manager( string &FN, string &LN, double S,int MPW,int VdPY) : base_employ (FN,LN,S)//metodata menager nasleduva od metodata base_employ
         { Meet_per_week=MPW;
           Vacation_days_pyear=VdPY;
         }
         void get_MPW(int MPW)
         { cin>>MPW; }
         int set_MPW()
         { return Meet_per_week;}
         void get_VdPY(int VdPY)
         {cin>>VdPY;}
         int set_VdPY()
         {return Vacation_days_pyear;}
         void Print_M_Stat()
         { base_employ::Print();
           cout<<"Meetingd per week:"<<" "<<Meet_per_week<<endl;
           cout<<"Vacation days per year:"<<" "<<Vacation_days_pyear<<endl;
         }};
         
class engineers : public base_employ
{ private:
          char Value; //Y ako znaat C++ & N ano ne go poznavaat
          int Years_Experience;
          string sort_engineer;
  public:
         engineers() [B]{}[/B]
         engineers( string &FN, string &LN, double S, char V, int YE,string &sortE ) : base_employ (FN,LN,S)
         { Value=V;
           Years_Experience=YE;
           sort_engineer=sortE;
         }
         void get_value(char V)
         {cin>>V;}
         char set_value()
         { return Value;}
         void get_YE(int YE)
         { cin>>YE;}
         int set_YE()
         {return Years_Experience;}
         void get_sortE(string sortE)
         {getline(cin,sortE);}
         string set_sortE()
         {return sort_engineer;}
         void print_E_Stat()
         { base_employ::Print();
           if(Value=='Y'){
           cout<<"The Engineer knows C++"<<endl;}
           cout<<"Years of Experience:"<<Years_Experience<<endl;
           cout<<"Type of Engineer:"<<sort_engineer<<endl;
         }}; …
tux4life 2,072 Postaholic

See now what happens when you post without code tags?
No? Well, I see it: you get unwanted smileys in your code (and how is someone supposed to quickly read such code?).
So please wrap your code in tags :)

tux4life 2,072 Postaholic

Yes. I just do not understand the logic that goes into the program.

Could you please post your code then?

tux4life 2,072 Postaholic

Have you already tried to solve it?

Edit:: Where are you having problems with?

tux4life 2,072 Postaholic

>Need an idea
Well, you've got pretty much everything you need to start writing code.

Could you first write some code and post it down?
Also tell use where you've problems with to implement.

Problems in writing code?
Try to break the problem down into separate small problems (this process is also known as the process of abstraction), then you try to solve each small problem, take a pen and write your program out on paper, it will definitely help you to write the code for your program.

Got stuck at some part?
No problem! Just post where you're having difficulties with to implement and we'll be glad to help you.

tux4life 2,072 Postaholic

I really need you all's help.

Great!
Start a thread in the appropriate forum (about the topic where you require help for) and we'll be glad to help you out.
Remember to not forget to read the forum rules + the member rules before starting any thread :)

tux4life 2,072 Postaholic

I understand that, but but Java Script is built right into every browser..even the older ones, isn't it? I don't remember ever having to install anything extra to view a java script page, and thats why I would rather use Java Script if possible...but Is Java Script capable of reading through a list of numbers, storing, and looping through them like my CPP app does?

Why wouldn't it be able to do that?
That are just the fundamentals of a language (though JavaScript is like the name says: a scripting language)
But: Keep one thing in mind: Because JavaScript is not compiled, others might be able to read your code.
(Though there are tricks to cloak your code a bit)

The reading of the file(s) might be a problem.

Isn't PHP a more suitable language for this?
(You'll have to keep in mind that PHP is a server-side language, which means that your programs run on the webserver, not on the user client's computer, so you'll have to host it on a webserver which supports PHP, there are plenty of free hosting providers who do).

tux4life 2,072 Postaholic

I used the Deleaker in such a case, and it always helped me to find not trivial errors and bugs.
<snip>

Deleaker costs money, and probably you even don't get as much features as the free Valgrind (as already mentioned in one of the previous posts).

tux4life 2,072 Postaholic

Actually it is: Tux4life :P (not with an 'l' or 'L')

tux4life 2,072 Postaholic

oooh thanks tom Guu "it realy wrok thank u very much"
thanks again:)

If it works now, what is stopping you then from marking this thread as solved?

tux4life 2,072 Postaholic

but i cant understand how it work and what it do?

What don't you understand?
Tom Gunn's code? It's just a check which prints the password read from the file and the password read from the user, so we can see what might be going wrong.

If you don't understand that, how did you come up then with the code you posted in this thread?

tux4life 2,072 Postaholic

the main problem in this code is that it can't compare two strings

Well, shall I give you some small examples on how to compare C/C++ strings?
Here you are:

  • Comparing two C++ strings:
    string a = "hello";
    string b = "world";
    
    if( a != b )
    {
      cout << "\'" << a << "\' is not equal to \'" << b << "\'" << endl;
    }
  • Comparing two C strings:
    char s1[] = "hello";
    char s2[] = "hello";
    
    if( !strcmp(s1, s2) )
    {
      cout << "Both strings are equal." << endl;
    }
  • Comparing a C string to a C++ string
    char a[] = "hello";
    string b = "hello";
    
    if( !strcmp( a, b.c_str() ) )
    {
      cout << "We are both equal." << endl;
    }
    
    // or
    
    if( string(a) == b )
    {
      cout << "We are both equal." << endl;
    }

Hope this helps!

tux4life 2,072 Postaholic

Could you please post using code tags?

Information about code-tags is available 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 where you actually typed your message in

tux4life 2,072 Postaholic

This post has nothing to do with the converting between those two, but with:
Bad coding practices!

  1. !fin.eof() Check out this.
  2. system("pause"); Check out this.
    You could use cin.get() as a replacement.
  3. return 1; Why would you want to return value 1 if your program has terminated correctly?
    Make it return 0;

:)