Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Post
  • Reply
Kilometers Davis
Jul 9, 2007

They begin again

To give you a basic idea on where i'm at, I'm just now getting into programming with no real experience. I'm starting off with C and Python, probably C for the most part though.

I picked up the C Programming Language 2nd Edition today thinking it was a good beginner tutorial but apparently it assumes you have basic programming knowledge. Is there a good primer on the internet to get myself up to speed to the point where I can use this book?

Also, what do I need on my PC (Windows Vista) to start programming? I've searched around for compilers but I don't really know what to look for.

Adbot
ADBOT LOVES YOU

hexadecimal
Nov 23, 2008

by Fragmaster

IntoTheNihil posted:

To give you a basic idea on where i'm at, I'm just now getting into programming with no real experience. I'm starting off with C and Python, probably C for the most part though.

I picked up the C Programming Language 2nd Edition today thinking it was a good beginner tutorial but apparently it assumes you have basic programming knowledge. Is there a good primer on the internet to get myself up to speed to the point where I can use this book?

Also, what do I need on my PC (Windows Vista) to start programming? I've searched around for compilers but I don't really know what to look for.

Dev C++ is probably the easiest and smallest IDE/Compiler package you can get, and is relatively user friendly when trying to compile small C/C++ programs. It is somewhat buggy though, just make sure you save a lot.

Avenging Dentist
Oct 1, 2005

oh my god is that a circular saw that does not go in my mouth aaaaagh
Dev-C++ is possibly the worst IDE I've ever used. Microsoft Visual C++ is free, and the IDE is amazing.

Nippashish
Nov 2, 2005

Let me see you dance!
Seconding Dev-C++ being a terrible IDE. CodeBlocks is nice and light and much less terrible than Dev-C++.

Janitor Prime
Jan 22, 2004

PC LOAD LETTER

What da fuck does that mean

Fun Shoe

Nippashish posted:

Seconding Dev-C++ being a terrible IDE. CodeBlocks is nice and light and much less terrible than Dev-C++.

CodeBlocks also has the advantage of being cross platform if that's important to you. DevC++ is a pile of poo poo, and I can't believe I had to use that for my C++ class. :barf:

Kilometers Davis
Jul 9, 2007

They begin again

The MVC++ install was insane. I think it took 2 hours total haha. It's seems great though even if I have no idea what i'm doing.

edit: How do you get the compiled .exe files to stay on the screen for more than 1/8th of a second or can you.

Kilometers Davis fucked around with this message at 00:45 on Dec 21, 2008

slovach
Oct 6, 2005
Lennie Fuckin' Briscoe

IntoTheNihil posted:

The MVC++ install was insane. I think it took 2 hours total haha. It's seems great though even if I have no idea what i'm doing.

edit: How do you get the compiled .exe files to stay on the screen for more than 1/8th of a second or can you.

Run it from the command line, or do something like this when you want to wait for input:

code:
std::cin.sync();
std::cin.ignore();
Also, I'm pretty sure it will keep the window open for you though if you run it from the IDE.

Nippashish
Nov 2, 2005

Let me see you dance!

IntoTheNihil posted:

The MVC++ install was insane. I think it took 2 hours total haha. It's seems great though even if I have no idea what i'm doing.

edit: How do you get the compiled .exe files to stay on the screen for more than 1/8th of a second or can you.

Put system("PAUSE"); or char c; cin >> c; at the end of main(). Or run the programs from cmd.exe.

Ari
Jun 18, 2002

Ask me about who Jewish girls should not marry!

IntoTheNihil posted:

The MVC++ install was insane. I think it took 2 hours total haha. It's seems great though even if I have no idea what i'm doing.

edit: How do you get the compiled .exe files to stay on the screen for more than 1/8th of a second or can you.

In Visual Studio, ctrl-f9 compiles, runs, and waits for user input at the end before terminating, whereas f9 disappears. It's in the Run menu.

niggaruto
Oct 25, 2007

Is there a more clever way for cross-platform conditionals than using
code:
#ifdef WIN32
...
#endif
Specifically regarding headers for classes as part of a static lib in which the implementation is compiled conditionally per-platform. I'd like to avoid having to include the header for the proper platform (ie: "thing_win32.h" vs. "thing_linux.h") any place I want to use the static lib. And I'd like to avoid having a giant single-file header that covers all possible platforms by using #ifdef.

Bitruder
Nov 29, 2003

Majoring in connect the dots and colouring
Create one thing.h that you include where you need it, and in that, have conditionals that include the platform specific header files.

niggaruto
Oct 25, 2007

Bitruder posted:

Create one thing.h that you include where you need it, and in that, have conditionals that include the platform specific header files.

Thanks :) I actually wound up doing just that, actually. It's not perfect, but definitely better than the alternatives!

Cosmopolitan
Apr 20, 2007

Rard sele this wai -->
I decided to give a go at the UVa problem sets. I'm doing Problem Set 100 right now, and I've hit a brick wall.

I can get it working if I just do one string at a time, but it's a requirement to be able to put in multiple strings at a time. I've noticed that a lot of their problems do this, but I have had trouble with defining multiple variables with a single line before. Some googling came up with using stringstream, and it seems to work with just one line of input.

However, after trying to get this working with several lines at a time, I think stringstream may be what's causing my current build to crash after the second line of input. I don't know if it's just not meant to reassign things or what.

Can someone take a look at my code and tell me if they spot something that's causing the crash? (I don't want anyone to write the code for me, since that would just defeat the purpose, but a hint would be extremely helpful.)

code:
// uvaps_100.cpp : main project file.

#include <stdafx.h>
#include <conio.h>
#include <iostream>
#include <string>
#include <sstream>

using namespace std;

...

int main()
{
	int i[] = {0}, j[] = {0};
	int n[] = {0};
	string instr[] = {""};
	string const sentinel = "end";
	bool valid;


	do
	{
		system("cls");
		
		cout << "Input integers:\n\n"

		for (int counter = 0; ; counter++)
		{
			cout << "Iteration: " << counter << "\n\n";

			do
			{
				//cin.ignore(100, '\n');
				getline(cin, instr[counter]);

				if (instr[counter] == sentinel)
				{
					break;
				}

				stringstream(instr[counter]) >> j[counter] >> i[counter];

				valid = errorCheck(i[counter], j[counter]);

			}while(!valid);
			
			if (instr[counter] == sentinel)
			{
				break;
			}

			else
			{
				n[counter] = nCalc(i[counter], j[counter]);
				cout << "\n";
			}
			
		}
		
		system("cls");

		cout << "Size of instr: " << sizeof(instr);
		pause();

		for (int counter = 0; counter < sizeof(instr); counter++)
		{
			cout << instr[counter] << " " << n[counter] << "\n";
		}

		valid = loop();

	}while(valid);

	pause();

    return 0;
}
Also, I don't think the conditional statement I have for the output's for loop at the end will work correctly, but I haven't even gotten there to debug yet. Any other general tips are appreciated as well.

Cosmopolitan fucked around with this message at 12:49 on Dec 23, 2008

Vanadium
Jan 8, 2005

This is not how arrays work, you are making those into single-element arrays and then you are accessing nonexistant elements.

You want to use std::vectors and push_back into them or something.

Cosmopolitan
Apr 20, 2007

Rard sele this wai -->

Vanadium posted:

This is not how arrays work, you are making those into single-element arrays and then you are accessing nonexistant elements.

You want to use std::vectors and push_back into them or something.

Oh, I thought not specifying a size, coupled with initializing it with a 0, would automatically fill the rest of any elements with a default of another 0. Now that I think about that, that doesn't make sense lol. Arrays are not dynamic. I'll look into vectors.

hexadecimal
Nov 23, 2008

by Fragmaster
dont' do system("cls") for uva problems.

Also you want to be careful about your main loop and how you consume input.

I had a lot of problem on this problem because of my first submission did not consume input correctly, and i think outputed an extra answer line. But here is a way that got AC.

code:
#include <iostream>
#include <vector>
#include <map>
#include <cmath>
#include <list>

using namespace std;

int compute_wayz( int n )
{
    int ret = 1;
    while( n > 1 )
    {
           n = n&1?n*3+1:n/2;
           ++ret;
    }
    return ret;
    
}

int main()
{
          int i,j;
          while( cin>>i>>j )
          {               
                 int a = min( i, j );
                 int b = max( i, j );
                 int max_a = -1;
                 for( int i1 = a; i1 <= b; ++i1 )
                 {
                      max_a = max( max_a, compute_wayz( i1 ) );
                 }
                 cout<<i<<" "<<j<<" "<<max_a<<"\n";
          }
          return 0;
}
also most of the time you want to abstain from C++ vector and just use static arrays. Make them as big as the boundary cases, and you'll be good.

hexadecimal fucked around with this message at 00:20 on Dec 24, 2008

Avenging Dentist
Oct 1, 2005

oh my god is that a circular saw that does not go in my mouth aaaaagh

hexadecimal posted:

also most of the time you want to abstain from C++ vector and just use static arrays. Make them as big as the boundary cases, and you'll be good.

Hm yase, let's make memory use shoot through the roof because we hate the STL.

floWenoL
Oct 23, 2002

hexadecimal posted:

also most of the time you want to abstain from C++ vector and just use static arrays. Make them as big as the boundary cases, and you'll be good.

Are you giving bad advice on purpose, or are you just a horrible programmer? I'm inclined to believe the latter because that is some truly horrible code.

hexadecimal
Nov 23, 2008

by Fragmaster

floWenoL posted:

Are you giving bad advice on purpose, or are you just a horrible programmer? I'm inclined to believe the latter because that is some truly horrible code.

It's a good advise for UVa problems because you are not trying to use good design here, just what works the fastest to solve particular problem. You allocate arrays with maximum size for some task because it will work for all test cases and you only make it once. You are shooting for speed and easy to program over memory usage.

Using vector is slightly more expensive, and usually you can do all of the same things just with large arrays that you can reuse between problems (don't have to resize vector, or create new one). memset works faster than vectorz.

Also what is so horrible about it. It was quick to write and passes the test cases.

hexadecimal fucked around with this message at 00:26 on Dec 24, 2008

Avenging Dentist
Oct 1, 2005

oh my god is that a circular saw that does not go in my mouth aaaaagh
Real men use a sparse array for something like this.

hexadecimal
Nov 23, 2008

by Fragmaster

Avenging Dentist posted:

Real men use a sparse array for something like this.

you mean storing previous 3n+1 solutions for some number?

floWenoL
Oct 23, 2002

hexadecimal posted:

It's a good advise for UVa problems because you are not trying to use good design here, just what works the fastest to solve particular problem. You allocate arrays with maximum size for some task because it will work for all test cases and you only make it once. You are shooting for speed and easy to program over memory usage.

Speed is a bullshit reason, especially if you are talking about vectors vs. static arrays. vectors are definitely easier to work with than static arrays for all but the most trivial tasks. At worst, you can just call .reserve() or .resize() if you want.

quote:

Using vector is slightly more expensive, and usually you can do all of the same things just with large arrays that you can reuse between problems (don't have to resize vector, or create new one). memset works faster than vectorz.

Speed is not a factor if you use vector right. I don't know what vectorz is, but I assume it's some vector-zeroing function you wrote. In any case, you can call memset on &v[0].

quote:

Also what is so horrible about it. It was quick to write and passes the test cases.

It's horrible enough that you had to clean it up a bit. ;)

hexadecimal
Nov 23, 2008

by Fragmaster
Jesus Christ, why is this thread the most emotionally charged in all of cobol.

Yea sure, vectors are fine, but a more important point was to just allocate them once as global variables then re-use them in your program between test cases.

I had to clean it up because I copied it from a file where I had other implementations.

Avenging Dentist
Oct 1, 2005

oh my god is that a circular saw that does not go in my mouth aaaaagh

hexadecimal posted:

Jesus Christ, why is this thread the most emotionally charged in all of cobol.

Yep, criticizing bad code/posts so that other people don't make the same mistake/follow bad advice is definitely "emotionally charged". :geno:

hexadecimal
Nov 23, 2008

by Fragmaster

Avenging Dentist posted:

Yep, criticizing bad code/posts so that other people don't make the same mistake/follow bad advice is definitely "emotionally charged". :geno:

It wasn't bad code and there are no mistakes as well as bad advise. You and floWenoL just nit picked one of my points in order to troll.

floWenoL
Oct 23, 2002

hexadecimal posted:

It wasn't bad code and there are no mistakes as well as bad advise. You and floWenoL just nit picked one of my points in order to troll.

It was bad, or at least poorly-worded, advice. Believe it or not, I'm more interested in talking about C++ programming, which includes pointing out bad code/practices/advice, than in trolling anyone. Perhaps rather than crying 'troll', you should reflect on why it seems like every one of your programming-related posts has someone criticizing it.

hexadecimal
Nov 23, 2008

by Fragmaster

floWenoL posted:

It was bad, or at least poorly-worded, advice. Believe it or not, I'm more interested in talking about C++ programming, which includes pointing out bad code/practices/advice, than in trolling anyone. Perhaps rather than crying 'troll', you should reflect on why it seems like every one of your programming-related posts has someone criticizing it.

It would be fine if it was constructive criticism, but I swear to god that half of my posts here just had responses trying to make fun of it for no good reason other than trolling.

And granted, I am still learning so I did make mistakes in my posts, and I feel like I learned from informative responses some people have made.

POKEMAN SAM
Jul 8, 2004

hexadecimal posted:

It would be fine if it was constructive criticism, but I swear to god that half of my posts here just had responses trying to make fun of it for no good reason other than trolling.

And granted, I am still learning so I did make mistakes in my posts, and I feel like I learned from informative responses some people have made.

If you'd just stop smoking pot all of the time I think you'd see all of the problems in your posts. Not to mention that half of the time you don't even properly capitalize, which shows how little you really care about your own posts.

Victor
Jun 18, 2004
You can make trolling constructive criticism by figuring out why you're being trolled and stopping it. Trust me, I have experience! (being trolled)

Cosmopolitan
Apr 20, 2007

Rard sele this wai -->
Alright, looks like I got it completely functional using vectors. I just wanted to ask a couple questions before submitting.

hexadecimal posted:

dont' do system("cls") for uva problems.

How come? And what is the alternative?

Also, are there any unanimous rules for UVa submissions that aren't listed in each problem description? (Such as the "No system("cls")" thing.)

Edit: Also, I think the sample output on that page is wrong. Are they wrong, or am I wrong?

Cosmopolitan fucked around with this message at 06:48 on Dec 24, 2008

rjmccall
Sep 7, 2007

no worries friend
Fun Shoe
Presumably the entries are evaluated on a Unix box, where cls is meaningless. Basically, you really shouldn't need anything system-specific for these problems; just print the correct results to the console and don't worry about anything that your system/shell might have put there before your program started running.

ShoulderDaemon
Oct 9, 2003
support goon fund
Taco Defender

Anunnaki posted:

How come? And what is the alternative?
There is no alternative. Clearing the screen does not make sense in this context.

Anunnaki posted:

Also, are there any unanimous rules for UVa submissions that aren't listed in each problem description? (Such as the "No system("cls")" thing.)
Your code has to be able to run on whatever (probably UNIX-derived) automated system they use for judging.

Anunnaki posted:

Edit: Also, I think the sample output on that page is wrong. Are they wrong, or am I wrong?
You are wrong. The sample output is consistent with the problem description.

Cosmopolitan
Apr 20, 2007

Rard sele this wai -->

ShoulderDaemon posted:

You are wrong. The sample output is consistent with the problem description.

Oh poo poo, okay, reading over the problem carefully, I realized I misunderstood the problem. Okay, I got it working correctly. My only problem now is that it seems if I enter a number with more than 4 digits, the program hangs at the input for the following line. Anyone know what's up with this?

Since I don't want to create a wall of text, but it seems necessary to share the source code in its entirety to figure out the problem, here is a download link.

Edit: Is this why they put a time limit on it?
Edit 2: Never mind, I figured it out.

Cosmopolitan fucked around with this message at 10:26 on Dec 27, 2008

UberJumper
May 20, 2007
woop
Okay so i am going to be working on a system developed with ArcObjects(uses COM) and the other half is straight C++.

Should i use char* or std::string?

I don't want to use CComBSTR since they only work on windows.. and half of the system is going to be run on Linux. So a general development point i was thinking of using STL for the middle ground.

Good or bad idea?

Equilibrium
Mar 19, 2003

by exmarx
I'm taking a second semester intro to programming course this spring where we're transitioning from learning basic programming concepts in Python to C++. In Python it was a snap to run a file in TextMate, see the output/errors via PyMate, and close the window to get back to editing. My question is simple: how can I get that same one-button compile and run for single .cpp files? It seems silly opening a new Xcode project for every little thing I want to mess around with, but opening a terminal to g++ untitled.cpp, ./a.out after every save is cumbersome.

EDIT: Updating the C .tmbundle with svn fixed it such that cmd+R runs stand alone. Problem solved!

Equilibrium fucked around with this message at 21:00 on Dec 27, 2008

Nippashish
Nov 2, 2005

Let me see you dance!

Equilibrium posted:

I'm taking a second semester intro to programming course this spring where we're transitioning from learning basic programming concepts in Python to C++. In Python it was a snap to run a file in TextMate, see the output/errors via PyMate, and close the window to get back to editing. My question is simple: how can I get that same one-button compile and run for single .cpp files? It seems silly opening a new Xcode project for every little thing I want to mess around with, but opening a terminal to g++ untitled.cpp, ./a.out after every save is cumbersome.

Keep the terminal open? "Up+Enter" is pretty quick.

POKEMAN SAM
Jul 8, 2004

Nippashish posted:

Keep the terminal open? "Up+Enter" is pretty quick.

Also, pro-tip: g++ untitled.cpp && ./a.out This will compile untitled.cpp, and will only run a.out if it compiles successfully.

shodanjr_gr
Nov 20, 2007
This is probably something extremely simple but my brain is fried and I cant figure it out....


foo.h

code:

#ifndef FOO_H
#define FOO_H
//some includes


class foo
{

public:

//some constructors, destructor

void doSomethingWithFoo();

private:

//some private members

}; //foo

#endif

and the foo.cc file:

code:
#include "foo.h"

//constructor code

void foo::doSomethingWithFoo()
{
//some code
}
Trying to compile this with g++, I get a

no 'void foo::doSomethingWithFoo()' member function defined in class 'foo' error...


What the heck am I doing wrong? The rest of the code compiles just fine


edit:
Compiling and linking the class separetely seems to have fixed this...Still if someone can explain why it happens in the first place...

shodanjr_gr fucked around with this message at 16:01 on Dec 26, 2008

Vanadium
Jan 8, 2005

shodanjr_gr posted:

Trying to compile this with g++

Works fine for me. :shobon:

Are you sure you omitted only irrelevant details?

Adbot
ADBOT LOVES YOU

shodanjr_gr
Nov 20, 2007

Vanadium posted:

Works fine for me. :shobon:

Are you sure you omitted only irrelevant details?

The only other thing in the class, is a second method, with a totally different prototype that works and compiles fine...:psyduck:

  • 1
  • 2
  • 3
  • 4
  • 5
  • Post
  • Reply