473,385 Members | 1,901 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,385 software developers and data experts.

i dont understand the use of cin

51
In the following program i dont how cin knows where day, stops and ch ,starts.
The same thing for month and year. When you type the input string without spaces.Can anyone help?thanks
Expand|Select|Wrap|Line Numbers
  1. #include <iostream.h>
  2. int main (void)
  3. {
  4. int day, month, year;
  5. char ch;
  6. cout << "Input a date as dd/mm/yy: ";
  7. cin >> day >> ch >> month >> ch >> year;
  8. switch (month) {
  9. case 1: cout << "January"; break;
  10. case 2: cout << "February"; break;
  11. case 3: cout << "March"; break;
  12. case 4: cout << "April"; break;
  13. case 5: cout << "May"; break;
  14. case 6: cout << "June"; break;
  15. case 7: cout << "July"; break;
  16. case 8: cout << "August"; break;
  17. case 9: cout << "September";break;
  18. case 10: cout << "October"; break;
  19. case 11: cout << "November"; break;
  20. case 12: cout << "December"; break;
  21. }
  22. cout << ' ' << day << ", " << 1900 + year << '\ n';
  23. return 0;
  24. }
  25.  
Oct 14 '08 #1
5 1716
I tried to compile this program using the following syntax:

g++ filename.c -o filename

Mainly because I couldn't see how it would work. C++ describes cin and cout as being able to split on a valid decimeter which it describes as a "a space, a tab character or a newline". Therefore the syntax:

cin >> day >> ch >> month >> ch >> year;

would not work on the at least the compilers that I use. In order to get this program to function you would need to enter an input string such as:

12 / 11 / 08 no 12/11/08
Oct 15 '08 #2
Ganon11
3,652 Expert 2GB
Correct. The >> operator, when used with cin, uses whitespace to separate what are parsed as tokens. So if you enter 05/11/1989, the entire string will go into month. You need to ask your user to enter the string with spaces (i.e. "05 11 1989"), or you need to read the whole string ("05/11/1989") into a single string, then break up the string yourself (using the .substr() functio).
Oct 15 '08 #3
boxfish
469 Expert 256MB
(using the .substr() functio).
Or maybe strtok... ?
Oct 15 '08 #4
boxfish
469 Expert 256MB
No, wait a minute. There's nothing wrong with the code there and the program works just fine. No need for breaking up the string yourself. When you cin into an integer variable such as day, cin skips over all leading whitespace, and then reads into day until it encounters a character that is not a digit, such as a /. It knows to stop as soon as it hits a non-digit, even if it isn't whitespace.
Hope this helps.
Oct 15 '08 #5
dynamo
51
Thanks for the help guys it feels so good to be part of a community with so many brilliant minds.I think the last reply explains everything thanks again.
Oct 17 '08 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

18
by: news.pandora.be | last post by:
I tried everything. I even tested it in combination with opera and mozilla. (I use IE 6 mostly) It's reduced to the simple example below. Please tell me what is wrong with my understaning of...
10
by: Simon Mansfield | last post by:
I have been given a list of exercises to do by my tutor, one of which is this: http://www.cems.uwe.ac.uk/~amclymer/Software%20Design%20and%20C++/Exercises/Exercise%208/Exercise.html Which i am...
1
by: Merrua | last post by:
I dont understand how to do this I created a dialog based project and that is fine for most of my programming project, but one dialog box is going to generate a graph, and as far as i know from...
7
by: Maarten | last post by:
Hi, I have a gridview (2.0), in the page load I remove a couple of columns from the gridview which I dont need. (All the columns are databound) This works fine. But after a postback all the...
3
by: Sheau Wei | last post by:
This is the code the code that i try to run it as my search engine for my database. But i dont understand why i run a notice that undefind index. Can u help me ? <form method="post"...
5
by: ryann18 | last post by:
I was just wondering, my teacher assigned a project for me and told me to "create one cat in SandBox and put all the methods (for example method_1) you need for the excercises in KittyKitty and call...
19
by: so many sites so little time | last post by:
the table is head the colunm is called body <?php //show_site.php // This script retrieves blog entries from the database. // Address error handing. ini_set ('display_errors', 1);...
2
by: Richard Coltrane | last post by:
Hello, Can anyone point me in the direction of some good introductory resources to MVC and Web. I have looked up several resources on MVC in general (none of which have any code samples) but I...
6
by: Bishop | last post by:
a += (UInt32)(url + (url << 8) + (url << 16) + (url << 24)); I'm trying to understand what the above line of code does. I understand that the url part of (url << 8) returns part of the string...
3
by: canabatz | last post by:
Hello all!! i got a small problem ,not big :) i got this example of a link in my site that display everything correctly! http://www.mysite.com/product.php?id=200 now my problem is : if...
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: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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...

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.