Connecting Tech Pros Worldwide Forums | Help | Site Map

cin.getline problems in case

Simon Gibson
Guest
 
Posts: n/a
#1: Jul 22 '05
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;
}



John Harrison
Guest
 
Posts: n/a
#2: Jul 22 '05

re: cin.getline problems in case



"Simon Gibson" <simon.gibson@blueyonder.co.uk> wrote in message
news:eFl%b.7477$IW1.6538@news-binary.blueyonder.co.uk...[color=blue]
> Hi there, im trying to write a program where you can write reports and[/color]
save[color=blue]
> them into an array. im having problems with getting the string into an[/color]
array[color=blue]
> tho it seems to be skipping over the cin.getline function entiryly and[/color]
going[color=blue]
> back upto the start of the menu. the problem seems to lie with '\n' bit if[/color]
i[color=blue]
> 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:[/color]

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-lit....html#faq-15.6

john


John Harrison
Guest
 
Posts: n/a
#3: Jul 22 '05

re: cin.getline problems in case


>[color=blue]
> This question is covered in the FAQ
>
> http://www.parashift.com/c++-faq-lit....html#faq-15.6
>
> john
>[/color]

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


Simon Gibson
Guest
 
Posts: n/a
#4: Jul 22 '05

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@hotmail.com> wrote in message
news:c1kqn8$1jkhbu$1@ID-196037.news.uni-berlin.de...[color=blue][color=green]
> >
> > This question is covered in the FAQ
> >
> > http://www.parashift.com/c++-faq-lit....html#faq-15.6
> >
> > john
> >[/color]
>
> 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[/color]
how[color=blue]
> many newlines have been read so far? You think the answer is one and so[/color]
you[color=blue]
> 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[/color]
and[color=blue]
> 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
>
>[/color]


Thomas Matthews
Guest
 
Posts: n/a
#5: Jul 22 '05

re: cin.getline problems in case


Simon Gibson wrote:
[color=blue]
> "John Harrison" <john_andronicus@hotmail.com> wrote in message
> news:c1kqn8$1jkhbu$1@ID-196037.news.uni-berlin.de...
>[color=green][color=darkred]
>>>This question is covered in the FAQ
>>>
>>>http://www.parashift.com/c++-faq-lit....html#faq-15.6
>>>
>>>john
>>>[/color]
>>
>>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[/color]
>
> how
>[color=green]
>>many newlines have been read so far? You think the answer is one and so[/color]
>
> you
>[color=green]
>>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[/color]
>
> and
>[color=green]
>>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
>>
>>[/color]
> 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[/color]

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

John Harrison
Guest
 
Posts: n/a
#6: Jul 22 '05

re: cin.getline problems in case


>[color=blue]
> 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.
>[/color]

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


Simon Gibson
Guest
 
Posts: n/a
#7: Jul 22 '05

re: cin.getline problems in case



"John Harrison" <john_andronicus@hotmail.com> wrote in message
news:c1l417$1k6n8b$1@ID-196037.news.uni-berlin.de...[color=blue][color=green]
> >
> > 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.
> >[/color]
>
> Yes that's right, apologies for my mistake. Without '\n' it means ignore[/color]
all[color=blue]
> input, with '\n' it means ignore all input up to the next newline, which[/color]
is[color=blue]
> 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
>
>[/color]

sorry about top posting didnt know it was a rule :\
thanks for the help guys its all clear now :)
si


Closed Thread