dimanche 28 février 2016

returning structure cause program to crash or return only first word

I can not figure out for the life of me why when i return pigLatin in the Word piglatin function it keeps crashing or not executing probably outside the function when i call it. here is my code if anyone wants to check it out. -Thank You

#include <iostream>
#include <string>
#include <cstring>
#include <cctype>
using namespace std;

struct Word
{
    string piglatin;
    string english;
};

Word piglatin(string latin, int &size);

int main() {

int sum=1; // how many words

string pig;

cout <<"Enter a sentence in piglatin to decode";
getline(cin,pig); // get user input

Word latinReturn[sum];
*latinReturn = piglatin(pig,sum); // call to function piglatin

 cout << endl << "test out 0:  " << latinReturn[0].piglatin;
cout << endl << "test out 1:  " << latinReturn[1].piglatin;
cout << endl << "test out 2:  " << latinReturn[2].piglatin;
return 0;
}


Word piglatin( string latin, int &size)


for (int i=0; i < latin.length(); i++) // find how many words
{
if(latin[i] == ' ')
    {
    size++;
    }
}
cout<< "size: " <<size <<endl;

    int x=latin.length();
    string temp;
    char ending = latin[x-1];
    int count=0;
    Word pigLatin[size];
    //pigLatin = new Word[size];

    for(int i=0;i < latin.length(); i++)  //lower case
        {
            latin[i] = tolower(latin[i]);
        }

        cout <<endl << "test2:  " << latin;

        for(int i=0; i < latin.length(); i++)  //remove extra
        {
            if(isalpha(latin[i]) || isspace(latin[i]))
            {
                temp += latin[i];
            }
        }

        latin =temp; // make latin into temp
        latin +=ending; //ending char
        cout << "test3: " <<endl << latin;

        for(int i=0; i <latin.length(); i++) //change into struct array
            {
                if(isalpha(latin[i]))
                {
                    pigLatin[count].piglatin += latin[i];
                }
                else if (isspace(latin[i])){
                    count++;
                }
            }
            cout<<endl <<"test final:" <<pigLatin[1].piglatin;
            cout<<endl <<"test final:" <<pigLatin[0].piglatin;
            cout<< endl << " count is: " << count;
        return *(pigLatin);

Aucun commentaire:

Enregistrer un commentaire