Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Old 04-14-2005, 07:38 AM   #1 (permalink)
mconn86
Registered User
 
Join Date: Apr 2005
Posts: 5
mconn86 is on a distinguished road
cin.getline problems

hi, im relativly new to C++, im just starting to write a simple program where i get the user to read in a string of characters.Im keeping it very simple at first so I can modify it later to use in my program, where I'm having problems is when I ask the user if they want to try again, the computer still holds the value of the original array in its memory and this causes the whole program to go a bit crazy. I think i should be using a cin.ignore statement or something like that but im not too sure how. You can view my program below (very basic,just want to get my head around the idea). Nyone out there with any advice,cheers!
Code:
#include <iostream.h>

main_program();

int main()
{

main_program();

return 0;
}

main program()
{
char code[14];
char reply;

cout << "Please enter your code" << endl;
cin.getline (code,14);
cout << code << endl;
cout << "Go again?" << endl;
cin >> reply;
 if (reply =='y')
    {
     main_program();
     }
return 0;
}

Last edited by Valmont; 04-14-2005 at 09:55 AM.
mconn86 is offline   Reply With Quote
Old 04-14-2005, 09:57 AM   #2 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
Valmont is on a distinguished road
Yes, there is a lot of info on this site about streams. Use the "search" button and try to search for "clearing stream", or something like that.

Also your main_program() function doesn't have a return type. Should be void at least.
__________________
Valmont is offline   Reply With Quote
Old 04-14-2005, 10:22 AM   #3 (permalink)
mconn86
Registered User
 
Join Date: Apr 2005
Posts: 5
mconn86 is on a distinguished road
im not woried about the void in my function just forgot putting that in just really want to find out how to clear my array,cant find anything on this site that has helped me so far,ive been experimenting with the following code and it achieves kinda what im looking for as long as i enter the maximum number of characters im my array.
#include <iostream.h>

int main()
{

char code[14];

cout << "Type code" << endl;
cin.getline (code,14);
cout << code << endl;
cin.ignore();
cout << "Enter another code" << endl;
cin.getline (code,14);
cout << code << endl;

return 0;
}

This program works fine as long as the full value of the array is entered i.e 13 characters
mconn86 is offline   Reply With Quote
Old 04-14-2005, 12:12 PM   #4 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
Valmont is on a distinguished road
Quote:
...cant find anything on this site that has helped me so far...
This forum offers some neat solutions when it comes to streams. This is one of them:

Code:
#include <iostream>
#include <climits>

//--
using std::endl;
using std::cout;
using std::cin;
//--

void reset_istream() 
{
  if(cin.eof())
  {
    cin.clear(); 
  } else 
  {   
    cin.clear(); 
    cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');   
  } 
} 

//---------------------------------------

int main()
{
  char code[14];
  char reply;
  cout << "Please enter your code" << endl;
  cin.getline (code,14);
  cout << code << endl;
  cout << "Go again?" << endl;
  cin >> reply;
  if (reply =='y')
  {
  }
  reset_istream();
return 0;

}
__________________
Valmont is offline   Reply With Quote
Old 04-14-2005, 12:22 PM   #5 (permalink)
mconn86
Registered User
 
Join Date: Apr 2005
Posts: 5
mconn86 is on a distinguished road
Notice the
Quote:
so far...
! Thats looks a bit further than ive learned so far, im doing a project in university so I might look a bit suspicious if i used that,I will have a good look at it though think I can understand most of it.Nyway I think ive found a solution that works,not sure how it does but it seems to do the trick.Time will tell...!
Cheers
mconn86 is offline   Reply With Quote
Old 04-14-2005, 02:02 PM   #6 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
Valmont is on a distinguished road
Aren't you allowed to study ahead in the university?

Ok. This part is all that you need right now. The other half is usefull in more complex situations where escape sequences are used to terminate input with CTRL+Z/X.
Code:
cin.clear(); 
    cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
cin.clear();
First we clear the stream in case some error occured. Let's start with a blank sheet.

cin.ignore(...
We are going to ignore a certain amount of stuff left in the stream.

std::numeric_limits<std::streamsize>::max()
How much are we going to ignore? Well, let's ignore the maximum number of items a stream can have...

'\n'
...until we find a newline.

#include <climits>
But to use the nice feature of numeric_limits<>max(), we need to include this header.

std::
Let's use definitions only defined by the ISO C++ standard.


using std::endl;
using std::cout;
using std::cin;

It is handy if these often used functions (endl, cout, and cin), can be typed without typing "std::" in front of it all the time. So let's predefine that we want to use the ISO C++ versions only.
Next time when we type "cout<<", the compiler knows we mean the ISO version "std::cout<<".

Another way
Code:
void reset_istream(std::istream & is)
{
  char ch;
  // Reset the state.
  is.clear();
  // Remove all characters until we find a newline or EOF.
  ch = is.get();
  while ((ch != '\n')&&(ch != EOF))
    ch = is.get();
  is.clear();
}
Use it like this:
Code:
reset_istream(std::cin);
__________________
Valmont is offline   Reply With Quote
Reply

Bookmarks

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
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
problems get local time using sys/time.h if13121 Standard C, C++ 2 10-14-2004 10:19 PM
Problems debugging visual c++ in vs.net 2003 Engineer MS Technologies ( ASP, VB, C#, .NET ) 2 07-20-2004 01:19 PM
no dial tone problems fabiopao Linux / BSD / OS X 4 02-23-2003 11:51 AM
I have problems... anon Lounge 4 01-12-2003 08:47 PM
MySQL problems sde Linux / BSD / OS X 5 12-07-2002 11:04 PM


All times are GMT -8. The time now is 05:59 AM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting