C/C++ Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 



Go Back   Dev Articles Community ForumsProgrammingC/C++ Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old November 6th, 2008, 06:47 PM
curtcoll08 curtcoll08 is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Nov 2008
Posts: 22 curtcoll08 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 h 56 m 52 sec
Reputation Power: 0
Urgent help with loops

right what im trying to do is make a grid which is 40x40 i know how thats done ive made it 10x10 atm but what im trying to do is that of when it loads ive got it so u have to type in the char then it displays the grid but i want to be able to set the char to [] also i want to be able to make it so that if i enter 8 on the numpad or 4 or 6 or 2 it will the put a x in the box in which direction i press so then itl display the grid with the new cords where x is, is it possible somone could help me with this im learning how to do c++ just cant seem to get around how to put it togetha if at all possible someone has msn add me curtcooll at hotmail .com just so that somone could lend me a hand

#include <iostream>
#include <string>

int main ()
{
std::string pause;
int row = 10, colums = 10;
char thechar;

std::cout << "enter letter ";
std::cin >> thechar;

for (int x = 0; x < row; x++)
{
for (int y = 0; y < colums; y++)

std::cout << thechar;

std::cout << "\n";
}

std::cin >> pause;
return 0;
}

Reply With Quote
  #2  
Old November 7th, 2008, 12:59 AM
jasonlang jasonlang is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Oct 2008
Posts: 164 jasonlang User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 Days 3 h 13 m 21 sec
Reputation Power: 10
one thing you can do is store a position of a pointer x and y value

eg

int pointerX, pointerY;

your drawing code would change to :

Code:
for (int x = 0; x < row; x++)
{
    for (int y = 0; y < columns; y++)
    {
        if(x == pointerX && y == pointerY)
            std::cout << "[x]";
        else
             std::cout << "[] ";
    }

    std::cout << "\n";
}


reading the keys is a another matter, with many techniques, depending on your platform, i suggest you read tutorials on c/c++ keyboard input. Methods vary depending on if you want realtime or wait for key to be pressed. for realtime changes you want "asynchronous" key reading.

the basic outline is :

start loop

draw grid

check keys

go back to start loop

Reply With Quote
  #3  
Old November 7th, 2008, 04:58 AM
curtcoll08 curtcoll08 is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Nov 2008
Posts: 22 curtcoll08 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 h 56 m 52 sec
Reputation Power: 0
its just simple all i want is the very simple but im just learning all this in the console i wan this to show up with out inputting any char then using the numpad the same as entering any number is so that the x will move in the direction so the grid apears in console as for new coords and u press say 8 so it will move up and thne display the new coords u get that just using for loops as i could use arrays whih is easy but i want to be able to learn this the harder way
[] [] [] [] [] [] [] [] [] []
[] [] [] [] [] [] [] [] [] []
[] [] [] [] [] [] [] [] [] []
[] [] [] [] [] [] [] [] [] []
[] [] [] [] [] [] [] [] [] []
[] [] [] [] [x] [] [] [] [] []
[] [] [] [] [] [] [] [] [] []
[] [] [] [] [] [] [] [] [] []
[] [] [] [] [] [] [] [] [] []
[] [] [] [] [] [] [] [] [] []

Reply With Quote
  #4  
Old November 7th, 2008, 05:07 AM
jasonlang jasonlang is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Oct 2008
Posts: 164 jasonlang User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 Days 3 h 13 m 21 sec
Reputation Power: 10
do something like this inside your main program loop

Code:
while(/*whatever*/)
{
    // draw map here
 
    // this bit updates the pointer
    char key = getch();

    if(key == '2' && pointerY > 0)  pointerY -= 1;
    if(key == '8' && pointerY < maxY)  pointerY += 1;

    if(key == '4' && pointerY > 0)  pointerX -= 1;
    if(key == '6' && pointerX < maxX)  pointerX += 1;
}


refer to my prev post for how to draw the bits differently depending on the location of pointerX, pointerY

i'll edit it slightly for you, take a look

Reply With Quote
  #5  
Old November 7th, 2008, 05:39 AM
curtcoll08 curtcoll08 is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Nov 2008
Posts: 22 curtcoll08 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 h 56 m 52 sec
Reputation Power: 0
right where am i going wrong as i cant seem get it to work any suggestions



#include <iostream>
#include <string>

int main()
{
std::string pause;
int row = 20;
int columns = 20;
int pointerX;
int pointerY;

for (int x = 0; x < row; x++)
{
for (int y = 0; y < columns; y++)
{
if(x == pointerX && y == pointerY)
std::cout << "[x]";
else
std::cout << "[] ";
}
while(int x == pointerX && int y == pointerY)
{
std::cout << "[][][][][]";
std::cout << "[][][][][]";
std::cout << "[][][][][]";
std::cout << "[][][][][]";
std::cout << "[][][][][]";

char key = getch();

if(key == '2' && pointerY > 0) pointerY -= 1;
if(key == '8' && pointerY < maxY) pointerY += 1;

if(key == '4' && pointerY > 0) pointerX -= 1;
if(key == '6' && pointerX < maxX) pointerX += 1;
}
}

std::cout << "\n";
}
std::cin >> pause;
}

Reply With Quote
  #6  
Old November 7th, 2008, 05:49 AM
jasonlang jasonlang is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Oct 2008
Posts: 164 jasonlang User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 Days 3 h 13 m 21 sec
Reputation Power: 10
you can re-edit posts you have already done, highlight code and click the icon which looks like a '#' symbol.

This adds tags which conserve indenting, formatting etc

makes it easier for others to read and understand you code

Reply With Quote
  #7  
Old November 7th, 2008, 05:59 AM
jasonlang jasonlang is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Oct 2008
Posts: 164 jasonlang User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 Days 3 h 13 m 21 sec
Reputation Power: 10
Code:
#include <iostream>
#include <string>
#include <stdio.h>

using namespace std; // means you don't have to put "std::" before anything

int main()
{
// start stuff : only runs once
    // i took out "pause" as you don't need it
    int rows = 20, cols = 20; // sets size of grid
    int pointerX = 0, pointerY = 0; // sets the starting position of pointer

    bool ok = true;

    while(ok) // this is the main "program loop", anything inside this loop gets repeated until ok is set to false
    {
        // this is "draw the scene"

        for (int y = 0; y < rows; y++) // y goes with rows, not x
        {
            for (int x = 0; x < cols; x++) // x goes with columns, not y
            {
                if(x == pointerX && y == pointerY) // this bit is just to draw a specific square different if the x and y vals match the pointer
                    cout << "[x]";
                else
                    cout << "[] ";
	    }

            cout << "\n";
        }

        // this bit waits for the user to hit a key, and then we check which key was hit
        char key = getchar();

        // had to swap the 8 and 2 keys coz with console
        // row zero is top of screen, not bottom
        if(key == '8' && pointerY > 0)  pointerY -= 1;
        if(key == '2' && pointerY < rows - 1)  pointerY += 1;

        if(key == '4' && pointerY > 0)  pointerX -= 1;
        if(key == '6' && pointerX < cols - 1)  pointerX += 1;

        if(key == 'q' || key == 'Q') ok = false; // this allows the user to hit q to leave program
    } // this bracket is where the "main while loop" finishes

    // This is where you put anything you want to run once the program is finishing up
}



hopefully this will illustrate the structure of a basic program :
Code:
INITIALIZE STUFF

BEGIN LOOP

   DRAWING

   INPUT (KEYS)

   UPDATE STUFF

END LOOP

CLEAN UP STUFF

Reply With Quote
  #8  
Old November 7th, 2008, 06:03 AM
curtcoll08 curtcoll08 is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Nov 2008
Posts: 22 curtcoll08 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 h 56 m 52 sec
Reputation Power: 0
ahh ok but i tried to implement what u said but i cant seem to get it to to work if u got msn could u add me i understand what ur saying yeah but like i dunno where to put what i know its simple c++ ive been able to do most of everything in the basics

Reply With Quote
  #9  
Old November 7th, 2008, 06:06 AM
curtcoll08 curtcoll08 is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Nov 2008
Posts: 22 curtcoll08 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 h 56 m 52 sec
Reputation Power: 0
Error 1 error C2064: term does not evaluate to a function taking 0 arguments c:\users\curtcooll\documents\visual studio 2005\projects\question 6.6\question 6.6\question6.cpp 38

char key = getch();

im getting this what is getch as its not the store for a char it doesnt seem to work ive tried int as well

and as well i need to implement a cout and cin to enable me to entr the number 8 6 2 4 to move the x around the grid any ideas i know its simple and im asking a lot but to learn c++ is easy yet time consuming i know to learn i should do it myself but just wanna get a few example programs done so that i can start then to move from this to then arrays and such

Reply With Quote
  #10  
Old November 7th, 2008, 06:14 AM
curtcoll08 curtcoll08 is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Nov 2008
Posts: 22 curtcoll08 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 h 56 m 52 sec
Reputation Power: 0
right i can see what ur getting at i understand the whole of what ur saying im just trying to get this to work but dont see what getch is as it is not delared varible

Reply With Quote
  #11  
Old November 7th, 2008, 06:20 AM
curtcoll08 curtcoll08 is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Nov 2008
Posts: 22 curtcoll08 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 h 56 m 52 sec
Reputation Power: 0
curtcoll08]right i can see what ur getting at i understand the whole of what ur saying im just trying to get this to work but dont see what getch is as it is not delared varible and when u got this is the draw scene i dont know how to do that only thing i can think of is cout [] [] [] [] [] like that draw whats that

Reply With Quote
  #12  
Old November 7th, 2008, 06:25 AM
jasonlang jasonlang is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Oct 2008
Posts: 164 jasonlang User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 Days 3 h 13 m 21 sec
Reputation Power: 10
getch is a standard key reader function from c language

i am looking up which header it is in i think it is in conio

you can use any key reader function you like instead of getch, there are lots to choose from

Reply With Quote
  #13  
Old November 7th, 2008, 06:28 AM
curtcoll08 curtcoll08 is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Nov 2008
Posts: 22 curtcoll08 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 h 56 m 52 sec
Reputation Power: 0
right so #include <getch> like that you mean if i can get the grid to display

[] [] [] [] []
[] [] [] [] []
[] [] [] [] []
[] [] [] [] []

like this without entering anything i can them implemtnt the cout and cin to be able to get the x in the box so that with the code u supllied i will be able to then use the store of x and y um ive confused myself lol how do u draw the grid as u said this is for drawing dont get what to do

Reply With Quote
  #14  
Old November 7th, 2008, 06:28 AM
jasonlang jasonlang is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Oct 2008
Posts: 164 jasonlang User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 Days 3 h 13 m 21 sec
Reputation Power: 10
my "draw the scene" comment was just to point out that the bit written below it draws the scene, nothing more

Reply With Quote
  #15  
Old November 7th, 2008, 06:30 AM
jasonlang jasonlang is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Oct 2008
Posts: 164 jasonlang User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 Days 3 h 13 m 21 sec
Reputation Power: 10
maybe type "getchar" instead of getch, it is accessed with #include <stdio.h> according to my research just now

Reply With Quote
  #16  
Old November 7th, 2008, 06:31 AM
jasonlang jasonlang is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Oct 2008
Posts: 164 jasonlang User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 Days 3 h 13 m 21 sec
Reputation Power: 10
just trust me that this code draws like i says it does, been doing games 15 years

Reply With Quote
  #17  
Old November 7th, 2008, 06:34 AM
jasonlang jasonlang is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Oct 2008
Posts: 164 jasonlang User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 Days 3 h 13 m 21 sec
Reputation Power: 10
this is a getchar example program I found with google just now

Code:
/* getchar example : typewriter */
#include <stdio.h>

int main ()
{
  char c;
  puts ("Enter text. Include a dot ('.') in a sentence to exit:");
  do {
    c=getchar();
    putchar (c);
  } while (c != '.');
  return 0;
}

Reply With Quote
  #18  
Old November 7th, 2008, 06:37 AM
curtcoll08 curtcoll08 is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Nov 2008
Posts: 22 curtcoll08 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 h 56 m 52 sec
Reputation Power: 0
ok guess im proper nub lol i cant get this getch to work i tried #include <getch> but that dont work what is getch as im getting a error withh it

Reply With Quote
  #19  
Old November 7th, 2008, 06:41 AM
curtcoll08 curtcoll08 is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Nov 2008
Posts: 22 curtcoll08 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 h 56 m 52 sec
Reputation Power: 0
what that is saying that it sets the char as c with a .

Reply With Quote
  #20  
Old November 7th, 2008, 06:42 AM
jasonlang jasonlang is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Oct 2008
Posts: 164 jasonlang User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 Days 3 h 13 m 21 sec
Reputation Power: 10
getch is not a header, it is inside <conio.h> header

u can google getch to get more info, or, use getchar like i showed. I have editied my earlier post to use getchar

Reply With Quote
  #21  
Old November 7th, 2008, 06:45 AM
jasonlang jasonlang is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Oct 2008
Posts: 164 jasonlang User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 Days 3 h 13 m 21 sec
Reputation Power: 10
cmon that example program is only about 6 lines do i have spell it out

c is a variable whose name is completely arbitrary, you could change the c's to "chickenpoop" and it would work exactly the same

the guy in that example program is using full stop as the sign to the program to stop, could use any other symbol instead if you want.

Reply With Quote
  #22  
Old November 7th, 2008, 06:47 AM
jasonlang jasonlang is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Oct 2008
Posts: 164 jasonlang User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 Days 3 h 13 m 21 sec
Reputation Power: 10
// SAME deal as above, but now uses hash (#) symbol to quit

Code:
/* getchar example : typewriter */
#include <stdio.h>

int main ()
{
  char crabapples;
  puts ("Enter text. Include a hash ('#') in a sentence to exit:");
  do {
    crabapples=getchar();
    putchar (crabapples);
  } while (crabapples != '#');
  return 0;
}

Reply With Quote
  #23  
Old November 7th, 2008, 06:51 AM
curtcoll08 curtcoll08 is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Nov 2008
Posts: 22 curtcoll08 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 h 56 m 52 sec
Reputation Power: 0
all thats doing is displaying what i type and using # as exit what do i learn from this all that i get is that it is looping and when i enter any char it displays this i i got dislexia so didnt read what it said miss things out mi bad lol

Reply With Quote
  #24  
Old November 7th, 2008, 06:56 AM
jasonlang jasonlang is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Oct 2008
Posts: 164 jasonlang User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 Days 3 h 13 m 21 sec
Reputation Power: 10
duh, stoopid, that is what the example program is all about

*slaps head*

It is called "Typewriter" after all

if you can't figure out how to use the keyboard func from that example in your own programs, there really is no future for you in programming

Reply With Quote
  #25  
Old November 7th, 2008, 07:03 AM
jasonlang jasonlang is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Oct 2008
Posts: 164 jasonlang User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 Days 3 h 13 m 21 sec
Reputation Power: 10
#include <stdio.h> // getchar is inside here

yourVariable = getchar(); // stores keyboard input in "yourVariable"

// use as needed

Reply With Quote
  #26  
Old November 7th, 2008, 07:12 AM
curtcoll08 curtcoll08 is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Nov 2008
Posts: 22 curtcoll08 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 h 56 m 52 sec
Reputation Power: 0
im such nub this is well confusing me i get what it does when storeing the char i put so i need to make it so that the char stored will then display on the gird atm im having trouble to even load the code given ill try if i can at least get the grid to display then itl be ok i can hen figure out how to implemnt the x moving up down left and right atm i cant get nothing im getting 2 errors in whic i may be able to sort out

Reply With Quote
  #27  
Old November 7th, 2008, 07:12 AM
jasonlang jasonlang is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Oct 2008
Posts: 164 jasonlang User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 Days 3 h 13 m 21 sec
Reputation Power: 10
BTW what i posted earlier (8:59pm) is a FULLY WORKING VERSION of all that you requested, arrow movement and everything included. you don't seem to grasp this fact.

just cut paste and compile

Reply With Quote
  #28  
Old November 7th, 2008, 07:19 AM
curtcoll08 curtcoll08 is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Nov 2008
Posts: 22 curtcoll08 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 h 56 m 52 sec
Reputation Power: 0
im sorry mate im just bit tired thing is i got dislexica and when i read things i miss stuff out so i didnt know it was fully working my mistake uve been a big help im just like blah lol

Reply With Quote
  #29  
Old November 7th, 2008, 07:29 AM
curtcoll08 curtcoll08 is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Nov 2008
Posts: 22 curtcoll08 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 h 56 m 52 sec
Reputation Power: 0
just one more question im looking at this and its looping the grid twice i cant see where its doing this and how to make it do it once i may figure it out if u can explain i will try read it or get me gf to

Reply With Quote
  #30  
Old November 7th, 2008, 07:31 AM
jasonlang jasonlang is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Oct 2008
Posts: 164 jasonlang User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 Days 3 h 13 m 21 sec
Reputation Power: 10
Hey no worries, sorry i got tetchy too, have had a long day.

trust me on those very short examples like "typewriter" (which i got off google) they are the best way to learn about new functions.

Its value lies in being so simple you don't have to look through hundreds of lines to work out what a function does.

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingC/C++ Help > Urgent help with loops


Developer Shed Advertisers and Affiliates


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2018, Jelsoft Enterprises Ltd.

© 2003-2018 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap