Narue 5,707 Bad Cop Team Colleague

Project properties?

Narue 5,707 Bad Cop Team Colleague

Who do you think this guy is?

What makes you think he's anything other than a model hired for that photo?

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

What operating system?

Narue 5,707 Bad Cop Team Colleague

Perhaps it would be a good idea to have "don't subscribe" as the default. IIRC, it defaults to email subscription.

Nick Evan commented: indeed +0
Narue 5,707 Bad Cop Team Colleague

I'd say you have other stuff in the project that's conflicting. The code is conforming (I recognize it from Accelerated C++), and builds just fine on a fresh project in Visual Studio 2008. Try creating a new project and see if you still get the error.

Narue 5,707 Bad Cop Team Colleague

And what error do you get?

Narue 5,707 Bad Cop Team Colleague

daniweb membership is like a bad virus - once you get you'll never get rid of it

Step 1) Remove all personal information
Step 2) Disable contact options
Step 3) Log out
Step 4) Enjoy the effect of a deleted account

The only hard part is ignoring the temptation to log back in because Daniweb is such an addictive community.

have fun trying to unsubscribe from all the emails you will be getting once in a while from this site

You make it sound like unchecking a check box in the control panel is an insurmountable feat. :icon_rolleyes: You're bitter that we don't delete accounts[*], I get that. But if you're going to badmouth us, at least do so without resorting to blatant lies. There are plenty of legitimate problems to complain about without inventing new ones.


[*] You get no sympathy because all of this stuff is very clearly outlined in the Acceptable Use Policy. Just like a software EULA, you can accept without reading it, but it's your own damn fault for entering a binding contract without understanding the consequences.

Narue 5,707 Bad Cop Team Colleague

So is the compiler not good?

The compiler that ships with Visual C++ is quite good. Your problem is very vague, but I suspect you're trying to use non-portable stuff. Can you give an example of what code "is not working"?

Narue 5,707 Bad Cop Team Colleague

I guess I missed the deadline. Well, now you have more time to do some reading up on how strings work in C. :)

Narue 5,707 Bad Cop Team Colleague

the code is working right now i do not want to go and mess it up

The code is not working right now. Right now the behavior is undefined because you're walking off the end of allocated memory. At a glance, you should terminate str before copying it.

See i have added the'\0' still dosent change

...

Narue 5,707 Bad Cop Team Colleague

Until you know the rules for braces, it's best to always use them:

if (secretnum<getint)
{
    cout<<"try a lower number";
    goto loop;
}
else if (secretnum>getint)
{
    cout<<"try a higher number";
    goto loop;
}
else
{
    cout<<"that is correct";
}

By the way, you should also avoid goto in favor of explicit loop structures, especially when starting out.

Narue 5,707 Bad Cop Team Colleague

>if(chooseType == "a" || "A"){
C++ parses it like this:

if(chooseType == "a" || "A" != 0){

Adjust accordingly.

Narue 5,707 Bad Cop Team Colleague

Those weird symbols mean you haven't terminated your strings with '\0'.

Narue 5,707 Bad Cop Team Colleague

In such a case it's best to post the code and ask how to make it shorter. :icon_rolleyes:

Narue 5,707 Bad Cop Team Colleague

and if i should not even use a goto statement..
what might be the alternative..??

Are you reading impaired? I've listed several alternatives, and they STILL depend on how strict your definition of iteration and recursion are. Stop asking the same damn question when you need to be more specific. Answer the following questions:

  • If I recursively spawn new threads or processes to do the work, does that count as recursion?
  • If I call a library that performs a loop to do the work, does that count as a loop?
  • If I call another pre-existing program that does the work in an unspecified manner, does that still count as a loop or recursion?
Salem commented: Give it up, they ain't worth the effort +17
Narue 5,707 Bad Cop Team Colleague

For the record, hiding a pointer behind a typedef is a fantastic way to confuse both yourself and others.

Narue 5,707 Bad Cop Team Colleague

I'll save you the trouble of searching around: qsort.

Narue 5,707 Bad Cop Team Colleague

Also, you *might* want to do

fflush(stdin);

right before every single "scanf" that you have.

Regards.

You might, if you want to suffer from undefined behavior. fflush is not defined for input streams and is the incorrect way of discarding unextracted input. The correct way is a loop:

void clear_stream(FILE *in)
{
    int ch;

    do
        ch = getc(in);
    while (ch != '\n' && ch != EOF);
}

A complete solution is not possible without losing portability. The remaining problem is how to avoid blocking when the stream is already empty.

Narue 5,707 Bad Cop Team Colleague

int *ptr=50;

This is attempting to point ptr to address 50. Typically, you can only point to addresses within your process' address space, otherwise you'll get a segmentation fault. Java references are actually gimped pointers, which should give you an idea of how to use C pointers for basic object access. First you need a valid object, then you assign the address of that object to your pointer. Finally, you dereference the pointer to access the object:

#include <stdio.h>

int main(void)
{
    /*
        These two lines are the rough equivalent of 

        Integer p = new Integer(50);
    */
    int obj = 50;
    int *p = &obj;

    printf("%d\n", *p);

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

so can we conclude that there is no chance to do this task without a loop or a recursion?

We can conclude that only because you've completely failed to be clear about what constitutes a loop or recursion. The usual solution is to simulate a loop with goto:

#include <stdio.h>

int main(void)
{
    int limit;

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

    if (scanf("%d", &limit) == 1) {
        int i = 1;

again:
        if (i < limit) {
            printf("%d\n", i++);
            goto again;
        }
    }

    return 0;
}

This depends on the loophole that "loop" means one of the explicit loop constructs: while, do..while, and for. While goto can be used for circular jumps (looping), that's not its only capability, which potentially excludes it from the restrictions.

Another solution uses looping/recursion hidden behind a library or by spawning threads/processes to simulate recursion. However, all of these can be labeled as loops and recursion depending on how strict you are, so a very specific definition of the program's restriction is needed before any of them can be called a solution.

Narue 5,707 Bad Cop Team Colleague

Not to promote the use of alcohol, but it is in most places legal at least.

Well I turned 21 a few months ago, so I'm wondering what everyone else drinks when relaxing on their off days?

I like:
Miller Lite
Bud Light
Other non-traditional (non-mass-produced) beers like dos equis, killian's irish red
Miller High Life is dirt cheap, so when I'm on a budget it's great.

So, what are you drinking this weekend?

A good single malt scotch whisky, spiced rum, and margaritas (in order of preference) make up my alcohol choices. Occasionally I'll drink wine or beer.

Narue 5,707 Bad Cop Team Colleague

Feel free. I look forward to the interesting things you can come up with.

Narue 5,707 Bad Cop Team Colleague

Really? You've found nothing on the web?

Narue 5,707 Bad Cop Team Colleague

But when I close the file, all the other values become null (0), and only the values I modified stay as they are.

I suspect you're doing more than you say, but each time you open the file strictly for output, it will be truncated to zero length. Try this instead:

fstream saveFile(saveFilePath, ios::in | ios::out | ios::binary);
yoni0505 commented: Helped me to solve a problem! +1
Narue 5,707 Bad Cop Team Colleague

To answer the question in your title, yes. write's first argument must be a pointer to char where the number of available characters starting at that pointer match the second argument.

everything between these values became null.

Can you elaborate? What do you want to happen (with examples) and what's actually happening (with examples)?

Narue 5,707 Bad Cop Team Colleague

Do you know what could be the issue?

Yes, the issue is you not understanding how substr works. The second argument is a count, not an index.

Narue 5,707 Bad Cop Team Colleague

There has to be a loop or recursion in some form somewhere. Since you clearly aren't able to refine your description of the problem to allow for semantic loopholes, the problem is impossible. It cannot be solved on a conventional computer.

Narue 5,707 Bad Cop Team Colleague

i want the question to be solved without any recursion or looping concept for unknown variable range of numbers..

You don't need to repeat the requirements, you need to clarify them. For example, if I write a program that calls another pre-existing program to do the looping, does that still count as using a loop? If I simulate recursion using signals, threads or by spawning new processes, does that still count as recursion?

Narue 5,707 Bad Cop Team Colleague

So you want to print an unknown, variable range of numbers, without using any form of loop or recursion. Pretty much any solution with those restrictions will rely on a hidden loop or recursion at a deeper level. It becomes a question of semantics: "Is this enough of a loop to fall under the restriction?"

Depending on how the question is answered, the problem could be solved with a trick, or it could be impossible.

Narue 5,707 Bad Cop Team Colleague

I have used calloc(), instead of combination of malloc and memset as a work around.

calloc is the functional equivalent of malloc + memset. It might be faster due to the potential for standard library optimization over hand rolled code, but probably not enough to make a big difference.

Have you considered

  • Avoiding dynamic allocation entirely?
  • Using malloc, but writing your algorithms such that memset is not necessary?
Salem commented: nice +17
Narue 5,707 Bad Cop Team Colleague

I believe I have this right ... don't you think so ?

Nope, I don't think so at all. It's clearly stated in our acceptable use policy that accounts are permanent as Daniweb's way of complying with anti-spam guidelines.

Narue 5,707 Bad Cop Team Colleague

I don't want to participate on any forum or receive any kind of information or emails.

This can be accomplished without deleting your account. In fact, I'll go ahead and turn off all notifications for you. Then you can simply never log back into this account again and the effect will serve your purposes.

Narue 5,707 Bad Cop Team Colleague

Maybe you'd simply be better off using MATLAB or Fortran. :icon_rolleyes: While it's possible to shoehorn behavior from other languages into C++ with specialized libraries, the results are often (if not always) sub-optimal. Your best bet with C++ is writing code to work with the language rather than fight it.

Narue 5,707 Bad Cop Team Colleague

If you want both, you're SOL. Some kind of copying has to be done to end up with the right type.

Narue 5,707 Bad Cop Team Colleague

Assuming your matrix is a two dimensional array, and the function takes a one dimensional array of the same type, you can do this:

some_function(my_matrix[3]);

I'm assuming a lot because you didn't provide nearly enough information to properly answer the question.

Narue 5,707 Bad Cop Team Colleague

just for giggles here is a generic approach-ish

Hmm, overkill maybe? That whole process can be accomplished with a non-member function, and the approach can be made fully generic (for types that support operator>>) with argument type deduction:

#include <iostream>
#include <istream>
#include <sstream>
#include <string>

template <typename T>
bool get_next(T& value, std::istream& in = std::cin)
{
    std::string line;

    try {
        if (std::getline(in, line)) {
            std::istringstream iss(line);

            if (iss>> value >> std::ws && iss.eof())
                return true;
        }
    } catch (...) {
        // Collect all exceptions into a failure result
    }

    return false;
}

int main()
{
    float num;

    while (!get_next(num))
        std::cerr<<"Invalid input, please try again: ";

    std::cout<<"Your number is "<< num <<'\n';
}

The problem, of course, is your assumption that there's one item of input for each line. You'll find that as you adjust the solution to allow multiple items per line, it begins to look more and more like how the standard library does things. ;)

Narue 5,707 Bad Cop Team Colleague

Your loop looks like this:

while (p != NULL)
{
    /* p is definitely not NULL, so this is safe */
    p = p->next;

    /* If p->next was NULL, p is now NULL, so this is *not* safe */
    if (p->value == a)
    {
        ...
    }
    else
    {
        ...
    }
}

See the problem?

Narue 5,707 Bad Cop Team Colleague

>ctrLet[ctc]++;
ctc is an int, which is fine. But ctrLet is also an int. The int type does not support indexing.

Narue 5,707 Bad Cop Team Colleague

>if(p->value = a)
Two things:

  • Comparison is done with the == operator. The = operator is for assignment. You're doing the equivalent of this:
    p->value = a;
    if (p->value != 0)
  • You've already advanced p at this point, so how do you know it's not NULL?
Narue 5,707 Bad Cop Team Colleague

What I thought was,with the above code,there won't be a second prompt for input,because,the '5' I entered is already lying in the stream and y will also be given the same value.But it isn't so.Why?

When you successfully read a value from the stream, that value is removed from the stream.

cin.get() extracts characters from the stream.But if it is empty does it ask for another one from the user?

It waits for further input, yes.

Narue 5,707 Bad Cop Team Colleague

1) Where did the "5" come from? Why specifically "5"?

You don't need to know or care yet. When you gain proficiency and want to look into how compilers work at a lower level, fine. But for now, studying how undefined behavior works in practice will just distract you from the task of learning proper C. Further, it might encourage you to rely on dangerous behavior.

Had it returned "0" then I would at least understand from where the value returned came from.

Where would the value of 0 come from if your code didn't explicitly return it? I fail to see how 5 is baffling and 0 is not when they're both complete garbage.

Why isn't there a warning or failure? Neither cc nor gcc complains, even with the -pedantic option.

Your warnings aren't turned up high enough, because I get a warning on all of my compilers. Try -Wall and -WExtra.

Actually, I was under the impression that K&R was the standard for many years!

K&R is split into two parts: a tutorial and a reference. The reference served as something of a poor man's standard until ANSI and ISO rolled out a sufficiently large tome. Provided you follow the best practices given in the book, your own code will be pretty good.

Though, I can recompile and run and still get "5" so I actually think that there is something intrinsic about the code that gives "5".

I just ran your code in …

Narue 5,707 Bad Cop Team Colleague

I think this is caused by malloc, or something else.

It's caused by something else. That something else is you misusing malloc. Tell me, what is sizeof(btree) and what makes you think it's equal to sizeof(nodo)? Allocating less memory than you use is a good way to corrupt the memory manager.

Narue 5,707 Bad Cop Team Colleague

All POST operations are performed at the end of a statement, and all PRE operations are performed at the beginning of a statement. Is that correct?

It's close enough, yes. To be strictly precise, the update can happen any time between the previous and next sequence point as long as the result of the expression is correct. So in your code, it could potentially work like this:

int numItems = 0;
int temp;

temp = numItems;
++numItems;
inventory[temp] = "sword";

temp = numItems;
++numItems;
inventory[temp] = "armor";

temp = numItems;
++numItems;
inventory[temp] = "shield";
Narue 5,707 Bad Cop Team Colleague

i want to write a program

the program should be written in C i need it for "borland c"

FYI, nobody here is going to write this for you. We'll help you write it yourself, but that's it.

Narue 5,707 Bad Cop Team Colleague

I never tried in C/C++ but in Java it was quite clear that ObjectInputStream.readInt() is much slower than FileInputStream.read().

It's also generally a safe assumption that reading a string and converting it to another type is slower than simply reading and storing a string.

Narue 5,707 Bad Cop Team Colleague

but why did it return anything at all?

Because you told it to:

int power(int x, int y);

This says "I will return an integer value". Even if you don't explicitly return something, the compiler will generate code to return a value. The compiler also won't stop calls to this function from using the return value. The assumption is that you, the programmer, didn't lie about returning a value. Since you lied, and didn't return a value, you get what you deserve: undefined behavior.

It would be null, 0, or some other logical value.

Okay, how do you plan to represent some logical value without eliminating one valid value from the type? Say you're returning int, every value in the range of int is perfectly valid. So which of those values shall be made not valid such that it can be used as the logical "null" value in the case where a programmer fails to return something? Further, why should the compiler cater to broken code at the expense of valid code?

I need to learn to read between the lines.

Maybe you're the kind of person who's better off just reading the standard rather than tutorials. At least then you'll have the painfully precise language lawyerese.

dotancohen commented: Thank you for your patience explaining. +2
Narue 5,707 Bad Cop Team Colleague

Note that in this case there is in fact a "useful value" returned, which is 5.

Just how is 5 useful? Sure, it's the correct type, but the value itself is completely unpredictable garbage.

Bug? Error in the text?

Error in reading the text. You apparently read it as "I can define a function that claims to return a value, not return a value, and magic happens". The actual meaning is that functions are not required to return a value. This is as opposed to languages that differentiate between procedures (which don't return a value in all cases) and functions (which do return a value in all cases).

Note that the first chapter is intentionally simplified, so many of the nuances will be left out. In practice, functions with a return type of void cannot return a value (you can use an empty return statement or fall of the end) while functions with any other return type must return a value or risk undefined behavior.

Salem commented: rep++ +17
Narue 5,707 Bad Cop Team Colleague

I just read in K&R that a function, even if not defined as void, need not return a value.

What page? From memory, the only place where K&R makes such a grievous mistake is in the beginning tutorial with the initial definitions of main. However, that's a pedagogical decision that they clearly explain a bit later.

Why is the function returning 5?

The usual explanation is that it's undefined behavior and anything could happen. But to speculate about what's happening in your case, 5 is coming from whatever was in already in the location where return values are stored. As an illustrative example, let's consider a hypothetical compiler that uses a hidden pointer parameter for return values. If you return a value it might look like this after compiler translation:

void power(int *__$retv, int x, int y)
{
    int i, total = 1;

    for (i = 0; i < y; i++)
        total *= x;

    *__$retv = total;
}

void main(int *__$retv)
{
    int __$power$retv;
    int __$printf$retv;

    power(&__$power$retv, 2, 5);
    printf(&__&printf&retv, "%d\n", __$power$retv);

    *__$retv = 0;
}

Without a return statement to translate, the power function would look more like this:

void power(int *__$retv, int x, int y)
{
    int i, total = 1;

    for (i = 0; i < y; i++)
        total *= x;
}

Notice how *__$retv is never set, and the hidden temporary in main is a local variable without any default initialization. This means that if you don't provide a return value in this …

Narue 5,707 Bad Cop Team Colleague

But any other number as long as I'll always get a number higher than the highest ASCII code will work just as good, no?

Sure. A robust implementation would store all parts of the scan code for detailed querying. Adding 256 to the latter half is something of a cop-out, but for the most part it works well for the purpose of detecting extended keys.