deceptikon 1,790 Code Sniper Team Colleague Featured Poster

You can get details about what happened after fopen() failure like this:

FILE* tmpfile = fopen(xxxxxx, "r");

if (!tmpfile)
{
    perror("Error opening file");
}
deceptikon 1,790 Code Sniper Team Colleague Featured Poster

I didn't understand that question. Could you elaborate and possibly provide a simple example outside of the encryption algorithm of what you're confused about?

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

Uninitialized pointers are not pointers to infinite memory. You have to allocate memory before trying to write to it:

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

int main(void)
{
    struct Person
    {
        char *firstName;
        char *lastName;
    };

    struct Person newPerson;

    newPerson.firstName = malloc(50);
    newPerson.lastName = malloc(50);

    printf("Enter your first name and last name: \n");
    scanf("%49s %49s", newPerson.firstName, newPerson.lastName);
    printf("Hello %s %s! \n", newPerson.firstName, newPerson.lastName);
    
    free(newPerson.firstName);
    free(newPerson.lastName);

    return 0;
}

It's also best to check and see if malloc() returns a null pointer, but I won't complicate the example with that yet.

DeanMSands3 commented: I was about to post something on malloc, but decided the poster wouldn't get it. Rock on, deceptikon. +4
deceptikon 1,790 Code Sniper Team Colleague Featured Poster

You'd just filter out whitespace by assigning clear as-is instead of shifting the character:

for(i=0; i<=x-1; i++)
{
    if (isspace(clear[i]))
    {
        cipher[i] = clear[i];
    }
    else
    {
        cipher[i] = clear[i]+3;
    }
}

Be sure to include <cctype> or <ctype.h> for the declaration of isspace().

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

Yes, there are more corrections. How many more depends on the compiler you're using, because this code makes assumptions about the language that aren't supported by modern compilers.

But if you ignore those problems, there are four corrections required to make the code compilable. You have options in what corrections to apply, there may be multiple instances of the same error type, and one correction may fix multiple errors, but there are still four distinct errors that need to be fixed.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

Your printword() function is making the results look different from the paper. Try this instead:

void printword(WORD A) {
    printf("%08X ", A);
}
deceptikon 1,790 Code Sniper Team Colleague Featured Poster

damn awsome links. simply saying "superb info". and i am still not getting my second question....."second question is that : when i don't give "\n" with the statement(number 12) (scanf("%d\n",&t), then it skip the gets() statement in the while loop , but when i give "\n"
then it works properly..please tell me because they are really good questions for me to have answers."

Whitespace in scanf()'s format string will tell scanf() to read and discard any and all whitespace. '\n' is the escape character for a new line, which is whitespace.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

So is the problem solved, or did you want to preserve whitespace and only encrypt non-whitespace characters?

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

Structurally the program makes no sense. main() is embedded inside circle, and circle isn't properly terminated. name is a variable local to main() but it's being used as a member of circle.

This compiles, but I didn't run it:

#include <iostream>
#include <iomanip>
#include <cmath>

using namespace std;

struct circle
{
    string name;
    int radius;
    float circumference;
    float area;

    float compute_area()
    {
        area = (radius*radius* 3.14);
        return(area);
    }

    float compute_circumference()
    {
        circumference=(2*radius*3.14);
        return (circumference);
    }
};

int main()
{
    circle test;
    char reply;

    do
    {
        cin.ignore(80);
        cout << "Enter the name of the Circle          : ";
        getline(cin,test.name);
        cout << "Enter the given Radius of the Circle  : ";
        cin >> test.radius;
        cout << "\nThe Area of the Circle is  : ";
        cout<< test.compute_area() << test.compute_circumference();
        cout << "\n\n";
        cout << "Do you want to continue y/n : ";
        cin >> reply;
        if (toupper(reply) == 'N')
        {
            cout << "\n\n";
            cout << "\t\t Thank You For Using This Software !!!";
            break;
        }
    }
    while (toupper(reply!='Y'));
    
    cout << "\n\n";
}
deceptikon 1,790 Code Sniper Team Colleague Featured Poster

I can't reproduce your problem, it encrypts the whole sentence for me. What compiler and OS are you using?

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

I'm pretty much all lost/braindead on step 6.

It's a variation of the algorithm for checking if an array is sorted. If it's not sorted in ascending or descending order, the profits fluctuated, which is pretty easy to check:

#include <iostream>
#include <string>

using namespace std;

string ProfitTrend(int a[], int size)
{
    // Check for an increasing trend
    for (int i = 0; i < size - 1; i++)
    {
        if (a[i] > a[i + 1])
        {
            break;
        }
        else if (i + 1 == size - 1)
        {
            return "Steadily INCREASED";
        }
    }
    
    // Check for a decreasing trend
    for (int i = 0; i < size - 1; i++)
    {
        if (a[i] < a[i + 1])
        {
            break;
        }
        else if (i + 1 == size - 1)
        {
            return "Steadily DECREASED";
        }
    }
    
    return "FLUCTUATED";
}

int main()
{
    int a1[] = {1, 2, 3, 4, 5}; // Steadily increasing
    int a2[] = {5, 4, 3, 2, 1}; // Steadily decreasing
    int a3[] = {1, 2, 5, 4, 2}; // Fluctuating unpredictably
    
    cout << "a1 trend: " << ProfitTrend(a1, sizeof a1 / sizeof *a1) << endl;
    cout << "a2 trend: " << ProfitTrend(a2, sizeof a2 / sizeof *a2) << endl;
    cout << "a3 trend: " << ProfitTrend(a3, sizeof a3 / sizeof *a3) << endl;
}

That's quick and dirty code, by the way. If you spend some time on it you can make it shorter and faster. But the basic idea is to check for a …

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

I am stuck in how does cin.clear() and cin.ignore(200, '\n') work

cin.clear() resets the error state of the cin object. So if any of cin.eof(), cin.fail(), or cin.bad() return true, they'll return false after calling cin.clear(). The reason you need to call cin.clear() is because until the error state is reset, you won't be allowed to do any input; all requests will fail:

#include <iostream>
 
using namespace std;
 
int main()
{
    // Force cin into an error state
    cin.setstate(ios::failbit);
 
    char ch;
 
    // Try to read from cin
    cout << (cin.get(ch) ? "It worked!" : "It failed!") << endl;
 
    // Reset the error state with cin.clear()
    cin.clear();
 
    // Try to read from cin again (you'll need to type something)
    cout << (cin.get(ch) ? "It worked!" : "It failed!") << endl;
}

does it really ignore 200 characters?

cin.ignore() essentially does this:

void my_ignore(int n, char stop)
{
    char ch;
    
    for (int i = 0; i < n; i++)
    {
        if (!cin.get(ch) || ch == stop)
        {
            break;
        }
    }
}

So your call to cin.ignore() reads and discards *up to* 200 characters. If there are fewer than 200 characters in the stream or there's an occurrence of the stop character, it will read and discard fewer than 200. But in your call 200 is the upper limit, it won't read more than that.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster
#include<stdio.h>
main()
{
    int a=1;
    switch(a)
    {
        case isdigit('1'):printf("Hello");
    }
}

This code works fine Hello is printed

#include<stdio.h>
main()
{
    int a=1;
    switch(a)
    {
        case isalpha('a'):printf("Hello");
    }
}

But this code gives an error
It says case label does not reduce to an integer

It doesn't reduce to a *constant integer expression*. So no, that code shouldn't compile because it has a constraint violation.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

When you say you "can't use arrays", what exactly does that mean? Because it's quite impossible to sort anything when you can't retain it, and while you can use 25 variables to get the same effect as an array, that's totally insane. I can't imagine a teacher expecting you to do that unless they subscribe to the school of hard knocks and want to teach you the importance of arrays.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

So...what's the problem, exactly?

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

The error says it all: regular functions cannot be nested in C++. Move your definition of selectionSort() outside of main() and call it in case 7 instead.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

Not the moderators (at least most of us are savvy enough to understand that)

Agreed, but I still think I'll leave the final call to Dani. ;)

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

There's no difference at all. The array notation for a function parameter is just syntactic sugar. Internally it's treated as a pointer, and you can use it as a pointer, which does indeed include pointer arithmetic:

#include <stdio.h>

void traverse(int a[], int n)
{
    while (--n >= 0)
    {
        printf("%d", *a++);
    }
}

int main(void)
{
    int a[] = {1, 2, 3, 4, 5};
    
    traverse(a, sizeof a / sizeof *a);
    
    return 0;
}
deceptikon 1,790 Code Sniper Team Colleague Featured Poster

You got neg rep from me because you are telling a student uncomfortable with
1) programming -- he's NEW!!
2) English -- he's from Malaysia

I'm sorry, I didn't realize I was supposed to read his mind and determine that what he asked for wasn't what he really wanted. :icon_rolleyes:

He just want's to know how to ask a question again when an improper answer was entered, not to take over the screen and bounce all around it!

Read the first post again. It's not clear that's what he wanted to know, and what he *asked* for was how to move the input cursor to the previous line. Maybe you should take such things into account before whipping out your bad attitude.

inb4 more negative rep from WaltP.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

The code continually allocates new memory to ptr, which leaks away the previous memory and loses any previously assigned numbers. Rather than outline all of the possible cases and explain why they're necessary, I'll just give you a function to study:

bool ResizeArray(int*& a, unsigned old_size = 0, unsigned new_size = 0)
{
    bool rc = false;
    
    if (new_size != 0 && old_size != new_size)
    {
        if (!a)
        {
            // Allocate a new array of non-zero size
            //
            try {
                a = new int[new_size];
                rc = true;
            }
            catch (...)
            {
                // No cleanup, just converting an exception into a return code
            }
        }
        else
        {
            // The new size is different on an existing array; try to resize
            //
            unsigned n = new_size > old_size ? old_size : new_size;
            int* temp = nullptr;
            
            try
            {
                temp = new int[new_size]; // Might throw
                
                for (int i = 0; i < n; i++)
                {
                    temp[i] = a[i]; // Might throw
                }
                
                delete[] a;
                a = temp;
                rc = true;
            }
            catch (...)
            {
                if (temp)
                {
                    delete[] temp;
                }
            }
        }
    }
    
    return rc;
}

Don't copy it mindlessly, try to understand what's being done, in what order, and why. There are three steps:

  1. Allocate a temporary array.
  2. Copy existing elements into the temporary array.
  3. Replace the original array with the temporary array.

But the logic varies depending on things like if the original array hasn't yet been allocated any memory and is a null pointer, and the relationship between the old size and the new size.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

>> kingsonprisonic.

???

The guy who posted using goto, once again assuming you're replying to me. Could you be more clear with whom you're replying to and why, please?

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

The negative rep probably was for your use of the goto statement instead of the more standard loop. The code you posted does not even attempt to do what you just described.

If you're replying to me, I'm not kingsonprisonic. ;)

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

You need to run the query before trying to parse the results. :D

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

Where do you give $banners_result a value?

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

Since two people have deigned to give me negative rep, I'll clarify my previous post.

It's impossible to do what the OP asked without taking full control over the console with something like curses, or a manual solution using some variant of gotoxy() or clrscr(). If that kind of control is needed, it's probably wise to consider a GUI instead of a console program in this day and age since you're going to hurt portability regardless of how the goal is accomplished. Why? Because once you accomplish jumping back by one line, you'll probably discover other irritants of the console's lack of flexibility and need to hack in more "fixes".

Not to mention that overriding how the console works is confusing to users, while GUIs are much more intuitive when it comes to non-linear non-sequential I/O.

That's not to say a GUI should be used if a you can live with default console behavior. In that case, a loop and reprint is sufficient. But if that were sufficient, would the OP have asked how to jump back one line?

deceptikon 1,790 Code Sniper Team Colleague Featured Poster
deceptikon 1,790 Code Sniper Team Colleague Featured Poster

it's there
(xampp/htdocs/test/test001/avatars/)

That may be your problem then. $location is set to "avatars/$name", but your avatars folder is two levels down from htdocs, where the server is looking. Unless you're using something like mod_rewrite, $location should be "test/test001/avatars/$name". Or better yet:

$location = IMAGE_PATH . "avatars/$name";

Where IMAGE_PATH is a manifest constant that holds "test/test001/".

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

Beej's guide has been the recommended first stop for many years. I'd recommend purchasing the book for offline reading too, 'tis worth it even though the book is rather thin. :)

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

Maybe you could elaborate on 'this code doesn't execute'.

I think it's the next evolution of "it doesn't work". :D

And the problem is with a call to scanf():

scanf("%d",&x);

Which should be this:

scanf(" %c",&x);

Note the leading space in the format string. This forces scanf() to ignore leading whitespace before looking for a character. %c is one of the specifiers that doesn't do that normally, which means it would normally pick up the '\n' character left over from the previous call to scanf().

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

This seems relevant.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

Cant we have a sneak peek?

The site hasn't been skinned yet, so it doesn't look like much. We don't really want to show off the raw site as folks have a tendency to focus on how it looks and ignore how it works.

It'll be ready for show soon, though. :)

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

It's certainly a suggestion worth looking into.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

So, it does not, IMO, belong in this C++ discussion thread.

Your opinion doesn't define what's topical for this forum. If you don't know C++/CLI or don't agree with it, you're not required to participate in any threads about it.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

the title says it all

If by "says it all" you mean that it says one can ignore your thread due to lack of information, I'd agree. :D Since I know jack squat about CDF, I'll just try to answer what I think is your question.

What I know is that I need to find numbers greater than zero, then number greater than one and so... but when I looked at it, I figured out that I cannot have like 50 if condition in the program to check that.

You can use a counted loop where the counter is the exclusive lower bound of numbers you're looking for:

#include <algorithm>
#include <functional>
#include <iostream>
#include <iterator>
#include <random>
#include <vector>
#include <ctime>

using namespace std;

int main()
{
    // Get a list of random numbers with possible negatives
    //
    auto rgen = bind(
        uniform_int_distribution<int>(-99, 99),
        mt19937((unsigned)time(nullptr)));
    vector<int> v;
    
    generate_n(back_inserter(v), 10, rgen);
    
    // Find the numbers greater than any given value in the range
    //
    int ubound = *max_element(v.begin(), v.end());
    vector<vector<int>> distribution(ubound);
    
    for (int i = 0; i != ubound; i++)
    {
        for_each(v.begin(), v.end(), [&](int x)
            {
                if (x > i)
                {
                    distribution[i].push_back(x);
                }
            });
    }
    
    // Display the raw results
    //
    for_each(distribution.begin(), distribution.end(), [](vector<int> x)
        {
            if (!x.empty())
            {
                copy(x.begin(), x.end(), ostream_iterator<int>(cout, " "));
                cout << endl;
            }
        });
}
deceptikon 1,790 Code Sniper Team Colleague Featured Poster

This is C++? Not on any planet I am familiar with, and I have only been programming C++ for 20 years, professionally!

C++/CLI, it's C++ extended to meet CLI requirements and fit into the .NET framework as a managed language.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

Why do it this way, instead of how you showed it? Because it is clearer in what you are doing, and will not take any more processor time. It may, in some situations, be safer as well.

But it only works for 32-bit... Labdabeta's way doesn't depend on the size of an int, only that the size of int and char be different. I'm not convinced that your method is clearer at all, much less sufficiently clearer to justify limiting it to only 32-bit ints at a time where 64-bit is becoming popular.


p.s. Labdabeta's test is reversed. The bigendian variable is set to true for little endian, but that doesn't invalidate the method being used. ;)

StuXYZ commented: GettSize independent code is always better! -- because the alternative is a maintainers nightmare. +9
deceptikon 1,790 Code Sniper Team Colleague Featured Poster

Language lawyers can argue a few reasons why that would fail, but in practice it's all but guaranteed to work.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

It's also probably not a good idea to clear the screen after every iteration of the loop without some kind of pause involved. The program will run too quickly to see the changes and will look like it's just printing a single random number.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

Is there a point writing my own heap for time optimization?

It's pretty unlikely that an ad hoc heap would be faster than std::priority_queue. Not to mention the effort of reinventing the wheel *and* the huge risk of bugs in your implementation that just aren't there with an existing oft used library.

Ask yourself if the potential speed improvements are worth the effort and risks. In my experience, the choice is nearly always to use the existing library instead of writing your own.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

Where is your avatars folder relative to the root folder? If the file is there, it sounds like the problem is as simple as getting the path correct in your img tag.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

The simplest solution would be to make subsequent queries into your table for the make, excluding the id that you're already displaying, and order the results by price. Then you can insert those into your expanded rows display.

Alternatively, depending on the size of the table, cost of database queries, and expected frequency of clicks to expand the make, you could simply dump the whole table into objects and organize them in a more convenient manner for your display table.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

With the given information one can only assume a truly random shuffle of a single list of tracks with no other variables involved, so the probability is 1/1000 every time a new track is selected.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

Is a=b+c+d considered as one instruction or 2 instructions?

Neither. Barring compiler optimizations, there are three operations going: two additions and an assignment. As far as instructions go, a naive interpretation would be:

mov r1, b
mov r2, c
add
mov r2, d ; Assuming add puts the result in r1
add
mov a, r1

Similarly is a+=b one instruction or 2?

It depends on how the compiler generates machine code for that kind of compound operator. But when working out time complexity, you should be looking at operations in the source language, not the resulting instructions after the source language is compiled. In that case your two example statements are 3 operations and 1 operation, respectively.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

The problem is not with fgets() reading a newline character, it's with scanf() leaving a newline character in the stream for fgets() to read. To fix the problem, change how you're using scanf(), or use a different method of input.

There are two common recommendations. First, you can avoid scanf() altogether and always use fgets() for stream input. Then the string retrieved by fgets() can be parsed in memory without any worry of mucking up the stream.

#include <stdio.h>

int main(void)
{
    char line[BUFSIZ];
    int value;
    
    if (fgets(line, sizeof line, stdin) &&
        sscanf(line, "%d", &value) == 1)
    {
        printf("The integer you typed is %d\n", value);
    }
    
    return 0;
}

Second, you can try to clean up the stream after calling scanf() like zeroliken's code:

#include <stdio.h>

int main(void)
{
    int value;
    int rc = scanf("%d", &value);
    
    if (rc != EOF)
    {
        int ch;
        
        // Clean up the stream (assumes everything left is garbage
        while ((ch = getchar()) != '\n' && ch != EOF)
        {
            // All work done in the condition
        }
    }
    
    if (rc == 1)
    {
        // Display the value if it was successfully read
        printf("The integer you typed is %d\n", value);
    }
    
    return 0;
}
zeroliken commented: I wasn't familiar with the first one so thanks for posting +8
deceptikon 1,790 Code Sniper Team Colleague Featured Poster

You *might* be able to use ANSI escape sequences by loading ANSI.SYS. But it's a really bad solution to the problem when Turbo C already supports such library functions as gotoxy() and textcolor().

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

In the general case you're getting into multithreading territory. Otherwise, you can just check the time every now and again to see if 60 seconds have passed. Even in my sample program, such a check wouldn't be more than half of a second off because of the Sleep() call.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

If you need that kind of control over the user experience, I'd say write a GUI-based program instead of a console program.

WaltP commented: Terrible suggestion! GIU is not a solution. -4
kingsonprisonic commented: Its a bad solution. -1
deceptikon 1,790 Code Sniper Team Colleague Featured Poster
Console.WriteLine(
    TimeSpan.Parse("01:11:00") +
    TimeSpan.Parse("00:10:00") +
    TimeSpan.Parse("01:56:00"));

What's the problem? ;)

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

Your question is ambiguous. Please provide an example with input and the desired result.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

strcmp returns the difference of the two strings. If the first string is "less" than the second string it returns a value less than 0. If the first string is "greater" than the second string it returns a value greater than 0. And if the two strings are identical, it returns 0.

Here's an example implementation:

int my_strcmp(char const* a, char const* b)
{
    // Find the first two characters that don't match
    while (*a == *b && *a)
    {
        ++a;
        ++b;
    }

    // Compare the mismatched characters and return the absolute difference
    if (*a < *b)
    {
        return -1;
    }
    else if (*a > *b)
    {
        return +1;
    }
    else
    {
        return 0;
    }
}