deceptikon 1,790 Code Sniper Team Colleague Featured Poster

And would you please tell me how to use the return statements for returning Matrices

Stop using arrays and switch to vectors (or a matrix class), that's how you do it:

vector<vector<int>> func();

There are some things that array types simply aren't suited for, and this is one of them.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

The trick is that arrays in C are passed by simulated reference. The syntax you're using obscures it a bit, but these three declarations are functionally identical:

void getmat(int a[50][50],int n);
void getmat(int a[][50],int n);
void getmat(int (*a)[50],int n);

So an array is actually passed as a pointer to the first element, which is how you simulate pass by reference in C. And that's why when you scanf into a[i][j], it actually modifies the original array passed in from main.

The weirdness of arrays in C makes a lot more sense when you have a good handle on pointers.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

What will be c++ equivalent of this snippet?

There would be no differences (barring the obvious syntax errors, but that would be broken in both languages). The FILE structure is a standard type inherited from C.

Also, is this struct correct for c++?

Yes.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

When the condition is checked. For a while loop the condition is checked before the body of the loop runs. For a do while loop, the condition is checked after the body of the loop runs.

In practice, this essentially means that the body of a do while loop will always run at least once, regardless of the condition.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

Strings don't work like that in C, they're really just arrays under the hood, so stuff like the == operator won't work like you expect. There are standard functions that support strings in C, and you can find them in the string.h header. Specifically for your needs, the function is strcmp.

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

void r_file(char*[]);

int main(int argc, char *argv[])
{
    if (argc != 3) {
        printf("no file name given\n");
    }

    if (strcmp(argv[2], "r") == 0) {
        r_file(argv);
    }

    return 0;
}

void r_file(char *fname[])
{
    FILE *fp;
    char ch;

    if ((fp = fopen(fname[0], "r")) == NULL) {
        printf("can't oprn file <%s>\n", fname[1]);
        fclose(fp);
        exit(1);
    }
    else {
        while ((ch = fgetc(fp)) != EOF) {
            putchar(ch);
        }

        fclose(fp);
    }
}
deceptikon 1,790 Code Sniper Team Colleague Featured Poster

I hate that new menu strip across the top which many programs are duplicating today. Very confusing.

Ribbons are universally hated by every IT department I've worked with (not an insignificant number). It's pandering to the dummy market and throwing power users under the bus.

I actually like the new Paint though. It's a nice happy medium between the bitmap editor it used to be and a fully featured package like GIMP.

What really bothers me is the current Windows Calculator. I hate switching between Programmer and Scientific mode because the former doesn't allow floating point and the latter doesn't allow alternate bases yet I use both on the regular.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

What I can do in one mouse click in XP takes up to 30 seconds to accomplish in 8.

Examples?

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

there is any method to overwriting data which is shorter or longer

No. If you're not overwriting data of exactly the same length, you have little choice but to create a temporary file.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

please tell me why we use mysql_query?

So that we can query a MySQL database? :P Your question is obvious and nonsensical, which probably means that's not really what you meant to ask. Can you be more specific?

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

A slightly simpler explanation is that the arrow operator is syntactic sugar for accessing a member of a pointer to structure or class: p->member is equivalent to (*p).member. Since the latter is awkward to type and it's very common to have a pointer to a structure or class, the arrow operator was introduced as a syntactic convenience.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

I know that having the same person appear in the same table more than once is troublesome.

Basically you've described the difference between a normalized and denormalized database. A normalized database stores the actual data in a single table and uses referential links (such as primary keys) to share records. A denormalized database duplicates the data as necessary.

There are pros and cons to each design. Normalized databases are cleaner and easier to reason about, but also more complex and slower due to the linking. Denormalized databases are usually designed that way because query performance is critical. For example, Daniweb's database is partially denormalized.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

3D seems to work best with high frame rate media. Otherwise it's just blurry and distracting. How 3D is utilized in the video is also critical. A lot of the time it's just an after thought gimmic rather than fully integrated into production from the start.

All in all, I'm not a fan, but there have been a handful of decent vids with 3D. Never seen one of the 3D TVs though.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

Yes, it's called a cross compiler. The compiler itself is targeted to Linux, but builds executables that target Windows.

Since cross compiling isn't my bag, my first stop in researching would be Google.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

Are you looking for an existing tool or suggestions on how to go about writing your own?

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

than also not work

You did read the part where I said "does not work" is not helpful, right? If my suggestion doesn't work then the problem is elsewhere, and likely due to you failing to provide sufficient information about your problem.

Please read this and try again.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

Keep in mind we are dealing with C here and not C++.

Just because it's in the C forum doesn't mean the OP isn't using some subset of C++ or compiling as C++. The error strongly suggests that string isn't being parsed as an object. Given that C++ supports a string type with the name string, it's not unreasonable to question which language we're working with here.

I wonder if the OP's compiler is assuming a one-parameter prototype, like extern int strcpy(char *x) as opposed to extern int strcpy(), which knows nothing about its indefinite number of parameters.

That's reaching a bit. ;) Any compiler that assumes the former rather than the latter is broken and non-conforming. I know for a fact that Visual C++ is conforming in this case.

Besides, with a mismatch between the call and definition given an implicit declaration, the actual error would happen at link time rather than the compile time syntax error the OP is seeing.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

but im not sure how i would fix that one as of now.

Move all declarations to the top of the current scope. For example with your main.c:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "player.h"
#include "other.h"

int main(int argc, char const *argv[])
{
    dug * one = malloc(sizeof(dug) * 1);
    player * us = malloc(sizeof(player) * 1);
    FILE *sy;

    us->cheat = malloc(sizeof(int)* 1);
    *us->cheat = 0;

    sy = fopen("story.txt", "a");

    us->name = malloc(sizeof(char)* 40);
    us->HP = malloc(sizeof(int)* 1);
    us->dir = malloc(sizeof(int)* 1);
    us->help = malloc(sizeof(int)* 1);

    one->torch = malloc(sizeof(int)* 1);
    *one->torch = 1;

    *us->HP = 10;
    *us->dir = 3;//1 north, 2 east,3 south,4 west
    *us->help = 0;

    one->WIN = malloc(sizeof(int)* 1);
    *one->WIN = 0;

    printf(" What is your name:");
    scanf("%s", us->name);
    fprintf(sy, "The journals of %s\n", us->name);

    RoomOne(us, one, sy);

    if (*one->WIN == 0) {
        printf("Game Over,%s\n", us->name);
        fprintf(sy, "\tLast entry, %s\n", us->name);
        if (*us->cheat == 1) {
            printf("winners never cheat, and cheater's never win.\n");
        }
    }
    if (*one->WIN == 1) {
        printf("You have escaped the dark, Now face the light.\n");
        fprintf(sy, "you have won the game, %s.\n", us->name);
    }

    fclose(sy);
    return 0;
}

Easy peasy. Prior to C99, C doesn't allow declarations to be mixed with executable code; declarations always come first.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

Were you perchance compiling as C99 or C++ on Linux? Because Visual Studio doesn't yet support C99's mixing of declarations and executable code unless you compile as C++.

All of your other main.c errors stem from the first error. Once you move all declarations to the top of the scope, that error will go away. You didn't show the commands.c file, so I can't comment on it.

Also note that warning C4996 is bullshit and can be ignored in all cases. Feel free to disable it entirely from the project configuration.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

g++ doesn't complain about the OP's code, even if I add #include <string> and using std::string.

But you didn't compile the code as it is, because it's only a snippet; you had to add scaffolding for it to compile. I bet you're assuming that the OP's example is something along the lines of this (assuming C++):

#include <string>

using namespace std;

int main()
{
    char string[6];

    strcpy(string, "HELLO\0");
}

Which compiles just fine in Visual Studio (boo for intrinsics), and probably would in g++ as well. However, it could very easily be this:

#include <string>

using namespace std;

char string[6];

int main()
{
    strcpy(string, "HELLO\0");
}

Which pukes with an ambiguity error. That's why more code is needed to diagnose this issue. When using variable names that could potentially be macro or type names, the exact usage is very important.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

Just because '\0' is added automatically doesn't mean you can't refer to it explicitly. The code is perfectly legal C, but I suspect the OP is compiling it as C++ where string is very likely to cause issues due to potentially being exposed as a type name.

The code as it stands is fine. A more complete example would help in diagnosing this error.

Try \x00 or leave the \0 off.

'\0' and '\x00' are synonymous.

So it is trying to copy 7 characters into 6 spaces, 5 for HELLO and 2 null characters. Avoid using the null character in the string literal.

strcpy stops copying at the first null character, so the problem you've described does not exist.

sepp2k commented: Nicely figured out. +6
Nutster commented: Quite right. I realized that when I reread my response, thinking, "Wait it will stop when it gets to the first null." Also that does not explain the compiler error.. +6
deceptikon 1,790 Code Sniper Team Colleague Featured Poster

I find it extremely unlikely that a teacher would give you an assignment without covering any of the material needed to complete it. The problem is more likely on your end in not going to class or not paying attention. In that case, you get what you deserve and the failing grade should be a good lesson in itself.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

For me theres really only one language that can truely be classed as an omnipotent language for all types of program. Machine code. Its slow to learn but once you have a good set of documented building blocks the rest is easy to adapt to fit whatever use you have and the good part is it works over all OS systems and machines.

...

why on earth we cant have a language written that simply uses english instead of needing a degree in Geek just to add 2 numbers together is beyond me.

They tried that as early as the 60s and those languages typically fail miserably. COBOL is the only one I can think of that was successful, and it uses a standardized "business" language rather than true natural language. The problem is that natural language is too verbose and difficult to reach expressive precision, which makes it extremely hard to parse. There's a reason that programming languages are based on mathematical theory: it's precise and consise.

Natural language parsing is getting to the point where a natural programming language might not be totally impractical, but I'm not holding my breath. ;)

Ive already started chewing a language together written in machine code that uses english to code in. GASP SHOCK HORROR ENGLISH !!!!

I'd be interested in seeing it.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

I have to subit my assignment tommorow :(

If it's your assignment, why are you asking for other people to do it? Please read our rules concerning homework.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

When you said "did not work", please be specific. I know what happened, but that's only because I can deduce it due to experience. Not everyone may be able to immediately call from memory that opening a file in write mode will truncate the file to 0 bytes. You can open the file in read-write (aka. update) mode to get the correct behavior:

ofstream outfile("test.txt", ios::in | ios::out);

Note that this method only way that works is if you perfectly overwrite what's already there. If the overwriting data is shorter or longer by even a single character, it "doesn't work". ;)

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

Flood control isn't presently enabled for voting, to the best of my knowledge. Dani may have implemented it in a non-obvious way without me noticing though. ;)

In light of the recent complaints, I've stepped up the priority of a moderator tool I wanted to write that lets us investigate (and possibly in the future, reverse) vote records without having to directly query the database. Pending Dani's approval, that should at least make it easier to determine if it's potentially a matter of harrassment.

LastMitch commented: Good Job, Autobot! =) +0
~s.o.s~ commented: What kind of a crazy sig is that? ;) +0
deceptikon 1,790 Code Sniper Team Colleague Featured Poster

You can preview the message at any time by clicking on the Spellcheck/Preview button on the editor. We also give you sufficient time to edit your post after submitting it.

Should you want a post deleted, you're welcome to report it with the Flag Bad Post button. This will place it in the queue for moderation and a moderator can delete it. However, keep in mind that if there are any replies to one of your threads that you want deleted or deleting one of your posts might break thread continuity, we won't do it.

Reporting a post after the edit window is also acceptable if you want reasonable changes made.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

There's no return statement, yet the method is supposed to return bool...

ChrisHunter commented: Bob-on +7
deceptikon 1,790 Code Sniper Team Colleague Featured Poster

There were always 8 bits in a byte.

That's not universally true. To the best of my knowledge there aren't any platforms where a byte is less than 8 bits, but there certainly are platforms where it's more.

Why create an encoding standard that is stored with multiple 0's when say this could be done with 16 bits or 8 bits..

Storage cost is only a part of the problem. When you have complex encoding methods, it takes time to process them. UTF-32 is the ideal when you can afford the space because it's a lot faster than encoding and decoding UTF-16 or UTF-8 compression. UTF-16 and UTF-8 were designed to reduce storage cost, and the price of that is reduced performance.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

<< doesn't represent a sequence point, so the example is undefined.

nitin1 commented: :p +1 thanks once again +3
deceptikon 1,790 Code Sniper Team Colleague Featured Poster

Because UTF-8 didn't exist in the beginning? It was proposed years later.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

Step 2. Initialize pos=0.
Step 3. Repeat step 4 till pos<=length.

When starting at 0, that implies a half open range. Step 3 is wrong in that it says to repeat until pos is less than or equal to length. In other words, the loop will never run because pos will never be less than length.

Instead, step 3 should read "Repeat step 4 until pos = length". This stops the loop when pos is equal to length so that the array isn't indexed out of bounds.

Secondly I need help on how can I move the found value at the first element of an array

So you want to move the located value to the first element? What happens to the other elements? Typically there are two approaches to a move-first algorithm:

  1. Swap the current first with the found value.

    for (i = 0; i < n; i++) {
        if (is_match(a[i])) {
            break;
        }
    }
    
    if (i != 0) {
        swap(a[0], a[i]);
    }
    
  2. Shift all values to fill in the vacated hole, then copy the value to the first position.

    for (i = 0; i < n; i++) {
        if (is_match(a[i])) {
            break;
        }
    }
    
    if (i != 0) {
        save = a[i];
    
        while (i > 0) {
            a[i] = a[i - 1];
            --i;
        }
    
        a[0] = save;
    }
    
deceptikon 1,790 Code Sniper Team Colleague Featured Poster

Think of programming like the martial arts. You can probably learn the basics quickly, but it's not very useful at that level. To reach and maintain a level where it is useful, you have to put in a lot of effort. And anyone who tells you it's easy is trying to sell you something. ;)

<M/> commented: Yup, that's accuarate +8
deceptikon 1,790 Code Sniper Team Colleague Featured Poster

what is the definition of 'correct use' here.

Use that doesn't encourage any of the pitfalls. Global variables are very case specific, and in our day and age of multithreading they're becoming less and less usable. "Correct" use is really a matter of knowing the potential risks and writing code to limit them as much as possible.

what type of mistakes can happen!

If everyone can see and modify a variable, everyone can break it. If code that relies on a global variable doesn't treat it as potentially volatile, that's a risk of data corruption right there.

Another risk stemming from the same source is if you do encounter a bug involving the global variable, you have to treat the entire source base as suspect in causing the bug. When your code is a bunch of small black boxes communicating through well defined interfaces, bugs are much easier to trace.

how can i be sure that i am not doing it correctly??

That's where guidelines and best practice come in. If there's any doubt, follow the guidelines and you'll limit risk naturally. The guideline for global variables is: "avoid global variables". ;)

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

Because C wasn't designed to protect stupid people from themselves, it was designed to give smart people a lot of power and flexibility. A potentially dangerous feature can still be used to great effect when used correctly. The unfortunate truth though is that such features are often not used correctly. ;)

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

In general, boolean logic precedence rules place NOT first, then AND, then OR. So in your example, it would go like this:

(NOT (X OR Y)) AND Z

Or broken down using temporaries:

A = X OR Y
B = NOT A
C = B AND Z
Q = C
deceptikon 1,790 Code Sniper Team Colleague Featured Poster

It seems like you don't even have a book or thorough tutorial to work with, which will drastically hinder your progress. I'd suggest buying a book and working through it, as most people won't be inclined to teach you when it seems like you aren't interested in putting any effort into it.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

You'll need to lock the handle before being able to access the data:

h = GetClipboardData(CF_TEXT) ;
strcpy( str, (char *)GlobalLock(h) ) ;
GlobalUnlock(h) ;
deceptikon 1,790 Code Sniper Team Colleague Featured Poster

Would make str str?

I don't understand your question, but if you're asking whether you want to use the strstr function then the answer is yes. strstr will return a pointer to the matched string, or NULL if it wasn't found. So you can repeatedly call strstr and then skip over the match:

char *p = line;
char *match;
int n = 0;

while ((match = strstr(p, key)) != NULL) {
    /* Update the match counter */
    ++n;

    /* Skip over the match so it isn't found again */
    p = match + key_len;
}
deceptikon 1,790 Code Sniper Team Colleague Featured Poster

A fixed width format doesn't change how you read and write files. Extract a whole line, then parse the array to get the data you want. A fixed width format actually makes that easier because now you don't have to parse the data, you have indexes and can simply check that they exist:

char doc_number[DOCNUM_START - DOCNUM_END + 1];
char line[LINE_WIDTH];

if (fgets(line, sizeof line, in) != NULL) {
    /* Parse field data from the line */
    if (strlen(line) < LINE_WIDTH || line[LINE_WIDTH - 1] != '\n') {
        /* Handle a bogus line; error or skip parsing */
    }

    /* Extract a document number: [DOCNUM_START,DOCNUM_END) */  
    doc_number[0] = '\0';
    strncat(doc_number, &line[DOCNUM_START], DOCNUM_END - DOCNUM_START);
}

Some formats allow blank lines, categories where the format changes, or comments, so it's all very case-specific. But it all comes down to a few basic techniques of which the above is the most important.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

hint : use %

That's a dead end hint that actually makes the solution more difficult. Instead, consider dividing the semester number by 3 and then taking the ceiling of the result. This will produce the correct year:

#include <math.h>
#include <stdio.h>

int main(void)
{
    int i;

    for (i = 1; i < 10; i++)
    {
        printf("%d -> %d\n", i, (int)ceil(i / 3.0));
    }

    return 0;
}

Basic arithmetic, baby. ;)

Alternatively, since you're looking at a tiny set of numbers, a brute force solution may be faster and easier to understand:

#include <stdio.h>

int main(void)
{
    int i;

    for (i = 1; i < 10; i++)
    {
        int year = 3;

        if (i < 4)
        {
            year = 1;
        }
        else if (i < 7)
        {
            year = 2;
        }

        printf("%d -> %d\n", i, year);
    }

    return 0;
}

Note, of course, that range checking the semester number was excluded in these examples but should be included in your code.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

It's old school templating before C++ supported (or widely supported) templates. I'd question how old your book is if that's an example. It must be at least 20 years old. Regardless, not a book you should be learning modern C++ from unless that example is prefixed, suffixed, and bordered with "DO NOT DO THIS".

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

Unless 5+ is closer to 10+ with an outstanding portfolio, you may want to be more realistic. Game development is extremely competitive, and systems development is difficult to get into due to the lack of open positions.

Though I'd wager systems developers are better paid in general.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

So you want me to distill my 15+ years of experience into a picture or short video? Software development isn't that trivial, sorry. I'd suggest doing a Google search and starting your own path of experience.

Alternatively, you could ask a specific question instead of what amounts to "teach me how to be a software developer!".

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

Do you know anything about software development? The usual process is to come up with a list of features, then derive a check list of requirements from that, then tests (for test driven development), and then write code to meet the requirements/tests. Rinse and repeat until the death of the program.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

That looks like a fixed width format, so you can probably grab everything from x column to y column and call it good.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

C... is a fickle beast ya know.

Sounds more like your file format is inconsistent. Any language will be fickle when the incoming data is difficult to parse. Do you have a way of determining whether column 2 will contain embedded whitespace? That's the key question here.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

This is a common problem. The easiest solution is to place an input request just before the point at which your program terminates. For example:

#include <iostream>

int main()
{
    // Do something
    std::cout << "Hello, world!\n";

    // Pause for input
    std::cin.get();

    // Program terminates here
}

There are cases where that won't work, particularly if previous input from your program is still left in the input stream. You can use a magic incantation in that case and learn how it works later:

#include <ios>
#include <iostream>
#include <limits>

int main()
{
    // Do something
    std::cout << "Hello, world!\n";

    // Clear extranous input
    std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');

    // Pause for input
    std::cin.get();

    // Program terminates here
}

Of course, if there's no leftover input, you'll have to hit the [Enter] key twice because cin.ignore blocks for input too.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

There's not really a 'for Dummies' level book because the topic is very complex. The O'Reilly book is probably your best bet for an introduction, and if it's too difficult you're probably lacking in prerequisite education.

Perhaps if you point out some parts of the book that are troublesome, we can clarify things or point you toward a resource for further learning.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

The answer is 214. All you need to do is recognize that you're working in base 8 rather than base 10.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

Then I'd probably go with a description of how arrays work in the presence of object orientation (specifically polymorphism), and practical usage of arrays of objects.