473,379 Members | 1,337 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,379 software developers and data experts.

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;
}
Jul 22 '05 #1
6 7133

"Simon Gibson" <si**********@blueyonder.co.uk> wrote in message
news:eF*****************@news-binary.blueyonder.co.uk...
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-lit....html#faq-15.6

john
Jul 22 '05 #2
>
This question is covered in the FAQ

http://www.parashift.com/c++-faq-lit....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 <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
Jul 22 '05 #3
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" <jo*************@hotmail.com> wrote in message
news:c1*************@ID-196037.news.uni-berlin.de...

This question is covered in the FAQ

http://www.parashift.com/c++-faq-lit....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 <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

Jul 22 '05 #4
Simon Gibson wrote:
"John Harrison" <jo*************@hotmail.com> wrote in message
news:c1*************@ID-196037.news.uni-berlin.de...
This question is covered in the FAQ

http://www.parashift.com/c++-faq-lit....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 <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

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

Jul 22 '05 #5
>
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
Jul 22 '05 #6

"John Harrison" <jo*************@hotmail.com> wrote in message
news:c1*************@ID-196037.news.uni-berlin.de...

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
Jul 22 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: ma740988 | last post by:
Consider: ifstrem MyFile("extractMe.txt"); string Str; getline(MyFile, Str); getline above extracts the contents of MyFile and place into the string object. Deduced using FROM/TO logic I...
5
by: Aleander | last post by:
First of all, greatings from Italy to everyone, and sorry for my bad english!! I have this problem with the instruction cin.getline that follow a simple cin instruction: Example: #include...
14
by: KL | last post by:
I am so lost. I am in a college course for C++, and first off let me state I am not asking for anyone to do my assignment, just clarification on what I seem to not be able to comprehend. I have a...
18
by: Amadeus W. M. | last post by:
I'm trying to read a whole file as a single string, using the getline() function, as in the example below. I can't tell what I'm doing wrong. Tried g++ 3.2, 3.4 and 4.0. Thanks! #include...
2
by: Assertor | last post by:
Hi, All. (VC++6.0) I found some strange thins when using getline() and seekg() of std::ifstream. After the file position of an open file was shift to the end of the file, seekg() did not...
2
by: Mark P | last post by:
Consider the following snippet of code to read lines from a text file: ifstream file_stream( "some_file.txt"); string read_line; while( file_stream) { getline( file_stream, read_line); } ...
9
by: Michele 'xjp' | last post by:
Hi there. I have some problems with cin. Here's the code: http://rafb.net/p/GhK3AU65.html If you press '6', and 'enter', it will have to ask for another insert of a string. However, in this...
6
by: JML | last post by:
Hi, I have some code which parses a text file and creates objects based on what is in the text file. The code works just fine on Windows, but when I compile it using XCode on OS X the parsing...
10
by: Terry IT | last post by:
hi, i'm using code like this string s while(getline(cin,s)){ process(s); } // this is the last line process(s);
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.