jonsca 1,059 Quantitative Phrenologist Team Colleague Featured Poster

I've only skimmed through it in the past, but check out http://www.freetechbooks.com/object-oriented-programming-with-ansi-c-t551.html (free and legal download). It does a lot of preprocessing on the code.

Something you might be interested in, as I think it's at least peripherally related is the cfront system that converted C++ to C (see http://en.wikipedia.org/wiki/Cfront) for compilation in the early years of C++.

jonsca 1,059 Quantitative Phrenologist Team Colleague Featured Poster

Use the "break;" that thelamb was suggesting. That will take you out of the j loop, but anything remaining within the i loop at the end will be executed. Then control will go back to the top of the i loop. There will be a fresh j loop at that point. You can experiment with it in your code and output i and j at each step to see these things.

jonsca 1,059 Quantitative Phrenologist Team Colleague Featured Poster

5. Is this homework? ;)

Why don't you write out what you think the answers are and someone will check them over for you.

jonsca 1,059 Quantitative Phrenologist Team Colleague Featured Poster

cin.ignore comes first. Post up your latest code with the changes and I'll take a look

jonsca 1,059 Quantitative Phrenologist Team Colleague Featured Poster

No, they are not from conio.h. Conio is outdated and non-standard. Some people do still use getch from conio, but for those reasons, they shouldn't. get and ignore are part of iostream. You put them where you would put system("pause");

You need the ignore because sometimes you'll have a situation like:

int n;
cin >> n;
//put in 10 and hit enter, the 10 is stored in the int, but where is the '\n' from enter stored?  Nowhere, it's left in the input "stream."

So if you follow the above by

cin.get()

the get will pull in the next character in the stream, which is the '\n' and so your program doesn't wait for a key, it's already got one. Putting in the ignore will sop up that extra character so the pause will work. Occasionally, you have to hit an extra enter with the ignore statement in there, but that's the only downside. The upside is that your code will be portable (on systems where there is no "pause" command) and there are issues of security (say all of the programs on your machine with sensitive information use system("pause"), then if someone places a nefarious "pause" program in the same directory, then there's trouble. Not exactly an issue with a college assignment, but something of which to be aware.

jonsca 1,059 Quantitative Phrenologist Team Colleague Featured Poster

You can use the characters of stringValue1 directly (line 7 doesn't work, which you probably found out already). stringValue1[word] gives the proper character, and stringValue1.size() gives you the length.

I just started C++ (new to programming) so I dont know all the functions.

Your professor knows that, which is why she/he probably wants you to do it the long way.

There are a couple of ways that you can do the next step, either way you have to get the digits from the numeric result you find. See what you can come up with.

jonsca 1,059 Quantitative Phrenologist Team Colleague Featured Poster
jonsca 1,059 Quantitative Phrenologist Team Colleague Featured Poster

27 has an extra semicolon that doesn't belong either.

jonsca 1,059 Quantitative Phrenologist Team Colleague Featured Poster

I'm not sure how you'd do a direct conversion, but I'm assuming you're using their REST API. There's probably a call you can make to the server that when given the location, it'll send back the lat/long coordinates.

jonsca 1,059 Quantitative Phrenologist Team Colleague Featured Poster

for system("PAUSE") etc? you can use windows.h

A more portable solution (and one that isn't platform dependent) is to put

cin.ignore();
cin.get();

at the very end of your code. Sometimes the stream gets clogged with extra characters and it gets run through like a stop sign at the mall, so you'd have to flush the input buffer using the techniques in the sticky thread of the forum. If there's just a stray '\n' or nothing in the stream, it works just fine.

Something to consider.

jonsca 1,059 Quantitative Phrenologist Team Colleague Featured Poster

Maybe you should try taking you call to main() out. It's still in there and now it's actually causing you a problem! Your code never reaches the end of the loop because you're calling main and circumventing it.

Good move getting Code::Blocks. It's got a nice interface to a debugger (gdb) that will be useful.

jonsca 1,059 Quantitative Phrenologist Team Colleague Featured Poster

Honestly, skip over the wxdevc++ and grab Code::Blocks. Much better, updated all the time, newer version of the compiler included.

Post what you currently have, since you've obviously made some changes

jonsca 1,059 Quantitative Phrenologist Team Colleague Featured Poster

so how do you propose i go to these functions if i shouldnt be using return...also is that why i couldnt put a second set of returns under the first set, because with the if statement they worked fine, but once i put in a new if statement inside of it, the returns started throwing errors

Just call the function outright:

multiplication();
division();
etc.

Calling them without something to accept the return value(i.e., mulitplication(); vs. int x = multiplication();) discards that value, but you aren't using them anyway.

jonsca 1,059 Quantitative Phrenologist Team Colleague Featured Poster

Wow what compiler is that? That's annoying that there is no text with it. Post your current code and try out another compiler. At least it gives you the line numbers, look at the settings and see if there's something you can change.

I think you have missunderstood the use of return....

Yes, thank you. I had told him to just eliminate them and I hadn't really explained why. The only reason you're lucking out and getting the functions to run is that whatever follows the return is evaluated before the return actually happens (so I could write return 2*3-6; and it would return 0).

jonsca 1,059 Quantitative Phrenologist Team Colleague Featured Poster
else
    {//see here
    cout << "It seems you have mistyped something, try again\n";
    system("pause");
    system("CLS");
    return main(); //this works too? wtf...
    }/*<<--not missing a closing brace*/while((s2!="Y") || (s1!="y")

^ is supposed to be the closing brace for the do/while loop, as it's got the while statement right next to it. You need another to close off the else. Try that, and if the error is still there, please copy/paste the error message. The error messages can be slightly cryptic, but they are not indistinguishable.

jonsca 1,059 Quantitative Phrenologist Team Colleague Featured Poster

There are a bunch of undeclared variables here. Look at line 28. Where is addNumPlayers declared? It appears out of nowhere. Look at line 12 and at line 28. Notice anything amiss? (you may have fixed that one because it doesn't appear on your current error list)

PlayerNames is an array of strings. You can't compare an array of strings to just one character on line 45.

In calAvgScore, avgScore comes out of nowhere. It still needs to be declared locally in that function.

On 51, i doesn't exist there (you probably had a loop and you took it out).

jonsca 1,059 Quantitative Phrenologist Team Colleague Featured Poster

thanks for putting up with me for those last few days, jonsca

It was never a problem. Glad everything is up and running!

jonsca 1,059 Quantitative Phrenologist Team Colleague Featured Poster

Someone can correct me if I'm off-base on this, but in order to use the string pointer for anything, there has to be a string somewhere to point at. So, in reality you may be bringing a pointer into the mix that you don't need (at least in this program).

If you needed to pass the strings around, you could pass by reference.

jonsca 1,059 Quantitative Phrenologist Team Colleague Featured Poster

You should use the <ctime> instead of the <time.h> because in ctime, all of the functions are declared in the the std:: namespace, whereas with time.h, they are all thrown into the global namespace.

I'll invert the question and ask you "Why?" I originally thought you were getting mixed up between C-strings (the char * variety) and std::string, but I'm not sure what your rationale is. There's the extra dereferencing step which is required to access the actual string that you totally avoid when the strings are not pointers.

jonsca 1,059 Quantitative Phrenologist Team Colleague Featured Poster

we can use spit()

In what library? That's not in the Standard C library for sure.

jonsca 1,059 Quantitative Phrenologist Team Colleague Featured Poster

Not a huge deal. It just prevents the OP from learning if they copy.

@PMorphy - I reread what I wrote and it sounded like I was being flippant. I'm sure you've already surmised that it is taken very seriously. I said what I did because I didn't want you to think that you'd be banished for all eternity for it.
(via my legal staff)

jonsca 1,059 Quantitative Phrenologist Team Colleague Featured Poster

What is the error that it is giving you with line 12?

14,18,22,26 - You still don't need to have "return" there.

33 you can dump, but we've already talked about that.

Your else clause is missing a closing bracket, and if you want it to be associated with all of the above if statements, you need to make the middle ones else if.

if( )
{

}
else if()
{

}
else if()
{

}
else
{

}
jonsca 1,059 Quantitative Phrenologist Team Colleague Featured Poster

Keep an array with the names of the months and search that instead of having that if/else block.

jonsca 1,059 Quantitative Phrenologist Team Colleague Featured Poster
string playerName[SIZE];
int score[SIZE];

You also should look up vectors.
The above both incurs an unnecessary limitation on the number of objects you can have and in most cases, it just creates lots of instances that are never actually used.

He/she might be restricted by the assignment to arrays. A lot of instructors still teach it that way.

jonsca 1,059 Quantitative Phrenologist Team Colleague Featured Poster

Right, but you need a stack to do those operations on, not a node. A node has the two things that you have in the struct typedef. I'm not sure where you think the stack is coming from...

jonsca 1,059 Quantitative Phrenologist Team Colleague Featured Poster

What are the messages that the compiler is telling you? Learning how to interpret them is an important skill. Start with the first error. If you can't figure out what it means, post it with the line number.

jonsca 1,059 Quantitative Phrenologist Team Colleague Featured Poster

A "node" doesn't have an empty() method (if it did, you'd have to access it as copyPtr->empty() anyway, because copyPtr is a pointer). Did you mean to pass in stacks instead of nodes?

jonsca 1,059 Quantitative Phrenologist Team Colleague Featured Poster

Your code is close. I had forgotten about DrawImage. You need to put it in a loop:

const int width = 80; //from your own picture
const int height = 80; 
const int multiplier = 5; //how many across and down
Image ^ picture = Image::FromFile("<your path here>");
Bitmap ^bmp = gcnew Bitmap(width*multiplier,height*multiplier);
Graphics ^ g = Graphics::FromImage(bmp);
for (int i = 0;i<height*multiplier;i+=height)
{
    for(int j = 0;j<width*multiplier;j+=width)
    {							
  	g->DrawImage(snake,(float)j,(float)i); //since x is width and y is height
    }
}

I only tested the code with a square image over a square area, so I may have something backwards, but you get the idea.

Then you can set a picturebox to hold bmp (and there should be no problem saving that out to a file, either). One thing to check, add a border to your picturebox for testing purposes. If nothing shows up in the window, you are probably missing the Controls.Add(yourpbox); call after you've set all of the parameters of the picture box.

if I had commands to delete the data from the Image and Bitmap files I opened at the last 5 lines of the function, like I did with the Filestream right above them, it would result in a GDI+ exception of an invalid parameter

That one I'm not sure of. Are you trying to clean up your objects? Unless you are seriously squeezed for resources, let the garbage collector take care of it. In terms of the exceptions, other objects may be relying …

jonsca 1,059 Quantitative Phrenologist Team Colleague Featured Poster

It's not just you, I've been having problems with navigating on the site tonight, too. There's a huge lag.

jonsca 1,059 Quantitative Phrenologist Team Colleague Featured Poster

You can use an iterator for what you are trying to describe (see the second post of http://stackoverflow.com/questions/3221812/sum-of-elements-in-a-stdvector). Or you could use std::accumulate (http://www.cplusplus.com/reference/std/numeric/accumulate/ or the above link)

jonsca 1,059 Quantitative Phrenologist Team Colleague Featured Poster

Is the solution posted here (http://www.daniweb.com/forums/thread90228.html) as a tutorial by narue not the best way as it handles more situations consistently? and i dont see that her code would not be portable?

Anyway many thanks for the feedback

I'm definitely not saying Narue is wrong in that case.

You can certainly flush the stream before you call the cin.get() but a cin.ignore() will get rid of a stray '\n' that could be picked up by your cin.get().

In your code comment, it sounded to me like you wanted to put in a system("pause"); , and that's more what I was trying to dissuade you from.

jonsca 1,059 Quantitative Phrenologist Team Colleague Featured Poster
cin.get(); /* i know this is soo wrong and bad but im just lazy in small test appls to use proper pause code here or to pipe output to a file */

This is actually one of the most portable and safest methods of pausing.

Just wanted to comment on that, I have no insight into a more effective algorithm.

jonsca 1,059 Quantitative Phrenologist Team Colleague Featured Poster

average is not an array. You can't take the i-th member of something that only has one value. What are you trying to accomplish with that line?

jonsca 1,059 Quantitative Phrenologist Team Colleague Featured Poster

It doesn't run for the first turn through. In general, if the condition on a for loop evaluates to false to begin with, the loop is skipped.

In this case, after the for is skipped, x is incremented to 1, the next cycle of the while loop takes place, and that's when you see the '+' signs start printing.

Edit: beaten to the punch on that one

jonsca 1,059 Quantitative Phrenologist Team Colleague Featured Poster

dir is an uninitialized pointer, it's not pointing to any memory, so when you use gets (which you shouldn't regardless) you can enter characters until the cows come home and run right over whatever memory is adjacent to it.

I would set up dir as a char array holding 256 characters and use fgets to read in the directory name. fgets can leave the '\n' in the string, so look for it and replace it with a null terminus '\0'

jonsca 1,059 Quantitative Phrenologist Team Colleague Featured Poster

Because bitwise complement is faster than negating the number (which is done by taking the bitwise complement and adding 1).

For example, let us say that the next element that is larger is 1 (or 0000 0001 binary). The bitwise complement is -2 (1111 1110) and the negation of the number is -1 (1111 1111). They just wanted to save a little time in both converting to and from the result.

Okay. Thanks for clarifying that.

I'll have to refresh my memory on how these steps are translated into CPU cycles.

To find an extra use for the bitwise complement operator?

Is there a committee for the promotion of rarely used operators? lol.

jonsca 1,059 Quantitative Phrenologist Team Colleague Featured Poster

I recently was using the BinarySearch method and I was curious about why the designers had coded the method with the following for a return value:

"Return Value

The zero-based index of item in the sorted List, if item is found; otherwise, a negative number that is the bitwise complement of the index of the next element that is larger than item or, if there is no larger element, the bitwise complement of Count."

(from http://msdn.microsoft.com/en-us/library/w4e7fxsh(v=vs.80).aspx)

I'm not concerned with the magnitude of the return value in my application, just whether it's positive or negative, but why did they not just design it to return the negative of the index of the next element or the negative of Count?

Thanks.

ddanbe commented: Good observation! +8
jonsca 1,059 Quantitative Phrenologist Team Colleague Featured Poster

See WaltP's suggestion in this thread: http://www.daniweb.com/forums/post742441.html#post742441

Try to code it, post what you have, and someone ought to be able to refine it.

As firstPerson said, this can be accomplished through some libraries, but the solutions may not be portable.

jonsca 1,059 Quantitative Phrenologist Team Colleague Featured Poster

most computer languages use C as its interface to the outside world.

Not to steal some of the spotlight from the thread, but I'm confused by what you mean when you say this.

Good points Adak.

see the assembly output of the compiler or interpreter

The OP wasn't specific, but with VB.NET there are a lot more possibilities for this kind of thing. Any of the .NET languages (C# and F# included) are translated into Intermediate Language (MSIL http://msdn.microsoft.com/en-us/library/c5tkafs1(v=vs.71).aspx), which means anything you can do in C# you can do in VB these days. MSIL is not quite assembly, but gets you one step closer to the machine. You can hand tune your MSIL even in VB, I believe.

My 0.02, although you didn't say you were trying to choose between these options, if you were in a position to, I would say instead of VB, learn C# in addition to one or both of C and C++. Most of the skills from C/C++ will transfer (data structures, looping, etc.). That way you'll be able to do .NET without having to worry about VB.

jonsca 1,059 Quantitative Phrenologist Team Colleague Featured Poster

There is a known problem with .eof(). That's what that thread addresses and tells how to avoid it.

Also, the OP is not reading into a class, he/she's reading into strings etc. from a text file. .read() is meant for binary files, IIRC.

jonsca 1,059 Quantitative Phrenologist Team Colleague Featured Poster

Yes, the do/while will work for that. Look for an example of one in your text. If the user hits y/Y, it goes around the loop again and reads in another operation. If not, the program ends right there.

Edit: PMorphy's code has a good example of a do/while, so try to see how it works.

jonsca 1,059 Quantitative Phrenologist Team Colleague Featured Poster

Fair enough jonsca... My first day here. Never been on a C++ blog b4, just got excited :)

Not a huge deal. It just prevents the OP from learning if they copy.

jonsca 1,059 Quantitative Phrenologist Team Colleague Featured Poster

That's a nice effort PMorphy, but you shouldn't give away code (I think that's a bit down the road for the OP anyway).

jonsca 1,059 Quantitative Phrenologist Team Colleague Featured Poster

Just because it works doesn't make it right. A car will work if you push it to the top of a hill and let go of the brake. Doesn't mean it should be done that way.

Get rid of the returns on line 19,23,27,and 31. You don't need them. On 38, main should return 0 (or another error code, but don't worry about that).

Wrap lines 13-37 in a do/while loop, and move lines 59-61 to within that loop. Get rid of the rest of that if statement, etc., leave the return 0, but again, there's no need to return anything to main from the function so it might as well be void (plus you're not using the return values in main anyway).

jonsca 1,059 Quantitative Phrenologist Team Colleague Featured Poster

Yes, the tiling is easier in a picturebox. I think you would be able to tile it on a picturebox (using the ImageLayout::Tile) then take the BackgroundImage as a new image and save it. I've never tried it like that, but it's worth a shot.

jonsca 1,059 Quantitative Phrenologist Team Colleague Featured Poster

Put it this way, on the scale of this program, you're not likely to run out of stack, but on a more substantial project it's more risky. I would guess that it could possibly fail to work if you change compilers or operating systems or any number of things. It's also good to learn good habits early on so you can do it the proper way.

Post the entire source if you need help fitting the parts in somewhere.

jonsca 1,059 Quantitative Phrenologist Team Colleague Featured Poster

If that's the case, then move the prompt code out into main and use it in a do/while loop

do
{
 cout<<"Addition function"<<endl;
 addition();
 cout<<"Enter Y or y to continue"<<endl;
//get the character from the user
 } while(character is Y or y);

(some pseudocode in there, but you get the idea)

jonsca 1,059 Quantitative Phrenologist Team Colleague Featured Poster

Do you want the user to be able to type 5 characters and no more, or is it a case where you can just take the first 5 off of what they type?

jonsca 1,059 Quantitative Phrenologist Team Colleague Featured Poster

You shouldn't call main like that. Basically, you don't go back to the main() that you came from, you start a "new" main when you call it.

I'm confused as to why you are needing to call main anyway if the function is going to return right after that.

Also, since your function isn't really returning anything, it could be void.

jonsca 1,059 Quantitative Phrenologist Team Colleague Featured Poster

Do your friends' computers have the Visual C++ Redistributables? (they come as part of the normal windows updates now, but they may have ignored them or removed them). If you have .dlls that go with your program, you have to make sure the other computer has them too.