Solved

seperating input from cin

Posted on 2000-03-27
1
166 Views
Last Modified: 2010-04-02
I need to be able to seperate values from cin to 2 separate variables.  For Example:

1430 needs to be put into two variables:

variable1 = 14, variable 2 = 30

Additionally, input could be formatted as:

14:30
and then,
variable1 = 14, variable2 = 30
0
Comment
Question by:psisung
1 Comment
 
LVL 22

Accepted Solution

by:
nietod earned 50 total points
Comment Utility
you will have to read the value in as an ASCII string and then convert the string to numbers, like

char Lin[80]
char NumStr[3];
int variable1 = 0;
int variable2 = 0;

cin.getline(Lin,80);
strncpy(NumStr,Lin,2);
NumStr[2] = 0;
variable1 = atoi(NumStr);

if (isdigit(Lin[2]))
  strncpy(NumStr,Lin+2,2);
else
  strncpy(NumStr,Lin+3,2);
variable2 = atoi(NumStr);

You may have to change this depending ont he exact rules you decide apon  for parsing the two numbers.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now