Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-4041-

C++ halp =/

Name: Anonymous 2007-06-12 10:52 ID:NALSuzi+

HAI
I'm learning some C++ and last night wrote this:

#include <iostream>
using namespace std;

int main()
{
    char gender[30];
    char fname[50];
    char sname[50];
    char hate[30];
    char wep[30];
    char hobby[50];
   
    cout<<"Please do not use spaces in your answer, it will mess up the program due to bad writing =/ "<<endl;
    cout<<endl;
    /* Yes I'm rubbish ok? */
    cout<<"Please enter either he/she, according to your gender: ";
    cin>> gender;
    cin.ignore();
    cout<<"And what is your first name?: ";
    cin>> fname;
    cout<<"Well hello there, "<< fname <<"! What is your surname?: ";
    cin>> sname;
    cin.ignore();
    cout<<"Thanks. So far you have told me that you are a "<< gender <<", and your name is "<< fname <<" "<< sname <<"."<<endl;
    cout<<endl;
    cout<<"What is the one thing you hate most?: ";
    cin>> hate;
    cin.ignore();
    cout<<"What is your weapon of choice?: ";
    cin>> wep;
    cin.ignore();
    cout<<"What is your favourite hobby/pastime?: ";
    cin>> hobby;
    cin.ignore();
    cout<<endl;
    cout<<"Ok, let's begin our story..."<<endl;
    cout<<endl;
    cout<<endl;
    cout<<""<< fname <<" "<< sname <<" thought "<< gender <<"'d take a break from "<< hobby <<"."<<endl;
    cout<<""<< gender <<" decided to rid the world of all traces of "<< hate <<", once and for all."<<endl;
    cout<<"So, brandishing a "<< wep <<", "<< gender <<" set off to seek out and destroy "<< hate <<"..."<<endl;
    cout<<"But "<< gender <<" died on the mission and "<< hate <<" remained forever more."<<endl;
    cout<<"Poor "<< fname <<", "<< gender <<" should have stuck with "<< hobby <<"."<<endl;
    cin.get();
   
    return 0;
}

As you can see, I'm a total n00b with only a basic knowledge of int, float and char. I was wondering which type i could use so that you choose one of 2 options, and most importantly, how I could allow spaces to be used in input without messing up how the program remembers it. Tried google but failed.

Name: Anonymous 2007-06-12 11:44 ID:Heaven

>>1
char gender[30];

FAIL

Name: Anonymous 2007-06-12 11:46 ID:NALSuzi+

>>2
thanks I'll try that

T_T I only started recently

Name: Anonymous 2007-06-12 11:50 ID:RChT0nzZ

>>1
enum {"Male", "Female", "Futanari"} gender;

Name: Anonymous 2007-06-12 11:50 ID:NALSuzi+

>>2
oh I see, quoting my thread and calling me a failure without actually showing how I could do better.
k.

Name: Anonymous 2007-06-12 11:51 ID:NALSuzi+

>>4
lol thanks

Name: Anonymous 2007-06-12 11:53 ID:NALSuzi+

So how do I allow spaces without them wrecking the program, expertz?

Name: Anonymous 2007-06-12 11:56 ID:rO+ytXoJ

Try doing getline with some options so spaces dont' wreck the program.

Use std::string instead of char arrays, they will resize automagically.

Name: Anonymous 2007-06-12 11:58 ID:NALSuzi+

>>8
I'll have to get some practise on strings then, thanks.
>>4

#include <iostream>
using namespace std;
int main()
{
    enum {"Male", "Female"} gender;
    cout<<"What is your gender?";
    cin>> gender;
    cin.ignore();
    cout<<"You are "<< gender <<"."<<endl;
    cin.get();
    return 0;
}

is giving errors, I couldn't work out what I'm doing wrong =/

Name: Anonymous 2007-06-12 12:00 ID:wV2/oU5j

Just switch to Java.

Name: Anonymous 2007-06-12 12:02 ID:NALSuzi+

>>10
lol maybe later

Name: Anonymous 2007-06-12 12:08 ID:Heaven

>>9
C++ is too hard for you. Maybe try becoming an expert HTML programmer.

Name: Anonymous 2007-06-12 12:12 ID:NALSuzi+

>>12
I know html

Name: Anonymous 2007-06-12 12:17 ID:NALSuzi+

Ah I see from the error log I need to look up init-declarors, any help? I'll google it anyway.

"expected init-declarator before "enum"
 expected `,' or `;' before "enum""

Name: Anonymous 2007-06-12 12:20 ID:NALSuzi+

*declarator, even

Name: Anonymous 2007-06-12 12:22 ID:rO+ytXoJ

What exactly are you trying to do/ you can't have strings in an enum. don't let these people discourage you, c++ is easy really.

Name: Anonymous 2007-06-12 12:25 ID:NALSuzi+

>>16
Thanks.
I was trying to make a multiple choice thing, such as hit M/F (male/female), or only allow male/female or he/she to be entered.
I'm also gonna have to work out strings, so when someone inputs with space characters the program remembers things correctly.

Name: Anonymous 2007-06-12 12:59 ID:NALSuzi+

Ok, I'm finding a lot of info on init-declarators, but can't work out which things I actually use.
Anyone know a site which might explain this better?

Name: Anonymous 2007-06-12 13:02 ID:NALSuzi+

>>18
meh, i'll just forget about that for now actually, considering what >>16 said. I'll learn up on strings instead.

Name: Anonymous 2007-06-12 13:04 ID:EQ/rLPL/

Blargh. Just read SICP (or watch the videos if you're lazy). Then go back to C++ if you still think it is a good idea (you won't).

Name: Anonymous 2007-06-12 13:14 ID:NALSuzi+

>>20
I want to learn C++.
I want to want to learn C++.

Name: Anonymous 2007-06-12 13:14 ID:Heaven

[code]#include <iostream>
using namespace std;
int main()
{
    char m[]="male";
    char f[]="female";
    char o[]="dunno lol";
    char* gender=o;
    std::string g;
    cout<<"What is your gender?";
    cin>> g;
    if(strcasestr(g.c_str(),"male")&&!strcasestr(g.c_str(),"female"))gender=m;
    if(strcasestr(g.c_str(),"female")&&!strcasestr(g.c_str(),"male"))gender=f;
    cin.ignore();
    cout<<"You are "<< gender <<"."<<endl;
    cin.get();
    return 0;
}[code]

Name: Anonymous 2007-06-12 13:21 ID:EQ/rLPL/

>>21
Okay. After ten years, at least remember Anonymous told you it was a very bad idea and see that he was right.

Name: Anonymous 2007-06-12 13:22 ID:NALSuzi+

>>22
That doesn't seem to work, but thanks for pointing me in the right driection.
I'll go and register on some c++ forum or other, no offence guys but all this saging, and general trying to put me off is starting to get to me.

Name: Anonymous 2007-06-12 13:23 ID:NALSuzi+

>>23
k

Name: Anonymous 2007-06-12 13:26 ID:Heaven

>>24
http://4-ch.net/wiki/#UsingSage
Sage is not a insult.
Contrary to popular belief, sage should never be considered as a rude gesture or insult. In fact, someone writing insults or trolls to the thread and posting with sage are doing a good thing, since their useless post won't be brought to the attention of the rest of the board. The idea of sage being an insult came from imageboards, where filling up a thread with enough posts would get it removed. However, threads on channel4 take one thousand posts before they are closed, so the effect doesn't work here.

Name: Anonymous 2007-06-12 13:29 ID:Heaven

>>24
I'll go and register on some c++ forum or other

I think that is a good idea in your case. You're greatly hurting your programming, and if you really want to do that, doing it with other people living the same mistake will probably be a lot less painful.

no offence guys but all this saging, and general trying to put me off is starting to get to me.

Saging is not an insult. People telling you to ditch C++ are seriously trying to help you. I made the mistake of blindly using C++ against all odds for over five years, and would not want anyone to repeat it, but I think no one will speak sense into your mind.

Name: Anonymous 2007-06-12 13:35 ID:NALSuzi+

>>27
So what the heck's wrong with C++?

Name: Anonymous 2007-06-12 13:38 ID:Heaven

>>28
everything that isn't c, and about half of what is.

Name: Anonymous 2007-06-12 13:41 ID:Heaven

>>28
Almost everything. The whole premise -- ``let's completely misunderstand OO and bolt on a horrible caricature of an OO system onto the macro assembler known as C for no obvious reason, and spice up the mix by bringing in non-features that serve no other purpose but to make the language more complex'' is ridiculous, to begin with.

All SICP trolling aside, I seriously suggest you forget everything you think you know about programming and at least watch through all the SICP video lectures once before continuing to hurt your learning with C++.

Name: Anonymous 2007-06-12 13:57 ID:NALSuzi+

=/

Name: Anonymous 2007-06-12 15:16 ID:Heaven

#include <iostream>
#include <string>

using namespace std;

int main() {
    string name;
    cout << "What is your name?\n";
    getline(cin, name);
    cout << "y helo thar " << name << "\n";
}

Name: Anonymous 2007-06-12 16:28 ID:xZgyyQaD

Seems to be more opposition to C++ as time goes by. Maybe it's getting old?

Personally I have mixed feelings about C++.

Sometimes I like how rigid it is, you really have to plan the structure of a program, the structures and the relationships and when you have your program finished it will be faster than most other langauges.

On the downside development using C++ is S L O W compared to other languages (I mean in the order of 10-100 times longer development times compared to using say Python).

So what's more important development time, or running time and  ego? ;)

(I'm >>16, I believe C++ has its uses but they are limited. Even game programmers are starting to use other languages (such as C#, could it be a dying language in a few years, or will the updates save it? (even though they have some nice changes I doubt it).

Name: Anonymous 2007-06-12 16:34 ID:Heaven

>>33
That's insane. Haskell gives you all the rigidness without the asshattery and failure of C++, together with an extremely high level language and very nice performance. The only actual need for C++ exists in 1337 gaem progm4mm4erz' small minds.

Name: Anonymous 2007-06-12 17:34 ID:NALSuzi+

>>32
#include <iostream>
#include <string>

using namespace std;

int main() {
    string name;
    cout << "What is your name?\n";
    getline(cin, name);
    cout << "y helo thar " << name << "\n";
    cin.get();
}
worked great thanks!

>>34
lol I'm only learning it for a hobby =/

Name: Anonymous 2007-06-12 17:47 ID:GuFgazvk

>>34

C++ is much easier to develop in than Haskell

Name: Anonymous 2007-06-12 18:13 ID:Heaven

>>36
That because you don't have to use your brain when programming in C++.

Name: Anonymous 2007-06-12 18:36 ID:NALSuzi+

>>37
=/

Name: Anonymous 2007-06-12 18:42 ID:zFwWLW41

It is easier to get some C++ code to compile.  The only problem is it won't do what you wanted (likely crash).

B&D type systems like Haskell (and Ada) make it much harder to get the code to compile, but when you do finally manage it, it almost always works.

Which do you prefer?  Grovelling through stack traces, or fixing bizarre (unless you've a PhD. in type theory) type checker errors.

Name: Anonymous 2007-06-12 18:45 ID:GuFgazvk

>>37

Ha ha funny one

Name: Anonymous 2007-06-12 18:56 ID:liVtOiv+

>>39
My Python code almost always works. It's not a matter of having a system that gets anal over things. It's a matter of proper design. Anal languages somewhat force you to design properly. QUACK MOTHERFUCKER languages don't require you to waste more time than strictly necessary (your classes still have type, but you don't restrict how things work as long as they work (exist in a dictionary)), and you don't get annoyed because it doesn't compile. Yes, you can fail it. Yet if you have the same skill you need for anal typing, you produce the same sensible code and get the same reliable results, without the hassle and without needless definitions.

Name: Anonymous 2007-06-12 18:59 ID:2ChRYdt0

>>36
But Haskell is faster to develop in, when you finally DO get it

Name: Anonymous 2007-06-12 19:19 ID:NALSuzi+

Ok here's my re-write; thanks for the help guyz.

#include <iostream>
#include <string>
using namespace std;
int main()
{
    string gender, fname, sname, hate, wep, hobby;
    cout<<"Hello!"<<endl;
    cout<<endl;
    cout<<"Please enter either he/she, according to your gender: ";
    getline(cin, gender);
    cout<<endl;
    cout<<"What is your first name?: ";
    getline(cin, fname);
    cout<<"Thanks, "<< fname <<"!";
    cout<<"What is your surname?: ";
    getline(cin, sname);
    cout<<endl;
    cout<<"Ok. So far you have told me that your name is "<< fname <<" "<< sname <<", and that you are a "<< gender <<".";
    cout<<endl;
    cout<<endl;
    cout<<"What is the one thing you hate most?: ";
    getline(cin, hate);
    cout<<"What is your weapon of choice?: ";
    getline(cin, wep);
    cout<<"What is your favourite hobby?: ";
    getline(cin, hobby);
    cout<<"Time to write the story!"<<endl;
    cin.get();
    cout<<endl;
    cout<<endl;
    cout<<""<< fname <<" "<< sname <<" thought "<< gender <<"'d take a break from "<< hobby <<"."<<endl;
    cout<<""<< gender <<" decided to rid the world of all traces of "<< hate <<", once & for all."<<endl;
    cout<<"So, brandishing a "<< wep <<", "<< gender <<" set off to seek out & destory "<< hate <<"..."<<endl;
    cout<<"But "<< gender <<" died on the mission, and "<< hate <<" remained forever more"<<endl;
    cout<<"Poor "<< fname <<", "<< gender <<" should have stuck with "<< hobby <<"."<<endl;
    cout<<endl;
    cout<<"The End"<<endl;
    cin.get();
    return 0;
}

If there's a way I can improve this I'd be glad to hear it.
in b4 more flaming...

Name: Anonymous 2007-06-12 22:58 ID:PmfpJL6j

I find it easy to spot the stack errors in smalltalk.
>without the hassle and without needless definitions.
needless definitions?
do you know TYPE INFERENCE?

(let me state here that I prefer dynamic typing)

Name: Anonymous 2009-01-14 14:19

FIOC

Name: Anonymous 2011-02-04 18:14

Don't change these.
Name: Email:
Entire Thread Thread List