cin.getline problems in case

S

Simon Gibson

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;
}
 
J

John Harrison

Simon Gibson said:
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
 
J

John Harrison


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
 
S

Simon Gibson

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
 
T

Thomas Matthews

Simon said:
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
 
J

John Harrison

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
 
S

Simon Gibson

John Harrison said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top