Reading help

I need help reading a line of input in a special way.
How do I read a something and split them into 2 different variables?

For example:

if my input is "$00002000", how do I store the "$" in one variable and the 00002000 in another?
1
2
3
char dollarSign;
int dollarAmount;
std::cin >> dollarSign >> dollarAmount;


If you want 00002000 to be a string just replace int dollarAmount with std:.string dollarAmount.
Okay now I understand that, how do I ignore a comma and move to the next part?

For example:

if my input is "$00002000, D0"

I know how to store the $00002000 but how do I read the part after the comma and store it into a variable?
1
2
3
4
5
char dollarSign;
int dollarAmount;
char comma;
std::string str;
std::cin >> dollarSign >> dollarAmount >> comma >> str;

You might want to verify that comma == ',' because it will read any char that is not whitespace
Yeah, I don't know how to verify if it's a comma, it's reading the first thing after the int.

$ = dollarSign
0 = dollarAmount
0 = comma
Let me explain what I'm basically trying to do:

This is going to be my input:
MOVE #79, $00002000

There are 3 parts in this instruction:
MOVE
#79
$00002000

I'm trying to store all the values into a variable so I know what to do with all of these parts.

Part 1, 1 variable:
MOVE

Part 2, 2 variables:
#
79

Part 3, 2 variables:
$
00002000
You can basically read your input as string and then process it.
A really helpful header file is cctype.

See for example http://www.cplusplus.com/reference/clibrary/cctype/
for function with which you can check if a char is digit (isdigit()), alphabetic (isalpha) , punctuation mark (ispunct) etc

For example to read the first num of a given string one could use:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include <iostream>
#include <string>
#include <cctype>

using namespace std;

int main()
{
    char c;
    int n;
    string str;
    int start,end;
    cout << "Give me the string\n";
    cin >> str;
    for(int i=0; i<str.size(); i++)
    {
        if(isdigit(str[i]))
        {
            start = i;
            break;
        }
        else
        {
            continue;
        }
    }
    for(int i=start+1; i<str.size(); i++)
    {
        if(!isdigit(str[i]))
        {
            end = i;
            break;
        }
    }
    cout << start << " " << end << "\n";
    cout << "num=" << str.substr(start,end-start);
    return 0;
}
Topic archived. No new replies allowed.