• 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

Lathentar

Looking for Pants
Would this be correct for a loop? Or will it not read the last line?
Code:
while (houseList.hasNextLine())
		{
			a = houseList.next();
			p = houseList.nextDouble();
			ar = houseList.nextDouble();
			r = houseList.nextInt();
			HouseList.add(a, p, ar, r);
		}

That should work.
 

pompidu

Member
That should work.
OK cool, now when i add those variables to the arryalist, it wont add to the list and gives me an error
Code:
while (houseList.hasNextLine())
		{
			a = houseList.next();
			p = houseList.nextDouble();
			ar = houseList.nextDouble();
			r = houseList.nextInt();
			HouseList.add(a, p, ar, r);
		}
but if i change it to this, i dont get errors but idk if this is correct
Code:
while (houseList.hasNextLine())
		{
			a = houseList.next();
			p = houseList.nextDouble();
			ar = houseList.nextDouble();
			r = houseList.nextInt();
			HouseList.add(new House(a, p, ar, r));
		}
 

Aeris130

Member
It's correct. You're creating a new House object (sending a, p, ar, r as indata to its constructor) then sending the result (i.e the House object) to the lists .add() method. It's a more compact version of this:

Code:
House myNewHouse = new House(a, p, ar, r);
HouseList.add(myNewHouse);

You should rename the Scanner to something else, since it's a scanner and not a list. The Arraylist 'HouseList' shouldn't begin with a capital letter, since that's a class-naming standard (especially since you have an actual class named HouseList). Lowercase for variables.

Add this to the House class:
Code:
@Override
public String toString() {
        return "Address: " + address + ", city: " + city + ", Rooms: " + numBedrooms + "\n";
}

Then call
Code:
System.out.println(yourArrayListWithHousesHere);
Inside the HouseList to see what's in it.
 
The issue is just a bit of confusion about where things are happening.

In your main method you have:

Code:
public static void main(String[] args)
{
	//ArrayList to store the data
	ArrayList<HouseList> availableHouses = new ArrayList<HouseList>();
	//Creating instance of houses.txt
	System.out.println(availableHouses);	
}

There are 2 issues here:
1) The HouseList class already has an ArrayList instance that it uses to store the list of houses, what you want to do is create an instance of a HouseList which will itself create the ArrayList to hold details of each House.
2) I'm fairly sure that println will not correctly print the contents of an ArrayList, although I could be wrong.

The way you would create a HouseList instance would be with:

Code:
String fileName = "name-of-file.txt";
HouseList houseList = new HouseList(fileName);

This will invoke the constructor of the HouseList class, which contains the code for reading in from the specified file.

You should add a method to the HouseList class that will print the contents of the ArrayList so that once the HouseList instance has been created you can call that print method from the main method. An example of what that might look like is:

Code:
public void printHouses()
{
    for (House house : houseList)
    {
        System.out.println(house);
    }
}

The reason you can just pass the house instance to println is that the House class implements a toString method. Println will call that to get the String representation of a House object. Once you have added the printHouses method to the HouseList class you should call it from your main method with a:

Code:
    houseList.printHouses();

Does that help at all?
 
There are 2 issues here:
1) The HouseList class already has an ArrayList instance that it uses to store the list of houses, what you want to do is create an instance of a HouseList which will itself create the ArrayList to hold details of each House.
2) I'm fairly sure that println will not correctly print the contents of an ArrayList, although I could be wrong.

It will print it, it just wont be pretty.
 
Your main function doesn't do anything. It makes a new, empty array list and then prints it.

Yeah it looks like you are never actually passing your test houses.txt file.

In your main class add:
Code:
availableHouses(houses.txt);

And change:
Code:
houseScan = new Scanner(new File("houses.txt"));
to:
Code:
houseScan = new Scanner(new File(filename));
 

upandaway

Member
What are the big differences between a Scanner and the other connection streams (mostly the buffer streams)?

From the API I'm pretty sure I could do all of those methods with a buffer stream.. am I wrong?
 

pompidu

Member
The issue is just a bit of confusion about where things are happening.

In your main method you have:

Code:
public static void main(String[] args)
{
	//ArrayList to store the data
	ArrayList<HouseList> availableHouses = new ArrayList<HouseList>();
	//Creating instance of houses.txt
	System.out.println(availableHouses);	
}

There are 2 issues here:
1) The HouseList class already has an ArrayList instance that it uses to store the list of houses, what you want to do is create an instance of a HouseList which will itself create the ArrayList to hold details of each House.
2) I'm fairly sure that println will not correctly print the contents of an ArrayList, although I could be wrong.

The way you would create a HouseList instance would be with:

Code:
String fileName = "name-of-file.txt";
HouseList houseList = new HouseList(fileName);

This will invoke the constructor of the HouseList class, which contains the code for reading in from the specified file.

You should add a method to the HouseList class that will print the contents of the ArrayList so that once the HouseList instance has been created you can call that print method from the main method. An example of what that might look like is:

Code:
public void printHouses()
{
    for (House house : houseList)
    {
        System.out.println(house);
    }
}

The reason you can just pass the house instance to println is that the House class implements a toString method. Println will call that to get the String representation of a House object. Once you have added the printHouses method to the HouseList class you should call it from your main method with a:

Code:
    houseList.printHouses();

Does that help at all?

YES! That worked! Which is what I originally thought I had to do but his crc card was saying to make an arraylist in the tester, which might have just been an error he overlooked, which he makes in every lab. His doesnt thoroughly check over his examples and never works so I cant use his examples that much. The project is now like 25% done thanks to you guys, much APPRECIATED!!
 

GSR

Member
Anyone here familiar with modern OpenGL? My graphics course is moving onto programming in modern OpenGL after we did our first project in legacy OpenGL and I'm wondering if there's any major pitfalls to avoid or useful tricks.
 
My favourite learning project is still Pacman. You should be reading in a level from a file, have multiple threads for each ghost/player, lots of collections and a little bit of graphics stuff too.
Thanks for the suggestion. I'll keep it in mind once I finish head first since I'm only in the middle of chapter 9 about constructors. I think I'll using codingbat, make simple projects, and the exercises in headfirst as priority. Pacman sounds like a difficult project out of my league at the moment.
 

Spoo

Member
Anyone here familiar with modern OpenGL? My graphics course is moving onto programming in modern OpenGL after we did our first project in legacy OpenGL and I'm wondering if there's any major pitfalls to avoid or useful tricks.

The most important thing is getting comfy with shaders. Modern OpenGL (Modern graphics programming) means programmable pipeline, which basically means you get to write the programs which work on the server side. Pixel (also called Fragment), vertex and possibly geometry shaders are in your future. This is more challenging than it might seem, but is the secret the unlocking really awesome things. Consume as much information as you can about them, and try to get a jump start by writing a shader class or something that goes through the process of loading in shader programs, compiling them, linking them, and then finally applying them.
 

Slavik81

Member
My favourite learning project is still Pacman. You should be reading in a level from a file, have multiple threads for each ghost/player, lots of collections and a little bit of graphics stuff too.
I don't see why you'd need multithreading for Pacman. The original game was almost certainly single-threaded.
 
Dumb question but for some reason my searches aren't working...

Can I do this (C)? I haven't used enum in a while.

Code:
enum {<status 1>, <status2>} *State

and malloc the size at runtime when it's determined by the user? I want to make it an array (obviously).

Edit: I can!
 
Anyone have any ideas on good ways to automatically build deployment packages for a complex C# project in Visual Studio (potentially with TFS)?

I currently have a complex solution (22 projects) containing several ASP.NET websites, Setup Projects, and .exe applications. Ideally I'd like a singular solution that would handle doing publishes and builds of all of the actual "executable" projects to a singular location that I could zip up and use as an install source.

I'm not looking to create any executable, just a .zip file that would be formatted like the following:

/website1/<website 1 publish results>
/website2/<website 2 publish results>
/formsapp1/<forms app build output folder>
/msi-files/<all msi files from setup projects>
/docs/<etc.>

TFS has automated builds on check-in, but the entire process seems horrifically complicated to set up, and requires extensive customization to build the Setup Projects and deploy the websites.

The only thing I can think of is adding another project to the solution, that would be comprised entirely of a post-deployment step that would handle copying all the files to a central location (or a console app that would do the same thing).

Overall it seems like that's a terrible idea and that there's something out there that should be able to handle this for me. Any thoughts?
 

usea

Member
Zimbu: I've always done that by creating a target in the msbuild script (.csproj file) which did all the copying etc. Often times this included copying published sites to a network share using robocopy. Copying and zipping files the way you described wouldn't be too difficult. You can get pretty fancy with it, but it does require some time to build out and upkeep.

We use Bamboo as a build server, and it was really painless to have it execute a certain target for a build. When we want to deploy a certain website to our QA instance, we pull changes into the QA repo, bamboo picks up the changes and executes the deploy target on the project, and in ~2 minutes we have a new version on the QA site.

If there's a better solution, I don't know about it. But that wouldn't be a shocker since I haven't really looked around. Let me know if you find anything.
 
Anyone know what's the best FREE C compiler for Windows?

I learned Assembly, C, and C++ ages ago in school, but didn't keep up with practicing programming since then. Anyway, I've got the itch to jump back in and take a shot at programming some simple games.
 
Anyone know what's the best FREE C compiler for Windows?

I learned Assembly, C, and C++ ages ago in school, but didn't keep up with practicing programming since then. Anyway, I've got the itch to jump back in and take a shot at programming some simple games.

GNU. For straight C I would not learn on Windows. Linux or embedded work is much more fun to me when working with C. Now, Visual Studio Express comes with probably the best C++ compiler on Windows, but it doesn't support C99.

If you want to learn a much more fun language, I highly recommend you look into Python. To me it's just a really fun language, and you can pretty much do whatever you can imagine.
 
Anyone know what's the best FREE C compiler for Windows?

I learned Assembly, C, and C++ ages ago in school, but didn't keep up with practicing programming since then. Anyway, I've got the itch to jump back in and take a shot at programming some simple games.

There's a fair bit of groundwork to do in C/C++ if you just want to get a simple graphical game up and running on windows. You might be better off looking at C# or python as above.

If you insist though (understandable, I did) I'd recommend grabbing either the latest version of Microsoft Visual C++ Express, or code::blocks.
 
If I wanna learn some basics about unix, what is a good website/book to start? I'm not even sure where to begin, someone recommended me cygwin.

Since it is a gaming website, and I assume you are on windows, why not try downloading virtualbox and installing Ubuntu? This means you can play around without worrying about dual booting. You'll also be more comfortable in windows for a while running your favourite apps or looking for help.

Then try to get Steam up and running and linked to your existing steam folder. (should be plenty of help around if you search for each problem as you hit it, which is always a great way to learn).

It would be a good practical exercise and should introduce you to concepts like moving around the file system, mounting drives, getting drivers to work and updating and so on.

Once you have that up and running, there is a lot you can do from there in terms of programming.
 

Slavik81

Member
If I wanna learn some basics about unix, what is a good website/book to start? I'm not even sure where to begin, someone recommended me cygwin.
I would start by dual-booting with a Linux distro. The best way to learn something is to use it.

Personally, I would not recommend trying to learn through cygwin. It will always feel foreign and weird doing things the Unix way on Windows.
 

squidyj

Member
Since it is a gaming website, and I assume you are on windows, why not try downloading virtualbox and installing Ubuntu? This means you can play around without worrying about dual booting. You'll also be more comfortable in windows for a while running your favourite apps or looking for help.

Then try to get Steam up and running and linked to your existing steam folder. (should be plenty of help around if you search for each problem as you hit it, which is always a great way to learn).

It would be a good practical exercise and should introduce you to concepts like moving around the file system, mounting drives, getting drivers to work and updating and so on.

Once you have that up and running, there is a lot you can do from there in terms of programming.

I've always used VMware, do you prefer virtualbox?
 
(AS3/Flex4) Dynamically created tab has an List that has a DataProvider.

How I can manipulate that List? I need to use something like TabNavigator.getElementAt(currentTabIndex) or List[currentTabIndex] or?
 

Slavik81

Member
Or easier than a dual boot is a VM.
Depends on if you want 3D graphics support. VMWare Workstation just started supporting OpenGL 2.1 on Linux as of August 2012... six years after it released.

Aside from modern 3d graphics, I've never had any troubles running my Linux VM under Windows 7. VMWare has worked nearly flawlessly for me. The worst issues I've ever encountered were ridiculously minor things, like the clipboard syncing occasionally failing.

Speaking of graphics, though, I'm pretty sure you're not going to get TF2 running under VirtualBox.
 

hateradio

The Most Dangerous Yes Man
Well, he/she said the basics of Unix (and by extension Linux). I don't think 3D stuff should be a problem if it's just for that.
 

sanath123

Banned
C++ is better than C. As C++ is object Oriented Programming language. Also C++ provides a Graphic Functionality As C programming language does not provide this. Also in C++ code re-usability function is used.
So C++ Programming language is better than C.
 

Nemesis_

Member
C++ is better than C. As C++ is object Oriented Programming language. Also C++ provides a Graphic Functionality As C programming language does not provide this. Also in C++ code re-usability function is used.
So C++ Programming language is better than C.

I am surprised GAF lets spammers through. >_>
 
I've been stuck for a while now on this problem in this "C++ Without Fear." It gives a prompt, then I'm supposed to enter a file location and name, but I'm not sure how to put the file location/name.

This is the sample code from the book's CD for the problem.

Code:
#include <iostream>
#include <fstream>
using namespace std;

int main() {
    char filename[81];

    cout << "Enter a file name and press ENTER: ";
    cin.getline(filename, 80);
    ofstream file_out(filename);
    if (! file_out) {
        cout << "File " << filename << " could not be opened.";
        return -1;
    }
    cout << "File " << filename << " was opened.";
    file_out << "I am Blaxxon," << endl;
    file_out << "the cosmic computer." << endl;
    file_out << "Fear me.";
    file_out.close();
    return 0;
}

The book says to enter it like "C:\\output.txt" but every time I do it returns an error, saying the file could not be created. I try it like "C:\\Users\\Main\\Desktop\\output.txt" but that doesn't work either. The weird thing is that the first couple times I did it, it worked fine and created the document even though I'm sure I entered it exactly the same way.

If it matters, I'm using CodeBlocks 10.05, on a Windows 7 64bit.
 
C++ is better than C. As C++ is object Oriented Programming language. Also C++ provides a Graphic Functionality As C programming language does not provide this. Also in C++ code re-usability function is used.
So C++ Programming language is better than C.

lol... , what the heck? Is this seriously a spammer?
 

Tamanon

Banned
I've been stuck for a while now on this problem in this "C++ Without Fear." It gives a prompt, then I'm supposed to enter a file location and name, but I'm not sure how to put the file location/name.

This is the sample code from the book's CD for the problem.

Code:
#include <iostream>
#include <fstream>
using namespace std;

int main() {
    char filename[81];

    cout << "Enter a file name and press ENTER: ";
    cin.getline(filename, 80);
    ofstream file_out(filename);
    if (! file_out) {
        cout << "File " << filename << " could not be opened.";
        return -1;
    }
    cout << "File " << filename << " was opened.";
    file_out << "I am Blaxxon," << endl;
    file_out << "the cosmic computer." << endl;
    file_out << "Fear me.";
    file_out.close();
    return 0;
}

The book says to enter it like "C:\\output.txt" but every time I do it returns an error, saying the file could not be created. I try it like "C:\\Users\\Main\\Desktop\\output.txt" but that doesn't work either. The weird thing is that the first couple times I did it, it worked fine and created the document even though I'm sure I entered it exactly the same way.

If it matters, I'm using CodeBlocks 10.05, on a Windows 7 64bit.

Shouldn't it be C:\output.txt? Not two slashes.
 

Onemic

Member
If I wanted to learn programming for the purpose of making games, should I learn C++ or C#? Also what resources/tools are there to help me learn such a thing? Is there anything like Code Academy for such languages?
 

Cheeto

Member
C++ is better than C. As C++ is object Oriented Programming language. Also C++ provides a Graphic Functionality As C programming language does not provide this. Also in C++ code re-usability function is used.
So C++ Programming language is better than C.

I learned so much from this post! Where do I sign up!
 
If I wanted to learn programming for the purpose of making games, should I learn C++ or C#? Also what resources/tools are there to help me learn such a thing? Is there anything like Code Academy for such languages?

It depends, you can code games in both languages. AFAIK C# has easier tools to work with (XNA) to make games.
 

usea

Member
Oh well, one less reason to learn C#.
C# is one of the best languages around, and is being used more and more in game development. From MonoGame to Unity, to other engines in C#, there are more reasons all the time. It's among the best languages for game development, all things considered.
 
If I wanted to learn programming for the purpose of making games, should I learn C++ or C#? Also what resources/tools are there to help me learn such a thing? Is there anything like Code Academy for such languages?

Making games? Neither. Grab an engine and you will be mostly scripting. In this context understanding OO is the key.

Making engines or learning? C++ is always good but learning c# will be better for a job later.

They are based on the same parent language so it doesn't matter all that much.

I've been stuck for a while now on this problem in this "C++ Without Fear." It gives a prompt, then I'm supposed to enter a file location and name, but I'm not sure how to put the file location/name.

This is the sample code from the book's CD for the problem.

Code:
#include <iostream>
#include <fstream>
using namespace std;

int main() {
    char filename[81];

    cout << "Enter a file name and press ENTER: ";
    cin.getline(filename, 80);
    ofstream file_out(filename);
    if (! file_out) {
        cout << "File " << filename << " could not be opened.";
        return -1;
    }
    cout << "File " << filename << " was opened.";
    file_out << "I am Blaxxon," << endl;
    file_out << "the cosmic computer." << endl;
    file_out << "Fear me.";
    file_out.close();
    return 0;
}

The book says to enter it like "C:eek:utput.txt" but every time I do it returns an error, saying the file could not be created. I try it like "C:UsersMainDesktopoutput.txt" but that doesn't work either. The weird thing is that the first couple times I did it, it worked fine and created the document even though I'm sure I entered it exactly the same way.

If it matters, I'm using CodeBlocks 10.05, on a Windows 7 64bit.

Permissions. You can't overwrite in c: typically in windows 7+

Try c:\temp
 
Making games? Neither. Grab an engine and you will be mostly scripting. In this context understanding OO is the key.

Making engines or learning? C++ is always good but learning c# will be better for a job later.

They are based on the same parent language so it doesn't matter all that much.



Permissions. You can't overwrite in c: typically in windows 7+

Try c:\temp
Or %APPDATA%-folder if all else fails
 

Onemic

Member
Making games? Neither. Grab an engine and you will be mostly scripting. In this context understanding OO is the key.

Making engines or learning? C++ is always good but learning c# will be better for a job later.

They are based on the same parent language so it doesn't matter all that much.



Permissions. You can't overwrite in c: typically in windows 7+

Try c:\temp

What's OO?
 
Top Bottom