• 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

Good Morning GAF

I had a question about Programming a Maze like game for a final project in my Java class. We are to create a Maze game and I believe the Maze is created from a 2D array using a imported text file.

I googled some videos on creating maze games and many of them are using JFrames? I'm not fimilar with this and I have a week to finish the project... How different are these two methods for creating a maze for a game? I hope my question is easy to understand, as I am not completely sure I'm asking it correctly.

Our assignment doesn't mention anything about using JFrames... Im currently looking up more information about JFrames.
 

Granadier

Is currently on Stage 1: Denial regarding the service game future
Good Morning GAF

I had a question about Programming a Maze like game for a final project in my Java class. We are to create a Maze game and I believe the Maze is created from a 2D array using a imported text file.

I googled some videos on creating maze games and many of them are using JFrames? I'm not fimilar with this and I have a week to finish the project... How different are these two methods for creating a maze for a game? I hope my question is easy to understand, as I am not completely sure I'm asking it correctly.

Our assignment doesn't mention anything about using JFrames... Im currently looking up more information about JFrames.

Does your project involve using Swing?
 

Rapstah

Member
Good Morning GAF

I had a question about Programming a Maze like game for a final project in my Java class. We are to create a Maze game and I believe the Maze is created from a 2D array using a imported text file.

I googled some videos on creating maze games and many of them are using JFrames? I'm not fimilar with this and I have a week to finish the project... How different are these two methods for creating a maze for a game? I hope my question is easy to understand, as I am not completely sure I'm asking it correctly.

Our assignment doesn't mention anything about using JFrames... Im currently looking up more information about JFrames.

JFrames are just windows. They probably want you to draw the maze into something. The JFrame itself wouldn't know that it contains a maze because it's a generic graphics class. Look up drawing in Java and layout managers, as well as the specific layout manager GridLayout (if the maze consists of squares of equal size).

You should probably figure out if they want you to draw the maze or if printing to console is okay first, though. A week isn't a lot of time to learn drawing in Java, but there should be decent tutorials on it out there.
 
Does your project involve using Swing?

I thought I would ask, but also looking at the second comment posted I think we are to make this in the console, I don't think we will be using any external graphics.

I belive this is all... He told us he wanted us to use just a few classes...

to be specific, we have a class for an entity with subclasses for the character and for two specific power up items, and one for the Maze itself. I dont specificly see anything for using imported classes like javax.swing... ( I got this from looking at one video that used a JFrame)

He said for the maze class we need a 20x50 size maze represented by a 2D character array and the structure is read in from a maze.txt file. Boundrys in the maze are represented by a bunch of *** characters.
 

Granadier

Is currently on Stage 1: Denial regarding the service game future
I thought I would ask, but also looking at the second comment posted I think we are to make this in the console, I don't think we will be using any external graphics.

I belive this is all... He told us he wanted us to use just a few classes...

to be specific, we have a class for an entity with subclasses for the character and for two specific power up items, and one for the Maze itself. I dont specificly see anything for using imported classes like javax.swing... ( I got this from looking at one video that used a JFrame)

He said for the maze class we need a 20x50 size maze represented by a 2D character array and the structure is read in from a maze.txt file. Boundrys in the maze are represented by a bunch of *** characters.

If you haven't been using Swing or GUI's at all in your course, then I doubt he would want you to learn and start using them for this one project.
 

usea

Member
I'm giving an hour-long talk on wednesday to the local user group (40-60 people) on functional programming. I'm a novice when it comes to functional programming, and I'm also new to technical speaking. Also I am under-prepared. Halp

Any tips on speaking? The audience will mostly be students. Should I bring slides on a laptop or should I use some fancy web slide thing on the provided computer?
 

Rapstah

Member
I'm giving an hour-long talk on wednesday to the local user group (40-60 people) on functional programming. I'm a novice when it comes to functional programming, and I'm also new to technical speaking. Also I am under-prepared. Halp

Any tips on speaking? The audience will mostly be students. Should I bring slides on a laptop or should I use some fancy web slide thing on the provided computer?

Slides are generally expected, but they also help you because you can cheat and look at the headlines on the slides to figure out what you're going to talk about next.
 

Granadier

Is currently on Stage 1: Denial regarding the service game future
I was wondering, is there a way to use the drawstring method to write to a JFrame the contents of a txt file?

There's ways, but I would avoid it if you haven't been working with Swing.

I'm giving an hour-long talk on wednesday to the local user group (40-60 people) on functional programming. I'm a novice when it comes to functional programming, and I'm also new to technical speaking. Also I am under-prepared. Halp

Any tips on speaking? The audience will mostly be students. Should I bring slides on a laptop or should I use some fancy web slide thing on the provided computer?

Slides please! And I'd recommend getting a PowerPoint or Prezi set up and stored on a flash drive beforehand. Then you don't have to fret over a possible web issue.
Other than that, no idea. I've never been in a situation where I had to give an hour long speech. I think I'd bail into the bathroom or something. Gulp.

Good luck!
 

Jokab

Member
I'm giving an hour-long talk on wednesday to the local user group (40-60 people) on functional programming. I'm a novice when it comes to functional programming, and I'm also new to technical speaking. Also I am under-prepared. Halp

Any tips on speaking? The audience will mostly be students. Should I bring slides on a laptop or should I use some fancy web slide thing on the provided computer?

I can't give any helpful tips, but I'm curious as to why you're giving a talk on functional programming when you're not very familiar with it.
 

Slavik81

Member
I'm giving an hour-long talk on wednesday to the local user group (40-60 people) on functional programming. I'm a novice when it comes to functional programming, and I'm also new to technical speaking. Also I am under-prepared. Halp

Any tips on speaking? The audience will mostly be students. Should I bring slides on a laptop or should I use some fancy web slide thing on the provided computer?
I suggest using your own laptop. The more you control, the better.

If you can, do a 5-minute flip-through of your slides on the actual projector the day before. That helps you discover:
  • How to turn on and connect to the projector.
  • How badly the projector distorts colors (so you don't end up with invisible text or images).
  • Any other unexpected technical problems.
 

iapetus

Scary Euro Man
I'm giving an hour-long talk on wednesday to the local user group (40-60 people) on functional programming. I'm a novice when it comes to functional programming, and I'm also new to technical speaking. Also I am under-prepared. Halp

Any tips on speaking? The audience will mostly be students. Should I bring slides on a laptop or should I use some fancy web slide thing on the provided computer?

Be confident. You know more than you think about functional programming, especially compared to your audience. And even if you don't they'll believe that you do, because you're the confident-sounding person at the front of the room.
 
Be confident. You know more than you think about functional programming, especially compared to your audience. And even if you don't they'll believe that you do, because you're the confident-sounding person at the front of the room.

Definitely this. I'd also add that if they ask you questions and you don't know the answer, just say so. It's so much worse to be caught out trying to make something up than to just admit you don't know. Another strategy that works with some questions is to say "I don't know the exact answer to that, but it sounds a bit like..." and try to relate it to something you can talk about.
 

usea

Member
The audience is mostly students and recent graduates, and the topic is introduction to functional programming with C#. I'm covering the basics of functional programming as a paradigm (immutability, referential transparency, functions as values, higher order functions), and applying as much of that as is reasonable to real C# development. Then briefly covering some features of real FP languages and libraries like discriminated unions, monad usage, pattern matching, lazy/eager evaluation, strong type inference, etc.

I've been fiddling with functional programming for about a year and a half. My uptake has been slow since I've been gradually incorporating it over time.

When I say I'm a novice it's because I feel that way. I mean, I feel that way about everything, but I'm definitely less confident about FP than some other topics.

The reason why I agreed to give the talk is because nothing helps you learn a subject like preparing a talk on it. And I gave a lightning round (10 min) talk on it a few months ago which was received well.

I am definitely using slides, I'm just not sure what kind. I use them mainly for myself to keep track of where I am. Like an outline that the audience can see.

Oh, and I'm going to do some coding demos, too. Even though supposedly coding during a presentation is a bad idea. Hopefully it will work out. I'm spending tomorrow practicing a lot so if it feels iffy I'll cut it.
 

cyborg009

Banned
i found the official bootstrap documentation to be really informative. the part pertaining to grid layout is here.

also, any reason you're not using 3.0 or above?

Thanks and I didn't know about the previous version I was using an old guide to make my site.

Last question I was trying to make search to look into my database, I wanted to know if interacting between two controllers will be possible? If not I'll just use one.
 

RustyO

Member
So, I'm a bit of a hack/untrained programmer, and thought I'd ask this daft question here.

I regularly build strings based off for/foreach loops, where the first line/item of the string will be unique to the rest. Was just curious if there is a "better" / more professional way to do it then the below?

Edit: C#

Code:
  private void foobar()
  {
     string myFooBar = "";
     for (int i = 0; i < 2; i++)
     {
	if ( i == 0) { myFooBar = "Foo"; }
        else { myFooBar = myFooBar + "Bar" + Convert.ToString(i); }
     }
  }
 

Hylian7

Member
So, I'm a bit of a hack/untrained programmer, and thought I'd ask this daft question here.

I regularly build strings based off for/foreach loops, where the first line/item of the string will be unique to the rest. Was just curious if there is a "better" / more professional way to do it then the below?

You could do that or just do something like this:

Code:
foo[0] = "Foo";
for(int i = 1; i < (size of your array, however you get that in your particular language); i++) {
foo += "Bar" + Convert.toString(i);
}
 

upandaway

Member
So, I'm a bit of a hack/untrained programmer, and thought I'd ask this daft question here.

I regularly build strings based off for/foreach loops, where the first line/item of the string will be unique to the rest. Was just curious if there is a "better" / more professional way to do it then the below?
You mean in Java?
 

hateradio

The Most Dangerous Yes Man
So, I'm a bit of a hack/untrained programmer, and thought I'd ask this daft question here.

I regularly build strings based off for/foreach loops, where the first line/item of the string will be unique to the rest. Was just curious if there is a "better" / more professional way to do it then the below?
Remember to use the code tags.

I don't really understand your question. It doesn't seem like there's a reason in your example for what you're doing. What is it that you're trying to achieve? Just different text at the front?


Code:
private void Foobar()
{
	string myFooBar = "";
	int i = 0;
	
	// do a test outside the loop to update myFooBar
	if (someRequirementIsMet) {
		myFooBar = "Foo";
		i = 1; // start the loop at 1
	}
	
	for (i; i < 2; i++)
	{
		myFooBar = myFooBar + "Bar" + Convert.ToString(i);
	}
}
 

Water

Member
I regularly build strings based off for/foreach loops, where the first line/item of the string will be unique to the rest. Was just curious if there is a "better" / more professional way to do it then the below?
If the language makes it reasonable, it's nicest to replace that kind of loop with a functional fold. In C# fold goes by the name of Aggregate:
http://stackoverflow.com/questions/7105505/linq-aggregate-algorithm-explained

Here's a piece of Haskell code doing a fold over the string "12345" but treating the first item separately and increasing it by one:
Code:
-- has result "22345"
let str = "12345" in
  foldl (\acc c -> acc ++ [c]) [succ $ head str] (tail str)
Fold takes three arguments:
  • initial value (in this case, [succ $ head str], the first character in the string increased by one and converted to a string)
  • list of items to process (in this case, (tail str) which means "2345")
  • a function that takes an initial value and the next value from the list, and produces a new initial value; in this case I just convert the list value (a char) into a string, and append the strings together
 
So, I'm a bit of a hack/untrained programmer, and thought I'd ask this daft question here.

I regularly build strings based off for/foreach loops, where the first line/item of the string will be unique to the rest. Was just curious if there is a "better" / more professional way to do it then the below?

Edit: C#

Code:
  private void foobar()
  {
     string myFooBar = "";
     for (int i = 0; i < 2; i++)
     {
	if ( i == 0) { myFooBar = "Foo"; }
        else { myFooBar = myFooBar + "Bar" + Convert.ToString(i); }
     }
  }

Well, you could just initialize the string as "Foo", then you don't need the if in the for loop, something like this:
Code:
  private void foobar()
  {
     string myFooBar = "Foo";
     for (int i = 0; i < 2; i++)
     {
          myFooBar += "Bar" + Convert.ToString(i); 
     }
  }
Secondly, you might want to use a StringBuilder if you're doing lots of string concatenations. (especially on long strings) The reason is that strings are immutable in C# and Java, so each time you do a + operation on two strings, you are creating a new string with the contents of the operands concatenated together. A StringBuilder is just a mutable string that you can append to and convert to a string. An example
Code:
private string StringBuilderTest() {
	var sb = new StringBuilder();
	for (int i = 0; i < 10000; i++) {
		sb.Append(i + "\n");
	}

	return sb.ToString();
}
I don't have Visual Studio installed at work, so I can't test the difference in C#, but in Java I get about 0.3 seconds for string concatenation and 0.008 seconds for the StringBuilder.
 
Another bit of advice on a lecture there always will be (even if attendance is not mandatory) people playing Angry Birds, talking to each other, searching the Internet, etc. instead of paying attention. Don't let that bother you it doesn't have anything to do with your presentation - do your best just for the students actually listening to your speech you and they deserve it.
 

usea

Member
Rusty, int has a ToString() method, you don't need to call Convert.ToString(). And when you're adding an int to a string, C# will actually coerce the int to a string automatically. So you can just say
"Bar" + i;

Also as others pointed out, you should get rid of the if, and simply initialize your string outside of the loop to "Foo" (and change the loop to start at 1).

Your loop only goes up to 1. I'm not sure if that was intentional. The result would be "FooBar1". You don't need a loop at all. You can just say
string myFooBar = "FooBar1"

Also your function doesn't return anything, so making the string doesn't do anything.

In the general case, I'd also recommend using Aggregate. If you have to concatenate a bunch of strings (more than 1000), you can use a StringBuilder in your Aggregate. I actually avoid the StringBuilder since it makes the code harder to read and performance rarely matters. But it is faster. Here's how you could do that for 1000 "BarX":

Code:
var bars = Enumerable.Range(1,1000)
  .Select(x => "Bar" + x)
  .Aggregate(new StringBuilder(),
    (sb, x) => sb.Append(x))
  .ToString();
return "Foo" + bars;
Enumerable.Range(1,1000) produces a list of the numbers 1-1000 (inclusive).

Select(x => "Bar" + x) transforms the numbers to strings of the format "BarX" where X is the number.

Aggregate(new StringBuilder(), (sb, x) => sb.Append(x)) does a Fold, basically accumulating everything in the list starting from the left. new StringBuilder() is the first element, and (sb, x) => sb.Append(x) is the function you're using to add each element together. sb is the previous element, and x is the BarX. You're appending BarX to the StringBuilder each time.

If that seems confusing, just use the for loop. But if the number of loops doesn't change (like you're putting the number 92 in there instead of basing it on the size of something else), you don't even need the loop. You could just write out the answer in your code since it will never change.

--------------

The above code only gets rid of half the raw string concatenations. You could do this to use StringBuilder for all of them:
Code:
var sbuilder = new StringBuilder();
var builders = Enumerable.Repeat(sbuilder, 1000);
var nums = Enumerable.Range(1,1000);
return "Foo" + builders.Zip(nums, (sb, i) => sb.Append("Bar").Append(i)).Last().ToString();
But it's not that much faster. If speed is the main concern, just use the loop. It'll be like 50% faster.
 

harSon

Banned
C++ question.

Could someone point me in the direction of how I could extract integer values from a string? For example, let's say I had the string "123456." How would I go about extracting an integer value from the string? Preferably each number separately since I'm looking to use them for a calculation.

I'm assuming I can using something like the at() function, but I'm having issues coming up with an elaborate way to utilize it in a simple manner (like a loop for example) to extract a large string of numbers.

Any ideas?

Edit: Without Arrays please
 
C++ question.

Could someone point me in the direction of how I could extract integer values from a string? For example, let's say I had the string "123456." How would I go about extracting an integer value from the string? Preferably each number separately since I'm looking to use them for a calculation.

I'm assuming I can using something like the at() function, but I'm having issues coming up with an elaborate way to utilize it in a simple manner (like a loop for example) to extract a large string of numbers.

Any ideas?

Edit: Without Arrays please

std::string myString = "123456";
int value = atoi(myString.c_str()); //value = 123456

Is probably the most basic way of extracting it. If its a char* you could maybe do something like

char* numString = "123456"
char curChar = numString[0];
std::vector vNumbers;

while (curChar != '\0' )
{
int tempInt = atoi(curChar);
vNumbers.push_back(tempInt);
}

Although I have no idea if this code will work, but you get the idea. I think you can also use ifstreams to do the same thing.
 

Water

Member
C++ question.

Could someone point me in the direction of how I could extract integer values from a string? For example, let's say I had the string "123456." How would I go about extracting an integer value from the string? Preferably each number separately since I'm looking to use them for a calculation.
So you want to process each digit separately?
First you loop over the string. You can use indexing like str[index], but the easiest way is to use a range-for loop like below.
The string contains ASCII characters like '0' and '1' whose numeric value is not the same as the digit the character looks like. But the numeric values are right next to each other, and so you can always extract the digit by subtracting '0' from the character. '0' - '0' is 0, '1' - '0' is 1, and so on.
Code:
auto s = string("123456");
for (char c : s) {
  int digit = c - '0';
  cout << "character: " << c << " value: " << (int)c;
  cout << " digit: " << digit << endl;
}
 
Not a programming question exactly, but more of a question directed towards professionals / people that interview candidates.

When it comes to the personal projects of a candidate, what do you look for? Do interviewers like to hear about massive, complex programs that you've been working on for years? Or could I talk about a project I've done that took a few months, isn't extremely complex, but I was very proud of, like a small game or something? I'm also assuming "shipped" products like iOS or Android apps are looked upon more highly?

Asking this because I'm a first year Software Engineering student, and unfortunately, I get to do very little actual coding since I'm bogged down in gen eds and other required classes this year and most of next year, which sucks and I'd like to start doing more things on my own. Anyway, I plan to start looking into internships next year and I'd like to have some cool things to talk about and/or show off.

Any advice about personal projects would be great. I've been toying with the idea of starting to work with Unity (especially when I have a lot more free time over summer) but I'm not sure if that's a relevant project if I'm not exactly shooting towards getting into the games industry.
 

Kalnos

Banned
Not a programming question exactly, but more of a question directed towards professionals / people that interview candidates.

In my experience:

1. Having a project is good, if the interviewer can see it running/the code it's even better.
2. Size of the program is irrelevant IMO. If you feel it's interesting and you're enthusiastic about it then the interviewer will notice. Make sure you can talk about your project.
3. You're a first year student... thus if you have anything at all then you're in good shape.

A game is a good project to show, even if you aren't looking for a related job. Games aren't easy to make. I made a simple game with a Raspberry Pi and some LEDs and I had interviewers at healthcare companies and insurance companies who were interested to hear about it.
 
God damn it.

(&#9583;°&#9633;°&#65289;&#9583;&#65077; &#9531;&#9473;&#9531;

Rule #1 of debugging has to be that once you've added enough logging to corner the problem, it stops happening.
 

Water

Member
God damn it.

(&#9583;°&#9633;°&#65289;&#9583;&#65077; &#9531;&#9473;&#9531;

Rule #1 of debugging has to be that once you've added enough logging to corner the problem, it stops happening.
That doesn't sound that bad. I'd take it over an intermittent problem that disappears completely whenever you compile in debug mode. A friend just fixed one of those. He works with huge piles of legacy C/C++ code written by some dude who has since retired and moved back to his farm to take care of cows, and apparently never quite learned C or C++ over 10+ years of writing them. The reason this time turned out to be that the farmerdude wanted to test the equality of some structs he wrote. And what better function to do it with than memcmp?
 
In my experience:

1. Having a project is good, if the interviewer can see it running/the code it's even better.
2. Size of the program is irrelevant IMO. If you feel it's interesting and you're enthusiastic about it then the interviewer will notice. Make sure you can talk about your project.
3. You're a first year student... thus if you have anything at all then you're in good shape.

A game is a good project to show, even if you aren't looking for a related job. Games aren't easy to make. I made a simple game with a Raspberry Pi and some LEDs and I had interviewers at healthcare companies and insurance companies who were interested to hear about it.

Thank you, good info to keep in mind. I definitely want to show off things that I have wanted to make rather than just showing course work projects.
 

Granadier

Is currently on Stage 1: Denial regarding the service game future
That doesn't sound that bad. I'd take it over an intermittent problem that disappears completely whenever you compile in debug mode. A friend just fixed one of those. He works with huge piles of legacy C/C++ code written by some dude who has since retired and moved back to his farm to take care of cows, and apparently never quite learned C or C++ over 10+ years of writing them. The reason this time turned out to be that the farmerdude wanted to test the equality of some structs he wrote. And what better function to do it with than memcmp?

Yep

I know some of those words.
 

harSon

Banned
Need some more C++ help. Here's my code thus far:
Code:
#include <iostream>
#include <string>
#include <sstream>

using namespace std;

char getCheckSum(string);
bool validateInput(string);
string formatWithHyphen(string);

    string isbn;
    int sum, n;
    int i = 0;
    bool isValid;

int main()
 {
    string isbn, contProgram;

    do
    {
    cout << "This program calculates the checksum of a 9-digit ISBN code and display the whole 10-digit ISBN." << endl;
    cout << "Enter 9 digits for the ISBN code: ";
    cin >> isbn;

    isValid = validateInput(isbn);
    while (!isValid && isbn != "X")
    {
        cout << "The input ISBN code is not valid." << endl;
        cout << "Please enter a 9 digit number." << endl;
        cin >> isbn;
        isValid = validateInput(isbn);
    }

    /*if (!isValid) {
        cout << "\nThe input ISBN code is not valid.\n";
        return (1);
    }*/
    char sum = getCheckSum(isbn);
    cout << "\nThe complete ISBN code is " << isbn + sum << endl;
    string hyphen = formatWithHyphen(isbn);
    cout << hyphen + sum << endl;
    cout << endl;
    } while(isbn != "X");

 }
bool validateInput(string isbn) {
    while (true) {
        stringstream ss(isbn);
        if (ss >> i && isbn.length() == 9) {
            return true;
        }
        else {
            return false;
        }
    }
}

char getCheckSum(string isbn) {

    for(sum = 0, i = 0, n = 1; i < 9; i++, n++)
    {
        char x = isbn.at(i);
        int xnew = x - '0';
        sum += xnew * n;
        cout << sum << endl;
    }
    int checksum = sum % 11;
    char checkSumReturn = checksum + '0';
    if(checksum == 10)
    {
        checkSumReturn = 'X';
        return checkSumReturn;
    }
    else{

        checkSumReturn = checkSumReturn;
        return checkSumReturn;
    }
}
string formatWithHyphen(string isbn)
{
    isbn.insert(1, "-");
    isbn.insert(5, "-");
    return isbn;
}

I'm getting a "Terminate called after throwing an instance of 'std::eek:ut_of_range" when typing "X" into the console to exit my program. Anyone have an idea how I can fix it?
 
That's the one!

Found it, though. std::atomic_flag doesn't quite do what I expected it to...

That doesn't sound that bad. I'd take it over an intermittent problem that disappears completely whenever you compile in debug mode. A friend just fixed one of those. He works with huge piles of legacy C/C++ code written by some dude who has since retired and moved back to his farm to take care of cows, and apparently never quite learned C or C++ over 10+ years of writing them. The reason this time turned out to be that the farmerdude wanted to test the equality of some structs he wrote. And what better function to do it with than memcmp?
This happens. Well, the memcmp thing shouldn't, but the problem in general. Why do you think I was adding logging? I do know how to gdb!
 

RustyO

Member
@ Hylian7, hateradio, Water, close to the edge and usea; thanks for the replies / comments.

I don't really understand your question. It doesn't seem like there's a reason in your example for what you're doing. What is it that you're trying to achieve? Just different text at the front?

Pretty much. It is probably not the best example, but yes, the first line is different, whilst all other lines are the same whilst looping.

Usually it's a bit more complex then that, so use StringBuilder and/or switch plus variables when doing so, just wondered if I was missing something obvious?

Remember to use the code tags.

Didn't even know they existed till I saw someone elses post :)
 

Cromat

Member
Hello dev-gaf I have a question. If you wanted to set up a social network like website with a rich Web interface and mobile app clients, what would that entail?

Am I right that you would need the following:

- Web App created in some language
- Web Server
- Database containing user information
- Server which actually performs operations on the data, the backbend of the service

Would this be a huge undertaking? How the hell did Zuckerberg do it in his dorm room?
 

tokkun

Member
Need some more C++ help. Here's my code thus far:

I'm getting a "Terminate called after throwing an instance of 'std::eek:ut_of_range" when typing "X" into the console to exit my program. Anyone have an idea how I can fix it?

Yeah, but in the spirit of "teach a man to fish", do you know how to use GDB? This probably shouldn't be that difficult to solve with a debugger.

Get the program running in GDB. After it throws the out of range exception, type

Code:
backtrace

. This will show you the 'stack trace' (what methods were being called when the error occurred, and what specific line was being executed when the error occurred). You can pick a specific method using the 'frame' command. Usually you will want to start with the lowest level method that is actually in your program and not in a library. So for example, you might type

Code:
frame 4

(or whatever frame number you wanted to look at. Once you are in a frame, you can see the value of local variables with the 'print' command. So, hypothetically, if the out of range exception was being thrown by

Code:
char x = isbn.at(i);

You change to the frame representing the checksum function, then you can examine the values causing the exception by

Code:
print i
print isbn.length()

If i >= isbn.length(), you will get an out of range exception. Then you might want to see what isbn was, by

Code:
print isbn

Since it's a string, which is a complex datatype, the print result will include some gibberish, but the contents of the string should be included. If it turn out that this ISBN is not one you wanted to process, you can use the frame command to switch to a function at a higher level in the stack and inspect what caused it to pass that ISBN. Eventually you will figure out the bug.
 
Would this be a huge undertaking? How the hell did Zuckerberg do it in his dorm room?

All of those things can run on one server when you are creating it; you just design it to be run in chunks on various servers even when it isn't. Server doesn't need to be in his dorm room.
 

Water

Member
Hello dev-gaf I have a question. If you wanted to set up a social network like website with a rich Web interface and mobile app clients, what would that entail?

Am I right that you would need the following:

- Web App created in some language
- Web Server
- Database containing user information
- Server which actually performs operations on the data, the backbend of the service

Would this be a huge undertaking? How the hell did Zuckerberg do it in his dorm room?
Not a webdev-gaffer, but the size of that undertaking will depend completely on the details. With the power tools of today, a guy who's used to that stuff can bang out something that nominally fills those criteria (user accounts, login, and a chatroom where different logins can post stuff) in maybe an hour or two, but there is no limit to the effort you can spend on making it do things that are actually useful, on polishing, on security, on scalability.

The "server which actually performs operations on the data" is often a module on the web server instead of a separate entity.
 

harSon

Banned
Yeah, but in the spirit of "teach a man to fish", do you know how to use GDB? This probably shouldn't be that difficult to solve with a debugger.

Ah, it seems my edit didn't go through . I fixed it shortly after making the post. I basically used an if statement to exclude the input "X" from the function that was giving me issues, since it's supposed to exit the program anyways.

I'm extremely thankful for the tips though, debugging is definitely an area I need to strengthen. Any recommendations for tutorials along that front? My professor is pretty terrible, and just reads the powerpoint that came with the text book for the entirety of lecture, so I've basically been teaching myself via the book, online tutorials, video tutorials, forum posts, etc. (Fortunately, I'm decent at self-learning, but about 60% of the class has dropped, if not more Lol. I'm definitely taking RateYourProfessor to hear next quarter!) So posts like yours are extremely helpful, and I really do appreciate the "teach a man to fish" mentality - since you truly do learn more in that manner.

As for the code I ended up with:

Code:
#include <iostream>
#include <string>
#include <sstream>

using namespace std;

char getCheckSum(string);
bool validateInput(string);
string formatWithHyphen(string);

string isbn;
int sum, n;
int i = 0;
bool isValid;

int main()
 {
    string isbn;

    do
    {
        cout << "This program calculates the checksum of a 9-digit ISBN code" << endl;
        cout << "and displays the whole 10-digit ISBN." << endl;
        cout << "\nEnter 9 digits for the ISBN code: ";
        cin >> isbn;

        isValid = validateInput(isbn);
        while (!isValid && isbn != "X")
        {
            cout << "\nThe input ISBN code is not valid." << endl;
            cout << "Please enter a 9 digit number: ";
            cin >> isbn;
            isValid = validateInput(isbn);
        }

        if (isbn != "X") {
        char sum = getCheckSum(isbn);
        cout << "\nThe complete ISBN code is " << isbn + sum << endl;
        string hyphen = formatWithHyphen(isbn);
        cout << "\nThe hyphenated ISBN code is: " << hyphen + sum << endl;
        cout << endl;
        }
    } while(isbn != "X");

 }
bool validateInput(string isbn) { 
    while (true) {
        stringstream ss(isbn);
        if (ss >> i && isbn.length() == 9) {
            return true;
        }
        else {
            return false;
        }
    }
}

char getCheckSum(string isbn) {

    for(sum = 0, i = 0, n = 1; i < 9; i++, n++)
    {
        char x = isbn.at(i);
        int xnew = x - '0';
        sum += xnew * n;
        cout << sum << endl;
    }
    int checksum = sum % 11;
    char checkSumReturn = checksum + '0';
    if(checksum == 10)
    {
        checkSumReturn = 'X';
        return checkSumReturn;
    }
    else{

        checkSumReturn = checkSumReturn;
        return checkSumReturn;
    }
}
string formatWithHyphen(string isbn)
{
    isbn.insert(1, "-");
    isbn.insert(5, "-");
    return isbn;
}

I'm definitely enjoying programming thus far. It's incredibly frustrating at times, especially when you hit a wall, but it's also incredibly exciting/ rewarding when you finally get over that hump.
 

mltplkxr

Member
Hello dev-gaf I have a question. If you wanted to set up a social network like website with a rich Web interface and mobile app clients, what would that entail?

Am I right that you would need the following:

- Web App created in some language
- Web Server
- Database containing user information
- Server which actually performs operations on the data, the backbend of the service

Would this be a huge undertaking? How the hell did Zuckerberg do it in his dorm room?

Scope is key. The Facebook that Zuckerberg created was much simpler than the Facebook you see today.

Facebook now is a huge undertaking. Facebook back then is a weekend project.

Pro-tip: before you decide what 'ware you're going to need, define precisely what you want to do and then make sure you can express all that in one sentence.

When an author decides to write a book, he doesn't say :"I'm going to do a hardcover with a center fold map and color pictures and the font will be Arial." It usually starts with "I'm going to write a cookbook based on a 100 mile hike in Northern England."
 

Cromat

Member
Thanks for the answers guys. Quite illuminating. I was asking to understand what knowledge would have to be involved. I mean, if you need to be a Web developer and also know the inside and out of running and using Webservers and databases it seems very difficult.

It's different from developing an application where you only need to know one programming language and some SDK.
 
Thanks for the answers guys. Quite illuminating. I was asking to understand what knowledge would have to be involved. I mean, if you need to be a Web developer and also know the inside and out of running and using Webservers and databases it seems very difficult.

It's different from developing an application where you only need to know one programming language and some SDK.

I recommend taking the Web Dev course at Udacity:

https://www.udacity.com/course/cs253

You won't really learn anything about design (so the visual part of making websites) or much client side programming (so, Javascript), but it will give you a really good overview of what goes into developing a web application from the ground up. The course uses Google App Engine as well, which simplifies a lot of the maintenance parts of making a web app.

The goal throughout the course is to build some basic blogging software, and the final assignment is to create a really basic wiki. You can go fairly far after learning that basic groundwork.

You can take the course for free, the paid version just gets you a mentor and a certificate or something like that.
 
Top Bottom