Solved

seperating input from cin

Posted on 2000-03-27
1
171 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
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…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
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.

739 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