Solved

seperating input from cin

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

Independent Software Vendors: 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

Suggested Solutions

Title # Comments Views Activity
C++ assignment question 7 268
Lambda for random numbers problem 7 120
Would like to move button in a function 3 83
Add values of each row in an array 3 70
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…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
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 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.

763 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