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

Pages: 1-

C++ code help

Name: Colossal McFaggot 2008-02-26 5:02

I heard /prog/ was a terrible place to come for advice on account of "everyone there is a fucking asshole," but I come with an open mind hoping at least one person will help me. 

I just started programming, I have had zero prior experience and have only read a few chapters into the book I'm using (C++ for dummies: complete desk reference)

So I made this VERY primitive text game, I don't know how to use classes and objects yet, so I'm pretty much just using a function within the main to handle commands, which is sloppy, but I honestly don't know jack shit yet. 

#include <cstdlib>
#include <iostream>
#include <string>
using namespace std;
string command(string input)
{
       string crntlocation;
       string output;
       string n1 = "You are in a dark room";
       crntlocation = n1;
       string error = "Huh?";
       if (input == "look") {
                output = crntlocation;
                return output;
       }
       else {
            output = error;
       }    
}
               
int main(int argc, char *argv[])
{
    string output;
    string playername;
    string deathmsg;
    string promptinput;
    string promptoutput;
    bool alive = true;
    cout << "What is your name?" << endl;
    cin >> playername;
    cout << "Hello, " << playername << "!" << endl; 
    while (alive == true){
          cin >> promptinput;
          promptoutput = command(promptinput);
          cout << promptoutput << endl;
    }
    cout << deathmsg << endl;
    system("PAUSE");
    return EXIT_SUCCESS;
}



The problem: When I put in multiple words when prompted by CIN, the words following the first will spill over to the next available CIN line.

How do I get it so that everything typed in prompted by a single CIN line is treated as a whole input, and not multiple inputs broken by spaces?  This would help me out a lot, and I promise I'll stop being a faggot and continue reading before I start asking questions.

Name: Anonymous 2008-02-26 5:11

You haven't used  LISP

Name: Anonymous 2008-02-26 5:26

Thanks for imparting that amazing little morsel of knowledge, I feel much wiser.

Real help would really be appreciated, I want to learn before I take my major, so I'll have more time to learn on my own since I'll already have experience in programming.

Name: Anonymous 2008-02-26 5:36

This would help me out a lot, and I promise I'll stop being a faggot and continue reading before I start asking questions.
    Quaecumque vera doce me

    Until you encounter another problem that requires thinking
and perhaps reading and consulting your reference manual.
What more is to be expected by a simpleton whose mental acumen
is below the de jure par?
A contrario folks who enjoy erudition would be studying.

    Sapienti sat.

--
    ``Alice came to a fork in the road.
`Which road do I take?' she asked.
`Where do you want to go?' responded the Cheshire cat.
`I don't know,' Alice answered.
`Then,' said the cat, `it doesn't matter'.''
    -- Lewis Carroll

Name: Anonymous 2008-02-26 9:22

>>3
Read SICP.

Name: Anonymous 2008-02-26 9:38

       string n1 = "You are in a dark room";

I stopped reading there, faggot.

Name: Anonymous 2008-02-26 9:47

>>1
I heard /prog/ was a terrible place to come for advice on account of "everyone there is a fucking asshole,"
You heard good.
but I come with an open mind hoping at least one person will help me.
Wrong. You seems like a colossal faggot to me.

But yeah: Don't write a text game in Sepples as your first program.

Name: Anonymous 2008-02-26 9:51

dont't use ``>>'', use ``getline''
cppreference.com for details
enjoy your sepples

Name: Anonymous 2008-02-26 10:12

>>8
>> is not getLine

Learn to Haskell please.

Name: Anonymous 2008-02-26 11:57

>>9
getLine is not (read-line)

Learn to Scheme please, and read SICP.

Name: Anonymous 2008-02-26 12:04

>>10
YHBT YHL PALM

Name: Anonymous 2008-02-26 12:43

Haha, this is really bad, my Pascal game is better.

{   This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>;. }

{   Todo list (This source only - see README for project goals overall);
      - Finish improved drawing (movedirection char to store "left", "right", etc.)
      - Add support for walls <-- done }

program roguelikeframework;
uses crt;

var
   x, y, wallx, wally : shortint;
   movekey          : char;
   runloop          : boolean;
   movedirection      : char;
   wall              : array [1..75,1..20] of boolean;

procedure blankwalls;
begin
   for wallx := 1 to 20 do
      for wally := 1 to 75 do
     wall[wallx,wally] := false;
end; { End procedure }

procedure setupwalls;
begin
   { Set the co-ordinates to implement a wall in that space
   wall[10,10] := true; for example }
   wall[10,10] := true;
   for wally := 1 to 20 do
      for wallx := 1 to 75 do
      begin { Begin 001 }
     gotoxy(wallx,wally);
        if wall[wallx,wally] = true then
           write('#');
      end; { End 001 }
end; { setupwalls }

procedure fencein;
begin
   for wally := 1 to 21 do
   begin
      gotoxy(76,wally);
      write('#');
   end;
   for wallx := 1 to 75 do
   begin
      gotoxy(wallx,21);
      write('#');
   end;
   for wally := 1 to 20 do
   begin
      gotoxy(1,wally);
      write('#');
   end;
   for wallx := 1 to 76 do
   begin
      gotoxy(wallx,1);
      write('#');
   end;
end;

procedure drawmove;
begin
   if movedirection = 'u' then begin { Check for direction to enable the efficient overwriting of previous player position - Begin 002 }
      gotoxy(x,y);
      write('@'); { Write new player icon }
      gotoxy(x,y + 1);
      write(' '); { Get rid of the old one }
   end { End 002 }
   else if movedirection = 'd' then begin { Begin 003 }
      gotoxy(x,y);
      write('@');
      gotoxy(x,y - 1);
      write(' ');
   end { End 003 }
   else if movedirection = 'l' then begin { Begin 004 }
      gotoxy(x,y);
      write('@');
      gotoxy(x + 1,y);
      write(' ');
   end { End 004 }
   else if movedirection = 'r' then begin { Begin 005 }
      gotoxy(x,y);
      write('@');
      gotoxy(x - 1,y);
      write(' ');
   end { End 005 }
   else begin { Begin 006 }
      gotoxy(x,y);
      write('@');
   end; { End 006 }
   gotoxy(x,y);
end;

{ Improved drawing -- redraws the character }

procedure moveup;
begin
   if wall[x,y - 1] = false then begin { Check if there's a wall in the way - Begin 007 }
      movedirection := 'u';
      if y > 2 then begin { Begin 008 }
     y := y - 1;
     drawmove;
      end; { End 008 }
      movedirection := ' ';
   end; { End 007 }
end;

procedure movedown;
begin
   if wall[x,y + 1] = false then begin { Begin 009 }
      movedirection := 'd';
      if y < 20 then begin { Begin 010 }
     y := y + 1;
     drawmove;
      end; { End 010 }
      movedirection := ' ';
   end; { End 009 }
end;

procedure moveleft;
begin
   if wall[x - 1,y] = false then begin { Begin 011 }
      movedirection := 'l';
      if x > 2 then begin { Begin 012 }
     x := x - 1;
     drawmove;
      end; { Begin 012 }
      movedirection :=' ';
   end; { Begin 011 }
end;

procedure moveright;
begin
   if wall[x + 1,y] = false then begin
      movedirection := 'r';
      if x < 75 then begin
     x := x + 1;
     drawmove;
      end;
      movedirection := ' ';
   end;
end;

begin
   { Initialise the variables that need it NB: x and y can be set differently according to where in the "map" they need to be }
   x := 5;
   y := 5;
   runloop := true;

   { Set the stage }
   clrscr;
   blankwalls;
   setupwalls;
   fencein;
   drawmove;

   { Main loop }
   while runloop do
   begin
      movekey := readkey;
      case movekey of
    #119 : moveup; { w moves up }
    #97  : moveleft; { a moves from left to right }
    #115 : movedown; { s moves down }
    #100 : moveright; { d moves from right to left }
    #113 : runloop := false; { q quits }
      end; { End case }
   end; { End 'while' loop }
   clrscr;
end.

Name: Anonymous 2008-02-26 12:49

>>12
Haha, this is really bad, my Scheme game is better.

(display '(you are in a dark room))

Name: Anonymous 2008-02-26 12:53

(display '(you are most likely to be eaten by a grue))

Name: Anonymous 2008-02-26 13:01

(if (= (random 10) 5) (display 'niggers))

Haven't used Scheme since I was at MIT, so excuse my grammar.

Name: Anonymous 2008-02-26 13:16

What's the syntax for random numbers in Haskell?

Name: Anonymous 2008-02-26 13:39

>>16
    x <- randomIO;

Name: Anonymous 2009-03-06 11:53


The menu showing It.

Name: Anonymous 2009-09-17 20:35

Lain.

Name: Anonymous 2009-09-17 20:36

Lain.

Name: Anonymous 2009-09-17 20:36

Lain.

Name: Anonymous 2009-09-17 20:37

Lain.

Name: Anonymous 2009-09-17 20:37

Lain.

Name: Anonymous 2009-09-17 20:38

Lain.

Name: Anonymous 2009-09-17 20:39

Lain.

Name: Anonymous 2009-09-17 20:39

Lain.

Name: Anonymous 2009-09-17 20:40

Lain.

Name: Anonymous 2009-09-17 20:40

Lain.

Name: Anonymous 2009-09-17 20:41

Lain.

Name: Anonymous 2009-09-17 20:41

Lain.

Name: Anonymous 2009-09-17 20:42

Lain.

Name: Anonymous 2009-09-17 20:43

Lain.

Name: Anonymous 2009-09-17 20:43

Lain.

Name: Anonymous 2011-02-03 2:39

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