473,672 Members | 2,656 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 7143

"Simon Gibson" <si**********@b lueyonder.co.uk > wrote in message
news:eF******** *********@news-binary.blueyond er.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(IN T_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.l earn.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
2142
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 could state getline's first parameter supports "FROM". The second parameter supports "TO". // later
5
6060
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 <iostream>
14
3877
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 ..txt file that I want to read into a multi-dimensional string array. Each line of the file needs to be read into the array. OK..sounds easy enough, but I can't get the getline(file_name array_name) to work. So...I am thinking it is definitely...
18
8245
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 <iostream> #include <fstream> #include <cstdlib> #include <string>
2
10574
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 work rightly. (i.e. I could not move the file position to the begin or some position using seekg())
2
6258
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); } I've tried this on two text files, one whose last line concluded with a
9
1992
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 case, it goes straight without waiting for input... any ideas?
6
6757
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 goes all wrong. Is there some known differences with file handling on OS X? My code is quite long, but one of the defect parts looks like this (sorry about the indentation - I'm new to posting code on a newsgroup):
10
5184
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
8832
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8611
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8685
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7449
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6240
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5709
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4230
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2821
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1819
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.