• Hey, guest user. Hope you're enjoying NeoGAF! Have you considered registering for an account? Come join us and add your take to the daily discourse.

Programming |OT| C is better than C++! No, C++ is better than C

For some reason my program keeps rolling the die over and over without stopping and I'm not exactly sure why.

It doesn't, but the fact that you think it does and copy/pasted the throwCalc() function suggests a misunderstanding of what's going on.

Code:
getCalc1 = throwCalc1();

Runs throwCalc1() and puts the resulting integer into getCalc1. That's it. It doesn't put a reference to the function into getCalc1, it just puts a number.

So, you don't need two throwCalc functions, you just need one, and you need to call it in the right places.
 

Korosenai

Member
I need some help with a program, here is the function that i'm stuck on:

"void write_books(string book_file_name, vector<book> &my_books);

This function will write the values from the vector my_books to a file with the name book_file_name. The string book_file_name should be the same as your input file for books (books.txt). If you write the values from the vector my_books to the input file, you will ensure that any changes made to the vector while the program is running will be reflected in the next run of the program."


So basically, I read six lines (name, price, stock, publisher, author, and isbn) from a file for a book object that needs to be stored into a vector. Once I read these six lines out, I create a book pointer, point to the six setter functions for these elements, and then push them into a vector. And it needs to be able to do it for an nth amount of books. Here is an example of the code:

Code:
void read_books(string book_file_name, vector<book> &my_books)
{
    string name, price, stock, publisher, author, isbn;
    book *bookpt;
    
    ifstream fin;
	string bookFile = "books.txt";
	fin.open(bookFile);
    
    while(fin >> name >> price >> stock >> publisher >> author >> isbn)
    {
        bookpt = new book;
        
        bookpt -> setName(name);
        bookpt -> setPrice(price);
        bookpt -> setStock(stock);
        bookpt -> setPublisher(publisher);
        bookpt -> setAuthor(author);
        bookpt -> setIsbn(isbn);
        
        my_books.push_back(*bookpt);
    }
    fin.close();
}

The end result is my_books[0] containing this when outputted to the screen using a print_info function (which is a parent class function):

Code:
Code:
for(int i = 0; i < all_books.size(); i++)
{
	my_books[i].print_info();
}

Output: 
Product Name: Book
Product Price: Price
Number in Stock: Stock
Publisher: Publisher
Author: Author
ISBN: ISBN

Now, onto the original question. I can read elements from a file, store them into a vector, and output them onto the screen. The thing i'm having problems with is outputting them into a new final file. Here is what I have so far for the function:

Code:
void write_books(string book_file_name, vector<book> &my_books)
{
    ofstream fout;
    book_file_name = "final_books.txt";
    fout.open(book_file_name);
    
    for(int i = 0; i < my_books.size(); i++)
    {
        fout << my_books[i] << endl;
    }
    
    fout.close();
}

I'm having problems with this bit of code right here:

Code:
 fout << my_books[i] << endl;

It won't let me output it like this for some reason. I've tried using iterators as well, nothing has worked so far.
 

Water

Member
I'm having problems with this bit of code right here:

Code:
 fout << my_books[i] << endl;

It won't let me output it like this for some reason. I've tried using iterators as well, nothing has worked so far.
The "book" struct or class is not standard C++, so naturally there's no suitable << operator defined for sticking it into an output stream. If you want to be able to write that line of code, define the operator yourself:
http://msdn.microsoft.com/en-us/library/vstudio/1z2f6c2k.aspx

(In the future when you are asking for help, the problem may not be immediately obvious, and giving us the actual error message would go a lot farther than "It won't let me".)
 
The latest in my ongoing low-content posts about things I hate:

NuGet is awful. Half the time the packages are never downloaded. Half the time the DLLs are checked into source control (so when you build the DLL gets exclusively checked out and no one else can build).

All because apparently dragging a DLL into a folder and clicking "Add Reference" was way too complicated.
 

Korosenai

Member
The "book" struct or class is not standard C++, so naturally there's no suitable << operator defined for sticking it into an output stream. If you want to be able to write that line of code, define the operator yourself:
http://msdn.microsoft.com/en-us/library/vstudio/1z2f6c2k.aspx

(In the future when you are asking for help, the problem may not be immediately obvious, and giving us the actual error message would go a lot farther than "It won't let me".)

Oh dang, I overloaded those operators in an earlier assignment and forgot about it. It worked too, awesome. And yeah next time I will, sorry about that.
 

Slavik81

Member
The latest in my ongoing low-content posts about things I hate:

NuGet is awful. Half the time the packages are never downloaded. Half the time the DLLs are checked into source control (so when you build the DLL gets exclusively checked out and no one else can build).

All because apparently dragging a DLL into a folder and clicking "Add Reference" was way too complicated.
Odd. I've mostly heard good things about it from C# developers.

By the way, I was wondering how Windows C++ developers manage their tools and libraries. On Linux it's really easy. Just install whatever packages you need and have your project specify its required packages and environment.

The best solution I can come up with for Windows is to run rpm through cygwin. But that kinda sucks. Is there a native solution?
 

usea

Member
The latest in my ongoing low-content posts about things I hate:

NuGet is awful. Half the time the packages are never downloaded. Half the time the DLLs are checked into source control (so when you build the DLL gets exclusively checked out and no one else can build).

All because apparently dragging a DLL into a folder and clicking "Add Reference" was way too complicated.
NuGet is fantastic. This is a weird post. Check your internet connection. Downloading things just works. Also, if you're checking the dlls into your code repository, that has nothing to do with nuget. You're just doing that. And if the people building your code don't have the dependency, again that's nothing to do with nuget.

Package managers are ubiquitous. They're a tool that make lives easier. You're having unrelated problems and blaming the wrong thing.
 
I need help guys.

There are so many resources on the internet, I find it difficult to decide where I should start.

I want to learn how to program, mostly how to build websites (decent websites). I know about the languages (php, html, css, python, ...) but I don't know which ones I should start with.
 
I need help guys.

There are so many resources on the internet, I find it difficult to decide where I should start.

I want to learn how to program, mostly how to build websites (decent websites). I know about the languages (php, html, css, python, ...) but I don't know which ones I should start with.

All the cool kids are doing a full JS stack now days. I'd recommend starting with Joomla or Wordpress if you want to just build "websites" where the user doesn't interact that much besides browsing and reading. Otherwise if you want a more robust app try RoR, Cake or Django.
 
I need help guys.

There are so many resources on the internet, I find it difficult to decide where I should start.

I want to learn how to program, mostly how to build websites (decent websites). I know about the languages (php, html, css, python, ...) but I don't know which ones I should start with.

html/css/javascript. After that you should know enough to decide if you want to go ruby, python or php.
 

Water

Member
All the cool kids are doing a full JS stack now days.
I'm ignorant about web dev, but this clearly makes sense for a newbie as well as the cool kids. You absolutely must know Javascript for web dev, because anything that happens on browser side uses it. For a programming newbie it helps a lot to stick to one language and get good at it before messing with others. Ergo, it makes sense to use JS on the server too.
 

Kinitari

Black Canada Mafia
All the cool kids are doing a full JS stack now days. I'd recommend starting with Joomla or Wordpress if you want to just build "websites" where the user doesn't interact that much besides browsing and reading. Otherwise if you want a more robust app try RoR, Cake or Django.

I'd add (jumping on the full stack JavaScript train) that really focusing on understanding JavaScript will be a really great asset with any website you make. Learn the basics using any site, than jump into a good book - for starters, Eloquent JavaScript
 

Water

Member
I'd add (jumping on the full stack JavaScript train) that really focusing on understanding JavaScript will be a really great asset with any website you make. Learn the basics using any site, than jump into a good book - for starters, Eloquent JavaScript
Most programming tutorials out there are bad, and I'm willing to bet that Javascript tutorials are even worse on average. Someone new to programming should *start* from a good book. The first version of the book you recommended, Eloquent Javascript, is free. If you open the "interactive" version, it even has a small custom Javascript console on the bottom that you can use to test the examples.
 

Kinitari

Black Canada Mafia
Most programming tutorials out there are bad, and I'm willing to bet that Javascript tutorials are even worse on average. Someone new to programming should *start* from a good book. The first version of the book you recommended, Eloquent Javascript, is free. If you open the "interactive" version, it even has a small custom Javascript console on the bottom that you can use to test the examples.

I mostly agree, the reason I tend to encourage people to try online interactive lessons is because they can get you excited, whereas a book might not have that same exciting effect. A good book does teach you the 'right' way however, and sometimes making the mistakes you see in a lot of online tutorials (== vs === for example) can help you understand why it's important to do it the right way.

That being said, if you don't need a flashy interactive tutorial to keep you engaged, by all means, Eloquent Javascript is probably one of the best starters!
 
I'm ignorant about web dev, but this clearly makes sense for a newbie as well as the cool kids. You absolutely must know Javascript for web dev, because anything that happens on browser side uses it. For a programming newbie it helps a lot to stick to one language and get good at it before messing with others. Ergo, it makes sense to use JS on the server too.

Not really. JS is an awful language and is async madness is not really suited for a lot of tasks. The JS community obsession with building everything out of JS is stupid.

Sometimes nodejs makes sense on the server, most of the times it doesn't.
 
I'm ignorant about web dev, but this clearly makes sense for a newbie as well as the cool kids. You absolutely must know Javascript for web dev, because anything that happens on browser side uses it. For a programming newbie it helps a lot to stick to one language and get good at it before messing with others. Ergo, it makes sense to use JS on the server too.

Well...feelings on NodeJS are pretty mixed. While only having to focus on one language is certainly a plus, I don't know if I would recommend Node for a beginner. (And some wouldn't recommend it for anybody.)

Personally, if you don't already know a server side programming language I would recommend learning some Python and using Flask.
 
In C#, what's the point with try-finally? I've been experimenting with it but it's useless in my very basic tests.

The output of this:

Code:
        public static void TryFinally()
        {
            int random = new Random().Next(0, 3);
            try
            {
                if (random == 2)
                    Console.WriteLine("It's 2!");
                else
                {
                    throw new Exception("Not 2!");
                }
            }
            catch (Exception g)
            {
                Console.WriteLine(g.Message);
            }
            finally
            {
                Console.WriteLine("Finally!");
            }
            Console.ReadLine();
        }

is the exact same as the output of this:

Code:
        public static void TryFinally()
        {
            int random = new Random().Next(0, 3);
            try
            {
                if (random == 2)
                    Console.WriteLine("It's 2!");
                else
                {
                    throw new Exception("Not 2!");
                }
            }
            catch (Exception g)
            {
                Console.WriteLine(g.Message);
            }
            Console.WriteLine("Finally!");
            Console.ReadLine();
        }

If you write the keyword "try", Visual Studio forces you to enter either catch or finally, so I figured maybe finally finishes the try statement even if it crashed or maybe catched it as well as finished it. But no, it doesn't.

This code will crash when you reach the for-loop:

Code:
        public static void LetsTryFinallyAgain()
        {
            int x = 2;
            int[] y = new int[1];
            try
            {
                Console.WriteLine("Phase one");
                while (x == 1)
                {
                    Console.WriteLine("Phase two");
                    break;
                }
                if (x == 3)
                    Console.WriteLine("Phase three");
                for (int i = 0; i < 2; i++)
                {
                    y[i] = 5;
                }
                Console.WriteLine("Phase four");
            }
            finally
            {
                Console.WriteLine("Phase five");
            }
            Console.ReadLine();
        }

You could add a catch to prevent the crash, but then there's no point in using finally.

I've googled but I don't get it. I'm probably completely misunderstanding this, so does anyone here know how finally should be used? I'm rather new to programming, and I'm trying to get a hang of the keywords. MSDN says try-catch-finally are usually used together and that finally releases the resources, but why would you even need a finally to do that?
 

Zoe

Member
I use it the most when I'm making database calls. try for working with the database, catch in case that fails for some reason, and then finally to close out the connection so that it closes whether it was successful or not.
 

cyborg009

Banned
I've been trying to figure out this database stuff for hours now... My professor database example works with my code but my database doesn't.
 

jokkir

Member
Okay, dumb question >__>

How do I access protected data members from one class while in another class? I guess I have to make the class with the protected data members have a "friend class" or something?
 

usea

Member
Okay, dumb question >__>

How do I access protected data members from one class while in another class? I guess I have to make the class with the protected data members have a "friend class" or something?
What language are you talking about?

Generally protected means it cannot be shared except to descendants. If you're looking for a way to share protected data, usually you've done something wrong.

Why is it protected? If you want to share it then change it to public. If you can't do that, you don't want to share it. Some languages have the notion of friend classes or assemblies like you mentioned. Rarely is doing that a good idea. You could also make a different, public member that shares the protected data.
 

jokkir

Member
What language are you talking about?

Generally protected means it cannot be shared except to descendants. If you're looking for a way to share protected data, usually you've done something wrong.

C++

Friends can also use that data, no? I'm just trying to make it so I don't add additional functions if I don't need to but if I do, I suppose I can just put getters to return the inaccessible data.
 

Slavik81

Member
Okay, dumb question >__>

How do I access protected data members from one class while in another class? I guess I have to make the class with the protected data members have a "friend class" or something?
In C++ use of protected and friend should be rare. It's useful on occasion, but beginners will use them both wrong 9 times out of 10.

In most cases, it's more appropriate to use public members than friends or protected members.

C++

Friends can also use that data, no? I'm just trying to make it so I don't add additional functions if I don't need to but if I do, I suppose I can just put getters to return the inaccessible data.
Friends can access public, protected and private data. This makes it much harder to reason about the state of a class that has friends; a lot of code can muck with the internal state of the class.
 

hateradio

The Most Dangerous Yes Man
In C#, what's the point with try-finally? I've been experimenting with it but it's useless in my very basic tests.

The output of this [snip] is the exact same as the output of this
I don't think this is particular to C#.

Finally is used when you want to make sure that a particular set of code runs regardless of exception or not. However, it is not run when the failure causes the system to exit.

If you know that an exception will be thrown, you should catch it, otherwise the system does the intended thing and throws the exception, which may not reach the Finally block, to the end user.
 

Kansoku

Member
Anyone knows where I can learn to program for Android? Oh and SQLite too.
I know a bit of Java and I already tried developer.android.com but it's not being very helpful.
 

pompidu

Member
Anyone knows where I can learn to program for Android? Oh and SQLite too.
I know a bit of Java and I already tried developer.android.com but it's not being very helpful.

For sql, buy a used database textbook. For android just download a bunch of source code/tutorials and learn it that way.
 

cyborg009

Banned
Can anyone help me out I'm trying to write to my database but its not writing.


Writing to database
Code:
#include "student.h"
#include "ui_student.h"
#include <QMessageBox>
#include "studentbl.h"

Student::Student(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::Student)
{
    ui->setupUi(this);

}

Student::~Student()
{
    delete ui;
}

/**
 * @brief Student::on_addNewStudentPushButton_clicked
 */
void Student::on_addNewStudentPushButton_clicked()
{
    StudentBL myStudent(ui->firstNameLineEdit->text(),
                      ui->lastNameLineEdit->text(),
                      ui->usernameLineEdit->text(),
                      ui->passwordLineEdit->text());

   [B] ui->statusLabel->setText(myStudent.add());[/B]

}

It seems to be connecting to the database.

edit: added that single line of code and it worked....strange..

There will also be two Android courses at http://coursera.org/ in the Spring. I'm not sure which one will be better, but one is geared at beginners.

Funny I took android class this semester.
 

msv

Member
In C#, what's the point with try-finally? I've been experimenting with it but it's useless in my very basic tests.
You're not testing it without debugging. Test it on a release, and you will see that the code after the finally will not be run after an exception that is not caught, whereas the finally block will be (not in ALL cases though).

Try this without debugging:

Code:
try
{
  throw new StackOverflowException();
}
  catch(InvalidOperationException ex)
{
  Console.WriteLine("Caught");
}
finally
{
  Console.WriteLine("Reached Finally");
}

Console.WriteLine("Reached after trycatch");
 
I use it the most when I'm making database calls. try for working with the database, catch in case that fails for some reason, and then finally to close out the connection so that it closes whether it was successful or not.

I don't think this is particular to C#.

Finally is used when you want to make sure that a particular set of code runs regardless of exception or not. However, it is not run when the failure causes the system to exit.

If you know that an exception will be thrown, you should catch it, otherwise the system does the intended thing and throws the exception, which may not reach the Finally block, to the end user.

You're not testing it without debugging. Test it on a release, and you will see that the code after the finally will not be run after an exception that is not caught, whereas the finally block will be (not in ALL cases though).

Try this without debugging:

I didn't mean that it was particular to C#. I just think it's a good idea to always specify what language you're talking about since keywords and exceptions can work differently in different languages.

In any case, I haven't started to learn databases yet but I will very soon, so I'll definitely try to learn the connection!

@hateradio; In my two examples, "Finally" would be printed no matter the case, so I don't quite get it.

@msy; Not sure what I'm supposed to see. Visual Studio will shut down the program no matter if I run it normally or debug. Nothing gets printed or such, finally or otherwise.
 

msv

Member
@msy; Not sure what I'm supposed to see. Visual Studio will shut down the program no matter if I run it normally or debug. Nothing gets printed or such, finally or otherwise.
Debug > Start without debugging.

You'll see that the finally block (and the exception) gets printed to the console and what comes after doesn't.
 

usea

Member
@msy; Not sure what I'm supposed to see. Visual Studio will shut down the program no matter if I run it normally or debug. Nothing gets printed or such, finally or otherwise.
The point of try/finally is that the finally block will be executed even if an exception occurs in the try block. Without using try/finally, the exception would cause the stack to unwind (ie: no more code in that function will be executed. essentially it aborts).

Here's a practical example:
Code:
var file = new FileInfo(@"C:\temp\whatever.txt");
var text = "some text";
var bytes = Encoding.ASCII.GetBytes(text);
FileStream fstream = null;
try
{
    fstream = file.OpenWrite();
    fstream.Write(bytes, 0, bytes.Length);
}
finally
{
    if(fstream != null)
    {
        fstream.Close();
    }
}
If an exception occurs inside of the try block, the finally block will be executed, closing the filestream if it's not null. Without the try/finally, if either of the lines in the try block threw an exception, the filestream would never be closed.

It's pretty simple. It allows you to execute code in a function after an exception has happened. Without it, the code will not be executed. Normally when there's an exception, the function is aborted, and the function that called it is aborted, and so on until either Main is hit and the program closes, or some catch block is hit. It "bubbles up."

Here's a simpler example:
Code:
public void Alpha()
{
    Console.WriteLine("one");
    throw new DivideByZeroException();
    Console.WriteLine("two");
}

public void Beta()
{
    try
    {
        Console.WriteLine("one");
        throw new DivideByZeroException();
    }
    finally
    {
        Console.WriteLine("two");
    }
}
Alpha prints "one", while Beta() prints both "one" and "two."

The best way to gain illumination on this subject is to write code like you did with your tests, and then step through it one line at a time with the debugger.
 
I have been kneedeep with JS/CSS lately and I still can't get over how shitty every single browser is. Well maybe not Firefox, but sometimes Firefox too. Whether it's IE compatibility issues or Chrome's ultra crappy font rendering that does not have an easy solution, there's always something that needs an exception of exception of exception. I am fairly sure that the correct way to approach building a browser is nuke the internet off the ground and start from zero with actual idea how things should universally work.

/rant
 

Ya no

Member
Hey GAF,

I'm learning java and I need a little help understanding overriding methods vs overloading methods.

fsMQEYx.png


I typed out the above code and found that the one with override prints out 10.0 10.0 and the overloading one prints out 20.0 20.0. The thing is I'm not sure why.

For the override one, does it print out 10.0 because the method in class A extends B replaces the method in class B? Is that what is happening? I feel like it's not very difficult, but I'm confused and I haven't found any helpful stuff from googling...

EDIT: Forgot to compile all my files after I changed p(double i) to p(int i) which led to the wrong results being printed out causing most of my confusion.
 
Regarding try/finally:

In C#, particularly legacy C#, I've found that a lot of developers used try/finally around databases, streams, and other resources that need to be released. In those instances, there's a far better construct for that: the using block, which is intended to dispose of resources that implement IDisposable (resources such as database connections, streams, etc.).

It is not often that I've had to use try/finally in normal coding.
 

Randdalf

Member
I learnt Python several weeks ago, and now I don't think I would rather use anything else (unfortunately impractical). It's so good.
 
I learnt Python several weeks ago, and now I don't think I would rather use anything else (unfortunately impractical). It's so good.

Having done for parallel programming in C this entire semester, I really miss being able to work with python and I'll be glad when I can use my free time to some python projects.
 
I am a junior in college pursing a CS degree and I am looking for some more places to apply for an internship in the summer. I have gotten turned down by a few places after first round interviews and I have an interview with Amazon later this week.

Anybody know some other cool places to try and get an internship?
 

jokkir

Member
How can I get certain values from a specific node in a linked list?

I'm trying to make it so that a main() creates a linked list object consisting of nodes that hold the variables like firstName, lastName, etc. The linked list and the notes are in a separate classes than the main(). What I'm trying to do here is get certain values from each node one by one (via the link list object) so it can display in a cout << list.firstName << list.lastName << etc.. in some sort of loop

Here's a sample of my code (I'm trying to omit stuff so some code might look off).

Main.cpp
Code:
int main(){
LinkedList list;

cout << list.firstName << endl; //I know this is wrong

return 0;
}

LinkedList.h
Code:
#include "Node.h"

class LinkedList{

public:
	LinkedList();
	void printList();
	void insert(Student&);
	void extract(unsigned int);

private:
	Node* head;
	int size;
};

Node.h
Code:
#ifndef NODE_H_
#define NODE_H_

#include <iostream>
#include <string>

#include "Student.h"

class Node{
	friend std::ostream& operator<<(std::ostream& os, const Node&);
	friend class LinkedList;
public:
	Node(std::string name = "none");

	Node(Student);

private:
	double mark;
	unsigned int age;
	unsigned int id;
	unsigned int courseCount;
	string firstName;
	string lastName;
	double average;

	std::string name; //Name of Node
	Node* next; //Points to next thing in the linked list.
};
 

hateradio

The Most Dangerous Yes Man
Hey GAF,

I'm learning java and I need a little help understanding overriding methods vs overloading methods.

http://i.imgur.com/fsMQEYx.png?1

I typed out the above code and found that the one with override prints out 10.0 10.0 and the overloading one prints out 20.0 20.0. The thing is I'm not sure why.

For the override one, does it print out 10.0 because the method in class A extends B replaces the method in class B? Is that what is happening? I feel like it's not very difficult, but I'm confused and I haven't found any helpful stuff from googling...

EDIT: Forgot to compile all my files after I changed p(double i) to p(int i) which led to the wrong results being printed out causing most of my confusion.

Code:
public void p(double i)

has the exact same signature (it has the same return and amount of parameters) as the one in super class, so it overrides the other one.

Code:
public void p(int i)

This one is different from the other because it takes integers, not doubles. Therefore it overloads p because p now has different implementations: one for doubles, one for integers.

I learnt Python several weeks ago, and now I don't think I would rather use anything else (unfortunately impractical). It's so good.
Since I learned Ruby first, it's always been more to my liking.

However, Scala's the language that has recently been really winning me over. Too bad that it's tied to the JVM. It's amazing. (&#12389;&#65377;&#10084;&#8255; &#10084;&#65377;)&#12389;

It's like everything I like Ruby, but with types and a lot more functional oriented.
 
How can I get certain values from a specific node in a linked list?

I'm trying to make it so that a main() creates a linked list object consisting of nodes that hold the variables like firstName, lastName, etc. The linked list and the notes are in a separate classes than the main(). What I'm trying to do here is get certain values from each node one by one (via the link list object) so it can display in a cout << list.firstName << list.lastName << etc.. in some sort of loop


I recommend adding some sort of "printList()" method for your linked list class. Something like this should do the trick...

Code:
void printList() {
    if(this->head == NULL) {
        std::cout << "Empty list" << std::endl;
        return;
    }
    Node *it = this->head;
    while(it->next != NULL) {
        std::cout << it->value << " -> "; // Replace this with whatever you want to print about this node
        it = it->next;
    }
    std::cout << it->value << std::endl; // To get the last node

    return;
}
 

jokkir

Member
I recommend adding some sort of "printList()" method for your linked list class. Something like this should do the trick...

Code:
void printList() {
    if(this->head == NULL) {
        std::cout << "Empty list" << std::endl;
        return;
    }
    Node *it = this->head;
    while(it->next != NULL) {
        std::cout << it->value << " -> "; // Replace this with whatever you want to print about this node
        it = it->next;
    }
    std::cout << it->value << std::endl; // To get the last node

    return;
}

Is there a way to make it so that it prints a specific node at a time? Like let's say I want to print out node 3 and not the rest.
 

PSGames

Junior Member
If any SQL gurus could point me in the right direction on this would be a huge help. I'm learning SQL and want to create a report that would generate the Account #s under the same Person ID that were created <= 30 days of each other and exclude any others. All the information needed is in the same table.

For example this code pulls up a list of Person's who have more than one Account and their Creation date:

Code:
	select accounttid, creationdate, personid from Table1 where personid in ( 
	select (personid) from Table1 group by personid having COUNT (accountid) > 1)
	
	accountid	creationdate	personid
	5501624	2013-05-01	101
	5501544	2013-05-03	101
	5510220	2013-10-24	10337
	5504204	2013-06-27	10337
	5502332	2013-05-21	1047
	5502628	2013-05-28	1047
	5508844	2013-10-01	1047

Not sure where to go from here. I want to then take these Account numbers and somehow compare the Creationdates for less than or equal to a 30 day differential but only compare them when the Person IDs are the same.
 
Is there a way to make it so that it prints a specific node at a time? Like let's say I want to print out node 3 and not the rest.

Do you already have access to that node? If not, then you need to implement a "find" function that traverses the list looking for the node. Linked lists have linear access time, and require that you traverse it to find and get information about a node. Once you have the node, you can just print whatever you need from it. The find function is pretty similar to that printList() function, you just need to put a check that compares the current node to what you're looking for on each iteration.
 

Zoe

Member
Regarding try/finally:

In C#, particularly legacy C#, I've found that a lot of developers used try/finally around databases, streams, and other resources that need to be released. In those instances, there's a far better construct for that: the using block, which is intended to dispose of resources that implement IDisposable (resources such as database connections, streams, etc.).

It is not often that I've had to use try/finally in normal coding.

I started using it in addition to usings to prove to a third party vendor that it wasn't my code causing their Oracle databases to not let release their connections. Now it's just habit.
 

jokkir

Member
Do you already have access to that node? If not, then you need to implement a "find" function that traverses the list looking for the node. Linked lists have linear access time, and require that you traverse it to find and get information about a node. Once you have the node, you can just print whatever you need from it. The find function is pretty similar to that printList() function, you just need to put a check that compares the current node to what you're looking for on each iteration.

Thanks for the help!

I did it in a very crude way, I think. I just created a common variable between the nodes called index and when I wanted to get a specific node, I just traversed through the list and stops when it reaches the index I wanted.

I'm sure there's a better way of doing this though but this will do for now.
 
Top Bottom