Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Nov 25th, 2004, 12:31 PM   #1
mswift
Newbie
 
Join Date: Nov 2004
Posts: 5
Rep Power: 0 mswift is on a distinguished road
Okay I am learning C++ using the book: Teach Yourself C++ third Edition.

I am trying to write a simple program, that asks the user for some text then, out puts that text as well as the count of characters used.

My program is using cin.get()
I need to use this so I can see the result of my program and make sure it is working correctly, but I can't seem to get cin.get() working. here is the code:

/*
A C++ program that uses I/O to prompt user for a striong and then display its length
*/

#include <iostream>
#include <string> // library that allows the use of cin.get(); (stops the program) I think
using namespace std;

int main()
{
char i;
double d;


cout << "Please enter some text.";

cin >> i;
cout << i;



cin.get();

return 0;

}
mswift is offline   Reply With Quote
Old Nov 25th, 2004, 1:00 PM   #2
tempest
Programming Guru
 
tempest's Avatar
 
Join Date: Oct 2004
Posts: 1,041
Rep Power: 5 tempest is on a distinguished road
Send a message via ICQ to tempest Send a message via AIM to tempest Send a message via Yahoo to tempest
#include <iostream>
#include <conio.h>

int main(int argc, char *argv[]) {
  char in[1024];
  int len = 0;
  std::cout << "Please enter a string... " << std::endl << " -> ";
  std::cin.getline(in, 1024);

  for(int i=0;i<1024;i++)
    if(in[i] != NULL)
      len++;
    else break;

  std::cout << std::endl;
  std::cout << "The length of your string is: " << len << " characters";
  
  getch(); 
}
__________________

tempest is offline   Reply With Quote
Old Nov 25th, 2004, 2:03 PM   #3
Eggbert
Professional Programmer
 
Eggbert's Avatar
 
Join Date: Nov 2004
Posts: 250
Rep Power: 4 Eggbert is on a distinguished road
>but I can't seem to get cin.get() working
It's best to assume that any use of cin>> [something] will leave a newline character in the stream. cin.get() isn't working because it finds a newline (which it terminates on) and finishes immediately without giving the user a chance to type anything. You'll find a similar problem, along with suitable solutions, here.

>getch();
getch should be avoided when there is a reasonable alternative that is also portable and standard.
Eggbert is offline   Reply With Quote
Old Nov 25th, 2004, 5:37 PM   #4
Ooble
I eat cake for breakfast.
 
Ooble's Avatar
 
Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9 Ooble is on a distinguished road
Is there a portable equivalent of getch() - a function that retuns a single character and doesn't echo?
__________________
Me :: You :: Them
Ooble is offline   Reply With Quote
Old Nov 25th, 2004, 7:48 PM   #5
Eggbert
Professional Programmer
 
Eggbert's Avatar
 
Join Date: Nov 2004
Posts: 250
Rep Power: 4 Eggbert is on a distinguished road
No, terminal operations are highly platform-dependent. Though it's generally a trivial task to simulate the functionality on a system that doesn't provide getch through a system API.
Eggbert is offline   Reply With Quote
Old Nov 26th, 2004, 10:09 AM   #6
Ooble
I eat cake for breakfast.
 
Ooble's Avatar
 
Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9 Ooble is on a distinguished road
Ah, well... worth a shot. Though it's a bit surprising there isn't something in std::cin.
__________________
Me :: You :: Them
Ooble is offline   Reply With Quote
Old Nov 26th, 2004, 11:52 AM   #7
Eggbert
Professional Programmer
 
Eggbert's Avatar
 
Join Date: Nov 2004
Posts: 250
Rep Power: 4 Eggbert is on a distinguished road
>Though it's a bit surprising there isn't something in std::cin.
Not when you consider that the standard input stream isn't necessarily a terminal. For example, how would getch work if the standard input stream were redirected to a file? The problem with getch is somewhat related to the screen clearing problem. How do you clear the screen? Well, how do you define a screen? How do you get everyone to use the same definition of a screen? What if the system doesn't have a screen? The standards body needed to take all of this into account since C++ is supposedly a "portable" programming language
Eggbert is offline   Reply With Quote
Old Nov 26th, 2004, 1:19 PM   #8
Ooble
I eat cake for breakfast.
 
Ooble's Avatar
 
Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9 Ooble is on a distinguished road
Fair enough
__________________
Me :: You :: Them
Ooble is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 12:26 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC