简体   繁体   中英

Organize character types of array into separate arrays in C++

I am trying to write a small program that takes a user-defined string of a limited selection of characters (lowercase letters, parentheses, and the + and * operators), looks at each of the characters, and organizes them into separate arrays. I thought my approach to this exercise would be fairly straightforward, but I have run into some issues that I cannot figure out.

My problem becomes evident when I attempt to print the individual arrays to the screen. If all of the characters are of the same type (for example, "abcd"), the arrays print as intended. But if there is a combination of character types (for example, "(a+b)"), the arrays print incorrectly. I'm ashamed to say I have been banging my head against this (probably obvious) problem for many hours now and cannot seem to figure out what I have done wrong. Any input would be appreciated - I'm not looking for help writing the program, I just want to know what I have done wrong. I have included my code below:

#include <iostream>
#include <string>

using namespace std;

void charOrganizer(int[], char[], char[], char[], int, int&, int&, int&);

int main()
{
//Variable declaration
string userExpression;
int expressionArray[userExpression.length()];
char letterToken[userExpression.length()];
char parenthesesToken[userExpression.length()];
char plusTimesToken[userExpression.length()];
int letterTokenPos = 0;
int parenthesesTokenPos = 0;
int plusTimesTokenPos = 0;

//Prompt user for string input
cout << "Please enter a mathematical expression only using lowercase letters of the \nalphabet, parentheses, and/or the addition/multiplication operators."<< endl;
cin >> userExpression;

int arraySize = userExpression.length();

for (int i = 0; i < arraySize; ++i)
{
    expressionArray[i] = userExpression[i];
}

charOrganizer(expressionArray, letterToken, parenthesesToken, plusTimesToken, arraySize, letterTokenPos, parenthesesTokenPos, plusTimesTokenPos);

//Print tokens to screen
cout << "LowerCase Letter Token values in your string:" << endl;
for (int i = 0; i < letterTokenPos; i++)
{
    cout << letterToken[i] << endl;
}

cout << "Parentheses Token values in your string:" << endl;
for (int i = 0; i < parenthesesTokenPos; i++)
{
    cout << parenthesesToken[i] << endl;
}

cout << "Operator Token values in your string:" << endl;
for (int i = 0; i < plusTimesTokenPos; i++)
{
    cout << plusTimesToken[i] << endl;
}

return 0;
}

void charOrganizer (int charValue[], char letArr[], char parArr[], char pluTimArr[], int size, int& letPosition, int&parPosition, int& operPosition)
{   
for (int i = 0; i < size; i++)
{
    if (charValue[i] > 96 && charValue[i] < 123)
    {
        letArr[letPosition] = charValue[i];
        cout << "Letter Copy Test: " << letArr[letPosition] << endl;
        letPosition++;
    }

    else if (charValue[i] == 40 || charValue[i] == 41)
    {
        parArr[parPosition] = charValue[i];
        cout << "Parentheses Copy Test: " << parArr[parPosition] << endl;
        parPosition++;
    }

    else if (charValue[i] == 42 || charValue[i] == 43)
    {
        pluTimArr[operPosition] = charValue[i];
        cout << "Operator Copy Test: " << pluTimArr[operPosition] << endl;
        operPosition++;
    }

    else
    {
        cout << "Invalid input" << endl;
    }
}

/*cout << "Print Array Test: " << endl;
for (int i = 0; i < letPosition; i++)
{
    cout << letArr[i] << endl;
    //cout << parArr[i] << endl;
    //cout << pluTimArr[i] << endl;
}*/
}

see your below code. You are using the length of a empty string to declare arrays size like below. In this case all the array size will zero size only. Correct it properly:-

//Variable declaration
string userExpression;
int expressionArray[userExpression.length()];// it would be just like int expressionArray[0];
char letterToken[userExpression.length()];
char parenthesesToken[userExpression.length()];
char plusTimesToken[userExpression.length()];

int expressionArray[30];
char letterToken[30];
char parenthesesToken[30];
char plusTimesToken[30];

Else you declare these variables after you enter your string.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM