Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

you can execute other programs with the system() function or with several os-specific api functions. For MS-Windows it might be either ShellExecute() or CreateProcess().

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

>>How can I terminate a server after some number of connections
Put exit(0); after the loop ends on line 21.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

fgets() adds the line terminator to the end of the string if there is one in the input stream, which is why you saw the " character on another line. You need to remove it before calling strcmp(). Put this inside the loop, before the strcmp() line.

if( charin[strlen(charin)-1] == '\0' )
    charin[strlen(charin)-1] = 0;

As for the value of the string being "temp", I can't answer that question because I would need to see the file's contents and the new code that you used to read it.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

A little more testing indicates that in the evaluate() function, when you find a digit it needs to convert everything up to the first non-digit to an integer and then push that result onto the stack. For example if I enter "10+20+30" the function should push the values 10, 20 and 30 onto the stack. What it is now doing is pushing the individual digits, 1, 0, 2, 0, 3, 0. You could fix that something like this:

for(i=0;pos[i]!='\0';i++)
    {
        if(isdigit(pos[i]))
        {
            int x = 0;
            while( pos[i] != '\0' && isdigit(pos[i]) )
            {
                x = (x * 10) + pos[i] - '0';
                i++;
            }
            push(&eval,x);/*use to find the integer value of it*/
        }

But that too only works for values less than 126 because you are using char as the data type of the stack. Use long long and you could get much larger values and would require some additional recoding.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

The problem is in the evaluate() function. The content of the parameter contains spaces, which should not be evaluated in that function.

if(isdigit(pos[i]))
		{
			push(&eval,pos[i]-'0');/*use to find the integer value of it*/
		}
		else if( !isspace(pos[i]) )
		{
			p2=pop(&eval);
			p1=pop(&eval);
			result=operation(p1,p2,pos[i]);
			push(&eval,result);
		}

That gets rid of the program crash, but it still doesn't work when I enter a little more complex expression.

Enter Infix Expression : 1+2+10+234


Postfix Expression is : 1 2 + 10 + 234 +
7
Press any key to continue . . .
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

Trailing spaces, which you will not see when you print them out unless you enclose the text with some special character, such as quotes, like printf("\"%s\"", charin);

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

We still need to see some example records. For example, might it be something like this?

one
one-two
one-three
%
two
two-two
two-three
%
etc
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster
bool WrongSize (ofstream& outData,int keylength,string answers)
{
    bool valid = false;
    if (answers.length() < keylength)
    {
         outData << "Too few answers!\n";
    }
    else if (answers.length() > keylength )
    {
        outData << "Too many answers!\n";
    }
    else
         valid = true;
    return valid;
}

int main()
{
  < snip >

  if( WrongSize (outData, keylength, answers) == true )
  {
	for( int count = 0; count < key.length(); count++)
       {
             if( answers[count] == key[count] )
                  grade++;
             else
                  outData << "Invalid answers" ;
}
  }
}
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

strcmp() is case sensitive, meaning that the word "Hello" is not the same as the word "hello". Maybe that is what is going on with that program. Try using a case in-sensitive comparison, such as stricmp(). The same function might be called something else on g++ compiler, such as comparenocase(), or something like that.

If that doesn't fix the problem then you need to post an example of the file. If its a large file just post the first two or three lines.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

Can you give more details how to align 2 hand side?

Sorry but I have no clue what you mean. what is "align 2 hand side" ???

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

If you use std::string for the final full path/filename, and a character array to get the filename from the text box, then just do this:

std::string path;
char filename[255];
// get the text from the text box and 
// save it in the character array is not
// shown here.

path = "\\Level\\";
path += filename;
// now open  the file for reading
ifstream in(path.c_str());
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

why? it's not necessary to null-terminate std::strings like it is with character arrays. As for the vector, its not necessary to null-terminate it either. If you want the number of strings in the vector then just call its size() method for(int i = 0; i < mylist.size(); i++) , or you could use iterators

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

Move line 37 up to just after line 30 so that it is called before the loop starts. The program will crash if answers string is not the same length as the key string. So WrongSize() function will have to be changed to return a value that indicates an error condition. Do not execute the loop on line 29 if WrongSize() returns an error value.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

Post new code

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

Haw -- he is a lot smarter than me.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

All you have to do is print the lines on the screen. You don't have to insert anything into the actual text. in a loop from 0 to length of line, print one character, increment a count. If the counter == maxlength, print '\n' and reset the counter back to 0. That's all there is to it.If you have to do word-wrap then the algorithm will become a little more complicated.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

>>Filenames is the container I am using.
What container? Post how Filenames is declared. Is it std::string, vector<string>, or something else? We need a lot more details about it in order for anyone to tell you how to serialize it.

Assuming Filenames is a vector<string>, then do it something like this:

for(size_t i = 0; i < Filenames.size(); i++)
{
     string s = Filenames[i];
     // make s constant width of 20 characters
     for(size_t j = s.length(); j < 20; j++)
          s += ' '; // add a space to end of string.
     out.write( s.c_str(), s.length() );
}

Now to read it back. Note: function you will have to code rtrim() function yourself. It removes trailing spaces.

char line[21];
while( in.read(line, 20) )
{
   line[20] = '\0'; // null terminate the string
   rtrim(line); // remove trailing spaces
   Filename.push_back(line);
}
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

The while loop is still wrong. It is not processing each line of the file, but only the last line. You need to put the loop starting on line 35 inside the while loop that starts on line 27.

Also, the use of eof() will not work. Here is what you should have while ( inData >> student >> answers ) With that you can delete lines 29 and 31.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

It just occurred to me that Narue is also named Aia. Aia is just too smart to be anyone other than Narue.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

>>Build: 0 succeeded, 0 failed, 1 up-to-date, 0 skipped

That tells you that it did not generate an executable program. Did you save the source file after you created it?

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

Yes, I have NOT done it. Its your program not mine, so don't expect anyone to just hand you over some code. If you want me to write your program for you please deposit $1,000.00 USD in my PayPal account and give me the name/email address of your teacher. Doing your homework is not cheap :)

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

The loop starting on line 31 does more than what is necessary. Just use subscripts into the two strings

for( int count = 0; count < key.length(); count++)
{
   if( answers[count] == key[count] )
       grade++;
   else
       cout << "Answer # " << count+1 << " is wrong\n";
}
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

Well, I'm one of those old farts you stereotyped. My wife and I went to a small-town restaurant about an hour ago for lunch. Food was ok, nothing to brag about, but the service was pretty good. One waitress served 10 tables; she had no spare time. So I gave her a 20% (or maybe a little more) tip.

But I can understand why many old people do not tip well, if at all. They can barely afford the cost of the food, let along a tip too. Old people have a lot of medical problems, and medicines are very expensive. I've seen a few people pay upwards of $500.00 USD for a tiny sack of pills (at WalMart). So for them its a matter of give you a 20% tip and skip a few days pills. I don't have that problem because I spent 23 years of my young life to get the medical insurance I now have. And I am now glad that I sacrificed all those years.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

If you download Code::Blocks binaries it will contain MinGW. If you are beginner programmer then you probably don't have enough c++ background to code a GUI program yet. Learn the basics of the language first, then in several months you MIGHT know enough to write your sprite program.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

post the file. Does one record consist of two or more lines in the file? If not, then why use %?

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

You need to write GUI programs if you want to add sprites. The easiest way to do it is probably with OpenGL or DirectX libraries.

>>Dev-C++ is what im running)
Ditch it and install Code::Blocks with MinGW. Its a better IDE and supports current versions of g++ while Dev-C++ is neither.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

Sometimes original work (code) is just crappy (e.g. faulty design) and requires re-coding to make it better -- I've done it myself. If you want congress to have many activities, and each activity to have many people, they you really have no choice but to have linked lists within linked lists, something like I posted.

This is one reason to write flow charts and design documents before line 1 of the program is coded. It makes your coding job a lot easier if you know what the program should be and how to go about making it do that.

BTW: it's customary for the next pointer in linked lists to be simply named next, not nextactivitiy or nextperson. And it makes coding a little simpler too (not so much to type and easier to understand).

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

Assuming the buffers contain binary, not string, data -- create a third buffer that is large enough to hold both the first and second buffers, e.g. char buf3[2048]; Then use memcpy() to copy each of the two original buffers into the third buffer.

If the two buffers actually contain null-terminated strings then the job is a lot simpler. std::string b3 = buffer1; b3 += buffer2;

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

what do you mean "join activity and person lists"? For each activity there is a list of persons? If that is yes, then do it like I posted.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

That's like saying "my car is broke. Anyone know how to fix it ?"

Did you google for the error message? Here is one idea (running out of resources)

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

You can not declare objects in a header file without the extern keyword. When you do it without that you will get the results you have because the objects will be defined in every *.cpp file in which the header file is included. Use extern in header files. Then in one, and only one *.c or *.cpp file declare them again but without extern.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

>>user[x] = static_cast<char> ( user[x] + 'a' - 'A' );

There is a lot easier way to do that too. user[x] = tolower(user[x]); line 30: That is reading the file twice (see line 28). Delete line 30.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

You use it exactly like you use the two pointers in struct congress. When you add a new node to activit1 or activity2 in struct congress, you add new person nodes to the head pointer in activities struct. The code below assumes you want to insert a new person node at the end of the linked list.

void AddPerson (struct Activity* act, struct person* p)
{
   p->next = NULL;
   if( act->head == NULL)
       act->head = p;
  else
  {
    // locate end of the linked list
    struct person* node = act->head; 
    while( node->next )
          node = node->next;
     node->next = p;
  }
}
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

This version creates the right path

int main(void)
{
	FILE *fp = fopen("/home/caars/Desktop/caars.properties.txt", "r");
	char *word;
	char *fname ="mytest.txt"; //filename
	char line[500];
	char *find;
	char *fpath;
	if(fp==NULL) 
      {
            printf("Failed to read input file\n");
	    return EXIT_FAILURE;
      }

	printf ("file read!!! \n");
    while(word = fgets(line,256,fp))
    {
        find = strstr(word,"/home/");
        fpath=find;
        if( find != NULL)
        {
            if( fpath[strlen(fpath)-1] == '\n')
                fpath[strlen(fpath)-1] = 0;
            break;
        }
    }
    fclose(fp);
    strcat(fpath,fname);
    printf("%s\n", fpath);
}
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

Actually, using strcpy() over the same buffer is not defined. See section 2.14.13 strcpy here. Also here and here.

You need to copy into a different buffer.

Agree -- memmov() is used for that purpose.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

>>There was a time when fgets did not exist.

There was a time when there were no computers either. So what? what does that prove? AFAIK fgets() was part of the original C standards.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

>>#define getchar() getc(stdin)

>>FILE *fopen(const char *filename, const char *mode);

Delete both those lines as they are declared in the header files. Its not necessary to repeat them in your programs.

for(i=0; i<argc; i++)
{
count1=0; count2=0; count3=0;
fp=fopen(argv[i], "r");

The first command line argument is argv[1], not argv[0]. The string in argv[0] is the name of the program that is being run. So that loop sould be for(i = 1; i < argc; i++)

while((ch=getc(fp))!=EOF)
{
if(isalpha(ch=getc(fp)))
count1++;
if(isspace(ch=getc(fp)))

That is just crazy coding :scared: Why are you getting characters from the file so often? Get a character at the beginning of that while loop then use the same character to test for isalpha(), isspace() etc . Reading the file for each of those is just plain dumb.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

I have been in at least one restaurant that does not allow tiping. There was a sign on each table that read something to the effect that "we pay our waiters a good salary, so tips are not allowed."

According to Wikipedia we can blame the Brits for the practice of tipping. It all started over there sometime in the 16th century.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

lines 26 and 36: >> for(x=1;x<265;x++)

Why are you hard coding the number 265 here? Just read the file until end-of-file is reachec

while( x < 255 && fromfile.getline(question[x], 255) )
{
  ++x;
}

You would be better off using an array of std::string objects instead of those two 2d character arrays, assuming you are allowed to use them.

The loop at lines 28-33: Why read the entire file just to find the answer to the question? Once the question is found (line 32) there is no point reading any more lines from the file. And why are you storing the read lines in memory? No point doing that either if you don't need to use them later in the program.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

Check the file system and see if the file actually exists.

file has 0 characters, 0 words, and 0lines

Is that what you typed on the command line for the name of the file ????

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

I've seen "report bad posts" forums that on other web sites too, but the way DaniWeb does it is a lot easier. We don't have to copy/paste a link into another forum to report it. Hitting the Flag Bad Post link does it all :)

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

The original poster's question little ambiguous. Did he mean to reverse a string or an integer? No one knows from the small amount of information he posted.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

Maybe something like this:

struct person
{
    char name[40];
    struct person* next;
};

struct activity
{
   char activity_name[40];
   struct activitiy* next;
   struct person* head; // head of linked list
};

struct congress
{
    struct activity* type1; // head of type 1
    struct activity* type2; // head of type 2
};
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

i have reversed the string here by recursion..

And what do you suppose will happen to your program if the original string has more than two characters? Hint: bomb!


If that is supposed to be a c++ why did you use a character array instead of std::string to hold the reversed string? You would have solved the above issue with a std::string.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

functions have to be declared before they can be used. Put int Mainmenu(); somewhere near the top of the program (such as between lines 5 and 6). That is called a function prototype.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

Is there any professional software being developed in India with Turbo C compiler? I surely hope not, but I have no way of knowing.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

You need to study your textbook or google for online tutorials about linked lists. They will show you how to write functions to insert and delete nodes in the linked lists.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

To remove blank lines, just ignore them. Don't do anything with them.

To remove trailing spaces: Set a pointer to the end of the character array, back up until the first non-white-space is found, then insert a string's null terminator at that point. For example, not that in the code below, isspace() is prototyped in ctype.h header file.

char line[] = "Hello World             "; // trailing spaces
char *ptr = line[ strlen(line)-1 ]; // start at end of line
// back up until either beginning of line is found
// or the first non-white-space character is found
while( ptr > line && isspace(*ptr) )
    ptr--;
// If the character at the current position is not
// a white-space character, then advance one 
// position.  If the character at the current position
//IS white-space, that means the entire line is just 
// white space, so make it a blank line.
if( !isspace(ptr) ) ptr++; // oops, went too far
*ptr = '\0'; // truncate trailing spaces
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

this programe is giving compile error:-

SEGMENTATION FAULT

No it isn't. A Seg fault is NOT a compiler error, but runtime error. The compiler has finished doing its job by the time you run the program.

line 18: c = (' ');

Remove the { and }. You don't put them around assignment values. Just make it c[i] = ' '; Are you supposed to replace the tab with 4 spaces on the screen only leaving the character array untouched ? Or are you supposed to put the 4 spaces in the character array? If the latter, then the program you write does not do what its supposed to do. When a tab is found, all remaining characters have to be moved to the right 3 places to open up enough room to insert three more spaces. Be careful not to overflow the character array when doing that.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

The idea of a universal heal care is an honorable one. The problem is that the program will more than likely bankrupt the nation. Where are we going to get the several trillion dollars per year to pay for it? Answer: taxes. Our (both private and business) taxes will more than likely double over the next 5-10 years.