Narue 5,707 Bad Cop Team Colleague

>Not to mention that his very own example is still unsafe!
Yea, that's why I felt the need to post something that's obviously going to break so that his little sheltered world of "I control the input" is shattered and he might actually realize that gets is impossible to use safely.

Narue 5,707 Bad Cop Team Colleague

>Here is the PROOF you need.....
I don't need proof that I'm right. I can't believe you actually thought that using gets in an extremely controlled environment would mean anything. By gets not being safe, here's my proof:

#include <stdio.h>

int main ( void )
{
  char pretest[20] = {0};
  char dummy[2];
  char posttest[20] = {0};

  int x;

  printf ( "Enter a number: " );
  fflush ( stdout );
  scanf ( "%d", &x );
  gets ( dummy );

  printf ( "pretest: \"%s\"\n", pretest );
  printf ( "posttest: \"%s\"\n", posttest );

  return 0;
}

Run that and type "123you're a moron". Overflowing a buffer is undefined, but with any luck you'll see something interesting.

Aia commented: Thanks for example. -Aia +1
Narue 5,707 Bad Cop Team Colleague

>Any reason for not using strlen ? :-)
None I can think of, but I didn't want to cause more confusion than necessary.

Narue 5,707 Bad Cop Team Colleague

>how did you get it quickly !
I happened to be active when you posted.

>it does not work as supposed to be
Your code is too complicated. Remove anything that's redundant, and be sure to walk through the execution on paper so that you know all of your variables have the right values. Then step through the code and verify that your paper run matches the actual execution. You several problems, try to figure out what I changed and why I changed it:

int isPalindrome ( const char *word )
{
  std::stack<const char> mystack;
  int count = 0;

  while ( word[count] != '\0' )
    ++count;

  int half = count / 2;
  int j;

  for ( j = 0; j < half; j++ )
    mystack.push ( word[j] );

  if ( count % 2 != 0 )
    ++j;

  while ( word[j] != '\0' ) {
    if ( word[j] == mystack.top() ) {
      mystack.pop();
      j++;
    }
    else
      return 0;
  }

  return 1;
}
Narue 5,707 Bad Cop Team Colleague

If you actually formatted your code intelligently, this error would be obvious. Here's the same code with better formatting. I've pointed out the error in red:

template <class Type>
int isPalindrome (Type * word)
{ 
  stackType <Type> mystack;
  int i=0,count=0;
  while(word[i]!=NULL)
  {
    count++;
    i++;
  }
  int half=count/2;
  for(int j=0;j<half;j++)
  { 
    mystack.push(word[j]);
  }

  if(count%2==1)//............... Odd No.
  {
    j=j+2;
    while(word[j]!=NULL)
    {
      if(word[j]==mystack.top())
      {
        mystack.pop();
        j++;
      }
      else
      { 
        return 0;
      }
    }
  }
  else //.................Even No.;
  { 
    j++;
    while(word[j]!=NULL)
    { 
      if (word[j]==mystack.top())
      { 
        mystack.pop();
        j++;
      }
      else
      { 
        return 0 ;
      }
    }
  }
}

return 1;
}
Narue 5,707 Bad Cop Team Colleague

>I knew that you were waiting - like a shark out for blood ,just take this apart line by line.
It's enlightened self interest, I assure you. If you teach people bad habits, I have to help them unlearn those habits.

>The gets(s) following the scanf() is not at all that evil if you know how to use it.
Really. Show me how to make any call to gets safe. You can do anything you want as long as it's a code change and doesn't rely on an outside force doing the right thing.

>This does work. It addresses the issue.
It addresses the symptom of the issue, not the issue itself. The issue is that scanf is being used improperly. Cleaning up the stream after scanf is just a Band-Aid that reeks of sloppy programming.

>With that in mind your comments are superflous.
I disagree. Any code posted here can be learned from, so I would be doing everyone a disservice by letting bad code go uncorrected.

>but it sounds like whatever the C equivalent of cin.ignore() would do the trick....
Sadly, there isn't a C equivalent. You have to do the C equivalent of this to remain portable:

char c;

while ( cin.get ( c ) && c != '\n' )
  ;
Narue 5,707 Bad Cop Team Colleague

"It works for me" is not a good reason to do something. Most of the time, if you can't prove that it's correct, it's horribly broken. ;)

Narue 5,707 Bad Cop Team Colleague

>scanf("%s",&answer);
That's a really bad idea. If answer is declared as char, you have a guaranteed buffer overflow. If answer is declared as an array of char, you have a potential buffer overflow and a type mismatch. All of them are undefined, so that line screws you over big time.

As a general rule of thumb, %s is off limits at all times.

Narue 5,707 Bad Cop Team Colleague

>Here is a code fragment which will assist you
I doubt it. That code is awful.

>main()
int main ( void )

>#define ADD 1;
>#define SUB 2;
>#define MUL 3;
>#define DIV 4;
Ending a macro with a semicolon is the path to disaster and cryptic error messages.

>char dummy[2]; /*** scanf trick buffer ***/
If you're using a buffer to trick scanf anyway, why not just eschew scanf in favor of something more suitable?

>printf("calc:==> ");
If you don't print a newline, you need to flush stdout. Otherwise the prompt may not be visible by the time scanf blocks for input.

>gets(dummy);
gets is evil. Don't ever use it. There are no excuses.

>}
Even though you cleverly used an implicit int in defining main, it still returns int.

Try this on for size:

#include <stdio.h>

int main ( void )
{
  char buffer[BUFSIZ];

  printf ( "Enter an expression (number operator number): " );
  fflush ( stdout );

  if ( fgets ( buffer, sizeof buffer, stdin ) != NULL ) {
    int a, b;
    char op;

    if ( sscanf ( buffer, "%d %c %d", &a, &op, &b ) == 3 ) {
      switch ( op ) {
      case '+': printf ( "%d\n", a + b ); break;
      case '-': printf ( "%d\n", a - b ); break;
      case '*': printf ( "%d\n", a * b ); break;
      case '/':
        if …
Narue 5,707 Bad Cop Team Colleague

>also it is givin me the wrong output
What's wrong about it?

>i need to list the avg of all the students entered with a *** next to the ones with 95% or higher
We haven't gotten to that part yet. You don't write everything in one swell foop, you write a little and test a little until all of the features are added and working properly. :icon_rolleyes:

Narue 5,707 Bad Cop Team Colleague

Because there are five students. The first average taken inside of the loop is the average of a single student's three grades. The second average taken outside of the loop is the average of all five student's averages.

Narue 5,707 Bad Cop Team Colleague

Here's how you would do it with the average being just another grade:

{
  double sum = 0;

  for (x=0; x<5; x++)
  {
    double avg = ( st[x].assignment + st[x].quiz1 + st[x].quiz2 ) / 3;

    if (st[x].id == 0)
      break;

    printf("\n %ld ", st[x].id);
    printf("\t %s ", st[x].name);
    printf("\t %.1f ", avg );

    sum += avg;
  }

  printf ( "Total average: %f\n", sum / 5 );
}

And I would appreciate it if you would keep related questions to the thread instead of sending me PMs. I watch the threads carefully, so PMing me isn't going to result in a faster response.

Narue 5,707 Bad Cop Team Colleague

Is assignment the average or just another grade? If it's the average then you need to sum them across all of the students:

{
  double sum = 0;

  for (x=0; x<5; x++)
  {
    if (st[x].id == 0)
      break;

    printf("\n %ld ", st[x].id);
    printf("\t %s ", st[x].name);
    printf("\t %.1f ", st[x].assignment );

    sum += st[x].assignment;
  }

  printf ( "Total average: %f\n", sum / 5 );
}

If it's not the average, then you need to calculate the average by adding the assignment grade and both quizes together, then dividing by 3 before adding that result to the sum.

What is that:-
double dummy = sin(0.0); //goodwin patch here!
double d = dummy;
dummy=d;

supposed to do?

My guess is that it's a floating-point hack for the compiler. Occasionally if you use a floating-point type (especially with older compilers), you'll get an error saying "floating-point formats not linked". It looks like some clueless instructor just started doing floating-point operations until the problem went away. :icon_rolleyes:

Narue 5,707 Bad Cop Team Colleague

>so ANYONE dare to try
You're making it sound like this program is difficult and it takes courage to complete when in reality it's trivial and takes little more than sufficient boredom to churn out a bit of code. So are you trying to get us to finish your homework for you? Or do you have a question that you'd like answered?

Narue 5,707 Bad Cop Team Colleague

Some battles aren't worth winning. ;)

Narue 5,707 Bad Cop Team Colleague

>I can bet the OP is using Turbo C++...
*sigh*

Narue 5,707 Bad Cop Team Colleague

>actually, i would recommend to use cin and cout
Those tend not to work in C. Judging from the code, I'd wager the OP is not using C++.

Narue 5,707 Bad Cop Team Colleague

Sorry, I didn't notice you were using custom objects. The easiest solution would be to do some research on remove_if. That function allows you to pass a predicate for custom comparisons. It would probably be easier than modifying your class to remove those errors.

Narue 5,707 Bad Cop Team Colleague

Eh, a general rule of thumb is not to loop with iterators if you're going to modify the container. The good news is that the algorithm header has a function template that you can use to remove all occurrences of an item:

#include <algorithm>

cout << "Input course number to delete: ";
cin >> target;

cache.erase ( remove ( cache.begin(), cache.end(), target ), cache.end() );
Narue 5,707 Bad Cop Team Colleague

>Try put a space before the %c
In my experience, if you say "Try <such and such>", you don't really know what's going on. If you do know that your suggestion works and why it works, please be sure to explain it so that you don't encourage inserting random characters with the "try it and see" attitude.

Narue 5,707 Bad Cop Team Colleague

>y = y – z + 1 --------------> y -= z + 1
Be careful when jumping to conclusions. These two expressions are not equivalent:

y -= z + 1    is equivalent to y = y - ( z + 1 )
y = y - z + 1 is equivalent to y = ( y - z ) + 1

The two produce different values.

>but what i was really wondering how does that work in the loop it kinda hard for me 2 picture it
Just change sum += save to sum = sum + save if it makes things easier. sum is a running total starting at zero, so if the loop has four iterations and save is set to 1, 2, 3, and 4, respectively, those values are added to sum and after the loop, sum is 10.

Narue 5,707 Bad Cop Team Colleague

>It's a bug in scanf(), you shouldn't use it.
It's not a bug in scanf, it's a bug in the code that uses scanf. But I do agree that if you don't know how to use something, you shouldn't use it until you do.

Narue 5,707 Bad Cop Team Colleague

>I installed TurboC++ version 3.0 in my computer
Okay.

>which runs a WindowsXP OS....
It might start without crashing, but a lot of old programs start on Windows XP and don't work correctly due to their age.

>The same Turboc++ works well in other computers with the exact configuration as mine..
Then they don't have the exact same configuration or you're not running it the same way. Do these other computers use Windows XP as well? Have you tried reinstalling the compiler?

>I know that Turboc++ is an ancient compiler, but my situation demands
>that i work in that environment.. so plz don't waste time by asking me to
>change to some other compiler...
You know, you could just say that you're being forced to use that compiler instead of being a jerk about it.

Narue 5,707 Bad Cop Team Colleague

>actually it did comple
Not as posted it didn't. If you copy that exactly into a source file with no extra framework and try to build, it'll fail.

Narue 5,707 Bad Cop Team Colleague

>o well nex time tell me its not compleated
Sorry, I thought that maybe the fact that it wouldn't compile without more code was a tiny hint. :icon_rolleyes:

Narue 5,707 Bad Cop Team Colleague

Where are you storing it? How you proceed depends on the quotation rules of whatever storage medium you're using. For example, if you were pasting that into a C++ string literal, you would need to escape the quotations for it to work:

"Set autoUpdateClient = CreateObject(\"Microsoft.Update.AutoUpdate\",strComputer)"
Narue 5,707 Bad Cop Team Colleague

I gave you enough code to understand the solution and complete it. But since that seems to be beyond you, I'll slave over the keyboard for five whole seconds to add one extra line:

int sum = 0;

while ( n <= i ) {
  int save = n * 26 * n;

  sum += save;
  cout<< save <<endl;

  ++n;
}

cout<<"\nSum: "<< sum <<'\n';
Narue 5,707 Bad Cop Team Colleague

Generally I require you to read a book instead of wasting my time with a question that can be more quickly and easily researched than posting and waiting for me to reply. But, a += b is logically equivalent to a = a + b .

Narue 5,707 Bad Cop Team Colleague

What don't you understand?

Narue 5,707 Bad Cop Team Colleague
int sum = 0;

while ( n <= i ) {
  int save = n * 26 * n;

  sum += save;
  cout<< save <<endl;

  ++n;
}
Narue 5,707 Bad Cop Team Colleague

If it's called anything, you want a splice. You're splicing a number of nodes out of the list and splicing them into another. Of course, if you don't want them to be removed from the first list, it's a copy rather than a splice.

Narue 5,707 Bad Cop Team Colleague

>Can anyone confirm that it is possible
Yes. Hey, I didn't have to read the rest of your post either! Imagine that...

>Any suggestions, outlandish or otherwise, would be greatly appreciated.
So are you really asking if it's possible or are you asking us how to do it?

Narue 5,707 Bad Cop Team Colleague

>void main()
int main()

>if((StringPtr = new char[26]) == NULL)
This may or may not correctly handle errors depending on how old your compiler is.

>*(StringPtr+i) = String;
You know, you can do StringPtr[i] = String[i]; too. It's a lot easier to read and write.

>cout << "StringPtr = " << StringPtr << endl;
You forgot the last character, the '\0'.

>delete [] String;
And why are you trying to delete an array?

>For "delete [] String;" part, I tried to use the for loop to see if it works.
Why don't you take that entire line away and see if it works? You're not supposed to release memory unless you explicitly allocate it.

Narue 5,707 Bad Cop Team Colleague

>maybe just maybe one of the experts out there could help me out.
No, sorry. We can make educated guesses (as I've already done), but you're simply not providing enough information. Go back to whoever gave you that code and tell them that you can't fill in a missing statement when you don't even remotely know how the statement is supposed to fit into the rest of the code. Here, this is a perfectly valid and correct answer to your question:

p = new NodeType;
p -> data = 18;
q = new NodeType;
q -> data = 32;

cout<<"Narue is kewl!\n";

q-> link = NULL;

Don't you see how absurd your request is?

Narue 5,707 Bad Cop Team Colleague

>If I knew then I wouldn't be asking for your help, now would I ?
There's a significant difference between knowing the problem and knowing the cause of the problem. Not the least of which being that I'm talking about the former and you're talking about the latter. Do you go to a doctor and just tell him to heal you? Do you go to a mechanic and tell him to fix any problems he finds? No, you go to a doctor when you're injured or ill, and can tell him where it hurts. You go to a mechanic when your car makes a funny noise that you can describe to the mechanic.

I'm the doctor, tell me where it hurts. I'm the mechanic, describe the noise you're hearing. Then I'll use my expertise to help you correct the problem. Now, what part of that are you having trouble understanding?

Narue 5,707 Bad Cop Team Colleague

>I fix the link part any responses
Um, good for you? You still haven't specified enough of a problem to be solvable. You tell us what the code is supposed to be doing, and we'll tell you why it isn't working. Until then, get used to not having any useful responses.

Narue 5,707 Bad Cop Team Colleague

>sorry but that all the info that i have
No wonder you can't figure it out, you don't even know what the problem is!

>Your struct doesn't contain a member called link ?
Yea, I saw that too...no, really. :o ;)

Narue 5,707 Bad Cop Team Colleague

p->link = q; , perhaps? It's hard to say with your little uninformative guessing game.

Narue 5,707 Bad Cop Team Colleague

>what do you think guys?
Your formatting is horried, void main is incorrect, gets is an extremely bad habit, and the algorithm is broken.

Let's start with the formatting. Braces are there for a reason. They tell you when a block starts and when a block ends. However, if you chain the closing braces all on the same line, it doesn't help people who are reading the code[1]. Here's a better format:

int underStringPos(char *source,char *under)
{
  char *sourceptr = source;
  int counter=0;
  while (*sourceptr){
    if (*sourceptr == *under){
      char *s = sourceptr, *u = under;
      while (*u){
        s++,u++;
        if (*s != *u)
          break;
        return counter;
      }
    }
    sourceptr++;
    counter++;
  }
}

Now it's obvious what's nested where and you can more easily follow the flow of execution.

I'll let void main and gets slide, but main returns int, and fgets is much safer than gets.

Now for the logic. Run your program with "testing" and "stupid algorithms are hard to get right". It returns 2 because your loop breaks early. If the first two characters of the substring are matched, the rest of the substring is completely ignored even if it doesn't exist in the source string (exercise: explain why). Perhaps if you return the index when *u is '\0'. That ensures that the loop has run to completion and the entire substring has been matched.


[1] This is a consistent rule of formatting. If you put multiple independent things on …

Narue 5,707 Bad Cop Team Colleague

>i dont know how to reverse the string in memory
How would you reverse "a"? Do nothing, clearly because the reverse of "a" is "a". What about "ab"? Mightn't you just swap the characters like this?

char save = str[0];
str[0] = str[1];
str[1] = temp;

Keep adding more characters and see how you would swap them around to reverse the string until you find a pattern. That's your algorithm for an in-place string reversal.

Narue 5,707 Bad Cop Team Colleague

>I really think this should just work.
It takes work to get things to work. Your pieces don't fit together because you don't define functions. The default constructor, destructor, and operator>> are never given bodies. That's the core of your problem.

Narue 5,707 Bad Cop Team Colleague

>return(intFirst, intSecond, intThird);
This doesn't actually return three values, it just seems that way because intFirst, intSecond, and intThird are global. What this expression actually does is evaluate (and subsequently ignore) all but intThird, which it then returns. So only intThird is returned, but because all of them are global, you can still access them in main and the values won't disappear.

Narue 5,707 Bad Cop Team Colleague

>i don't know if that's the rite procedure.
The string class is designed to act like a first class type, so the operations you use on numbers should work for string objects as well. There are a few problems with your code though.

>int nTemp;
nTemp is the temporary storage for the item you save. Since you're saving a string, ntemp should be defined as a string:

string nTemp;

>for (int iCv = 1; iCv < nLength; ++iCv)
One of the things you need to keep in mind with for loops is if you declare the counter in the loop, you can't use it after the loop. If you need iCv after the loop ends (which you do), it has to be defined outside of the loop:

int iCv;

for (iCv = 1; iCv < nLength; ++iCv)

>for (int k = iCv-1; k >= 0 && arnList[k] > nTemp; k--)
Ditto here. You use k after the loop, so it can't be defined inside the initialization clause.

The fixes are minor. All in all, you were very close to a working solution. :)

#include <iostream>
#include <string>
using namespace std;

int main()
{
  string arnList[9]={"John", "Dave", "Steve", "Kevin","Andrew","Scott","Colin","Timothy","Zenon"};
  int nLength=9;
  string nTemp;
  int iCv;

  for (iCv = 1; iCv < nLength; ++iCv)
  {
    //the new value to be inserted into a temporary location 
    nTemp = arnList[iCv];
    // k is the index of the number to the left of the iCv.
    int …
Narue 5,707 Bad Cop Team Colleague

>This is a really newb quest but how exactly do threads work.
It's not a newb question at all, so no worries. A thread is a thread of execution, which is a fancy way of saying that CPU time is divided between the running threads and each one is worked on in chunks. The net effect is that it appears as if multiple threads are executing simultaneously.

The nice thing about C# and .NET in general is that threads are dead easy to work with. Here is a tutorial introduction, and also a whole website that you should bookmark when working with .NET. :)

Narue 5,707 Bad Cop Team Colleague

>i am trying to make the program to ask user if he or she wants to continue
>when they click anywhere on the program.
That's extremely unconventional behavior. If I may offer a recommendation, how about using a progress bar and a cancel button? Naturally the encryption should be done in a separate thread so that the GUI is still usable. Not only is that easier that what you're asking, it's also what users will expect to see.

Narue 5,707 Bad Cop Team Colleague

>I believe sarcasm was supposed to be out of Daniweb..
What made you think that?

>And ya, if i need sarcasm , i would go, listen to my boss.
What you quoted wasn't sarcasm, it was a legitimate complaint about the readability of what you posted. Since we'd rather assume that you're an intelligent person and know how to post code, the only viable conclusion was that it was given to you poorly formatted.

>try it... u might like it..
Well, it's ugly and rather inelegant, but not a bad solution. I'm honestly not sure I could do better with the restrictions at first glance, but I would have to give it a think to be sure.

Narue 5,707 Bad Cop Team Colleague

>I found no help from this Forum...it is westing of time..
The feeling is mutual. After seeing how grateful you are that I saved you from making a huge mistake by not learning anything, I feel like I've wasted my time.

>People know how to give advice but don't know how to answer...
Here's an answer: Do your own homework! I've already done this stuff for myself, so why should I do it for you? What if you sail through your coarses and then become my co-worker? Then I have to deal with your incompetence because I did your work for you. Sorry, but I'm on a quest to improve the field, and people like you just drag it further into the gutter.

Narue 5,707 Bad Cop Team Colleague

>Well after two semesters and three absolutely 'horrible' teachers I've
>decided to drop my C, C++ courses and move over to a different line of study at college.
To be perfectly frank, if that's all it takes for you to quit, you wouldn't have made a good programmer anyway.

>If anyone knows of any good C/C++ certificate courses that can be done online, please let me know.
I know of a couple, but they're worthless and nobody cares about them. Certifications and diplomas don't mean nearly as much as a good portfolio, and most of the best programmers taught themselves. You might consider a bit of self study before jumping ship. Programming is a very rewarding field.

Narue 5,707 Bad Cop Team Colleague

>You gotta love our new search bbcode
It's a whole new level of catering to lazy people.

Narue 5,707 Bad Cop Team Colleague

The point of the questions is for you to do the analysis and come up with an answer. Any loser can find the answer by exhaustively searching for it online (well, maybe not any loser ;)); a promising computer scientist, as I'm sure you are, isn't afraid to figure it out for himself.