Computing.Net > Forums > Programming > get input from keyboard with c++

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Click here to start participating now! Also, check out the New User Guide.

get input from keyboard with c++

Reply to Message Icon

Name: rde
Date: September 5, 2003 at 09:18:15 Pacific
OS: WIN98 SE
CPU/Ram: AMD Athlon 1800, 256 MB
Comment:

hi all

I need to get an input from the keyboard in my c++-prog. This can by done with getch(), cin or scanf, i know. The programm executs fine until i should enter the char. But then, NOTHING HAPPENS!! The programm is just ignors what im typing on my keyboard and waits and waits and waits!! It doesnt matter which of the methods i use, my keystrokes just arent remarked by the programm.
Why does my computer do this to me? I have absolutely no clue. Any help will be greatly appreciated.

rde



Sponsored Link
Ads by Google

Response Number 1
Name: Click_er
Date: September 5, 2003 at 10:19:41 Pacific
Reply:

what are you trying to do after you get the input character

is the input an array, are you trying to get the data while in a loop???

give more info


0

Response Number 2
Name: gpp
Date: September 5, 2003 at 10:40:08 Pacific
Reply:

Buying a newer faster computer might solve the problem!

Why dont you post your code so we can see you're trying to do..


0

Response Number 3
Name: rde
Date: September 5, 2003 at 11:29:34 Pacific
Reply:

The prog is really very simple. Its not the prog i try to write, but its the easiest way to use cin(or equivalent) i can think of and its enough to explain my problem.
If I run the prog it prints "Enter a character" on the screen (as it should) and waits for an input. When i try to make an input, nothing happens, absolutely nothing. The computer just goes on waiting for an input. All i can do then is terminate the prog.

rde

------

#include
#include

void main()
{

char c;

cout > c;

cout endl endl c endl;

getch();
}

-----------Code end------------------


0

Response Number 4
Name: rde
Date: September 5, 2003 at 11:33:34 Pacific
Reply:

Damn forum. Is there a way to post code correctly? I used substitute signs instead.


#include [conio.h]
#include [iostream.h]

void main()
{

char c;

cout [[ "Enter a character" [[ endl;
cin ]] c;

cout [[ endl [[ endl [[ c [[ endl;

getch();
}


0

Response Number 5
Name: gpp
Date: September 5, 2003 at 12:41:25 Pacific
Reply:

Got this to work...?


#include <iostream>
#include <stdlib.h>

using namespace std;

int main(int argc, char *argv[])
{
  char c;
  std::cout << "Please enter a character: " << endl;
  cin >> c;
  cout << "Your char is: " << c << endl;
  system("PAUSE");     
  return 0;
}


0

Related Posts

See More



Response Number 6
Name: rde
Date: September 5, 2003 at 12:59:36 Pacific
Reply:


No. I get some compiling errors.

Line 4:
Namespace name expected

Line 9:
Qualifier 'std' is not a class or namespace name
Statement missing;

There is no spelling mistake, i checked it more than once. Since i never used namespaces before, i dont know exactly how they work and whats wrong.

rde


0

Response Number 7
Name: gpp
Date: September 5, 2003 at 13:43:03 Pacific
Reply:

Sorry, that was sloppy. This example will clear it up.. This is basically just saying I using classes from the standard lib.

the system("PAUSE"); is to keep the window open.

#include <iostream>

int main(int argc, char *argv[])
{
  char c;
  std::cout << "Please enter a character: " << std::endl;
  std::cin >> c;
  std::cout << "Your char is: " << c << std::endl;
  system("PAUSE");     
  return 0;
}


0

Response Number 8
Name: Infinite Recursion
Date: September 5, 2003 at 14:59:11 Pacific
Reply:

Isn't it standard now to use the namespace?

At least when I try to compile under Linux with the new gcc and do not have the namespace listed... it bombs on me.

-

No reason the code above would not work unless its a compiler problem or typo when you are transcribing it.

IR


0

Response Number 9
Name: gpp
Date: September 5, 2003 at 15:10:18 Pacific
Reply:

Yeah, it is. I get bombed too.. You have to either declare it at the top or when you call a method. I dont really use C++ anymore, but I think iostream is a deprecated class...?


0

Response Number 10
Name: Chi Happens
Date: September 5, 2003 at 16:58:04 Pacific
Reply:

I don't believe iostream is depreciated, it is what cin and cout and std are defined in.

This code works fine in the following compilers (VC++, BCB, BCC5.5, OpenWatcom, CodeWarrior):

#include <iostream> 
#include <conio.h> 

using namespace std; 

int main(void) 

  char c; 
  cout << "Please enter a character: " << endl; 
  cin >> c; 
  cout << "Your char is: " << c << endl; 
  getch();
  return 0; 
}


Chi Happens


0

Response Number 11
Name: nesh
Date: September 12, 2003 at 14:51:53 Pacific
Reply:

Ok, sorry to sound COMPLETELY ignorant here, but ive been looking through a number of posts on C++, and I'd like to know what this endl; statement is? Please! :-)


0

Response Number 12
Name: Infinite Recursion
Date: September 12, 2003 at 15:01:15 Pacific
Reply:

the "endl" when used with "cout" generates a new line... similarly you can use "\n".

IR


0

Response Number 13
Name: Nesh
Date: September 14, 2003 at 19:43:43 Pacific
Reply:

Thanks, Ive come across the "\n", but the endl was making no sense :-)


0
Reply to Message Icon

Vb 6.0 help installing .NET 2003: vdt...



Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: get input from keyboard with c++

get date from CMOS in C++ www.computing.net/answers/programming/get-date-from-cmos-in-c/5922.html

Help with C++ www.computing.net/answers/programming/help-with-c/7471.html

going fullscreen in console with c, HOW? www.computing.net/answers/programming/going-fullscreen-in-console-with-c-how/1567.html