Narue 5,707 Bad Cop Team Colleague

>reading it line by line forward costs too much...
And reading it line by line backward doesn't? :icon_rolleyes: Regardless of your solution, you've got the penalty of reading the file. Unless it's a random access file (unlikely), that involves sequential front-to-back access. Oh, and have you tried any of the suggestions and proven that they all cost too much? I'm willing to bet that you haven't.

Narue 5,707 Bad Cop Team Colleague

>what container would you suggest?
A linked list since you might not know how many lines are in the file.

>is it possible to use some kind of strtok but backwards?
No, the trick here is that you have to get the lines from the file into memory before you can do anything with them. So your problem boils down to either reading the entire file into memory, or using tricky seeking to actually read the file from end to beginning.

Narue 5,707 Bad Cop Team Colleague

Read it forward, store it in a reversible container, and then walk through the container backward. Or if you know the exact length of each line, you could open the file as binary and use fseek with read to get the lines in reverse. Or you could physically reverse the file before reading it. Or you could use recursion, but that's an exceptionally poor solution. ;)

Narue 5,707 Bad Cop Team Colleague

>that's c not c++
sprintf is C++ too.

>do u know anyother sol. in c++
Yes, use the hex manipulator with either cout or a string stream. Alternatively, you can do the conversion manually, but since that's available in C as well, I guess it doesn't count as C++. :icon_rolleyes:

Narue 5,707 Bad Cop Team Colleague

>Read the first line in the 5th post and then see who should take lessons.
I still think it's you. You see, you had some weird looping problem, but my code worked perfectly. So, since I answered the question you asked, and your broken attempt at my answer didn't work, I didn't answer your question? That's some good logic, and it explains why you can't write working code. Please don't take your incompetence out on me.

Narue 5,707 Bad Cop Team Colleague

Here's an example of what I mean, m3an, since words don't seem to have sufficient effect. I've corrected your errors and added enough to "use dynamic memory and classes together":

#include <iostream>
#include <string>
#include <new>

using namespace std;

class Names { 
private: 
  string title;
  string *p;
  int i;
public:
  Names ( const string& init ): title ( init ) {}
  ~Names() { delete[] p; }
  void enter_new();
  void display();
}; 

void Names::enter_new()
{
  cout<<"How many movies would you like to type? ";
  cin>> i;
  cin.ignore();

  p = new(nothrow) string[i];

  if ( p == 0 )
    cout << "Error: memory could not be allocated";
  else {
    for ( int a = 0; a < i; a++ ) {
      cout<<"Actor "<< a + 1 <<": "; 
      getline ( cin, p[a] );
    }
  }
}

void Names::display()
{
  cout<<"Title: "<< title <<"\nActors:\n";

  for ( int a = 0; a < i; a++ )
    cout<<'\t'<< p[a] <<'\n';
}

int main()
{
  Names film ( "My film" );

  film.enter_new();
  film.display();
}

Have I still not answered your question?

Narue 5,707 Bad Cop Team Colleague

>you are trying to access a private member with your object in the main function.
No, he's not. He's trying to access a private member of an object from within a public member function of the same class. Note the function definition:

void Names::enter_new()

Unless the rules have recently changed without my knowing, that's not a correct definition of main. The access is legal.

Narue 5,707 Bad Cop Team Colleague

>"Triangle with Rounded Corners" ? I do not get it.
It's called a joke. Chill out.

Narue 5,707 Bad Cop Team Colleague

>Why should it run Perfect under windows??
Pure luck.

>Why the ruckuss in Linux?
The problem is here:

strcpy(str1,cat[b].lname);//flagged
strcpy(str2,cat[b].fname);
strcpy(str3,cat[b].title);

str1, str2, and str3 don't point to anywhere predictable, and certainly not to arrays (that you own) large enough to hold the strings your copying into them. You need to allocate memory to these pointers before you can use them as temporary buffers.

Narue 5,707 Bad Cop Team Colleague

>That wasnt my problem, read the post again smrt..
I guess I'm not smart enough. Please point out where you said a dynamic array was required. The closest you got was saying "I want", which clearly says that you have a choice. Therefore, I suggested a better choice.

>and my codes is very long
Then make it shorter for posting. There's a sticky at the top of this forum that tells you how to post like an intelligent human being.

>and i added nothrow after i posted
Then you should say "thanks" and leave it because my comment was accurate when you posted. As I said, I'm not psychic, so I don't know what changes you made after you posted.

>think again! u hvnt answered any of my questions physc...
BS. Maybe you should take some lessons in reading for comprehension, because the fourth post in this thread answers your question fully. If it doesn't, you asked the wrong question.

Now, on a side note, I'm going to ask you to use proper spelling and grammar in your posts and proofread them before hitting the "Submit Reply" button. Your silly abbreviations are perilously close to breaking our Keep-It-Clean rule.

Narue 5,707 Bad Cop Team Colleague

>ya, i have to manage it, its a requirment.
I'm glad you mentioned that beforehand. :icon_rolleyes:

>ya i knw, im using nothrow
I'm glad you posted the actual code you're using. :icon_rolleyes:

On a side note, despite the fact that I can make extremely accurate guesses, I'm not psychic.

Narue 5,707 Bad Cop Team Colleague
Narue 5,707 Bad Cop Team Colleague

>i guess, easier for programmers, im just a beginner
Easier for beginners too because you don't have to worry about managing the memory.

>im having trouble with using dynamic memory and classes together
Compare and contrast:

void Names::enter_new()
{
  cout<<"How many movies would you like to type? ";
  cin>> i;

  p = new int[i];
 
  for ( int a = 0; a < i; a++ ) {
    cout<<"\n Please enter the name: "; 
    cin>> p[a];
  }
}

You're not just working with the film object in a member function, you're working with any possible object of the Names class. Also, you want the memory to persist until the object is destroyed, which means not deleting it until the destructor is called. Finally, new doesn't return a null pointer if it fails anymore, it throws an exception.

Narue 5,707 Bad Cop Team Colleague

>>>fflush (0);
>That is non-standard
No, it's well defined by the standard. If the argument is a null pointer, all open output streams are flushed.

Narue 5,707 Bad Cop Team Colleague

You're using strings, why not use vectors too? Much easier.

Narue 5,707 Bad Cop Team Colleague

>num[a] = num[a] = '0';
= is not the same as -. One is assignment, the other is subtraction. This is the line you want that will give you the correct sum:

num[a] = num[a] [B][I]-[/I][/B] '0';
Narue 5,707 Bad Cop Team Colleague

Yes.

Narue 5,707 Bad Cop Team Colleague

That works.

Narue 5,707 Bad Cop Team Colleague

>Does it have something to do with how I got scores?
It probably has something to do with returning an int from a function that's defined to return a string... Also, your function probably doesn't do what you want. Can you describe what you think it should be doing?

Narue 5,707 Bad Cop Team Colleague

>why they both have the same address??
Why do you keep asking the same question? Is it that difficult to understand that you can treat an address as multiple data types? If you don't get it yet, me explaining it umpteen more times probably won't help. So let's just say that this is how it is, and it really doesn't matter why it's that way yet.

>Its right, but you wouldnt call the array itself because they have different
>values, so the first value of the array is the same with the array itself if
>and only if the array only contains 1 value which is the first value..
What? Explain that a different way, because I didn't get what you're trying to say.

Narue 5,707 Bad Cop Team Colleague

>cout << &arr << endl << arr[0];
I assume you meant this:

cout << &arr << endl << &arr[0];

>Then what is wrong here?
Nothing. On your machine, &arr, and &arr[0] have the same address but they're treated as different entities because of a different data type. Let's say I have an array. I can tell the compiler that it's an int and as long as it behaves like an int when used like an int, the compiler doesn't know the difference. But I can still use it as an array if I want just by telling the compiler that it's an array. It's the same object, but the data type I use it as determines what it is to the compiler. Likewise, &arr is a pointer to an array, and &arr[0] is a pointer to an int. They both start at the same address, but the data type determines what they are to the compiler.

Narue 5,707 Bad Cop Team Colleague

>..or just take the numbers in a single go in a string variable and do something like:
I'm not sure an absolute zero fill is desired. I could be wrong, but it seems to me that he wants 10 numbers and if there aren't ten numbers, complete the list using zeros. So if you type 1 2 3 4 5, the resulting string would be 1 2 3 4 5 0 0 0 0 0.

Narue 5,707 Bad Cop Team Colleague

>i'm prepared to listen and learn.i'm not here to copy any stqatements i'm here tyo learn
:icon_rolleyes:

>and also this programming has to be done using if statement only
That's exactly why I think you're meant to use recursion. The only other way is to write the code inline:

int main()
{
  // Print the square and cube of 0
  // Print the square and cube of 1
  // Print the square and cube of 2
  ...
  // Print the square and cube of 9
}

That doesn't use an if statement, and neither does your current solution. So my conclusion is that you need to approximate this:

int main()
{
  for ( int i = 0; i < 10; i++ ) {
    // Print the square and cube of i
  }
}

Which can be done with a recursive function using an if statement as the base case:

void f ( int i )
{
  if ( i < 10 ) {
    // Print the square and cube of i
    f ( i + 1 );
  }
}

int main()
{
  f ( 0 );
}

>you can just write
>using namspace std;
That's generally considered a bad practice though because it opens up names that you don't use and defeats the purpose of namespaces in avoiding naming collisions.

>but still whenever i compile this program it is leaving this numbers in
>between so i don't know how to …

Narue 5,707 Bad Cop Team Colleague

Okay then, the only way to count your numbers is to have some kind of sentinel value at the end of the list. This could be a newline, EOF, or something else. In the case of EOF, you can just read numbers until EOF and maintain a counter up to 10. If the loop breaks and the counter isn't 10, fill in the rest of the string with zeros. Like so:

string s = ""
integer n = 0

while n < 10 and not eof() do
  s = s + get_word() + " "
  n = n + 1
loop

while n < 10 do
  s = s + "0 "
  n = n + 1
loop
Narue 5,707 Bad Cop Team Colleague

>cores += " " + getScores;
How is getScores declared?

Narue 5,707 Bad Cop Team Colleague

I imagine the problem was designed to force you to use a recursive solution. That's really the only way to loop without using a loop.

Narue 5,707 Bad Cop Team Colleague

Unsigned.

Narue 5,707 Bad Cop Team Colleague

>I dont understand anything can someone explain me how its works?
I did. Multiple ways, as simply as possible. You keep making the same mistake, which makes me think that you're not even trying to understand what you're told. This suggests one of three things:

1) You're not cut out for programming.
2) You're hoping that we'll stuff knowledge into your brain without any effort on your part.
3) You're trying to learn things that are too advanced for your present level.

I'm leaning toward 3 with a nice helping of 2. Why don't you slow down and try to learn some more beginner level topics?

Narue 5,707 Bad Cop Team Colleague

>So the first element and the array its self are at same memory location
Not necessarily. There's no guarantee that the address of the array as a whole is the same as the address of the first element. That's usually the case though.

>and when i use the array name the compiler treats its like a pointer , but
>its not a pointer right?
No, it is a pointer. When you use the array name in value context, the compiler actually converts it to a pointer to the first element. This is only the array name though, the array itself is never a pointer.

Narue 5,707 Bad Cop Team Colleague

>I cant understand this
What a surprise.

>If the address of the array is the same as its value, why?
The address of the array is the memory location that the array starts at, right? The address of the first element of the array is also the memory location that the array starts at. Why shouldn't they be the same? The only difference is the resulting data type.

Narue 5,707 Bad Cop Team Colleague

>I can still build BGI programs with no problem with Turbo C++ 1.0 on up-to-date XP.
"It works for me" has never been a good argument. A lot of the problems I've encountered on forums such as this one stem from trying to use an obsolete compiler on a modern OS.

Narue 5,707 Bad Cop Team Colleague

>I did the changes but still having issues.
It would be nice if you told us what those issues were. :icon_rolleyes:

>cout endl;
That won't compile. You probably meant cout<<endl; .

>grade= ((10- (grade/10)) + 64);
grade doesn't have a predictable value but you use it in an expression...

Narue 5,707 Bad Cop Team Colleague

>we are taking 8 bytes of memory , then we are giving to 4 bytes of it the
>value 2 and the next 4 bytes of it the value 3.
No. An int doesn't have to be 4 bytes. The statement is otherwise correct, but I would word it like this: Enough memory is set aside for two integers. The first integer is given the value 2 and the second integer is given the value 3.

>then "arr" is converted to a pointer to the first element.
No, the array name is only converted to a pointer in value context. That is, when you try to use it. This is a declaration, not a use. If you try to print the first item, that's value context and the array name is converted to a pointer:

/* arr is not converted to a pointer */
int arr[2] = {2, 3};

/* arr is converted to a pointer */
printf ( "%d\n", arr[0] );

>What about "arr" location in memory
All of the following can and often do produce the same address:

int arr[2] = {2, 3};

/* Address of the first element */
printf ( "%p\n", (void*)&arr[0] );

/* Address of the array converted to a pointer */
printf ( "%p\n", (void*)arr );

/* Address of the array as an object */
printf ( "%p\n", (void*)&arr );

This doesn't mean they're the same thing though. There's a difference in type between the address of …

Narue 5,707 Bad Cop Team Colleague

>I just was wondering why the array its self are a pointer to the first element?
Because it was a design decision by the language creator.

>and what is the difference between an array and a pointer?
An array is a collection of objects, and a pointer is a variable whose value is an address. A pointer can of course point to any item in an array, or to the array itself. In value context, an array name is converted to a pointer to the first element. In object context, an array name refers to the array as a whole. The three object contexts are:

1) As the operand to sizeof
2) As the operand to the address-of operator
3) As a string constant initializer

Every other use of an array is in value context.

Narue 5,707 Bad Cop Team Colleague

I'm constantly amazed how people use an ancient compiler with an obsolete graphics library and expect it to work seamlessly with the latest operating system...

Narue 5,707 Bad Cop Team Colleague

>*ptr = toupper(*ptr);
You forgot to include ctype.h.

>/* first letter of string */
This is a benign logic error. The end result is that it doesn't cause a problem if the first character in the string isn't a letter, but the fact that you always do it suggests a flaw in your thinking.

>/* going thru string */
This is not a benign logic error. More than one space between words will produce unexpected output. You need to consider that a word could be anywhere in the string and surrounded by anything:

#include <stdio.h>
#include <string.h>
#include <ctype.h>

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

  printf ( "Enter a string: " );
  fflush ( stdout );

  if ( fgets ( buffer, sizeof buffer, stdin ) != NULL ) {
    size_t i = 0;

    /* Strip the optional newline */
    buffer[strcspn ( buffer, "\n" )] = '\0';

    /* Capitalize the first letter of every word */
    for ( ; ; ) {
      /* Ignore spans of whitespace */
      while ( buffer[i] != '\0' && isspace ( buffer[i] ) )
        ++i;

      if ( buffer[i] == '\0' )
        break;

      buffer[i] = (char)toupper ( buffer[i] );

      /* Ignore spans of non-whitespace */
      while ( buffer[i] != '\0' && !isspace ( buffer[i] ) )
        ++i;
    }

    printf ( "|%s|\n", buffer );
  }

  return 0;
}

It's easy to translate something like that into pointer arithmetic, if you're into that sort of thing.

Narue 5,707 Bad Cop Team Colleague

For the fifth time, YES. You're correct. Geez. Most people are wrong and don't care, but you're right and lack the confidence to believe it.

Narue 5,707 Bad Cop Team Colleague

>Is it correct?
If I say yes, will you believe me and stop trying to repeat your explanation over and over?

Salem commented: It's a marathon isn't it. +6
Narue 5,707 Bad Cop Team Colleague

>i am new to it, if you could please be a bit kind not to use those words for my code
We're not going to blow sunshine up your butt just because you're new. Eventually someone is going to tell you that you're a bad programmer, and it's better that you hear it sooner than later so that you can make improvements. A better reaction would be to ask us why your code is bad and how you can improve it.

Narue 5,707 Bad Cop Team Colleague

>but then its depends on our option
>/GF OR /Gf
Too specific. Just because your compiler has this switch doesn't mean mine does too. Up to this point you're long winded, but correct. I'm worried what will happen when you come across something truly complicated. It'll take us years to explain it to you. :icon_rolleyes:

Narue 5,707 Bad Cop Team Colleague

>Why here the assignment is legal?
Because string literals weren't always const. Thus, a lot of existing code assigns a string literal to a non-const pointer. So rather than break all of that code, the standard allows this particular assignment as an exception.

Narue 5,707 Bad Cop Team Colleague

>so maybe you just tell me what is correct once for good??
I did. Everyone did. Given char *ptr = "String"; , "String" is const, ptr is not, the assignment is legal, but you can't use ptr to modify "String". I can't put it any more simply.

Narue 5,707 Bad Cop Team Colleague

>Thats it??
Close enough. How many times do you need to repeat the same thing to get it?

Narue 5,707 Bad Cop Team Colleague

Either you're incredibly dense, or you're putting way too much thought into something simple. Given char *str = "Hello"; , "Hello" is const, str is not. The assignment is allowed, but you can't use str to modify "Hello". That's as deep as it goes, so stop digging.

John A commented: Wow, I wish I could explain stuff that simply. +11
Narue 5,707 Bad Cop Team Colleague

>its a very simple logic
You'd think, but that might not be the case.

>but the problem is its showing segmentation fault.
I'd wager my next pay check that you're trying to strtok a string literal.

Narue 5,707 Bad Cop Team Colleague

>You can check ending & starting of function's bracket by simply pressing the key (Ctrl + e ).
It sounds very much like you're describing a feature of your favorite text editor and not a universal capability. :icon_rolleyes:

Narue 5,707 Bad Cop Team Colleague

C-style strings aren't compared with ==, you have to use strcmp or equivalent. What you're doing now is comparing two addresses that are unlikely to be the same, not the contents of two strings.

Narue 5,707 Bad Cop Team Colleague

>yahh , my code is too complicated with comparing with your code
You can create good code by taking mediocre code and chipping away at it until nothing else can be removed. And of course, great code is designed to be elegant from the start. ;)

>But i want to know what does std:: mean ?
No doubt you have using namespace std; somewhere in your code. If you take that out, you have to prefix each name that's in the std namespace with std::.

>error C2039: 'stackType' : is not a member of 'std'
I didn't have your stackType, so I used the standard stack template. It's in the std namespace, but your custom stack type isn't, so you need to remove the std:: prefix.

>error C2143: syntax error : missing ';' before '<'
>error C2143: syntax error : missing ';' before '<'
>'mystack' : undeclared identifier
>left of '.push' must have class/struct/union type
>left of '.top' must have class/struct/union type
>left of '.pop' must have class/struct/union type
These are waterfall errors that stem from the first error. If you correct it, they'll disappear.

Narue 5,707 Bad Cop Team Colleague

My Pascal is a tad rusty, but I'm reasonably sure the C++ translation is thus:

void gPacker::InvertInteger ( unsigned int i )
{
  Result = ( i & 0xff ) << 24;
  i >>= 8;
  Result += ( i & 0xff ) << 16;
  i >>= 8;
  Result += ( i & 0xff ) << 8;
  i >>= 8;
  Result += i;
}
Narue 5,707 Bad Cop Team Colleague

My pleasure. :)