• 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.

C++ Help?

Status
Not open for further replies.

Arsenic

Member
Hey guys. I've been following this thread since the start of my semester and was in awe at some of the coding I've read, thinking I'd get to that level at the end of my term (referring to the guys who were in Intro to C++).

But now that the semester is over and finals coming up, I still see some of the coding from the intro guys to be a little more advanced than what I was taught. I go to a community college here in NYC, and obviously im not expecting the best education, but should I be concerned and put in extra outside work into my education or just relax?

We've gotten as far as classes (very simple, didn't use examples in actual programs) and linear sorting, not bubble. In strings we've covered basic functions, and about 8 of them.

Basically i'm just looking for a range as to where I am in comparison to everyone else, if that makes sense (I had a few beers). Ok thanks!
 

RiZ III

Member
Hey guys. I've been following this thread since the start of my semester and was in awe at some of the coding I've read, thinking I'd get to that level at the end of my term (referring to the guys who were in Intro to C++).

But now that the semester is over and finals coming up, I still see some of the coding from the intro guys to be a little more advanced than what I was taught. I go to a community college here in NYC, and obviously im not expecting the best education, but should I be concerned and put in extra outside work into my education or just relax?

We've gotten as far as classes (very simple, didn't use examples in actual programs) and linear sorting, not bubble. In strings we've covered basic functions, and about 8 of them.

Basically i'm just looking for a range as to where I am in comparison to everyone else, if that makes sense (I had a few beers). Ok thanks!

If you want to have a profession in programming, then yea you absolutely should be programming on your own. It's one of the best ways to learn.
 
Hey guys. I've been following this thread since the start of my semester and was in awe at some of the coding I've read, thinking I'd get to that level at the end of my term (referring to the guys who were in Intro to C++).

But now that the semester is over and finals coming up, I still see some of the coding from the intro guys to be a little more advanced than what I was taught. I go to a community college here in NYC, and obviously im not expecting the best education, but should I be concerned and put in extra outside work into my education or just relax?

We've gotten as far as classes (very simple, didn't use examples in actual programs) and linear sorting, not bubble. In strings we've covered basic functions, and about 8 of them.

Basically i'm just looking for a range as to where I am in comparison to everyone else, if that makes sense (I had a few beers). Ok thanks!

Your first class (excuse the pun) really won't take you far at all, unfortunately. Start reading some books on your own, maybe sit down and try to make a simple game.
 
Hey guys. I've been following this thread since the start of my semester and was in awe at some of the coding I've read, thinking I'd get to that level at the end of my term (referring to the guys who were in Intro to C++).

But now that the semester is over and finals coming up, I still see some of the coding from the intro guys to be a little more advanced than what I was taught. I go to a community college here in NYC, and obviously im not expecting the best education, but should I be concerned and put in extra outside work into my education or just relax?

We've gotten as far as classes (very simple, didn't use examples in actual programs) and linear sorting, not bubble. In strings we've covered basic functions, and about 8 of them.

Basically i'm just looking for a range as to where I am in comparison to everyone else, if that makes sense (I had a few beers). Ok thanks!

Every single language that I've used in a non-trivial manner, I learned it on my own time. Before university, I knew more C++ than what it is covered in an intro CS1 level class before I took that class. Looking back I would say that the best programmers I know are those who thinker with things on their own time. What you learn in class is the bare minimum and in some cases, depending where you go to school, less that what you need.

I should also add that the key is to keep programming and solving problems. A natural next step if you are in school is to take a data structures a.k.a. CS2 class. Bear in mind that becoming a competent programmer takes time, but just keep making progress every day.

Read the following: Peter Norvig 10000 hours
 

Arsenic

Member
If you want to have a profession in programming, then yea you absolutely should be programming on your own. It's one of the best ways to learn.

Your first class (excuse the pun) really won't take you far at all, unfortunately. Start reading some books on your own, maybe sit down and try to make a simple game.

I thought about that, but I wasn't sure if I had even background knowledge to go ahead and try to do things on my own yet. I'm taking advanced C++ next semester, and not sure what topics will be covered.
 

TheFatOne

Member
So gaf I just finished the intro course to computer science at my local community college. I want to become a good programmer, but if the next teacher is like my current teacher I'm going to have to learn everything by myself. Can anyone recommend some good books on C++? I want to make sure I really learn C++.
 
I thought about that, but I wasn't sure if I had even background knowledge to go ahead and try to do things on my own yet. I'm taking advanced C++ next semester, and not sure what topics will be covered.

Just get in and do it. When you find yourself thinking "Uhh, how do I make an app that works on Windows?" or "How do I make something draw to the screen?" just ask around, do some internet detective work until you figure it out. The best way to learn is by throwing yourself into the unknown until you figure it out.
 

Arsenic

Member
Every single language that I've used in a non-trivial manner, I learned it on my own time. Before university, I knew more C++ than what it is covered in an intro CS1 level class before I took that class. Looking back I would say that the best programmers I know are those who thinker with things on their own time. What you learn in class is the bare minimum and in some cases, depending where you go to school, less that what you need.

I look at school as a medium that forces assignments on you to practice on. I didn't find it easy to find these online. I guess i'm more worried about my credentials. It'll be a month before I start up again, any where I should look at (books included) to keep me busy until then?
 

Arsenic

Member
Just get in and do it. When you find yourself thinking "Uhh, how do I make an app that works on Windows?" or "How do I make something draw to the screen?" just ask around, do some internet detective work until you figure it out. The best way to learn is by throwing yourself into the unknown until you figure it out.

Okay I'll man up then. lol thanks
 
So gaf I just finished the intro course to computer science at my local community college. I want to become a good programmer, but if the next teacher is like my current teacher I'm going to have to learn everything by myself. Can anyone recommend some good books on C++? I want to make sure I really learn C++.

I look at school as a medium that forces assignments on you to practice on. I didn't find it easy to find these online. I guess i'm more worried about my credentials. It'll be a month before I start up again, any where I should look at (books included) to keep me busy until then?

In case you haven't read it already, grab yourselves a copy of Accelerated C++. It will surprise you that for the first half of the book it will teach you completely new material that you probably didn't cover in class i.e. the C++ Standard Library. Make sure you do the exercises as there are a few that are challenging enough to be projects at any university CS1 course.

Also why don't you try something a little more "fun" like trying to code a simple game like Tic-Tac-Toe on the command window.
 

Arsenic

Member
You can always ask here if you don't even know where to look :) People will generally point you in the right direction, show you what links to read, etc.
Will do. I hope it won't be too annoying though.

In case you haven't read it already, grab yourselves a copy of Accelerated C++. It will surprise you that for the first half of the book it will teach you completely new material that you probably didn't cover in class i.e. the C++ Standard Library. Make sure you do the exercises as there are a few that are challenging enough to be projects at any university CS1 course.

Also why don't you try something a little more "fun" like trying to code a simple game like Tic-Tac-Toe on the command window.
See, I wouldn't even think of how to start to write a program like that. It feels intimidating lol so I just put my tail in between my legs and pray I'd come across something in college. I constantly think this whenever I imagine writing an app for the iOS or android platform, I just feel so far away.

As for that book, I just ordered it :) so thanks a bunch. I've been eyeing this book for a few months but was told to stay away from it until I've gained some background knowledge first.
 

RiZ III

Member
Just get in and do it. When you find yourself thinking "Uhh, how do I make an app that works on Windows?" or "How do I make something draw to the screen?" just ask around, do some internet detective work until you figure it out. The best way to learn is by throwing yourself into the unknown until you figure it out.

This. This is how I learned most of what I know.
 
See, I wouldn't even think of how to start to write a program like that. It feels intimidating lol so I just put my tail in between my legs and pray I'd come across something in college. I constantly think this whenever I imagine writing an app for the iOS or android platform, I just feel so far away.

As for that book, I just ordered it :) so thanks a bunch. I've been eyeing this book for a few months but was told to stay away from it until I've gained some background knowledge first.

Aww come on, think harder. No book will teach you this kind of stuff. You can start by writing a simple app like you're used to for the class you just finished. It looks like this:

Code:
   |   | 
---+---+---
   |   |
---+---+---
   |   |


Enter x coordinate: 1
Enter y coordinate: 2

   |   | 
---+---+---
   |   |
---+---+---
   | X |

Computer moving at (2,2):


   |   | 
---+---+---
   |   |
---+---+---
   | X | O

Enter x coordinate:

Etc etc.

After you finish that, think to yourself "ok, now how can I draw a tic-tac-toe board on a blank window, graphically? Then take it from there.
 
So gaf I just finished the intro course to computer science at my local community college. I want to become a good programmer, but if the next teacher is like my current teacher I'm going to have to learn everything by myself. Can anyone recommend some good books on C++? I want to make sure I really learn C++.

"C++ Primer" has been good for me, and I hear a lot of praise in this thread and elsewhere about Accelerated C++, as mentioned above, planning to grab a copy myself. Also gotten quite a lot out of "C++ for game programmers" - not just gaming concepts but also has good chapters on the STL, memory management, exception handling, templates etc. And been using Bjarne Stroustrup's (c++'s creator) book "The C++ Programming Language" as a reference text.

like everyone else has said, practice practice practice!! And set yourself goals. Earlier this year I decided I wanted to learn to use OpenGL in C++. It seemed like a mother fucker of a mountain at first but six months later I'm pushing polygons like no-one's business and most of the info came from reading tutorials and online references, and just having simple goals one at a time, and working towards them.
 

Arsenic

Member
Aww come on, think harder. No book will teach you this kind of stuff. You can start by writing a simple app like you're used to for the class you just finished. It looks like this:

Code:
   |   | 
---+---+---
   |   |
---+---+---
   |   |


Enter x coordinate: 1
Enter y coordinate: 2

   |   | 
---+---+---
   |   |
---+---+---
   | X |

Computer moving at (2,2):


   |   | 
---+---+---
   |   |
---+---+---
   | X | O

Enter x coordinate:

Etc etc.

After you finish that, think to yourself "ok, now how can I draw a tic-tac-toe board on a blank window, graphically? Then take it from there.

Aw man, why you do this to me right before I'm about to leave. I promise I'll get back to you in a couple of days. You're right though, I must "visualize" what is supposed to happen and then, make it happen. Funny that's the way I was thought Classes and Objects
 

Spoo

Member
Making little games like tic-tac-toe are incredible ways to learn how to program quickly.

One of the things that really solidified C++ I had learned in school was saying "I don't know how to make a game with this" -- doing some reading, finding the SDL, using a tutorial to get me started, and whammo, 900 horrible lines later (realizing after I could've done it in a lot less if I wasn't doing it in one sitting with no clue what was the best way!) I had written Pong with AI so good it consistently kicked my ass 90% of the time.

Probably because it cheated. But nobody needs to know that!

So buck up, Arsenic. Most programmers are only good at their craft because they were so scared they couldn't solve a problem that they solved it so they could sleep at night :D
 

dc`

Neo Member
Gah, I really need to learn C++. I know C, Java, Objective-C, Python, Bash, Perl, and a bit of Prolog and Haskell, but not C++...it's kind of embarrassing. Does anyone have a book or document to recommend for someone who knows OOP already, but not C++?
 

heyf00L

Member
Gah, I really need to learn C++. I know C, Java, Objective-C, Python, Bash, Perl, and a bit of Prolog and Haskell, but not C++...it's kind of embarrassing. Does anyone have a book or document to recommend for someone who knows OOP already, but not C++?

Why? Do you need C++ for something? If you know all those languages well (especially C and Java), then you can pick up C++ in a day. So just learn it when you need it.
C# might be more useful anyway.
 

Slavik81

Member
Gah, I really need to learn C++. I know C, Java, Objective-C, Python, Bash, Perl, and a bit of Prolog and Haskell, but not C++...it's kind of embarrassing. Does anyone have a book or document to recommend for someone who knows OOP already, but not C++?

Aside from basic syntax stuff, you should read Effective C++ by Scott Meyers. It's basically a list of gotchas with the C++ language.
 

Spoo

Member
Gah, I really need to learn C++. I know C, Java, Objective-C, Python, Bash, Perl, and a bit of Prolog and Haskell, but not C++...it's kind of embarrassing. Does anyone have a book or document to recommend for someone who knows OOP already, but not C++?

If you're comfortable with C and already know OOP, don't bother buying a new book or something unless it's one that focuses on STL / Effective "series" of books or something exclusive to the language.

A Google search of "C++ for C programmers" is probably all you need to pick out the pieces you don't know.
 

TTG

Member
In case you haven't read it already, grab yourselves a copy of Accelerated C++. It will surprise you that for the first half of the book it will teach you completely new material that you probably didn't cover in class i.e. the C++ Standard Library. Make sure you do the exercises as there are a few that are challenging enough to be projects at any university CS1 course.

Do you have anything to point to that's just a step or 2 ahead of that?


Basically, I'm on winter break and at this rate, the next time I open up Visual Studio will be at the beginning of the Spring semester. I would like to get some practice in, I feel like I'm in the same situation as the guy who was asking the question... I have no real experience outside of a few programming classes. I've covered the basics of C++ and assembly along with a Data Structures class(which was also in C++). So, I know the principles behind recursion, classes, inheritance, pointers etc. Anyone know of helpful resources or problems to solve, hopefully something affordable.

EDIT: I got the tic tac toe game to work, but it's a mess of if and for loops. Used a 2d array to organize it. For the computer generated moves, it could either block(in all directions) or fill the next empty spot starting from top left.
 

dc`

Neo Member
Why? Do you need C++ for something
C# might be more useful anyway.
Nope, don't currently need it for anything. Just would like to learn it. I've been meaning to get into C# as well.


Jesus, that's an expensive book. Just get Accelerated C++ instead.

Yeah, a lot more than I was hoping to pay. Accelerated C++ looks like it may be what I'm after.

Thanks for the replies
 
Do you have anything to point to that's just a step or 2 ahead of that?


Basically, I'm on winter break and at this rate, the next time I open up Visual Studio will be at the beginning of the Spring semester. I would like to get some practice in, I feel like I'm in the same situation as the guy who was asking the question... I have no real experience outside of a few programming classes. I've covered the basics of C++ and assembly along with a Data Structures class(which was also in C++). So, I know the principles behind recursion, classes, inheritance, pointers etc. Anyone know of helpful resources or problems to solve, hopefully something affordable.

EDIT: I got the tic tac toe game to work, but it's a mess of if and for loops. Used a 2d array to organize it. For the computer generated moves, it could either block(in all directions) or fill the next empty spot starting from top left.

Now maybe start looking into algorithms and data structures. After that maybe a few books on programming challenge problems. Then just create an idea and implement it. Also look at some open source stuff
 

squidyj

Member
It's definitely useful to just try to power through and figure out how to do something but I made a program in high school that quasi-emulated space invaders using vector based graphics, although I had trouble getting the buffer swap working. The problem with it was that anyone who would actually look at my code would go white and make the sign of the cross. I used goto statements like fuggin' candy. GOTO STATEMENTS.

There's learning how to get things done and just simply getting into the act of doing it, and that's great but it's not the only thing.
 

f0rk

Member
I am doing a maths degree and have done introduction classes in Maple and C++. The C++ class was 'Scientific Programming' and was just a retread of stuff I'd done in Maple which wasn't very complicated, just iteratively solving a differential equation with Euler's Method. I'm currently on a job placement with a bank and have got quite good with SAS as well. So basically my knowledge is very much based on mathematical uses for programming and not actually creating a game or an app or whatever.
Does that Accelerated C++ book start from the beginning? Would I be OK coming in from a maths background rather than computer science? Does it include interesting problems?
 

TTG

Member
Now maybe start looking into algorithms and data structures. After that maybe a few books on programming challenge problems. Then just create an idea and implement it. Also look at some open source stuff

I've been looking at linked lists and binary trees all semester, and I got part 2(2-3 trees etc) in the spring. We've also done most of the popular searches, radix search was pretty cool. What's a good resource for programming challenge problems?
 

TTG

Member
I am doing a maths degree and have done introduction classes in Maple and C++. The C++ class was 'Scientific Programming' and was just a retread of stuff I'd done in Maple which wasn't very complicated, just iteratively solving a differential equation with Euler's Method. I'm currently on a job placement with a bank and have got quite good with SAS as well. So basically my knowledge is very much based on mathematical uses for programming and not actually creating a game or an app or whatever.
Does that Accelerated C++ book start from the beginning? Would I be OK coming in from a maths background rather than computer science? Does it include interesting problems?

Just looking at their bullet point list, it covers most of the basics. Who knows what it files under "basic class design", but considering it also goes over inheritance and overloaded operators, it sounds fine. The only thing I see missing at a glance is recursion.

My only concern is the description makes it sound like you'll be relying on the STL a bit. I've been told to avoid that, as a beginner it's better to write stuff from scratch etc. But, someone with more experience can give better advice on this issue.
 
I've been looking at linked lists and binary trees all semester, and I got part 2(2-3 trees etc) in the spring. We've also done most of the popular searches, radix search was pretty cool. What's a good resource for programming challenge problems?

Two books:

Programming Challenges (forgot the author)
Programming Pearls (Jon Bentley)

Keep in mind these challenges are hard. Think of it like a non-mathematical version of project euler.

Speaking of which, its often educational to try your hand at some project euler problems.

Theres no one right way to proceed from where you are, and theres a long road ahead, so figure out which option seems the most interesting at the moment and dive in, and feel free to jump around and do different things
 

Arsenic

Member
In case you haven't read it already, grab yourselves a copy of Accelerated C++. It will surprise you that for the first half of the book it will teach you completely new material that you probably didn't cover in class i.e. the C++ Standard Library. Make sure you do the exercises as there are a few that are challenging enough to be projects at any university CS1 course.

This book arrived for me yesterday. I've skimmed through it real quick and, even though most of the stuff inside I've covered already, it'll be good to go through it all during winter break.

As for the tic tac toe game, I'll work my way around it after finals and work this week! I guess it'll be something fun to explore and learn (I haven't taken a data structures class yet)
 

maharg

idspispopd
Just looking at their bullet point list, it covers most of the basics. Who knows what it files under "basic class design", but considering it also goes over inheritance and overloaded operators, it sounds fine. The only thing I see missing at a glance is recursion.

My only concern is the description makes it sound like you'll be relying on the STL a bit. I've been told to avoid that, as a beginner it's better to write stuff from scratch etc. But, someone with more experience can give better advice on this issue.


FFS NO. I wish this meme would die. Beginners write horrible linked list/hash table/tree classes that the world is better off without. You have to understand how these things are *used* before you can competently write one yourself, and even then unless you have some particular performance constraint driving you to write your own you *should* use proven algorithm implementations.

Accelerated C++ teaches you to use the full C++ language, including its standard library. That's what makes it the absolute best beginner/intermediate C++ book out there, especially in bang for buck/page count. It doesn't dive on page 23 into writing a linked list container you'll never ever want to use in a poor attempt to teach you bad OO practices, and that's a good thing.
 

tokkun

Member
FFS NO. I wish this meme would die. Beginners write horrible linked list/hash table/tree classes that the world is better off without. You have to understand how these things are *used* before you can competently write one yourself, and even then unless you have some particular performance constraint driving you to write your own you *should* use proven algorithm implementations.

I feel like the purpose is actually the reverse of that.

You write those classes as a means of understanding them. Then you go on to use the ones in the standard library. Writing your own implementation makes it easier to understand the fundamental performance tradeoffs of each type of storage.
 

maharg

idspispopd
Ask any newb who learned from a C++ book what the the performance tradeoffs of a hash table vs. a tree map are and they'll stare at you like you just asked them whether martians taste better than cake.

Trying to intermingle algorithms with language is a bad way to teach, and it's not at all necessary. And C++ books almost universally do a bad job of teaching algorithms.

I've worked on far too many codebases where people have written their own stupid broken linked lists, probably almost right out of one of these C++ books, to buy into any argument that what you suggest is the right way around. Fixing problems in these codebases has always been a non-trivial waste of my time.

The algorithmic complexity of the C++ standard containers is well documented and the implementations are largely close to optimal in terms of those specifications. Bad implementations from C++ books tend not to be.
 

SRG01

Member
Ask any newb who learned from a C++ book what the the performance tradeoffs of a hash table vs. a tree map are and they'll stare at you like you just asked them whether martians taste better than cake.

Trying to intermingle algorithms with language is a bad way to teach, and it's not at all necessary. And C++ books almost universally do a bad job of teaching algorithms.

I've worked on far too many codebases where people have written their own stupid broken linked lists, probably almost right out of one of these C++ books, to buy into any argument that what you suggest is the right way around. Fixing problems in these codebases has always been a non-trivial waste of my time.

The algorithmic complexity of the C++ standard containers is well documented and the implementations are largely close to optimal in terms of those specifications. Bad implementations from C++ books tend not to be.

Yeah, I have to agree with this. The beginner/intermediate should have an idea of how they work, but any work they produce are going to be sub-optimal compared to the standard library.

That, and algorithms should be a separate course outside of C++ programming.
 

tokkun

Member
I do not disagree that a beginner will not be able to produce results that will beat the standard library. What I am saying is that by writing your own implementation from scratch, you learn more about the fundamentals of how those structures work.

When I was learning C, I had to implement my own malloc/calloc/free libraries. I'm probably never going to write a more efficient memory allocator, but now I know enough about the inherent challenges and tradeoffs of allocators that I can use them more efficiently.
 

SRG01

Member
I do not disagree that a beginner will not be able to produce results that will beat the standard library. What I am saying is that by writing your own implementation from scratch, you learn more about the fundamentals of how those structures work.

When I was learning C, I had to implement my own malloc/calloc/free libraries. I'm probably never going to write a more efficient memory allocator, but now I know enough about the inherent challenges and tradeoffs of allocators that I can use them more efficiently.

But see, this is exactly why we get poor CS grads out of college/university (just like that other thread). Literacy and methods (on how to do things) should be emphasized first, while hardcore theory should be reserved until much later.

I've been teaching (edit: beginner) C++ and various microcontroller programming for a while now, and from my experience, teaching high-level content at an early level is not conducive to their learning. A student, when faced with a sufficiently difficult problem that they do not fully understand, will simply go through the motions of completing the task and not retain the material.

Learning about memory management is fine, but leave it until they have the background knowledge to fully appreciate it.
 

TTG

Member
Two books:

Programming Challenges (forgot the author)
Programming Pearls (Jon Bentley)

Keep in mind these challenges are hard. Think of it like a non-mathematical version of project euler.

Speaking of which, its often educational to try your hand at some project euler problems.

Theres no one right way to proceed from where you are, and theres a long road ahead, so figure out which option seems the most interesting at the moment and dive in, and feel free to jump around and do different things

Thanks for the info and the advice, I'll definitely be looking into those.


FFS NO. I wish this meme would die. Beginners write horrible linked list/hash table/tree classes that the world is better off without. You have to understand how these things are *used* before you can competently write one yourself, and even then unless you have some particular performance constraint driving you to write your own you *should* use proven algorithm implementations.

Well, I would never think of using the horrible linked lists and binary trees I've written in class, but I do feel I've learned a lot by writing them. Does that make sense?
 

KorrZ

Member
Hey guys.. I just started trying to learn C++, very very beginner. Using the book "C++ without fear, second edition" One of the example programs to introduce the math library is a prime number tester, but I can't for the life of me get it to work. I've spent about an hour on this trying different things. I've got it literally perfectly accurate to what's written in the book example and it just won't work. I've copied the entire thing even down to the comments, can someone please tell me whats wrong here?
#include <iostream>
#include <stdlib.h>
#include <cmath>
6QD6K.png


The error that I keep seeing is:

call of overloaded `sqrt(int&)' is ambiguous
 

Tomat

Wanna hear a good joke? Waste your time helping me! LOL!
Just a random C++ question here, but does anyone know how hard it would to write a simply dos prompt style program that loads up on a PC at a certain time ever day?
 

IceCold

Member
Hey guys.. I just started trying to learn C++, very very beginner. Using the book "C++ without fear, second edition" One of the example programs to introduce the math library is a prime number tester, but I can't for the life of me get it to work. I've spent about an hour on this trying different things. I've got it literally perfectly accurate to what's written in the book example and it just won't work. I've copied the entire thing even down to the comments, can someone please tell me whats wrong here?

6QD6K.png


The error that I keep seeing is:

call of overloaded `sqrt(int&)' is ambiguous

From here: http://stackoverflow.com/questions/6233132/c-compiler-error-ambiguous-call-to-overloaded-function

The problem is that in C++, there are three functions named sqrt - one taking in a float, one taking a double, and one taking a long double. When you try calling

sqrt(aux.size());

The compiler tries to determine which of these functions you want to call. Since aux.size() returns a string::size_type, which is neither a float, double, nor long double, it tries to see if string::size_type is implicitly convertible to any of these three. But since string::size_type is convertible to all three of these types, the compiler marks the call as ambiguous, since it's unclear which of the conversions you want to do.

Just make the type of n a long instead of an int.
 

KorrZ

Member

If I do that I get an error of

invalid operands of types `long double' and `int' to binary `

specifically referring to :
if (n % i == 0)

It doesn't seem to like the %if I change it from an INT. I've tried changing it to long double, long float, float tried changing it and the i variable both away from integers. I'm starting to come to the conclusion that this example program just doesn't work at all.

Here's the pages straight out of the book for reference
1: http://i.imgur.com/9L6K7.png
2: http://i.imgur.com/DdCRQ.png
 

dabig2

Member
Sounds like this is more of a specific IDE quirk. Try just casting your int n as a long or double inside the sqrt function itself.

Code:
int n;
//....rest of code

i = 2;
while (i <= sqrt(long(n)) {
//...rest of code
 

tokkun

Member
If I do that I get an error of

invalid operands of types `long double' and `int' to binary `

specifically referring to :
if (n % i == 0)

It doesn't seem to like the %if I change it from an INT. I've tried changing it to long double, long float, float tried changing it and the i variable both away from integers. I'm starting to come to the conclusion that this example program just doesn't work at all.

Here's the pages straight out of the book for reference
1: http://i.imgur.com/9L6K7.png
2: http://i.imgur.com/DdCRQ.png

Yeah, that's because you can't use the modulo operator on a double.

Try this instead:

int n;
...
...

while(i <= sqrt( (float)n ))
....
 

KorrZ

Member
Sounds like this is more of a specific IDE quirk. Try just casting your int n as a long or double inside the sqrt function itself.

Code:
int n;
//....rest of code

i = 2;
while (i <= sqrt(long(n)) {
//...rest of code

tokkun said:
Yeah, that's because you can't use the modulo operator on a double.

Try this instead:

int n;
...
...

while(i <= sqrt( (float)n ))
....

Oh my god, thank you guys so much! Either one of those work for me. I was literally getting a headache like I'm getting stuck and I'm just barely almost at chapter 3 of this book. I suppose it might just be because of what I'm using (Dev-C++) but the book doesn't mention doing that at all -.-
 

tokkun

Member
The reason that the code failed is that there are three different versions of the function sqrt (overloaded) and none of them takes the type int as a parameter.

The compiler is able to automatically convert int to those other types, but unless you tell it which type you want, it doesn't know whether to convert to float, double, or long double (ambiguous).

Whey you say " (float)n ", that is just telling the compiler "convert this to a float". Once that is done, it knows which version of sqrt to call.
 

KorrZ

Member
The reason that the code failed is that there are three different versions of the function sqrt (overloaded) and none of them takes the type int as a parameter.

The compiler is able to automatically convert int to those other types, but unless you tell it which type you want, it doesn't know whether to convert to float, double, or long double (ambiguous).

Whey you say " (float)n ", that is just telling the compiler "convert this to a float". Once that is done, it knows which version of sqrt to call.

Thank you, I spent quite a bit of time on google before resorting to posting here, and I saw the above (bolded) over and over again, but I could never find an example of how to actually correct it while still functioning with the "divide with a remainder" %
 
Status
Not open for further replies.
Top Bottom