Narue 5,707 Bad Cop Team Colleague

>Do you know a real open source database that i can learn from
MySQL isn't real? A good way to irritate the people who try to help you is ignoring them completely.

Narue 5,707 Bad Cop Team Colleague

>whenever I enter "asdf" for the first input, the first ignore count comes
>out to be 1 when it seems to me that it should be 4 (ignoring 'sdf\n')
Run it through a debugger and check the values returned by ignore_line. I imagine you'll find them to be correct, and that should help you pinpoint the real problem.

Narue 5,707 Bad Cop Team Colleague

>I still don't fully understand why it remains in an error state, even if the ignore() is ignored.
It doesn't remain in an error state, but because the bad data isn't extracted, you immediately enter the same error state again as soon as you say in >> var; in extractr after trying to ignore the line.

>why does it behave like that?
If the stream is empty, sungetc will return eof(). If the stream is not empty and the last extracted character is not '\n', the entire line will be ignored. Here's what happens with "asdf" as the first input and "hjkl" as the second input:

  1. 'a' is extracted by the first call of extractr.
  2. 's' causes the second call of extractr to fail (it's not an int) and because the last extracted character was 'a', the condition in ignore_line is met and ignore is called. This extracts the rest of the line and leaves the stream empty.
  3. extractr prompts for correct input, but the next character is 'h', which fails because it isn't an int. However, because the stream is empty, sungetc returns eof() and the ignore isn't performed. Nowhere else in the error loop of extractr do you clear out bad characters, so the loop will run infinitely by erroring on 'h'.

>is there a usable and possibly portable solution?
Add another level of abstraction where you can count the number of times ignore_line fails to clear the stream. If it …

Narue 5,707 Bad Cop Team Colleague

>Question 1) What exactly does widen() do?
It takes the character you pass and converts it to the locale the stream is imbued with. For example, you want to convert a character literal to the appropriate wchar_t value if the stream is wide oriented, but leave it as is if the stream is not wide oriented.

>Question 2) Why does the first one work perfectly
>but the second one not fix the input stream?
It's hard to say without seeing how you're testing the code, but I'd guess you're immediately entering invalid data. Because no characters have been successfully extracted, sungetc returns eof() and the ignore isn't performed. The effect is the same as if the stream were empty.

The first function works because you're extracting characters and not relying on the previously extracted character, which might not exist for this particular usage, but it basically negates the solution provided in the second function because you're back to blocking reads.

Narue 5,707 Bad Cop Team Colleague

Moved to the C++ forum.

Narue 5,707 Bad Cop Team Colleague

>Remember, letters are stored actually as ascii code
There's no requirement that characters be stored as ASCII in C++. Assuming any relation beyond the decimal digits (which are required to have consecutive values) is non-portable and a bad practice to encourage.

Narue 5,707 Bad Cop Team Colleague

>Is there a open source relational data base?
MySQL.

Narue 5,707 Bad Cop Team Colleague

>You don't seem to have neither a namespace nor a class defined, both of which are needed.
A namespace isn't required, but for obvious reasons at least one class is.

scru commented: You are right. +4
Narue 5,707 Bad Cop Team Colleague

>toupper(ch);
>i thought this would convert all the text to uppercase but for some reason it didnt
You didn't do anything with the result. toupper returns the converted value, it doesn't (and can't) change the argument because it's passed by value.

ch = toupper ( ch );

>thanks though
FYI, I find that incredibly insulting. It's like saying "I found your help completely worthless, but I'll pretend to be polite anyway".

Narue 5,707 Bad Cop Team Colleague

There's a function called toupper. Check your C reference for more details.

Narue 5,707 Bad Cop Team Colleague

>Is it possible to some how tell the compiler to use
>these symbols even if they aren't in ASCII?
You need to specify what you mean by "tell the compiler". Of course you can write code to make use of different character sets, but it's not as trivial as using the basic character set (which is itself a subset of ASCII in terms of the available character glyphs). There's a whole field of research and development covering that practice, called internationalization and localization. You can also write characters outside of the basic character set directly in your source code if the compiler and text editor support them. So the answer to your question is yes, as with pretty much any "is it possible?" question you can come up with.

Alex Edwards commented: Whoo! I didn't know custom and foreign characters were a part of a field of study O_O - good info! =) +4
Narue 5,707 Bad Cop Team Colleague

>I think that is where my problem is.
I think your messed up main function is where the problem is. Compare your code to this:

int main()
{
  ifstream in ( "test.txt" );

  if ( !in )
    cout<< "There was an error unable to read file!";
  else {
    string totalwords[size];
    string word;
    int result;

    cout<< "Enter the word you wish to search for: ";
    cin >> word;

    int i = 0;

    while ( i < size && getline ( in, totalwords[i++] ) )
      ;

    result = binarySearch ( totalwords, i, word );

    if ( result == -1)
      cout<<"Sorry, that word is not in this file.\n";
    else {
      cout<< word <<" was found\nThe search conducted took "
        << iCompares <<" compares";
    }
  }
}
Narue 5,707 Bad Cop Team Colleague

>I'm going to handle this for once and for all today.
I'd love to see the transcript for that call. Seeing you lay down the law would be entertaining, I imagine. :)

Narue 5,707 Bad Cop Team Colleague

>Can you point me to a place where I can learn to parse it as a struct and not as a string?
You're already breaking the string down into component parts that can be assigned to the struct instance:

string phone, name, address;

getline(dfile, phone, ':');
getline(dfile, name, ':');
getline(dfile, address, '\n');
 
key = atoi(phone.c_str());
data.name = name;
data.address = address;

tree.insert (key, data);

There's no magic to parsing. All you're doing is recognizing and extracting component parts (as strings!) that can be converted to the appropriate types and used within your application logic.

Narue 5,707 Bad Cop Team Colleague

I'd be willing to bet that it's the string literal causing you problems. Try this:

#include <windows.h>
#include <iostream>

DWORD WINAPI MyThreadFunction( LPVOID lpParam );

int main()
{
  DWORD   dwThreadId;
  HANDLE  myThread;
  char *param = "HELLO";

  myThread = CreateThread(
    NULL,
    0,
    MyThreadFunction,
    param,
    0,
    &dwThreadId);

  while(1){
    Sleep(3);
    std::cout << "main";
  }
  std::cin.get();
  return 0;
}

DWORD WINAPI MyThreadFunction( LPVOID lpParam )
{
  std::cout << lpParam;
  while(1){
    Sleep(10);
    std::cout << "thread";
  }
  return 0;
}
Narue 5,707 Bad Cop Team Colleague

What compiler are you using?

Narue 5,707 Bad Cop Team Colleague

>It was provided to us by our professor
Then presumably it should work and you don't need to worry about it.

>tree.insert (key, data);
Don't forget to assign the parsed values to the data object before storing it in the tree.

Narue 5,707 Bad Cop Team Colleague

Well, you didn't show any of your tree, so the best I can do is link you to a tutorial I feel is good: http://eternallyconfuzzled.com/tuts/datastructures/jsw_tut_bst1.aspx

Narue 5,707 Bad Cop Team Colleague

Post a complete program that exhibits the problem, please. Also post the exact error message rather than summarizing.

Narue 5,707 Bad Cop Team Colleague

No, that's a legit form of advertising for Daniweb. It's called IntelliTXT ("Contextual Ads" in your control panel, IIRC). You can disable it in the miscellaneous options for your account.

Narue 5,707 Bad Cop Team Colleague

>Why is it a bad practice to use it?
Laziness. Most of the time, people who use goto are too lazy to consider all of the alternatives and choose the best solution. They fall back on goto, but because they're lazy, they don't use it wisely and the result is the infamous "spaghetti code" you hear horror stories about.

It's also said to be a bad practice because of ignorance. This has nothing to do with the actual use of goto, but rather an incomplete understanding of the how, why, and when by those who make the most noise about goto being "evil".

However, with that said, if you have to ask why it's a bad practice, you're probably not yet qualified to use it wisely.

vmanes commented: Well put! +6
Narue 5,707 Bad Cop Team Colleague

>Can anybody help me with an efficient C-Algorithm for this conversion?
printf directly supports printing characters, decimal, and hexadecimal, so that's easy. Converting between ASCII and EBCDIC is also easy and highly efficient if you use two conversion tables:

unsigned char ascii[] = {
  /* All characters in ASCII */
};

unsigned char ebcdic[] = {
  /* All characters in EBCDIC */
};

unsigned char to_ascii ( unsigned char c )
{
  return ascii[c];
}

unsigned char to_ebcdic ( unsigned char c )
{
  return ebcdic[c];
}
Narue 5,707 Bad Cop Team Colleague

>But the respective comment is made by other person...
I'm aware of that. I was defending you, in case it wasn't obvious. Further, I'd like to amend my own statement:

Granted, cutedipti probably doesn't compare to the quality of programmer that writes the standard library code yet, but suggesting that the code won't be good is kind of insulting.

Narue 5,707 Bad Cop Team Colleague

>I see you are using goto.
Every time I use goto, somebody jumps on it like ants on a cheese danish. Sometimes I wonder what their intentions are, but I'm afraid I already know the answer to that question. ;)

>And I know programmers don't like to use goto
Many don't even know why they don't like goto. They were told it's evil by their mentors and propagate that myth without fully understanding the feature or the reasoning behind avoiding it. You can verify this by asking why they don't like goto (they'll give you the classic canned answers) and when goto should be used (they'll invariably say "never").

>so if I may ask: why?
Laziness. I wasn't interested in spending extra time to polish the code. What you see is shooting from the hip draft code. However, that's not to say that in polishing I would necessarily remove the gotos, just that I didn't fully weigh the cost to benefit ratio and went for the quickest, cleanest solution.

>Did you just use it because it's fastest to write code like that?
I rarely worry about performance beyond a bird's eye view. This makes it easier to optimize when I need to and doesn't waste my time optimizing when I don't.

Narue 5,707 Bad Cop Team Colleague

>How do i export integers to another function?
Ideally you would import values through parameters and arguments, and export values through the return:

#include <iostream>

int foo() 
{
  return 10;
}

void bar ( int import )
{
  std::cout<< import <<'\n';
}

int main()
{
  bar ( foo() );
}
Narue 5,707 Bad Cop Team Colleague

>Yes it is possible
Correct.

>but the code will be large enough and not good from good programmers view
Um, how do you think printf was written? Granted, cutedipti probably doesn't compare to the quality of programmer that writes the standard library code, but suggesting that the code won't be good is kind of insulting.

By the way, the code for printf is long because it does a lot of work and interfaces with quite a bit of framework. If you check the source code for any commercial version, you'll find that while long and complex, the code is actually quite lean. Similar functions that take a variable number of arguments but do less work will be quite a bit shorter.

>Can we write a function similar to printf()?
Yes, here's an example (possibly buggy, and longer than it could be if I spent time on it):

#include <stdarg.h>

int spaste ( char *dst, size_t size, const char *fmt, ... )
{
  va_list args;
  const char *fill;
  int rc = 0;

  va_start ( args, fmt );

  while ( --size > 0 ) {
    switch ( *fmt ) {
    case '\0':
      rc = 1;
      goto finish;
    case '%':
      switch ( *++fmt ) {
      case 's':
        fill = va_arg ( args, const char * );

        while ( *fill != '\0' && --size < (size_t)-1 )
          *dst++ = *fill++;

        if ( *fill != '\0' )
          goto finish;

        ++fmt;
        break;
      case '%':
        *dst++ = '%';
        break;
      default: …
Narue 5,707 Bad Cop Team Colleague

>list<Item> l;
Perhaps you meant std::list<Item> l; ?

Narue 5,707 Bad Cop Team Colleague
template<typename T>
LinkedQueue& LinkedQueue::operator=(const LinkedQueue& queue)
{
	ll = queue.ll;
	return *this;
}

You need to be more careful about specifying template parameters:

template<typename T>
LinkedQueue<T>& LinkedQueue<T>::operator=(const LinkedQueue<T>& queue)
{
	ll = queue.ll;
	return *this;
}
chunalt787 commented: thanks +1
Narue 5,707 Bad Cop Team Colleague

>Is there a better way?
Yes, design the class hierarchy such that you don't need to know the dynamic type of the object. Perhaps some form of strategy pattern would clean up that selection code.

Alex Edwards commented: Definitely the easiest way to fix the problem! =) +4
Narue 5,707 Bad Cop Team Colleague

>it's basically okay to write big functions recursive, large functions iterative
What if I want to write a large function recursive and a big function iterative? :D

Narue 5,707 Bad Cop Team Colleague

It's best to think of the preprocessor as a completely different language than C. In particular, #define statements do not generally end with a semicolon because the semicolon is considered to be a part of the replacement text. This is what your compiler sees:

int mem[64000;];
int frames[6400;];

At which point the error is easy to locate.

Narue 5,707 Bad Cop Team Colleague

>what is difference between the final binary output of the same program
>written in c and written in an assembly if they do exactly the same thing.
Hand-crafted assembly written by a skilled assembly programmer will probably be more optimized than the object code produced by a compiler.

>if they are not same then how the programs function the same?
You can do things very differently in a program and still end at the same result. That's why you can ask ten programmers to write the same program to the same specification and get ten completely different solutions.

Narue 5,707 Bad Cop Team Colleague

>which would be the best for a beginner with no prior programming experience
It really doesn't matter. You'll learn how to program regardless of which language you choose, and the only way to figure out if you'll be comfortable for the long haul in learning a language is to start learning it. So pick a language that interests you and start teaching yourself the basics of it. If you ultimately decide that the language isn't for you at the time, you'll still have gained some experience.

Narue 5,707 Bad Cop Team Colleague

>I'm just surprised that there isn't a built-in (= optimized) function for doing that even in C++.
There is, it's called the std::vector class. ;)

>PS. How do I do this, do I add to your rep first and
>then mark solved, the other way around, or just either
Marking a thread as solved and giving rep are independent. Do either, neither, or both as you feel necessary.

>I thought it was a little too..basic for my needs
It's a basic overview, so no worries.

>I'm also more intressted in pointer to object etc.
Pointers are very regular. Once you know a few basic rules about them, you can apply those rules everywhere. A pointer to an int is no different fundamentally from a pointer to an object in terms of how the pointer works.

Narue 5,707 Bad Cop Team Colleague

>if understand you correctly, there is in C, NO way to accomplish what I desired? (i.e. create
>a "arbitrary pointer" for a multidimensional array/pointer-array for later assignment as in my
>third example)
Correct. That's not to say that you can't do it at all, you just can't do it with actual arrays due to the type mismatch. You can easily simulate a multi-dimensional array using dynamic memory, and from there you can use straight pointers. Since you mentioned C, I'll give you a C example even though this is the C++ forum:

int **arr = malloc ( 10 * sizeof *arr );
int **p;
int i;

for ( i = 0; i < 10; i++ )
  arr[i] = malloc ( 10 * sizeof *arr[i] );

/* Use it just like a 2D array */
for ( i = 0; i < 10; i++ ) {
  int j;

  for ( j = 0; j < 10; j++ )
    arr[i][j] = i * j;
}

p = arr; /* OK! */

This kind of goes without saying, because both arr and p were declared as int**, but perhaps the allocation method for simulating a multi-dimensional array will be useful to you.

Narue 5,707 Bad Cop Team Colleague

I don't want to sound mean, but you should try a different method of study, or perhaps consider that you may not be suited to programming. I've watched you ask questions for months without making any progress at all, and it's kind of depressing.

Narue 5,707 Bad Cop Team Colleague

When you use an array name, it's converted to a pointer to the first element. That's why this works:

int arr[10];
int *p = arr;

By a rights and purposes, an array shouldn't be compatible with a pointer and the assignment should fail. But because arr is converted to a pointer to int before the assignment, it works like this:

int arr[10];
int *p = (int*)&arr[0];

This conversion only applies to the first dimension, so even though you might expect the following to be valid, it's not:

int arr[10][10];
int **p = arr;

The rule is the same: when you use arr, it becomes a pointer to the first element. However, because the rule isn't applied recursively, what you get is a pointer to an array of 10 int, not a pointer to a pointer to int. This is the correct typing:

int arr[10][10];
int (*p)[10] = arr;

// Likewise with further dimensions
int arr2[10][10][10];
int (*p2)[10][10] = arr2;
stilllearning commented: thanks , this was really insightful ! +2
Narue 5,707 Bad Cop Team Colleague

>always exits after 2 guesses abd says the guess was correct even if it's wrong
Not necessarily. If you enter 10, then 70, you get three guesses. By the look of things, the correct message won't print unless the guess really is correct, but you only ever have one iteration of the loop because your return statement is inside the loop.

Compare and contrast:

#include <stdio.h>

#define NUMBER 50

int main ( void )
{
  int count = 1;
  int guess;

  do {
    printf ( "Guess> " );
    fflush ( stdout );
    scanf ( "%d", &guess );

    if ( guess < NUMBER )
      printf ( "Too low\n" );
    else if ( guess > NUMBER )
      printf ( "Too high\n" );
    else
      printf ( "Correct! That took you %d guesses\n", count );
  } while ( count++ < 5 && guess != NUMBER );

  if ( count > 5 )
    printf ( "Sorry, you only get 5 guesses\n" );

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

>this is all ive got
I suppose it could be worse.

>cout<<enter massage"<<endl;
You forgot the opening double quote for the string. This will cause your code to fail compilation.

>cin.ignore();
There's exactly no point to this line. In fact, it'll probably surprise users.

>int z=msg.length();
Type mismatch. length returns an unsigned quantity, the type of which can be acquired by saying string::size_type instead of int.

>fo(int n=z;n>-1;n--)
"fo", huh? I don't recall that on the list of C++ keywords. You also have an off-by-one error where you access msg[msg.length()] . Indexing in C++ is zero-based, so you're accessing the string beyond the last character.

In the interests of helping you improve, I'll teach you a trick for using unsigned types in a loop that counts down to zero. It's helpful in cases like this where you need to stop after zero, but can't actually get below zero. The trick is to rely on unsigned wraparound:

string::size_type n = msg.length();

while ( --n != (string::size_type)-1 )
  cout<< msg[n];

When you cast -1 to an unsigned type, you get the largest value of that unsigned type. This has the same effect as decrementing an unsigned type with a value of zero.

Narue 5,707 Bad Cop Team Colleague

>i write viod main coz it saves me tym from writing return = o
The return statement is optional in standard C++, so void main actually costs you an extra keystroke.

>so what actualli is the difference ?????
void main isn't guaranteed to work, int main is.

>whats the big deal if i write it one way or the other ??
If your compiler supports it and you never plan to use another compiler (including upgrades to your current compiler), then the only problem is people will think you're a bad programmer.

Narue 5,707 Bad Cop Team Colleague

>It is sorta implied I write a program
You can compile the implementation file without writing anything else. However, in some cases parts of a class won't be compiled (and thus won't throw errors if there's something wrong) unless you actually use them. So for a complete test you should write a simple main function that accesses the whole of the class' public interface.

Narue 5,707 Bad Cop Team Colleague

>i hate this poping up advertisement boxes
Dani doesn't allow invasive popups. Post a screenshot so she can determine where it came from and chastise accordingly.

Narue 5,707 Bad Cop Team Colleague

>i was trying to count the letters until i got to the space in between "Adam Sandler" and then
>return that as FN (aka first name) and then make all char's after the space be LN (aka last
>name).
That's a reasonable solution. It looks like you're using a non-standard string class, but this might help you figure out how to fix your code. It's based on your solution:

#include <iostream>
#include <string>

int main()
{
  std::string src = "Adam Sandler";
  std::string first;
  std::string last;
  std::string::size_type i = 0;

  while ( i < src.size() && src[i] != ' ' )
    first += src[i++];

  // Skip the whitespace
  while ( i < src.size() && src[i] == ' ' )
    ++i;

  while ( i < src.size() )
    last += src[i++];

  std::cout<< last <<", "<< first <<'\n';
}
Narue 5,707 Bad Cop Team Colleague

>i don't know algorithms and functions
Yes, I figured as much.

>Please provide the simplest code .
It doesn't work that way. You prove that you've made an honest attempt, then we provide help to boost you along.

Narue 5,707 Bad Cop Team Colleague

Since you didn't give nearly enough information, or provide even a rudimentary attempt, this is all you get. Here's how a real-world programmer would do it:

#include <algorithm>
#include <string>

std::string s = "hello";
std::reverse ( s.begin(), s.end() );
Narue 5,707 Bad Cop Team Colleague

>How do you find out if ThingType thing is in the list?
Loop through all of the nodes and stop when you have a match. It's a basic linear search:

bool TheList::search ( ThingType thing )
{
  Node *it = listPtr;

  // Assuming your list is terminated with a null pointer
  while ( it != 0 ) {
    if ( it->thing == thing )
      break;

    it = it->next;
  }

  return it != 0;
}
Narue 5,707 Bad Cop Team Colleague

>I don't know the size
Change your code so that you do. Put simply, if you don't know the size of an array parameter in a function you wrote, you've written bad code.

Narue 5,707 Bad Cop Team Colleague

>any chance to know why that is?
No, no chance at all.

Narue 5,707 Bad Cop Team Colleague

Not without resorting to non-portable solutions. Just do what everyone else does and don't lose the size that was passed to the allocation function.

Narue 5,707 Bad Cop Team Colleague

Well You can use the sort function to list and get the max value .

For that you will need to write a bool comp() function for your datatype.

Comparison function that, taking two values of the same type than those contained in the list object, returns true if the first argument goes before the second argument in the specific order (i.e., if the first is less than the second), and false otherwise.

After sorting. The item in your list will be the greatest i guess.

Erm, what's wrong with a simple loop?

double max = Value1[0];

for each ( double value in Value1 ) {
  if ( value > max )
    max = value;
}

>Will this be as fast as the sortingmethod or perheps this is difficult to say ?
I'd be surprised if the sorting method was faster.