 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Simon Gibson Guest
|
Posted: Thu Feb 26, 2004 12:30 pm Post subject: cin.getline problems in case |
|
|
Hi there, im trying to write a program where you can write reports and save
them into an array. im having problems with getting the string into an array
tho it seems to be skipping over the cin.getline function entiryly and going
back upto the start of the menu. the problem seems to lie with 'n' bit if i
change that to say 'a' it will run fine but accept a as ending the input.
any help getting the 'n' working is appreciated!!
heres the code:
//---------------------------------------------
//program to store match reports into an array and view them
//
//
//---------------------------------------------
#include <iostream>
using namespace std;
int main()
{
const int MAX = 600;
char matches[10][MAX]; //array declaration
int choice = 0; //number choice
int count = 0; //array location count
int i = 0; //I initialised here cos cant in
case??
while (choice != 3)
{
cout << "Welcome to the match database"; //menu.
cout << endl
<< "1. View Entrys"
<< endl
<< "2. Add Entry"
<< endl
<< "3. End program"
<< endl
<< "please choose an option: ";
cin >> choice;
switch(choice) //choice selection
{
case 1: //view entrys
for(i = 0;i < count; i++) //reset i to view full list
each time
{
cout << endl
<< "Match report number: " << i
<< endl
<< *(matches+i); //view matches.
}
break;
case 2: // add entry
cout << endl // Prompt for input
<< "Enter Your Report. it must be less than "
<< MAX << " characters:"
<< endl;
cin.getline ( matches[count], MAX, 'n'); // add to array
count++; //inc count
break;
case 3: //quit program
return 0;
break;
default:
cout << endl
<< "not a valid number "
<< endl;
}
}
return 0;
}
|
|
| Back to top |
|
 |
John Harrison Guest
|
Posted: Thu Feb 26, 2004 12:52 pm Post subject: Re: cin.getline problems in case |
|
|
"Simon Gibson" <simon.gibson (AT) blueyonder (DOT) co.uk> wrote
| Quote: | Hi there, im trying to write a program where you can write reports and
save
them into an array. im having problems with getting the string into an
array
tho it seems to be skipping over the cin.getline function entiryly and
going
back upto the start of the menu. the problem seems to lie with 'n' bit if
i
change that to say 'a' it will run fine but accept a as ending the input.
any help getting the 'n' working is appreciated!!
heres the code:
|
The problem is that you don't fully understand how the combination of
cin >> i;
and
cin.getline()
works, (hint cin >> i never reads a newline and cin.getline() reads upto the
next newline, so what do you think will happen when you combine the two?).
This question is covered in the FAQ
http://www.parashift.com/c++-faq-lite/input-output.html#faq-15.6
john
|
|
| Back to top |
|
 |
John Harrison Guest
|
Posted: Thu Feb 26, 2004 1:04 pm Post subject: Re: cin.getline problems in case |
|
|
I think the FAQ is less than helpful on this question, here's exactly what
happens.
Your program asks for a menu option and the user types
2 <newline>
Then your program asks for a report and the user types
this is my report <newline>
At this point cin.getline() starts reading characters. Now at this point how
many newlines have been read so far? You think the answer is one and so you
can't understand why getline seems to skip the report. But the answer is
zero, because cin >> i NEVER reads newlines (assuming i is a number). The
newline that was typed after the menu option is still waiting to be read and
getline reads it and stops.
This is why, after interactive line based input its a good idea to add
cin.ignore(INT_MAX);
after the input of a number, to clear out any unread newlines. Use the
header file <limits.h> to get the INT_MAX constant.
john
|
|
| Back to top |
|
 |
Simon Gibson Guest
|
Posted: Thu Feb 26, 2004 2:40 pm Post subject: Re: cin.getline problems in case |
|
|
Cheers for that, one thing tho, i tried using the cin.ignore(INT_MAX); but
when i entered the number 2 to go into the enter match bit it didnt come up
it just kept letting me press enter with nothing happening. i tried using
cin.ignore() and it works fine, could you explain the INT_MAX bit? im only
just learning so any info is appreciated.
cheers
si
"John Harrison" <john_andronicus (AT) hotmail (DOT) com> wrote
| Quote: |
This question is covered in the FAQ
http://www.parashift.com/c++-faq-lite/input-output.html#faq-15.6
john
I think the FAQ is less than helpful on this question, here's exactly what
happens.
Your program asks for a menu option and the user types
2 <newline
Then your program asks for a report and the user types
this is my report
At this point cin.getline() starts reading characters. Now at this point
how
many newlines have been read so far? You think the answer is one and so
you
can't understand why getline seems to skip the report. But the answer is
zero, because cin >> i NEVER reads newlines (assuming i is a number). The
newline that was typed after the menu option is still waiting to be read
and
getline reads it and stops.
This is why, after interactive line based input its a good idea to add
cin.ignore(INT_MAX);
after the input of a number, to clear out any unread newlines. Use the
header file <limits.h> to get the INT_MAX constant.
john
|
|
|
| Back to top |
|
 |
Thomas Matthews Guest
|
Posted: Thu Feb 26, 2004 3:25 pm Post subject: Re: cin.getline problems in case |
|
|
Simon Gibson wrote:
| Quote: | "John Harrison" <john_andronicus (AT) hotmail (DOT) com> wrote in message
news:c1kqn8$1jkhbu$1 (AT) ID-196037 (DOT) news.uni-berlin.de...
This question is covered in the FAQ
http://www.parashift.com/c++-faq-lite/input-output.html#faq-15.6
john
I think the FAQ is less than helpful on this question, here's exactly what
happens.
Your program asks for a menu option and the user types
2 <newline
Then your program asks for a report and the user types
this is my report
At this point cin.getline() starts reading characters. Now at this point
how
many newlines have been read so far? You think the answer is one and so
you
can't understand why getline seems to skip the report. But the answer is
zero, because cin >> i NEVER reads newlines (assuming i is a number). The
newline that was typed after the menu option is still waiting to be read
and
getline reads it and stops.
This is why, after interactive line based input its a good idea to add
cin.ignore(INT_MAX);
after the input of a number, to clear out any unread newlines. Use the
header file <limits.h> to get the INT_MAX constant.
john
Cheers for that, one thing tho, i tried using the cin.ignore(INT_MAX); but
when i entered the number 2 to go into the enter match bit it didnt come up
it just kept letting me press enter with nothing happening. i tried using
cin.ignore() and it works fine, could you explain the INT_MAX bit? im only
just learning so any info is appreciated.
cheers
si
|
1. Don't top-post. Replies go at the bottom or are interspersed.
2. I believe the ignore syntax should be:
cin.ignore(INT_MAX, 'n');
This says to ignore either INT_MAX characters or 'n', which
ever occurs first.
--
Thomas Matthews
C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book
|
|
| Back to top |
|
 |
John Harrison Guest
|
Posted: Thu Feb 26, 2004 3:43 pm Post subject: Re: cin.getline problems in case |
|
|
| Quote: |
1. Don't top-post. Replies go at the bottom or are interspersed.
2. I believe the ignore syntax should be:
cin.ignore(INT_MAX, 'n');
This says to ignore either INT_MAX characters or 'n', which
ever occurs first.
|
Yes that's right, apologies for my mistake. Without 'n' it means ignore all
input, with 'n' it means ignore all input up to the next newline, which is
what you want.
You can do this (say)
cin.ignore(10, 'n');
which means ignore up to 10 characters or until the next newline whichever
comes first. INT_MAX is just a way of saying ignore *all* input up to the
next newline, which is normally what you want.
john
|
|
| Back to top |
|
 |
Simon Gibson Guest
|
Posted: Thu Feb 26, 2004 3:57 pm Post subject: Re: cin.getline problems in case |
|
|
"John Harrison" <john_andronicus (AT) hotmail (DOT) com> wrote
| Quote: |
1. Don't top-post. Replies go at the bottom or are interspersed.
2. I believe the ignore syntax should be:
cin.ignore(INT_MAX, 'n');
This says to ignore either INT_MAX characters or 'n', which
ever occurs first.
Yes that's right, apologies for my mistake. Without 'n' it means ignore
all
input, with 'n' it means ignore all input up to the next newline, which
is
what you want.
You can do this (say)
cin.ignore(10, 'n');
which means ignore up to 10 characters or until the next newline whichever
comes first. INT_MAX is just a way of saying ignore *all* input up to the
next newline, which is normally what you want.
john
|
sorry about top posting didnt know it was a rule :
thanks for the help guys its all clear now
si
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|