Jump to content

Welcome to Geeks to Go - Register now for FREE

Need help with your computer or device? Want to learn new tech skills? You're in the right place!
Geeks to Go is a friendly community of tech experts who can solve any problem you have. Just create a free account and post your question. Our volunteers will reply quickly and guide you through the steps. Don't let tech troubles stop you. Join Geeks to Go now and get the support you need!

How it Works Create Account
Photo

Some beginner C++ questions.


  • Please log in to reply

#1
Fealos

Fealos

    Member

  • Member
  • PipPip
  • 17 posts
I have a couple of very basic questions for anyone who has a minute.

First, I remember learning a function in my first programming class to make a program wait before running the next bit of code. That was about 5 years ago, and I've completely forgotten it. I've found a way to do this, but I think there might be a more efficient way to do it (such as the function I was taught long ago).

Here's the way I found:
#include <iostream>
using namespace std;

#include <ctime>
using namespace std;

void delay( int );

int main()
{
     cout << "Waiting 5 seconds before printing overused phrase...\n";
     delay ( 5 );
     cout << "Hello World!";
}

void delay( int seconds )
{
      time_t currentTime1;   //pre-defined variable for holding time
	time_t currentTime2;   //pre-defined variable for holding time
	
	time ( &currentTime1 );   //stores the current time in currentTime1
	
	while ( currentTime2 <= (currentTime1 + seconds) )
	{
  time ( &currentTime2 );   //stores the current time in currentTime2
	}
}

This may not be the best way there is, but I'm sure one of you can point me in a better direction.



Secondly, (and I can't think of any way to do this) I need to be able to accept a single character of input from the keyboard without the user having to hit the return key afterward. I am asking yes or no questions and I want the user to be able to hit 'y' or 'n' and I don't want them to have to hit return to input the data. How is this done?

Thanks to everyone who gives me a hand!

Edited by Fealos, 23 May 2005 - 12:29 AM.

  • 0

Advertisements


#2
scicatur

scicatur

    Member

  • Member
  • PipPip
  • 16 posts
depends on your operating system.
if Unix then:
#include <unistd.h>
  .

sleep( msec );
if Windows then:
#include <windows.h>
  .

Sleep( msec );

Edited by scicatur, 23 May 2005 - 04:07 AM.

  • 0

#3
Fealos

Fealos

    Member

  • Topic Starter
  • Member
  • PipPip
  • 17 posts
Dang, and I'm not developing on either of those. I guess my way works for now, though I'd like to be able to use milliseconds rather than whole seconds.

EDIT: I found that I can do milliseconds by just turning my variable into a double, and sending the function decimals.

Anyone have any input about my second problem?

Edited by Fealos, 23 May 2005 - 02:35 PM.

  • 0

#4
bdlt

bdlt

    Member

  • Member
  • PipPipPip
  • 876 posts
single keystroke using kbhit():

while(1) {
if( kbhit() ) {
ch = getch(); // where ch is a char
break;
}
}
  • 0

#5
Fealos

Fealos

    Member

  • Topic Starter
  • Member
  • PipPip
  • 17 posts

single keystroke using kbhit():

while(1) {
  if( kbhit() ) {
ch = getch(); // where ch is a char
break;
}
}

View Post



While this definitely helped by giving me much to search for, my searches have yielded disappointing results. What I've found is that Xcode doesn't include <conio> and that you need to use another header file. But, this header only has getch(), and not kbhit(). so, looks like I need to find a heavily trafficked C++ dev forum for apple developers. At least I've learned a bunch. Thanks everyone!
  • 0

#6
scicatur

scicatur

    Member

  • Member
  • PipPip
  • 16 posts
well Faelos, if you really want some help you should have kindly revealed from the start that it was apple you were developing for.. and even "apple" is not a name of operating system. At least the newest OS in apples is named MAC OS X. The older versions being MAC OS 8 and 9. And there is a huge difference between versions 9 and X.

As far as I know the MAC OS X is based on or resembles unix so you could still try the unistd.h ... but since i know little about mac os it's just guessing..
  • 0

#7
Fealos

Fealos

    Member

  • Topic Starter
  • Member
  • PipPip
  • 17 posts
I had no idea that the library in question wouldn't be platform independant. I haven't run across anything yet that isn't until now. So, I wasn't aware that I should indicate the platform (or OS) for which I am developing. Call me a newbie, you'd be right. I really haven't been doing this for that long, and I'm not exactly sure where I need to be specific, and what about. Sorry about that.

I'm developing on an Apple running OS 10.3.9 for myself in order to learn. I'm also sort of pissed that I just deleted my linux partition, because I would have had a better time developing there. I may just reinstall it tonight.

I'll try using <unistd.h> for now. Thanks for the further advice!
  • 0

#8
Fealos

Fealos

    Member

  • Topic Starter
  • Member
  • PipPip
  • 17 posts
OK, so it compiled with <unistd.h> included. No warnings, no errors. I assume that this means it found this header file. It also didn't flag either kbhit() or getch(), so I also assume that they are included in this header file. I can't figure out exactly how to implement them, though.

Tell me now if I'm being too much of a pain.

Here's what I don't exactly get:

in the code snippet you gave me, scicatur, it says
while (1)

What does the condition (1) do for this while loop? Was I supposed to fill in my own condition here?

Second, this is my understanding of what this loop is doing:
while(1) {            // while  some condition is true
     if( kbhit() ) {      // if the system detects a keyboard hit
          ch = getch();        // check to see whether the key hit is this key (defined where "ch" is)
          break;
     }
}

I may be way off, and if I am could you please help me out a bit? I basically want to ask the user a question, have them hit 'y' for yes or 'n' for no without having to hit return afterward, and store their answer to a variable.
  • 0

#9
bdlt

bdlt

    Member

  • Member
  • PipPipPip
  • 876 posts
don't feel bad Fealos - you will run into more 'independent' issues with c++. the fun things involve data types. ints on one system may be 32 bits and on another 64 bits. this will cause some difficultly in debugging code since it's relatively easy to get code to compile with c++.

here comes the sales pitch: java is c++ without the headaches. consider starting any new projects using java instead of c++. development and debug time is reduced drastically. and since you are already familiar with c++, you only need to learn the fun aspects of java.
  • 0

#10
bdlt

bdlt

    Member

  • Member
  • PipPipPip
  • 876 posts
while(1) is an infinite loop - usually a bad idea. in this case it will loop until the next keystroke. break gets it out of the while loop.

define ch with the other declarations in this function.
char ch = ' '; // fill it with the space character

after the while loop check ch

if( ch == 'y' || ch == 'Y' ) {
// do something
}
else if( ch == 'n' || ch == 'N' ) {
// do something else
}
else {
// tell the user to enter y or n
}
  • 0

Advertisements


#11
Fealos

Fealos

    Member

  • Topic Starter
  • Member
  • PipPip
  • 17 posts
OHHHHHHHHH! as soon as I read that, I understood what was happening in that while loop, and why it needs to be in the loop form.

Yeah, I'm just beginning as a CS student at CSU in Ft. Collins. My first courses start in a few days, and I'm just writing this stuff to oil the greasy wheels. It's been so long since I learned this stuff, so I'm rusty. In the CS program here they focus mainly on java, but I'm coding some C++ just to get back into a logical frame of mind.

You're right, I can't wait to learn Java.

About the help - I completely understand what you're doing there, but I think there must already be something in the keyboard buffer that is getting read before the program enters the loop. is there any way I can clear it out? In my searches for info about this stuff I saw a function that I think was called fflush(). Is this what fflush() is used for?

Thanks a lot, bdlt!

EDIT:
Nevermind. I guess it's just getch() that it doesn't understand. I tried adding it before any of my cin istreams, and it still gives me this error. Everything compiles fine, but when I run it, as soon as it hits the getch() function it exits and gives me this output:

ZeroLink: unknown symbol '__Z5getchv'
Abort

any ideas?

Edited by Fealos, 24 May 2005 - 11:33 PM.

  • 0

#12
bdlt

bdlt

    Member

  • Member
  • PipPipPip
  • 876 posts
if you are only getting y or n from the user, remove the cin code. I haven't had much luck with cin(borland, visual c++, unix). It may be better since I've tried cin, but problems seems to surface eventually when using cin.
the error could be a coding error or a c++ peculiarity.
paste the code in a personal message(use the PM button) and I'll take a look at it.
  • 0

#13
Fealos

Fealos

    Member

  • Topic Starter
  • Member
  • PipPip
  • 17 posts
Wow. I missed this completely. I had found a workaround that a Metrowerks employee had posted for the lack of getch() and kbhit() functions on the mac platform, and part of it was to declare those as functions in your program directly. It didn't work, because it only works for those using Metrowerks' Codewarrior to compile. I had removed the actual functions from my code after it didn't work, but I had forgotten to remove the function declarations. so, that's why it compiled while I was using them. I thought it was because I added the <unistd.h> header. So, now I'm back to square one. sucky.
  • 0

#14
bdlt

bdlt

    Member

  • Member
  • PipPipPip
  • 876 posts
sorry to hear about the lack of getch() and kbhit() on mac. this type of thing rarely happens with java(sales pitch #2).

time to search the net for a solution.
  • 0

#15
Fealos

Fealos

    Member

  • Topic Starter
  • Member
  • PipPip
  • 17 posts

time to search the net for a solution.

View Post


Word. Maybe I'll just reinstall linux and try again. Does <conio.h> work on linux?

Tell me when I've reached my question limit for the forum. I feel like I'm taking too much of your time for so frivolous a search.
  • 0






Similar Topics

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

As Featured On:

Microsoft Yahoo BBC MSN PC Magazine Washington Post HP