0

As a part of my project, I am expected to receive input in the following format.

AXXX AXXX AXXX .....

where A is a letter (any capital), and XXX is any integer (note that this number does not have to be limited to 3 digits). The letter is associated with some functionality (e.g. A means Add XXX to a list of numbers). I want to make a function that can read each term in the input and recognise what actions need to be taken (e.g. if I put A123 A983 A828 i know that i want to store the numbers 123 983 and 828 on a list). I hope that this was not confusing. If you are wondering why I am doing this, my project is on linked lists and requires input from the user to add nodes to the linked list.

VVSTITAN
  • 113
  • 1
  • 1
  • 8
  • 3
    What is your question? What code have you written? – Christopher Schneider Oct 19 '16 at 13:30
  • 2
    The [standard string class](http://en.cppreference.com/w/cpp/string/basic_string) have functions to split the string into [sub-strings](http://en.cppreference.com/w/cpp/string/basic_string/substr). And it's easy to [convert a string to an integer](http://en.cppreference.com/w/cpp/string/basic_string/stol). – Some programmer dude Oct 19 '16 at 13:31
  • But that would only work if I had a single phrase.Here I have a few alphanumeric phrases, and I don't know the index of each of the letters in the string. – VVSTITAN Oct 19 '16 at 13:36
  • 3
    Read each alphanumeric phrase into its own string using the `>>` operator of the stream. Then split it with the information Joachim Pileborg provided. – NathanOliver Oct 19 '16 at 13:38

2 Answers2

0

This should work, but it does check for errors. It assume data is coming in proper format and order.

#include<iostream>
#include<sstream>
using namespace std;

int main()
{
    string input( "A123 B123123 C12312312" ); //should be trimmed and in order
    stringstream ss(input);
    char c;
    int data;
    while (ss >> c >> data)
    {
        std::cout << c << " " << data << std::endl;
    }
}
sanjay
  • 705
  • 1
  • 5
  • 22
  • 1
    You should read ["Why is iostream::eof inside a loop condition considered wrong?"](http://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong). – Some programmer dude Oct 19 '16 at 13:54
  • Thanks Sanjay. I type in an if statement inside the while loop so that i can do some further calculations e.g. – VVSTITAN Oct 19 '16 at 14:02
  • @JoachimPileborg Thanks for the information. But i have already mentioned my assumptions that data should be in proper format. Its a trivial solution. It can have many other issues. – sanjay Oct 19 '16 at 14:05
  • @vaibhava96 please go through http://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong and handle the error checks. – sanjay Oct 19 '16 at 14:05
  • sorry, something went wrong with my previous comment, it was meant to say e.g. if(c == "A"){ //stuff happens }. When i type this in the while loop you have suggested, the compiler gives me an error saying "ISO C++ forbids comparison between pointer and integer". I am not creating any pointers so why would this happen? – VVSTITAN Oct 19 '16 at 14:07
  • it should be (c == 'A') with single quote, otherwise it become string literal – sanjay Oct 19 '16 at 14:08
  • It doesn't matter if the input is otherwise correct. Your loop as it is now will iterate once to many and will attempt to read input that's doesn't exist. If you change the loop to `while (ss >> c >> data)` it will work. – Some programmer dude Oct 19 '16 at 14:08
  • @JoachimPileborg updated – sanjay Oct 19 '16 at 14:22
0

does your data provide a end-symbol?

this implementation use '\0' as end-delimiter and does not check for wrong data (is this possible?)

void exec_function(char operation, int data) {
    switch(operation) {
    //...
    }
}

std::string input = "A123A983A828";

char operation=0;
int data=0;
int index=-1;
// for all characters
for (int i = 0; i < input.length(); i++) {
    // current character is a capital letter or '\0'
    if (input[i] > 64 && input[i] < 91 || input[i] == '\0') {
          // if we already found a letter
          //between 2 letters are only numbers
          if (index != -1) {
              int base=1;
              // this is just a simple str2int implementation
              for (int j=i-1; j != index; j--) {
                   data += (index[j]-48)*base;
                   base*=10;
              }
              // operation and data are available
              exec_function(operation, data);
          }
          // clear old data              
          data=0;
          // set new operation
          operation=input[i];
          // update position of last found operation
          index = i;
    } 
}
Domso
  • 918
  • 1
  • 9
  • 19