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

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

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