Question

Hi I'm trying to take a c-string from a user, input it into a queue, parse the data with a single space depending on its contents, and output the kind of data it is (int, float, word NOT string).

E.g. Bobby Joe is 12 in 3.5 months \n

Word: Bobby

Word: Joe

Word: is

Integer: 12

Word: in

Float: 3.5

Word: months

Here's my code so far:

int main()
{
    const int maxSize = 100;

    char cstring[maxSize];

    std::cout << "\nPlease enter a string: ";
    std::cin.getline(cstring, maxSize, '\n');

//Keyboard Buffer Function
buffer::keyboard_parser(cstring);

return EXIT_SUCCESS;
}

Function:

#include <queue>
#include <string>
#include <cstring>
#include <iostream>
#include <cstdlib>
#include <vector>

namespace buffer
{
    std::string keyboard_parser(char* input)
{
    //Declare Queue
    std::queue<std::string> myQueue;

    //Declare String
    std::string str;

    //Declare iStringStream
    std::istringstream isstr(input);

    //While Loop to Read iStringStream to Queue
    while(isstr >> str)
    {
        //Push onto Queue
        myQueue.push(str);

        std::string foundDataType = " ";

        //Determine if Int, Float, or Word
        for(int index = 0; index < str.length(); index++)
        {
            if(str[index] >= '0' && str[index] <= '9')
            {
                foundDataType = "Integer";
            }
            else if(str[index] >= '0' && str[index] <= '9' || str[index] == '.')
            {
                foundDataType = "Float";
                                    break;
            }
            else if(!(str[index] >= '0' && str[index] <= '9'))
            {
                foundDataType = "Word";
            }
        }

        std::cout << "\n" << foundDataType << ": " << myQueue.front();
        std::cout << "\n";

        //Pop Off of Queue
        myQueue.pop();
        }
}   
}

Right now with this code, it doesn't hit the cout statement, it dumps the core.

I've read about using the find member function and the substr member function, but I'm unsure of how exactly I need to implement it.

Note: This is homework.

Thanks in advance!

UPDATE: Okay everything seems to work! Fixed the float and integer issue with a break statement. Thanks to everyone for all the help!

Was it helpful?

Solution 2

std::istreams are very useful for parsing text in C++... often with an initial read of a line from a string, then further parsing from a std::istringstream constructed with the line content.

const char* token_type(const std::string& token)
{
    // if I was really doing this, I'd use templates to avoid near-identical code
    // but this is an easier-to-understand starting point...
    {
        std::istringstream iss(token);
        int i;
        char c;
        if (iss >> i && !(iss >> c)) return "Integer";
    }
    {
        std::istringstream iss(token);
        float f;
        char c; // used to check there's no trailing characters that aren't part
                // of the float value... e.g. "1Q" is not a float (rather, "word").
        if (iss >> f && !(iss >> c)) return "Float";
    }
    return "Word";
}

const int maxSize = 100;  // Standard C++ won't let you create an array unless const
char cstring[maxSize];
std::cout << "\nPlease enter a string: ";
if (std::cin.getline(cstring, maxSize, '\n'))
{
    std::istringstream iss(cstring);
    std::string token;
    while (iss >> token) // by default, streaming into std::string takes a space-...
        token_queue.push(token);    // ...separated word at a time

    for (token_queue::const_iterator i = token_queue.begin();
         i != token_queue.end(); ++i)
        std::cout << token_type(*i) << ": " << *i << '\n';
}

OTHER TIPS

Your queue is sensible: it contains std::strings. Unfortunately, each of those is initialised by you passing cstring in without any length information and, since you certainly aren't null-terminating the C-strings (in fact, you're going one-off-the-end of each one), that's seriously asking for trouble.

Read directly into a std::string.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top