• 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

Kind of stuck. I need to outputs the number of vowels.
#include <iostream>
#include <cmath>
#include <iomanip>

bool isVowel(char bill);
char e;
int number;
using namespace std;

int main()
{
cout << " Enter any phrase or word:" << endl;
cin >> e;

cout << " The number of vowels is " << number;
number = isVowel(e);

return 0;
}
bool isVowel(char bill)
{
if (bill == 'A' || bill == 'a' || bill == 'E' || bill == 'e' || bill == 'i' || bill == 'I' || bill == 'O' || bill == 'o' || bill == 'u' || bill == 'U')
return true;
else
return false;
}

Is something to do with number = isVowel(e);?
 

vypek

Member
Kind of stuck. I need to outputs the number of vowels.


Is something to do with number = isVowel(e);?

Might be better to use the code tags since it makes things easier to read. I haven't used C++ in a while but at first glance it looks like you are outputting "number" before you even calculate it with isVowel.
Also, why cmath and iomanip libraries?
Oh, you also seem to want to return the number of vowels in the string but you don't count them. You are checking to see if the info passed into "e" is one of the vowel characters only.
You are trying to return a number but isVowel is a bool method so it won't return the number of them either.

A very straight forward way to "brute" this is to iterate through the string provided and check each letter to see if its one of the vowels. And if it is, keep a running tabulation of the number.


Someone else can help clarify or catch any mistakes I have made with my post.
 
Yeah I just needed a loop thanks.

Might be better to use the code tags since it makes things easier to read. I haven't used C++ in a while but at first glance it looks like you are outputting "number" before you even calculate it with isVowel.
Also, why cmath and iomanip libraries?
Oh, you also seem to want to return the number of vowels in the string but you don't count them. You are checking to see if the info passed into "e" is one of the vowel characters only.
You are trying to return a number but isVowel is a bool method so it won't return the number of them either.

A very straight forward way to "brute" this is to iterate through the string provided and check each letter to see if its one of the vowels. And if it is, keep a running tabulation of the number.


Someone else can help clarify or catch any mistakes I have made with my post.

I usually put some headers down just in case.
 
Are there laptops that can handle this stuff reliably or do I have to move up to a desktop? I'm a college student so I kinda prefer the mobility of a laptop. Does anyone else ever have to deal with buggy drivers or software? Generally how do you work around issues in your coding environment?

I've had a fair bit of troubles with Nvidia GPUs on laptops, especially over time and especially with 2010-2012 laptops. Which is unfortunate because Nvidia also tend to be the forerunners when it comes to desktop OpenGL features and cutting edge GPU technologies.

For a laptop, I'm strongly considering buying one that only has an Intel integrated chipset, just because the hardware actually seems to be reliable. Even if their GL support tends to be quite a bit behind, and their drivers have their own set of issues.

Unfortunately, I think you might be right on the money there. GPUs on desktops tend to be better ventilated and they aren't kept in such close proximity to extremely critical circuitry. Perhaps if GPUs via Thunderbolt or other forms of fast external connections catch on, the laptop GPU space will be more reliable to work with.

The main benefits to working with a desktop system are that you can build it yourself on the cheap, and GPUs are extremely easy to swap out. It's probably the best way to go if you need the extra GPU horsepower in your work on a regular basis. And you can probably set up some VPN solution or similar to remotely work with it from a more portable device, though that depends on what your school network will let you get away with.

Honestly there aren't many people who write software against the GPU with interfaces like OpenGL, Direct3D, or GPGPU interfaces. Most of these problems end up slipping by, grunted about only by the few who have to slog through them. :)
 

Ambitious

Member
I'm thinking about getting into Google Go. I don't have any particular plans for projects or anything; I just thought it would be a nice thing to have under my belt. Especially as my main language is still Java.

Which book would you recommend? The top results on Amazon are The Go Programming Language and Programming in Go. Neither have particularly many reviews, but the former was written by Brian Kernighan and a Google engineer from their Go team, so that one has to be the best choice.
 

diaspora

Member
I'm 1-2 terms away from a Diploma and I'm no closer to working competency than from when I started. GG. I've learned more watching tutorials on how to make sites/apps than in class where we learn useless algorithms to make diamonds and shit.
 
I'm 1-2 terms away from a Diploma and I'm no closer to working competency than from when I started. GG. I've learned more watching tutorials on how to make sites/apps than in class where we learn useless algorithms to make diamonds and shit.

If you want working competency go to a vocational school. Computer Science is -- as the name suggests -- the *science* of how computers work.
 

Makai

Member
I'm 1-2 terms away from a Diploma and I'm no closer to working competency than from when I started. GG. I've learned more watching tutorials on how to make sites/apps than in class where we learn useless algorithms to make diamonds and shit.
That stuff will prove useful later.
 

diaspora

Member
If you want working competency go to a vocational school. Computer Science is -- as the name suggests -- the *science* of how computers work.
Canadian colleges are the equivalent of US trade/community colleges AFAIK. Computer Science would have earned me a degree, not a diploma.
 

Mr.Mike

Member
If you want working competency go to a vocational school. Computer Science is -- as the name suggests -- the *science* of how computers work.

... I don't like this definition of computer science...

Personally I'd probably define it something like "The (mathematical?) science of algorithms and information". Computers are obviously important but computers exist for computer science, not the other way around.

I'm not an expert really, but it is one of my pet peeves when people think that computer science is "about computers" or "making videogames" or whatever. Not that I doubt you understand what computer science is :p
 
ok, so I'm fucking stumped right now. Never encountered this before so any help is greatly appreciated. C++ btw

I'm trying to set a the dimension of a Cluster (which holds a bunch of vectors) to the dimension of the first vector. This makes it so the Cluster won't have a bunch of different vectors of different sizes/dimensions and checking and stuff.

I'm getting a seg fault 11 whenever I try and set my dimension variable to the value of the first vector in the... well vector storage unit (The project calls for the class to be named Vectors and I'm using vectors to store them, I know, it's kinda confusing at first)

now the issue isn't access because within the Cluster::setDim() method I can do

Code:
cout<<storage.at(0)->getDim(); //This calls the first Vector of the storage and then calls the getDim() method.

This spits out the correct dimension no issue, so I know I'm not fucking up the storage part.

But once I do

Code:
dimension = storage.at(0)->getDim( );

I get a seg fault. I tried to just use a simple constant int to try it out

Code:
dimension  = 1;

Causes seg fault.

I have no idea, it's the same thing I'm doing in the Vector class and the Vector class works fine, zero issues.



here is the relevant code (removed non-relevant stuff

Cluster.h
Code:
class Cluster
{
	int dimension;
	vector<double> centroid;
	vector<Vector *> storage;

public:
	Cluster();

	int getDim();
	void setDim();

};

Cluster.cpp

Code:
int Cluster::getDim()
{
	return dimension;
}

void Cluster::setDim()
{
	int temp;
	temp = storage.at(0)->getDim(); //WORKS
	cout<<storage.at(0)->getDim()<<endl; //WORKS
	dimension = storage.at(0)->getDim(); //SEG FAULT

}

Vector.cpp

Code:
void Vector::setDim()
{
	dimension = inputVect.size();
}

int Vector::getDim()
{
	return dimension;
}

Do I have this right? Cluster and Vector are two different classes, both of which have... exactly the same methods and both of which have a dimension member? Maybe you could post Vector.h

My guess is that `this == nullptr` inside of `Cluster::setDim`. (You probably would have noticed this if you used a debugger, btw)
 
Do I have this right? Cluster and Vector are two different classes, both of which have... exactly the same methods and both of which have a dimension member? Maybe you could post Vector.h

My guess is that `this == nullptr` inside of `Cluster::setDim`. (You probably would have noticed this if you used a debugger, btw)

I think I figured it out. And yea, Vector and Cluster are two different classes, and have similar methods for the set and get methods, but they are seperate and each have seperate dimensions.

Had nothing to do with the method, it has something to with the function that implements the classes. I just created a new object in main and tried everything out, and it worked fine. So I'm gonna have to look at the function and see what's causing the fault.

I have Xcode, I'm using Sublime Text right now, really like the simplicity of it over all the shit that was thrown at me when I coded in Xcode. Although some of the auto writing and debugging in Xcode was pretty useful
 

kingslunk

Member
Do I have this right? Cluster and Vector are two different classes, both of which have... exactly the same methods and both of which have a dimension member? Maybe you could post Vector.h

My guess is that `this == nullptr` inside of `Cluster::setDim`. (You probably would have noticed this if you used a debugger, btw)

I see a lot of post where people would figure things about if they knew how to use a debugger. Maybe I'll write a blog once I finish up my last final about how to use VS debugger and/or gdb/ddd for people who have no experience with them.
 
I see a lot of post where people would figure things about if they knew how to use a debugger. Maybe I'll write a blog once I finish up my last final about how to use VS debugger and/or gdb/ddd for people who have no experience with them.

It honestly pisses me off that they teach you programming without teaching you how to use a debugger. Intro to Computer Science 101 should have actual assignments that invovle using a debugger.

Almost every "help me find the bug in this program" that gets posted in this thread takes about 30 seconds to find with a debugger :(

I think I figured it out. And yea, Vector and Cluster are two different classes, and have similar methods for the set and get methods, but they are seperate and each have seperate dimensions.

Had nothing to do with the method, it has something to with the function that implements the classes. I just created a new object in main and tried everything out, and it worked fine. So I'm gonna have to look at the function and see what's causing the fault.

I have Xcode, I'm using Sublime Text right now, really like the simplicity of it over all the shit that was thrown at me when I coded in Xcode. Although some of the auto writing and debugging in Xcode was pretty useful

Add this line to that function right before the line that segfaults:

Code:
std::cout << "this = 0x" << std::hex(std::cout) << this << std::endl;

I'm betting it prints "this = 0x00000000";
 

TurtleTracks

Neo Member
I've had a fair bit of troubles with Nvidia GPUs on laptops, especially over time and especially with 2010-2012 laptops. Which is unfortunate because Nvidia also tend to be the forerunners when it comes to desktop OpenGL features and cutting edge GPU technologies.

For a laptop, I'm strongly considering buying one that only has an Intel integrated chipset, just because the hardware actually seems to be reliable. Even if their GL support tends to be quite a bit behind, and their drivers have their own set of issues.

...

Haha, I got my laptop in 2012, so maybe it was part of the last batch of terrible ones. Coding to the Intel GPU is probably a lot more stress free, but I need that SPEED, haha, not that too many of my projects have been that power hungry.

I probably will end up with a desktop eventually, but maybe closer to graduation. Or I might get one of those gaming laptops, though I hate their bullk, but I figure NVidia's a lot better about getting their drivers to run smoothly on those.
 

Koren

Member
I'm thinking about getting into Google Go.
I started to learn it, then talked with a Google developper. In less than 3 minutes, he managed to convinced me that the language has huge flaws (and suggested me Scala, which I like now). I don't remember the details anymore, but if even people from Google avoid it...

Do some people here actually use Go?


Also, is there opinions about Nim? I was thinking about trying this one next (I still stick to the idea of learning at least a language each year, even if I don't use it much afterwards)
 
I started to learn it, then talked with a Google developper. In less than 3 minutes, he managed to convinced me that the language has huge flaws (and suggested me Scala, which I like now). I don't remember the details anymore, but if even people from Google avoid it...

Well that's anecdotal evidence if I ever seen one.
 
I started to learn it, then talked with a Google developper. In less than 3 minutes, he managed to convinced me that the language has huge flaws (and suggested me Scala, which I like now). I don't remember the details anymore, but if even people from Google avoid it...

Do some people here actually use Go?

Also, is there opinions about Nim? I was thinking about trying this one next (I still stick to the idea of learning at least a language each year, even if I don't use it much afterwards)

Google is not one single hivemind. I see this on GAF too. Someone creates a thread "OMG you guys were just saying you hate this game, now you like it?" People have different opinions and stuff. ;-)

Go is still used pretty heavily inside Google. Just like Python, C++, and Java. And for each of those languages, you can find people that hate it inside of Google too.

Now, if you want my own personal opinion about Go: Fucking thing sucks, long live C++
 

Koren

Member
Well that's anecdotal evidence if I ever seen one.

Google is not one single hivemind. [...] People have different opinions and stuff. ;-)
Obviously ^_^ I probably haven't expressed my thoughts properly. I haven't implied that it's a universal opinion inside Google, obviously.

I haven't left it because of a single opinion, but because his arguments against the language were really, *really* convincing. I know it's used in Google (and elsewhere). Not as much as I would have expected at first, though.


BTW, I love i86 assembly, I've been explained for decades that it's a nonsense... Go figure, you'll find people enjoying anything ^_^

And for each of those languages, you can find people that hate it inside of Google too.
Indeed, but I while I have my own views on Python, C++, Java, I haven't seen anything that convinced me that any of those plainly sucks (that I don't especially want to use them, yes... I really, really don't like coding in Java). Go is the only language for which I've been convinced that learning it is a waste of time in less than five minutes and twenty lines of code.

That's the reason why I was curious if some people here are actually using it, and why they like it.
 

Zeus7

Member
I have an application that takes the touch coordinates from onTouchEvent and and puts them in to an SQLite Database.

My three relevant classes:
Main class: TouchDetector.
Custom View class: TouchDetectorView.
Database class: TouchDatabaseHandler.

TouchDetectorView captures the touches and stores the x, y values in variables. I need to access the TouchDatabaseHandler class from this view class in order to send them to the database.

Constructor inside TouchDatabaseHandler class where data is sent to database:
Code:
public TouchDatabaseHandler(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
        super(context, DATABASE_NAME, factory, DATABASE_VERSION);
    }

View class constructor.
Code:
public TouchDetectorView(Context context, AttributeSet attrs) {
        super(context, attrs);
        dbHandler = new TouchDatabaseHandler([U][B]this[/B][/U], null, null, 1);
}

What do I replace the this with to make my code work? The tutorial I was following had the dbHandler inside the onCreate but since I am in a View class I do not have on. I apologise if this is very vague, I am a beginner and would appreciate any help and/or feedback.
 

RiZ III

Member
TouchDatabaseHandler constructor takes in a Context object. In the TouchDetectorView function, there is a Context being passed in. So pass that to TouchDatabaseHandler.

dbHandler = new TouchDatabaseHandler(context, null, null, 1);
 

Zeus7

Member
TouchDatabaseHandler constructor takes in a Context object. In the TouchDetectorView function, there is a Context being passed in. So pass that to TouchDatabaseHandler.

dbHandler = new TouchDatabaseHandler(context, null, null, 1);

You are a hero my friend!

Haha, seriously, thank you very much. I can't believe I never thought of trying that!
 

ElTorro

I wanted to dominate the living room. Then I took an ESRAM in the knee.
I started to learn it, then talked with a Google developper. In less than 3 minutes, he managed to convinced me that the language has huge flaws (and suggested me Scala, which I like now). I don't remember the details anymore, but if even people from Google avoid it...

Once you get into Scala you can never go back.

I wish the language and its standard library had a robust implementation on top of LLVM and native APIs to produce native code.
 

kingslunk

Member
Google is not one single hivemind. I see this on GAF too. Someone creates a thread "OMG you guys were just saying you hate this game, now you like it?" People have different opinions and stuff. ;-)

Go is still used pretty heavily inside Google. Just like Python, C++, and Java. And for each of those languages, you can find people that hate it inside of Google too.

Now, if you want my own personal opinion about Go: Fucking thing sucks, long live C++

Can't just pick one language. Depends on what you're doing. I'm not gonna use C++ for server side web dev. I'm not going to write a game engine in perl. A lot of people don't seem to realize language is just a tool to accomplish whatever task youre trying to do.
 

injurai

Banned
Can't just pick one language. Depends on what you're doing. I'm not gonna use C++ for server side web dev. I'm not going to write a game engine in perl. A lot of people don't seem to realize language is just a tool to accomplish whatever task youre trying to do.

However C++ is pretty much THE systems multi-purpose zero-cost abstraction language. Recommending it is very different than recommending other programming languages. Because as you say languages are tools, you can pin yourself into a certain line of work. Like Python, python tends to be used for particular types of tasks. But it also scales pretty atrociously and has horrible run-time. So even in it's domain it's not the best recommended tool. Despite it having nice semantics and it being relatively approachable.

Go is something I don't think I would ever bother picking up unless I had to, and I'd fight to use something else in it's place. Like haskell or rust.

C++ has improved a lot of the past years. I'm not sure if it can hold out against the domain rust is carving. But C++ seems to be cutting into a lot of other modern languages' domains since c++11 and 14.
 
Well, I'm a bit surprised about the opinions on Go. I was under the impression that it had a way better reputation.

As with each language, it depends what you mainly use it for to evaluate if it is good or if it makes sense for you to use. C/C++ guys are getting easily defensive when new languages approach that are touted to be the next big thing to finally replace C/C++.

The reality is though that all the languages can co-exist and one is better suited for certain tasks than the other. The mainstream success, in my opinion however, stands and falls with library support and more so with a standard library. That is why, again in my opinion, Rust has a bit of a lead over Go. Both are the current "poster children" of next-gen programming languages.
 

ElTorro

I wanted to dominate the living room. Then I took an ESRAM in the knee.
In the end, the programming language you (have to) use is determined by factors like existing systems, existing investment into that language, and developer availability. Projects in which can start on a completely clean slate are rare.

And luckily enough, you can learn and have fun with many different languages at the same time; and reuse insights you gained when working with other languages.
 

Two Words

Member
I'm looking for help with object oriented design on making a simple game. I have a recursive function that will supply all of the ways a word can be spelled using the periodic table elements. So if you enter "because", you would get "BeCAuSe" and "BeCaUSe". It works perfectly now, so I have no concern about that part.

I want to make a 2 player game that has them taking turns trying to come up with words that can be spelled with the periodic table. My scoring would be that you get a point for each letter in the word and it is multiplied by how many ways the word can be spelled.

The part I need help with is deciding the object oriented design choices. Here's a rough outline of my classes.

game class:
Contains references to 2 player objects.
Contains logic for determining valid answers and applying score
Contains the dictionary for the game.

player class:
Contains the player's name.
Contains the player's score.

Are there any design choices I should make that are different?
 

Makai

Member
I'm looking for help with object oriented design on making a simple game. I have a recursive function that will supply all of the ways a word can be spelled using the periodic table elements. So if you enter "because", you would get "BeCAuSe" and "BeCaUSe". It works perfectly now, so I have no concern about that part.

I want to make a 2 player game that has them taking turns trying to come up with words that can be spelled with the periodic table. My scoring would be that you get a point for each letter in the word and it is multiplied by how many ways the word can be spelled.

The part I need help with is deciding the object oriented design choices. Here's a rough outline of my classes.

game class:
Contains references to 2 player objects.
Contains logic for determining valid answers and applying score
Contains the dictionary for the game.

player class:
Contains the player's name.
Contains the player's score.

Are there any design choices I should make that are different?
Split it up more.

Player.cs
PlayerController.cs
AnswerValidator.cs
ScoreApplyer.cs

If your class has multiple responsibilities (managing players, validating answers, applying score, holding dictionary), you probably want to divide it up.
 

Makai

Member
Seems like overkill to me. AnswerValidator.cs probably contains like 5 lines of code for this simple of a game, and similarly with ScoreApplyer.cs
No such thing.

I was permanently scarred by enterprise code with literally zero encapsulation at my first job
 

Two Words

Member
Should I make the game class contain the number of rounds? I want the game to allow the user to decide how many rounds will be in the game and if a tie breaking is allowed. I feel like just throwing stuff like that in the game class.
 

Makai

Member
Should I make the game class contain the number of rounds? I want the game to allow the user to decide how many rounds will be in the game and if a tie breaking is allowed. I feel like just throwing stuff like that in the game class.
This is why you should split stuff up. You'll want to implement new features and just dump it into the God class. Tightly-coupled code will be hard to change when you decide you want to take your game in another direction. If you plan on maintaining this project, you want to encapsulate stuff as much as possible. Store totalRounds in TurnSwitcher.cs or something like that.
 
Should I make the game class contain the number of rounds? I want the game to allow the user to decide how many rounds will be in the game and if a tie breaking is allowed. I feel like just throwing stuff like that in the game class.

I would. Honestly if your game were more complex I'd give you a different answer, but over-engineering is just as big of a problem as under-engineering.

YAGNI
 

Two Words

Member
I asked this in the other thread in OT, but it is better suited here. What is the best way to actually store the answers here once out of the recursive function? The function outputs all of the ways to spell a word with the periodic elements. But since it is done recursively, I can't easily return it. I was thinking of making an array that is constantly filled, but then I have to guess the size. I can make it very large in the hopes that it won't get filled, but that seems messy.

The code is in Java.

Code:
public static void elementalWord(String word){
        String table[] = {"H", "He", "Li", "Be", "B", "C", "N", "O", "F", "Ne", "Na", "Mg", "Al", "Si", "P", "S", "Cl", "Ar", "K", "Ca", "Sc", "Ti", "V", "Cr", "Mn", "Fe", "Co", "Ni", "Cu", "Zn", "Ga", "Ge", "As", "Se", "Br", "Kr", "Rb", "Sr", "Y", "Zr", "Nb", "Mo", "Tc", "Ru", "Rh", "Pd", "Ag", "Cd", "In", "Sn", "Sb", "Te", "I", "Xe", "Cs", "Ba", "La", "Ce", "Pr", "Nd", "Pm", "Sm", "Eu", "Gd", "Tb", "Dy", "Ho", "Er", "Tm", "Yb", "Lu", "Hf", "Ta", "W", "Re", "Os", "Ir", "Pt", "Au", "Hg", "Tl", "Pb", "Bi", "Po", "At", "Rn", "Fr", "Ra", "Ac", "Th", "Pa", "U", "Np", "Pu", "Am", "Cm", "Bk", "Cf", "Es", "Fm", "Md", "No", "Lr", "Rf", "Db", "Sg", "Bh", "Hs", "Mt", "Ds", "Rg", "Cn", "Uut", "Uuq", "Uup", "Uuh", "Uus", "Uuo"};
        elementalWord( word, "", table);

	}

	private static void elementalWord(String word, String pWord, String table[]){
		if (pWord.equalsIgnoreCase(word)){
			System.out.println(pWord);
		}
		else{
			for (int i = 0; i < table.length; i++){
				if (word.toLowerCase().startsWith((pWord + table[i]).toLowerCase()))
					elementalWord(word, pWord + table[i], table);
			}
		}	
	}
 

Two Words

Member
Just use an arraylist. The performance difference is going to be negligible.

But does an array list object work the same as an array across a function call? If I have an array passed recursively through the function, it will directly make changes to the array itself. But if I use an array list, I thought it would be creating new array list objects each time it hits a new recursive call.
 
But does an array list object work the same as an array across a function call? If I have an array passed recursively through the function, it will directly make changes to the array itself. But if I use an array list, I thought it would be creating new array list objects each time it hits a new recursive call.

Java objects are passed by reference (purists will want to argue this but it's so subtle I feel it's not worth arguing about). So yea, you can pass your ArrayList into the function, or you can use the solution I suggested in the other thread.
 

Makai

Member
But does an array list object work the same as an array across a function call? If I have an array passed recursively through the function, it will directly make changes to the array itself. But if I use an array list, I thought it would be creating new array list objects each time it hits a new recursive call.
I dunno, but it's still negligible even if it does. This is overengineering. The strings you are mapping are like 1-15 characters long.
 

Two Words

Member
Java objects are passed by reference (purists will want to argue this but it's so subtle I feel it's not worth arguing about). So yea, you can pass your ArrayList into the function, or you can use the solution I suggested in the other thread.

I think making it a class could be helpful as you said in the other thread.
 
Top Bottom