Character place

How can I take characters from their place and put them into variables?
Example:
User input : 5+9+7
a=5(1st)
b=+(2nd)
...

I hope you get what I mean =P

-EDIT-
Oh yeah, my friend said something about using "ordinal"? =S
Last edited on
Well, you could just use getline() and put the input into the string, then you can just iterate through the characters to get each part.
hmm...a simple example perhaps? =D

TNX
Well, the example would basically be doing it for you because it's so simple XD.

Read:
http://www.cplusplus.com/reference/string/getline.html
and possibly
http://www.cplusplus.com/reference/string/string/
hmm...
I tried something like this...
It should calculate equation of type a(+/-)b(+/-)c...
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
#include <iostream>
#include <string>
using namespace std;

int main ()
{
   string str;
   cin >> str;
  
  int d=0;
  int e=0;
  int a = str.at(1);
  int b = str.at(3);
  int c = str.at(5);
  char sum1 = str.at(2);
  char sum2 = str.at(4);
  
  if(sum1 == '+')
  d=a+b;
  else if(sum1 == '-')
  d=a-b;
   
  if(sum2 == '+')
  e=d+c;
  else if(sum2 == '-')
  e=d-c;
   
  cout << e << endl;
  
  system("PAUSE");
    return 0;
}


It just breaks down... =( xP
indices start from 0, not from 1.
notice that str.at will return a character eg: '5', not the number 5.

By the way, isn't easier something like this?
1
2
3
int a,b,c;
char sum1,sum2;
cin >> a >> sum1 >> b >> sum2 >> c;
Yes it is but I wanted to do it using str.at...
Topic archived. No new replies allowed.