c++ again Created 15 years ago2009-10-18 14:47:12 UTC by Striker Striker

Created 15 years ago2009-10-18 14:47:12 UTC by Striker Striker

Posted 15 years ago2009-10-18 14:47:12 UTC Post #274550
I wonder if you guys can help me with this. I'm a beginner(I only do c++ console programs from time to time for amusement).

I just learned a few things about 1dimensional arrays. But I want to do something with char arrays.
So let's say I have
char word[10];
How can I asign a certain character to any box of the array I want ? Say I want to asign the character "g" to box word[3]. Notice that I'm a beginner and by box I mean the ... well... the box :crowbar: .
User posted image
Don't tell me about cin.getline(word,9);
I know what does that and it doesn't do what I want.
Striker StrikerI forgot to check the oil pressure
Posted 15 years ago2009-10-18 15:01:47 UTC Post #274551
word[3] = 'g';

or

unsigned int index = 3;
word[index] = 'g';

?

I haven't written C or C++ in months...
Oskar Potatis Oskar Potatis🦔
Posted 15 years ago2009-10-18 15:06:28 UTC Post #274552
Thanks ! Altough if I don't asign a character to each of the box, the program will generate random characters for each box. That's odd :\
Striker StrikerI forgot to check the oil pressure
Posted 15 years ago2009-10-18 15:10:33 UTC Post #274553
If you want word to contain the text "Hi", set word[0] to 'H', word[1] to 'i' and word[2] to '\0' (or just 0).
Oskar Potatis Oskar Potatis🦔
Posted 15 years ago2009-10-18 15:27:06 UTC Post #274554
The program will not generate random chars. It will only allocate memory for the array, and if you access an element that has not been initialized you will just get stuff from the memory at that particular location.
Thus if you want to have a clean array, you should use memset.
Posted 15 years ago2009-10-18 16:13:16 UTC Post #274556
What SpaG said or initialize it on definition.
[blue]char[/blue] word[30] = {[m]''[/m]};
That'll set every element in your array to null.
Daubster DaubsterVault Dweller
Posted 15 years ago2009-10-18 16:46:27 UTC Post #274557
There's a problem Daubster. If I try to make an array of letter ( a to z) like this :
char[]={'a','b','c'....,'z'}; , it compiles, but the program crashes :|
Striker StrikerI forgot to check the oil pressure
Posted 15 years ago2009-10-18 16:48:50 UTC Post #274558
try

char x[]={'a','b','c'....,'z',0};
Oskar Potatis Oskar Potatis🦔
Posted 15 years ago2009-10-18 16:50:32 UTC Post #274559
That shouldn't compile Striker.

Your variable needs a name: char alpha[] = {'a','b','c'...'z'};

Edit: Got ninja'd :(
TheGrimReafer TheGrimReaferADMININATOR
Posted 15 years ago2009-10-18 16:57:09 UTC Post #274560
LOL, in my face Striker !
LOLOLOL I'm such a dum, it's char *word="abcdef";

and from that string I can extract whatever character I want.
light bulb over my head

[EDIT] I really have to go now it's late an I have to wake up at 7 am.
Striker StrikerI forgot to check the oil pressure
Posted 15 years ago2009-10-18 17:05:34 UTC Post #274561
.. okay.
Daubster DaubsterVault Dweller
Posted 15 years ago2009-10-18 17:06:23 UTC Post #274562
Hey, it's like a coders convention in here!
Notewell NotewellGIASFELFEBREHBER
Posted 15 years ago2009-10-18 17:13:03 UTC Post #274563
[EDIT] I really have to go now it's late an I have to wake up at 7 am.
We noticed..
Posted 15 years ago2009-10-19 09:56:19 UTC Post #274594
Guys, can a too complex "if" statement alter the mode the program is working ?

I have a very long if statement.
Striker StrikerI forgot to check the oil pressure
Posted 15 years ago2009-10-19 09:58:29 UTC Post #274595
What do you mean by mode? And no. I think you just made a logic mistake.
Posted 15 years ago2009-10-19 10:51:28 UTC Post #274597
Striker, if you have a long if statement, its best that you replace it with the Select Case statement. I don't know the C++ equilevant of the Select Case statement, but it sure does make your code more "readable".
Posted 15 years ago2009-10-19 12:28:03 UTC Post #274598
think you just made a logic mistake
You were right, I made a logic mistake, but in another if statement.

[EDIT] Well, I'm finished ! I made a word inventor !

Link on my blog and download link

Sorry it's in romanian, but all you have to do is type in the number of characters. The optimal number is between 7-10. For each character it takes 900 miliseconds + 500 miliseconds if the third character after the last vowel is a consonant and it has to make a random vowel.
Those words have no meaning, but it's fun :D
Striker StrikerI forgot to check the oil pressure
Posted 15 years ago2009-10-19 13:03:27 UTC Post #274601
I don't know the C++ equilevant of the Select Case statement
It's
[blue]switch[/blue]
Daubster DaubsterVault Dweller
Posted 15 years ago2009-10-19 13:10:34 UTC Post #274602
I don't know the C++ equilevant of the Select Case statement
It's
[blue]switch[/blue]
. Here's the syntax should you need it, Striker.
[blue]switch[/blue] (<variable>) {
   [blue]case[/blue] <value>:
      do stuff
   [blue]break[/blue];

   [blue]case[/blue] <another value>:
      do stuff
   [blue]break[/blue];

   [blue]default[/blue]:
      <works like the else statement in if; if none of the previous conditions are met, it does whatever you specify here>
   [blue]break[/blue];
}
Daubster DaubsterVault Dweller
Posted 15 years ago2009-10-19 13:22:35 UTC Post #274603
Ah yes, Switch, i remember now. C# also uses Switch.
Posted 15 years ago2009-10-19 13:52:38 UTC Post #274605
I don't know if c++/C# work this way too, but I remember seeing this structure abomination in VB6 at my last job:

<long SQL statement>
Select Case True
[blue]Case[/blue] name="John Smith"
    <do stuff>
[blue]Case[/blue] name="Frank Jones"
    <do stuff>
[blue]Case[/blue] name="pjackson"
    <do stuff>
[blue]Case[/blue] account="2545456"
    <do stuff>
[blue]Case Else[/blue]
    <do stuff>
End Select

The bad part is, I did learn something from it.
Posted 15 years ago2009-10-19 14:31:42 UTC Post #274607
cough , what about my program ?
Striker StrikerI forgot to check the oil pressure
Posted 15 years ago2009-10-19 23:40:13 UTC Post #274656
Wasn't your question answered already?
Posted 15 years ago2009-10-20 00:05:19 UTC Post #274659
I think he wants us to check out his program.
Jessie JessieTrans Rights <3
Posted 15 years ago2009-10-20 00:17:25 UTC Post #274662
Oh, right. 900 ms?? Isn't that a bit too slow? That's pretty bad for long words.

I tend to be skeptical about running random .exes downloaded from teh internetz. Especially when I don't know Romanian :P
Posted 15 years ago2009-10-20 13:03:17 UTC Post #274671
You should be afraid if someone tells you to download a russian exe. :P

[EDIT] I optimized the program and made it generate words instantly. Also, you can now generate a lot of words at once, randomly. I also made a loop instead of a recursive main() function :D.

http://sjackm.wordpress.com/2009/10/19/the-program-that-makes-words-c/ and acces the Download-Beta3 link :P.
Striker StrikerI forgot to check the oil pressure
Posted 15 years ago2009-10-20 13:13:13 UTC Post #274680
Maybe you should try making a windows form application of it.
Posted 15 years ago2009-10-20 13:50:10 UTC Post #274681
Lol, that sounds impossible for me. I think I'd need a ton of code just to make a window ! What about buttons etc...
Isn't there a simplified method to do this ? Like create the buttons in a graphic style and assign different lines of code to each of them ?
Striker StrikerI forgot to check the oil pressure
Posted 15 years ago2009-10-20 14:26:52 UTC Post #274683
No, just port it over to .Net. Or just make it a .dll and make the UI in .Net.
Posted 15 years ago2009-10-20 14:35:42 UTC Post #274686
.Net sucks, it's platform specific (Windows only). Avoid it. I would look into GTK+ if I were you.
Oskar Potatis Oskar Potatis🦔
Posted 15 years ago2009-10-20 15:19:16 UTC Post #274687
Lol, that sounds impossible for me. I think I'd need a ton of code just to make a window ! What about buttons etc...
You think wrong.
Creating a form (window) with buttons, labels, checkboxes, radiobuttons and whatnot, doesn't require a single line of code. The only code that you'll write are the actions and events when you interect with those controls.

For example, to show a messagebox to the user when you click a button:

MsgBox("Hello!")

This is the case with Visual Basic .Net and C#, don't know about C++ though.
Posted 15 years ago2009-10-20 15:33:56 UTC Post #274689
I also made a loop instead of a recursive main() function
Recursive main? What the hell?
Posted 15 years ago2009-10-20 15:47:22 UTC Post #274690
Yeah, it's no wonder it took so long.
.Net sucks, it's platform specific (Windows only). Avoid it. I would look into GTK+ if I were you.
Mono?
Posted 15 years ago2009-10-20 15:50:39 UTC Post #274691
Port it ? Wtf people, I already have a universal programming language ( c was created for unix systems ...) and it's a wonderful programming interface. Why would I bother doing such a thing ?
Striker StrikerI forgot to check the oil pressure
Posted 15 years ago2009-10-20 19:13:47 UTC Post #274697
C was created for Unix, C++ was not. C++ is a rubbish language, if I were you I'd start learning plain C or upgrade to a managed language such as C# or maybe a scripting language. And don't listen to potatis, .NET is great and nobody gives a rat's ass about *nix.
Penguinboy PenguinboyHaha, I died again!
Posted 15 years ago2009-10-20 19:56:36 UTC Post #274698
C++ is out of my league, so is Actionscript3. However I'm having a fucking awesome time with Actionscript2!

Made this.
Rimrook RimrookSince 2003
Posted 15 years ago2009-10-21 00:26:25 UTC Post #274704
For some reason, only now I notice the extension of that is .swf. I first read .pdf and I was pondering how utterly complicated was what Adobe was doing to the pdf format.
Posted 15 years ago2009-10-21 02:07:49 UTC Post #274706
Personality isn't supposed to go over 25 points total though, doesn't it?
Just puttin that out there.
Jessie JessieTrans Rights <3
Posted 15 years ago2009-10-21 13:42:33 UTC Post #274720
C++ is a rubbish language, if I were you I'd start learning plain C or upgrade to a managed language such as C# or maybe a scripting language. And don't listen to potatis, .NET is great and nobody gives a rat's ass about *nix.
That's too rough :\ . Or maybe you're sarcastic :nya:
Also, there is sourceforge, a lot of servers are linux based, there are lots of linux distros... I don't think unix systems are that shitty :\ .
Striker StrikerI forgot to check the oil pressure
Posted 15 years ago2009-10-21 15:15:33 UTC Post #274722
If you want to give Windows Forms Applications a go, try C#. C# looks a lot like C++. You can also create Windows Forms Applications with Visual Basic .NET, this language is much easier to understand.
I can help you getting started with Visual Basic .NET.
Posted 15 years ago2009-10-21 18:57:05 UTC Post #274725
Or, you could try something like GTK+ or Qt, not have to change language and not limit yourself to one operating system. I have to agree with Penguinboy though, C++ is a pretty shitty language.
nobody gives a rat's ass about *nix.
I thought the TWHL server was running FreeBSD.
Oskar Potatis Oskar Potatis🦔
Posted 15 years ago2009-10-21 20:12:33 UTC Post #274726
When it comes to a choice in language, everything is relative. I want to direct this to all of you that are half my age and thinking about languages you may want to learn.

If you are making programs for your personal use, use the language you are most comfortable with.

No programming language is rubbish or shitty, only the programmer. No matter what language you begin learning, if you take a class, the first thing a teacher should tell you is "user = looser. If there is a way to break your program, the user will find it. And a program is only as good as the programmer."

I would encourage any aspiring programmer to learn several languages and not dismiss a single one.

For example, C++ began as an extension to C. And C# is, as one professor at my old University called it, "C on steroids". For C#, you can include C++ and C program code if you know how. With C++ you can include C as well.

COBOL (COmmon Business Oriented Language) was the industry standard for decades and many businesses still rely on legacy code from this language. C++ replaced it and is now being replaced by C# and .NET. But businesses are still heavily reliant on C++, which means learning it is a good idea if you want a job.

Here's a kicker... many modern languages still rely on Assembly Language and other such low-level languages and can be incorporated into C, CoBOL, C++, C# and so on. It is one of the most basic and closest to machine instruction languages you can learn.

Visual Basic is almost completely useless... but some businesses rely on it for their program interfaces. It can also be fun to play around with and make little programs for fun.

Java, J++, and other languages and variations of languages all have their parts in the world. And a great many languages tie into other languages to get their work done.

So, as I said earlier, if you make a program for personal use... use whatever language you like. If you are trying to build a resume or find work... learn more then one and fill out your tool chest so you can do most anything an employer would like.

Many military applications still use FORTRAN (Formula Translator). In fact... its actually a fairly important language in this modern age. But it can be painful to write code in.

In the end, my point is that terms like "rubbish" and "shitty" denote an opinion of the ease of use, compatibility, and a wealth of other variables of that specific person's opinion. Don't base your programming language decision on what someone else tells you is "rubbish".

For example, here's a quote from the GTK+ website.
GTK+ it is written in C, but has bindings to many other popular programming languages such as C++, Python and C# among others.
In the end, all these languages more or less tie together in one way or another. I would suggest that, for personal use, you test out various suggestions these guys have made. Like potatis suggested GTK+ specifically because of its relationships to C, C++, C# and so on and I would assume he likes that particular tool. You might not like it, so keep all your options open.

Find one that you like specifically and try to make yourself a more well rounded programmer.

As for the original topic of this post. Striker, I tried your beta 3 and, I think it could use some work but its a neat idea. Keep it up. Developing a new program will take several attempts and several revisions. I'd say you're off to a good start.
Posted 15 years ago2009-10-22 10:56:26 UTC Post #274733
Thank you for your opinions. Yes it was long, but I read it all. I think I;m going to stay on C++ for now, until I get sufficiently skilled to learn other programming languages. My program will be fun to develop as I learn new things in c++.
Striker StrikerI forgot to check the oil pressure
Posted 15 years ago2009-10-22 13:54:54 UTC Post #274734
Visual Basic is almost completely useless
If you're talking about the old Visual Basic (6 and lower), then yes, but Visual Basic .NET is great!
Posted 15 years ago2009-10-22 18:46:36 UTC Post #274738
To respond to the original question: in C++, you'd use std::string for things like this. Makes programming in it a little easier.

Also, string literals (the "abcdef" in your 'char *word="abcdef"') are immutable: so you can't use that pointer to modify the string it points to.

As for languages, I used to be a big fan of C++, but now that I've used it for a few years, and now that I've gotten to know a few other languages... Let's just say I'm not that fond of it anymore. I'm using Python a lot more these days. It allows me to focus on what I actually want to build, rather than making me wrestle with tedious language details first. Ah well, each language has it's strong and weak points. In the end they're tools - pick the one that's best for the given job.
Posted 15 years ago2009-10-22 21:46:29 UTC Post #274740
If you're talking about the old Visual Basic (6 and lower)
Yeah, VB 6 was specifically the version I was thinking about.
My program will be fun to develop as I learn new things in c++.
Good to hear. Have fun and learn something new. Bettering one's self is always a good thing.
Posted 14 years ago2009-11-09 20:56:02 UTC Post #275366
Just found the thing again. Entered 8 and the last word I got was:
azagepow?&#9516;&#9488;w'?@
That's not 8 chars long. Nor easily pronounceable.

*End of alpha test. Product rejected*

Edit: I suspect the TWHL server doesn't support the chars I got.
You must be logged in to post a response.