Solved

seperating input from cin

Posted on 2000-03-27
1
172 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 22

Accepted Solution

by:
nietod earned 50 total points
ID: 2661483
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

724 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